dubbo+zookeeper平台

新的工作,用到了分布式框架,刚接触时,项目组长说了几个我不懂的名词(dubbo,Zookeeper,kafka,Redis等),经过几天的学习和接触,现对dubbo+zookeeper平台做如下总结,以方便经后自己回忆。

先来一张网上down来的图:

节点角色说明:
Provider: 暴露服务的服务提供方。
Consumer: 调用远程服务的服务消费方。
Registry: 服务注册与发现的注册中心。
Monitor: 统计服务的调用次调和调用时间的监控中心。
Container: 服务运行容器。

调用关系说明:
0 服务容器负责启动,加载,运行服务提供者。
1 服务提供者在启动时,向注册中心注册自己提供的服务。
2 服务消费者在启动时,向注册中心订阅自己所需的服务。
3 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
4 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
5 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

zookeeper在上图中充当了注册中心的角色。

开发过程:

1、项目中加入服务接口;

2、实现接口的具体服务;

3、pom.xml(maven工程)中加入dubbo,zookeeper等Jar包;

4、在dubbo-provider.xml中暴露服务;

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 5         http://www.springframework.org/schema/beans/spring-beans.xsd  
 6         http://code.alibabatech.com/schema/dubbo  
 7         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
 9   
10     <!-- 具体的实现bean -->  
11     <bean id="demoService" class="com.xxx.service.impl.DemoServiceImpl" />  
12   
13     <!-- 提供方应用信息,用于计算依赖关系 -->  
14     <dubbo:application name="provider" />  
15   
16     <!-- 使用multicast广播注册中心暴露服务地址 -->  
17     <!--<dubbo:registry address="multicast://xx.xx.xx.xx:xxxx" /> -->  
18       
19     <!-- 使用zookeeper注册中心暴露服务地址 --即zookeeper的所在服务器ip地址和端口号 -->  
20     <dubbo:registry address="zookeeper://xx.xx.xx.xx:2181" />  
21   
22     <!-- 用dubbo协议在20880端口暴露服务 -->  
23     <dubbo:protocol name="dubbo" port="20880" />  
24   
25     <!-- 声明需要暴露的服务接口 -->  
26     <dubbo:service interface="com.xx.service.DemoService" ref="demoService" />  
28   
29 </beans>  

5、在网关端配置服务消费者:dubboService.xml生成远程代理服务

 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <beans xmlns="http://www.springframework.org/schema/beans"  
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"  
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans  
 5         http://www.springframework.org/schema/beans/spring-beans.xsd  
 6         http://code.alibabatech.com/schema/dubbo  
 7         http://code.alibabatech.com/schema/dubbo/dubbo.xsd">  
 9   
10     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->  
11     <dubbo:application name="consumer" />  
12   
13     <!-- 使用zookeeper注册中心暴露服务地址 -->   
15     <dubbo:registry address="zookeeper://xx.xx.xx.xx:xx" />  
16   
17     <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->  
18     <dubbo:reference id="demoService"interface="com.xx.service.DemoService" />  
20   
21 </beans>  

先写这么多,后面想到了再添加。

posted on 2017-03-27 16:08  s小小的我  阅读(567)  评论(0)    收藏  举报

导航