创建三个Maven Project:
- dubbo-service:公共模块,包括服务接口(packaging:jar)
- dubbo-service-impl:服务提供方,提供服务接口的具体实现,需要依赖dubbo-service(packaging:jar)
- dubbo-consumer:服务调用方,需要依赖dubbo-service(packaging:jar/war)
一、dubbo-service
public interface DemoService {
public String demo(String name);
}
二、dubbo-service-impl
1. pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.linhw.demo</groupId>
<artifactId>dubbo-service-impl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.linhw.demo</groupId>
<artifactId>dubbo-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
<!-- 访问 zookeeper 的客户端 jar -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
</project>
2. 接口实现类
public class DemoServiceImpl implements DemoService{
@Override
public String demo(String name) {
return "dubbo RPC " + name;
}
}
3. 新增配置文件dubbo-provider.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 给当前provider自定义一个名字 -->
<dubbo:application name="demo-provider"/>
<!-- 配置注册中心 -->
<dubbo:registry address="192.168.178.5:2181" protocol="zookeeper"/>
<!-- 配置协议及端口 -->
<dubbo:protocol name="dubbo" port="28888"/>
<!-- 注册功能 -->
<bean id="demoService" class="com.linhw.demo.service.impl.DemoServiceImpl"/>
<dubbo:service interface="com.linhw.demo.service.DemoService" ref="demoService"/>
</beans>
4. 启动容器
(1) 通过 spring 方式启动:对dubbo-provider.xml的位置没有要求
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("dubbo-provider.xml");
ctx.start();
System.out.println("启动成功");
System.in.read();
(2) 使用 dubbo 提供的方式启动(推荐使用这种方式):要求 dubbo-provider.xml必须放入类路径下/META-INF/spring/*.xml
Main.main(args);
查看是否发布成功,可以启动Dubbo Admin,在管理控制台查看。
三、dubbo-consumer
1. 服务调用
import com.alibaba.dubbo.config.annotation.Reference;
import com.linhw.demo.service.DemoService;
import com.linhw.demo.service.TestService;
public class TestServiceImpl implements TestService{
@Reference
private DemoService demoService;
@Override
public String sayHello(String name) {
name = "hello";
return demoService.demo(name);
}
}
2. 新建applicationContext-dubbo.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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 给当前 Provider 自定义个名字 -->
<dubbo:application name="dubbo-consumer"/>
<!-- 配置注册中心 -->
<dubbo:registry address="192.168.178.5:2181" protocol="zookeeper"/>
<!-- 配置注解扫描 -->
<dubbo:annotation package="com.linhw.demo.service.impl"/>
<bean id="testServiceImpl" class="com.linhw.demo.service.impl.TestServiceImpl"/>
</beans>
3. 测试
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-dubbo.xml");
TestService testService = ctx.getBean("testServiceImpl", TestServiceImpl.class);
System.out.println(testService.sayHello("hello"));
转载于:https://www.cnblogs.com/myitnews/p/11484786.html