开始
是在之前的项目上
https://blog.csdn.net/ko0491/article/details/85166785
provider更改
新建spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 当我们需要使用 注解时 开启注解 -->
<context:annotation-config />
<!--配置扫描包 -->
<context:component-scan base-package="com.ghgcn.dubbo.service" />
<!--导入dubbo配置文件 -->
<import resource="service-provider.xml"/>
</beans>
service-provider改变
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider" />
<!-- 注册中心地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 声明协议与商品 -->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明服务的实现类 旧的方式-->
<!-- <bean id="greetingService" class="com.ghgcn.dubbo.service.impl.GreetingServiceImpl"
/> -->
<!-- 声明服务 spring -->
<dubbo:service interface="com.ghg.dubbo.api.GreetingService"
ref="greetingService" protocol="dubbo" validation="false" />
</beans>
在provicer实现的service上加注解@Servcie
package com.ghgcn.dubbo.service.impl;
import org.springframework.stereotype.Service;
import com.ghg.dubbo.api.GreetingService;
@Service("greetingService") //名称
public class GreetingServiceImpl implements GreetingService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
main启动类改变
第一版
package com.ghgcn.dubbo;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws IOException {
// com.alibaba.dubbo.container.Main.main(args);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{ "META-INF/spring/service-provider.xml" });
context.start();
System.out.println("provider启动了");
System.in.read(); // press any key to exit
}
}
第二版
package com.ghgcn.dubbo;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws IOException {
// com.alibaba.dubbo.container.Main.main(args);
//
// ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
// new String[]{ "META-INF/spring/service-provider.xml" });
/**
* 使用spring文件
*/
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{ "META-INF/spring/spring.xml" });
context.start();
System.out.println("provider启动了");
System.in.read(); // press any key to exit
}
}
正常启动
consumer改造
新建spring.xml
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<!-- 当我们需要使用 注解时 开启注解 -->
<context:annotation-config />
<!--配置扫描包 -->
<context:component-scan base-package="com.ghgcn.consumer" />
<!--导入dubbo引入配置文件 -->
<import resource="dubbo-client.xml" />
</beans>
dubbo-client.xml 无更改
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-consumer" />
<!-- 注册中心 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 声明要引用的服务 -->
<dubbo:reference id="greetingService" check="false" interface="com.ghg.dubbo.api.GreetingService" />
</beans>
新建helloService与实现类
package com.ghgcn.consumer.service;
public interface HelloService {
String sayHello(String name);
}
package com.ghgcn.consumer.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ghg.dubbo.api.GreetingService;
import com.ghgcn.consumer.service.HelloService;
/**
* 加注解service
*
* @author Administrator
*/
@Service
public class HelloServiceImpl implements HelloService {
/**
* 可以作 @Resource @Autowired等psring注解
*/
@Autowired
private GreetingService greetingService;
@Override
public String sayHello(String name) {
return greetingService.sayHello(name);
}
}
main方法启动更改
第一版
package com.ghgcn.consumer;
import java.util.Date;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ghg.dubbo.api.GreetingService;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{ "sping/dubbo-client.xml" });
context.start();
GreetingService greetingService = (GreetingService) context.getBean("greetingService"); // get remote service
while (true) {
try {
Thread.sleep(1000);
System.out.println(greetingService.sayHello("刘楠* " + new Date()));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
第二版
package com.ghgcn.consumer;
import java.util.Date;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ghgcn.consumer.service.HelloService;
public class Main2 {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{ "sping/spring.xml" });
context.start();
HelloService helloService = context.getBean(HelloService.class);
while (true) {
try {
Thread.sleep(1000);
System.out.println(helloService.sayHello("刘楠* " + new Date()));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
启动正常
结合spring可以做微服务很方便
github :https://github.com/ln0491/dubbo-demo