1.示例场景
使用dubbo模拟用户服务(消费者)调用资源服务(生产者)
2.示例图

- 生产者:生产者服务启动时会把地址注册到zookeeper,以服务名称作为key,value为服务地址(地址可以用数组存储多个用来搭建集群)
- 消费者:消费者服务启动后会订阅注册中心,获取注册中心中的服务地址列表,然后在本地实现rpc远程调用
- zookeeper事件通知:当生产者增加节点(服务器ip地址),zookeeper接收到新增地址会通过事件通知告诉消费者
3示例代码
项目目录结构
resources-producer-impl -资源服务实现
resources-producer-public-api -资源服务接口
user-consumer-impl -用户服务实现

1)创建resources-producer-public-api资源服务接口模块(生产者)
- 创建ResService资源接口类
public interface ResService {
//根据资源id获取信息
String getResId(String id);
//获取总记录数
int getCount();
}
2)创建resources-producer-impl资源服务实现模块
- 创建ResServiceImpl实现类
public class ResServiceImpl implements ResService {
public String getResId(String id) {
if(id.equals("1")){
return "第一条记录查询成功";
}
return null;
}
public int getCount() {
return 10;
}
}
- resources-producer-impl模块的pom.xml引入jar依赖
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>jyb_dubbo</artifactId>
<groupId>com.jyb</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>resources-producer-impl</artifactId>
<dependencies>
<dependency>
<groupId>com.jyb</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>resources-producer-public-api</artifactId>
</dependency>
<!-- - ZK客户端工具 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- dubbo底层框架基于Netty实现 -->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
</dependency>
<!-- spring框架组件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<!-- dubbo框架 -->
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
- 创建producer.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.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--定义了提供方应用信息,用于计算依赖关系;在 dubbo-admin 或 dubbo-monitor 会显示这个名字,方便辨识 -->
<dubbo:application name="jyb-resources"/>
<!--使用 zookeeper 注册中心暴露服务,注意要先开启 zookeeper -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!--使用 dubbo 协议实现定义好的 api.PermissionService 接口 -->
<dubbo:service interface="com.jyb.service.ResService"
ref="resService" protocol="dubbo"/>
<!--具体实现该接口的 bean -->
<bean id="resService" class="com.jyb.impl.ResServiceImpl"/>
</beans>
- 创建Producer启动资源服务类
public class Producer {
//启动dubbo
public static void main(String[] args) throws Exception {
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"producer.xml"});
context.start();
System.out.println("Provider started.");
System.in.read(); // press any key to exit
}
}
3)创建user-consumer-impl用户模块(消费者)
- user-consumer-impl模块的pom.xml引入jar依赖
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>jyb_dubbo</artifactId>
<groupId>com.jyb</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user-consumer-impl</artifactId>
<dependencies>
<dependency>
<groupId>com.jyb</groupId>
<version>1.0-SNAPSHOT</version>
<artifactId>resources-producer-public-api</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- dubbo底层框架基于Netty实现 -->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
</dependency>
<!-- spring框架组件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<!-- dubbo框架 -->
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
- 创建consumer.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.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="order-consumer"/>
<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送 -->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口 -->
<dubbo:reference id="resService"
interface="com.jyb.service.ResService"/>
</beans>
- 创建Consumer启动类调用接口信息
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
context.start();
//获取远程服务代理
ResService resService = (ResService) context.getBean("resService");
// 执行远程方法
String result = resService.getResId("1");
int num = resService.getCount();
// 现实调用结果
System.out.println("id是1的返回结果为:"+result);
System.out.println("总共有:"+num+"条记录");
}
}
4)运行结果



网友评论