1 zookeeper

Zookeeper 是 Apacahe Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用。
257510580bada206415c619727e8f81d.jpg
流程说明:

  1. 服务提供者启动时,向 /dubbo/com.foo.BarService/providers 目录下写入自己的 URL 地址
  2. 服务消费者启动时,订阅 /dubbo/com.foo.BarService/providers 目录下的提供者 URL 地址。并向 /dubbo/com.foo.BarService/consumers 目录下写入自己的 URL 地址
  3. 监控中心启动时,订阅 /dubbo/com.foo.BarService 目录下的所有提供者和消费者 URL 地址

支持以下功能:

  1. 当提供者出现断电等异常停机时,注册中心能自动删除提供者信息
  2. 当注册中心重启时,能自动恢复注册数据,以及订阅请求
  3. 当会话过期时,能自动恢复注册数据,以及订阅请求
  4. 当设置 时,记录失败注册和订阅请求,后台定时重试
  5. 可通过 设置 zookeeper 登录信息
  6. 可通过 设置 zookeeper 的根节点,不设置将使用无根树
  7. 支持号通配符 `<dubbo:reference group=”” version=”“>`,可订阅服务的所有分组和所有版本的提供者

1.1 配置

1.1.1 单机配置

  1. <dubbo:registry address="zookeeper://10.20.153.10:2181" />
  2. <dubbo:registry protocol="zookeeper" address="10.20.153.10:2181" />

1.1.2 集群配置

  1. <dubbo:registry address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181" />
  2. <dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />

1.1.3 同一 Zookeeper,分成多组注册中心

  1. <dubbo:registry id="chinaRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="china" />
  2. <dubbo:registry id="intlRegistry" protocol="zookeeper" address="10.20.153.10:2181" group="intl" />

2 Multicast

Multicast 注册中心不需要启动任何中心节点,只要广播地址一样,就可以互相发现。
9cc4165c4565abf72e85e9f738444307.jpg

  1. 提供方启动时广播自己的地址
  2. 消费方启动时广播订阅请求
  3. 提供方收到订阅请求时,单播自己的地址给订阅者,如果设置了unicast=false,则广播给订阅者
  4. 消费方收到提供方地址时,连接该地址进行 RPC 调用。

2.1 配置

  1. <dubbo:registry address="multicast://224.5.6.7:1234" />
  2. <dubbo:registry protocol="multicast" address="224.5.6.7:1234"/>

为了减少广播量,Dubbo 缺省使用单播发送提供者地址信息给消费者,如果一个机器上同时启了多个消费者进程,消费者需声明 unicast=false,否则只会有一个消费者能收到消息:

  1. <dubbo:registry address="multicast://224.5.6.7:1234?unicast=false"/>
  2. <dubbo:registry protocol="multicast" address="224.5.6.7:1234">
  3. <dubbo:parameter key="unicast" value="false"/>
  4. </dubbo:registry>

3 Redis

基于 Redis 实现的注册中心 。
6869d91e7a726c7fe54a88636e141b1a.jpg

调用过程:

  1. 服务提供方启动时,向 Key:/dubbo/com.foo.BarService/providers 下,添加当前提供者的地址;并向 Channel:/dubbo/com.foo.BarService/providers 发送 register 事件
  2. 服务消费方启动时,从 Channel:/dubbo/com.foo.BarService/providers 订阅 register 和 unregister 事件;并向 Key:/dubbo/com.foo.BarService/providers 下,添加当前消费者的地址
  3. 服务消费方收到 register 和 unregister 事件后,从 Key:/dubbo/com.foo.BarService/providers 下获取提供者地址列表
  4. 服务监控中心启动时,从 Channel:/dubbo/* 订阅 register 和 unregister,以及 subscribe 和unsubsribe事件
  5. 服务监控中心收到 register 和 unregister 事件后,从Key:/dubbo/com.foo.BarService/providers 下获取提供者地址列表
  6. 服务监控中心收到 subscribe 和 unsubsribe 事件后,从 Key:/dubbo/com.foo.BarService/consumers 下获取消费者地址列表

使用 Redis 的 Key/Map 结构存储数据结构:

  1. 主 Key 为服务名和类型
  2. Map 中的 Key 为 URL 地址
  3. Map 中的 Value 为过期时间,用于判断脏数据,脏数据由监控中心删除

使用 Redis 的 Publish/Subscribe 事件通知数据变更:

  1. 通过事件的值区分事件类型:register,unregister,subscribe,unsubscribe
  2. 普通消费者直接订阅指定服务提供者的 Key,只会收到指定服务的 register,unregister 事件
  3. 监控中心通过 psubscribe 功能订阅 /dubbo/*,会收到所有服务的所有变更事件

3.1 配置

  1. <dubbo:registry address="redis://10.20.153.10:6379" />
  2. <dubbo:registry address="redis://10.20.153.10:6379?backup=10.20.153.11:6379,10.20.153.12:6379" />
  3. <dubbo:registry protocol="redis" address="10.20.153.10:6379" />
  4. <dubbo:registry protocol="redis" address="10.20.153.10:6379,10.20.153.11:6379,10.20.153.12:6379" />

3.2 选项

  • 可通过 设置 redis 中 key 的前缀,缺省为 dubbo。
  • 可通过 设置 redis 集群策略,缺省为 failover:
    • failover:只写入和读取任意一台,失败时重试另一台,需要服务器端自行配置数据同步
    • replicate:在客户端同时写入所有服务器,只读取单台,服务器端不需要同步,注册中心集群增大,性能压力也会更大

4 Simple

Simple 注册中心本身就是一个普通的 Dubbo 服务,可以减少第三方依赖,使整体通讯方式一致。

4.1 配置

4.1.1 将 Simple 注册中心暴露成 Dubbo 服务

  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"
  4. xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
  5. 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">
  6. <!-- 当前应用信息配置 -->
  7. <dubbo:application name="simple-registry" />
  8. <!-- 暴露服务协议配置 -->
  9. <dubbo:protocol port="9090" />
  10. <!-- 暴露服务配置 -->
  11. <dubbo:service interface="com.alibaba.dubbo.registry.RegistryService" ref="registryService" registry="N/A" ondisconnect="disconnect" callbacks="1000">
  12. <dubbo:method name="subscribe"><dubbo:argument index="1" callback="true" /></dubbo:method>
  13. <dubbo:method name="unsubscribe"><dubbo:argument index="1" callback="false" /></dubbo:method>
  14. </dubbo:service>
  15. <!-- 简单注册中心实现,可自行扩展实现集群和状态同步 -->
  16. <bean id="registryService" class="com.alibaba.dubbo.registry.simple.SimpleRegistryService" />
  17. </beans>

4.1.2 引用 Simple Registry 服务

  1. <dubbo:registry address="127.0.0.1:9090" />
  2. <dubbo:service interface="com.alibaba.dubbo.registry.RegistryService" group="simple" version="1.0.0" ... >
  3. <dubbo:registry address="127.0.0.1:9090" group="simple" version="1.0.0" />

此 SimpleRegistryService 只是简单实现,不支持集群,可作为自定义注册中心的参考,但不适合直接用于生产环境。