在《 Dubbo 源码分析 —— 调试环境搭建一》主要是Dubbo 的官方介绍,Dubbo 提供了 dubbo-demo
项目,用于开发者快速入门。 先说下注册中心,官方推荐使用 Zookeeper 注册中心
1.Registry
Dubbo 提供了多种 Registry 的实现,参见 《注册中心参考手册》 。
在 dubbo-demo
项目中,使用 Multicast Registry 。 实际生产环境中,使用 Zookeeper Registry 。
友情提示:「Multicast Registry」 和 「Zookeeper Registry」 二选一,建议先使用前者搭建调试环境,因为简单方便。
Multicast Registry
Multicast Registry 基于 组播协议 实现,详细参见文档 《Multicast 注册中心》 。
Multicast 注册中心不需要启动任何中心节点,只要广播地址一样,就可以互相发现。
Multicast 注册中心不需要启动任何中心节点,只要广播地址一样,就可以互相发现。
-
提供方启动时广播自己的地址
-
消费方启动时广播订阅请求
-
提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了
unicast=false
,则广播给订阅者 -
消费方收到提供方地址时,连接该地址进行 RPC 调用。
组播受网络结构限制,只适合小规模应用或开发阶段使用。组播地址段: 224.0.0.0 – 239.255.255.255
配置
<dubbo:registry address="multicast://224.5.6.7:1234" />
或
<dubbo:registry protocol="multicast" address="224.5.6.7:1234" />
为了减少广播量,Dubbo 缺省使用单播发送提供者地址信息给消费者,如果一个机器上同时启了多个消费者进程,消费者需声明 unicast=false
,否则只会有一个消费者能收到消息:
<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />
或
<dubbo:registry protocol="multicast" address="224.5.6.7:1234">
<dubbo:parameter key="unicast" value="false" />
</dubbo:registry>
注意**,不需要启动!!!简单的说,本小节的内容可以直接跳过。
zookeeper 注册中心
Zookeeper Registry 基于 Zookeeper 实现,详细参见文档 《zookeeper 注册中心》 。
Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 [1]。
流程说明:
-
服务提供者启动时: 向
/dubbo/com.foo.BarService/providers
目录下写入自己的 URL 地址 -
服务消费者启动时: 订阅
/dubbo/com.foo.BarService/providers
目录下的提供者 URL 地址。并向/dubbo/com.foo.BarService/consumers
目录下写入自己的 URL 地址 -
监控中心启动时: 订阅
/dubbo/com.foo.BarService
目录下的所有提供者和消费者 URL 地址。
支持以下功能:
-
当提供者出现断电等异常停机时,注册中心能自动删除提供者信息
-
当注册中心重启时,能自动恢复注册数据,以及订阅请求
-
当会话过期时,能自动恢复注册数据,以及订阅请求
-
当设置
<dubbo:registry check="false" />
时,记录失败注册和订阅请求,后台定时重试 -
可通过
<dubbo:registry username="admin" password="1234" />
设置 zookeeper 登录信息 -
可通过
<dubbo:registry group="dubbo" />
设置 zookeeper 的根节点,不设置将使用无根树 -
支持
*
号通配符<dubbo:reference group="*" version="*" />
,可订阅服务的所有分组和所有版本的提供者
使用
在 provider 和 consumer 中增加 zookeeper 客户端 jar 包依赖:
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
或直接下载。
Dubbo 支持 zkclient 和 curator 两种 Zookeeper 客户端实现:
使用 zkclient 客户端
从 2.2.0
版本开始缺省为 zkclient 实现,以提升 zookeeper 客户端的健状性。zkclient 是 Datameer 开源的一个 Zookeeper 客户端实现。
缺省配置:
<dubbo:registry ... client="zkclient" />
或:
dubbo.registry.client=zkclient
或:
zookeeper://10.20.153.10:2181?client=zkclient
需依赖或直接下载:
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
使用 curator 客户端
从 2.3.0
版本开始支持可选 curator 实现。Curator 是 Netflix 开源的一个 Zookeeper 客户端实现。
如果需要改为 curator 实现,请配置:
<dubbo:registry ... client="curator" />
或:
dubbo.registry.client=curator
或:
zookeeper://10.20.153.10:2181?client=curator
需依赖或直接下载:
<dependency>
<groupId>com.netflix.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>1.1.10</version>
</dependency>
Zookeeper 单机配置:
<dubbo:registry address="zookeeper://10.20.153.10:2181" />
或:
<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181" />
Zookeeper 集群配置:
<dubbo:registry address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181" />
或:
<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />
同一 Zookeeper,分成多组注册中心:
<dubbo:registry id="chinaRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="china" />
<dubbo:registry id="intlRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="intl" />
zookeeper 安装
安装方式参见: Zookeeper安装手册,只需搭一个原生的 Zookeeper 服务器,并将 Quick Start 中 Provider 和 Consumer 里的 conf/dubbo.properties
中的 dubbo.registry.addrss
的值改为 zookeeper://127.0.0.1:2181
即可使用。
可靠性声明
阿里内部并没有采用 Zookeeper 做为注册中心,而是使用自己实现的基于数据库的注册中心,即:Zookeeper 注册中心并没有在阿里内部长时间运行的可靠性保障,此 Zookeeper 桥接实现只为开源版本提供,其可靠性依赖于 Zookeeper 本身的可靠性。
兼容性声明
因 2.0.8
最初设计的 zookeeper 存储结构不能扩充不同类型的数据,2.0.9
版本做了调整,所以不兼容,需全部改用 2.0.9
版本才行,以后的版本会保持兼容 2.0.9
。2.2.0
版本改为基于 zkclient 实现,需增加 zkclient 的依赖包,2.3.0
版本增加了基于 curator 的实现,作为可选实现策略。
-
建议使用
2.3.3
以上版本的 zookeeper 注册中心客户端
搭建步骤如下:
1、启动 Zookeeper 服务。
$ pwd /Users/abel/dev/zookeeper-3.4.13 $ bin/zkServer.sh start bin/zkServer.sh start ZooKeeper JMX enabled by default Using config: /Users/abel/dev/zookeeper-3.4.13/bin/../conf/zoo.cfg Starting zookeeper ... STARTED
2、修改 dubbo-demo-provider.xml
,启动 Provider 。
<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
3、修改 dubbo-demo-consumer.xml
,启动 Consumer 。
<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
2.Provider
在 IDEA 中,右键 Debug 运行 com.alibaba.dubbo.demo.provider.Provider
。
当输出如下日志,表示成功:
[18/02/18 10:04:42:042 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@cd2dae5: startup date [Sun Feb 18 10:04:42 CST 2018]; root of context hierarchy [18/02/18 10:04:42:042 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/dubbo-demo-provider.xml] [18/02/18 10:04:42:042 CST] main INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter [18/02/18 10:04:53:053 CST] main INFO config.AbstractConfig: [DUBBO] The service ready on spring started. service: com.alibaba.dubbo.demo.DemoService, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:13:013 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.alibaba.dubbo.demo.DemoService to local registry, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:13:013 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.alibaba.dubbo.demo.DemoService to url dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.1.116&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&qos.port=22222&side=provider×tamp=1518919503284, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:13:013 CST] main INFO config.AbstractConfig: [DUBBO] Register dubbo service com.alibaba.dubbo.demo.DemoService url dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.1.116&bind.port=20880&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&qos.port=22222&side=provider×tamp=1518919503284 to registry registry://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.0&pid=43392&qos.port=22222®istry=multicast×tamp=1518919493265, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:13:013 CST] main INFO transport.AbstractServer: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.116:20880, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] main INFO multicast.MulticastRegistry: [DUBBO] Register: dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] main INFO multicast.MulticastRegistry: [DUBBO] Send broadcast message: register dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284 to /224.5.6.7:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: register dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] main INFO multicast.MulticastRegistry: [DUBBO] Subscribe: provider://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] main INFO multicast.MulticastRegistry: [DUBBO] Send broadcast message: subscribe provider://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284 to /224.5.6.7:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:20:020 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: subscribe provider://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 10:05:21:021 CST] main WARN multicast.MulticastRegistry: [DUBBO] Ignore empty notify urls for subscribe url provider://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43392&side=provider×tamp=1518919503284, dubbo version: 2.0.0, current host: 192.168.1.116
注意,如果使用 Multicast Registry 作为注册中心,在 IDEA 中需要使用 Debug 模式运行,原因参见:dubbo-demo set “java.net.preferIPv4Stack” not work 。
3. Consumer
在 IDEA 中,右键 Debug 运行 com.alibaba.dubbo.demo.consumer.Consumer
。
当输出如下日志,表示成功:
[18/02/18 09:45:22:022 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@cd2dae5: startup date [Sun Feb 18 09:45:22 CST 2018]; root of context hierarchy [18/02/18 09:45:22:022 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/dubbo-demo-consumer.xml] [18/02/18 09:45:22:022 CST] main INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Register: consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Send broadcast message: register consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457 to /224.5.6.7:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Subscribe: consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: register consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Send broadcast message: subscribe consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457 to /224.5.6.7:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: subscribe consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Receive multicast message: register dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43355&side=provider×tamp=1518918226970 from /192.168.1.116:1234, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO multicast.MulticastRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457, urls: [dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43355&side=provider×tamp=1518918226970], dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO transport.AbstractClient: [DUBBO] Successed connect to server /192.168.1.116:20880 from NettyClient 192.168.1.116 using dubbo version 2.0.0, channel is NettyChannel [channel=[id: 0x1b7bac12, /192.168.1.116:63856 => /192.168.1.116:20880]], dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] DubboMulticastRegistryReceiver INFO transport.AbstractClient: [DUBBO] Start NettyClient 123312.local/192.168.1.116 connect to the server /192.168.1.116:20880, dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO multicast.MulticastRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.1.116/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.0&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333&side=consumer×tamp=1518918323457, urls: [dubbo://192.168.1.116:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43355&side=provider×tamp=1518918226970], dubbo version: 2.0.0, current host: 192.168.1.116 [18/02/18 09:45:45:045 CST] main INFO config.AbstractConfig: [DUBBO] Refer dubbo service com.alibaba.dubbo.demo.DemoService from url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.0&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=43363&qos.port=33333®ister.ip=192.168.1.116&remote.timestamp=1518918226970&side=consumer×tamp=1518918323457, dubbo version: 2.0.0, current host: 192.168.1.116 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880 Hello world, response form provider: 192.168.1.116:20880