1 Dubbo支持多种注册中心

  • Multicast注册中心
  • Zookeeper注册中心
  • Redis注册中心
  • Simple注册中心

1.1 Multicast注册中心

使用配置

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<adubbo:registry protocol="multicast" ddress="224 5.6.7:1234">3<dubbo:parameter key="unicast" value="false" />4</dubbo:registry>

注解方式参数配置:

1dubbo.registry.address=multicast://224.5.6.7:1234

1.1.1 工作原理

局域网使用。

惊喜!Dubbo注册中心全新升级,助力业务快速扩张!_zookeeper

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

1.1.2 特点

  • Multicast注册中心不需要启动任何中心节点,只要组播地址一样,就可互相发现
  • 组播受网络结构限制,只适合小规模应用或开发阶段使用。组播地址段: 224.0.0.0-239.255.255.255

1.2 Zookeeper注册中心

1.2.1 使用配置

  1. 在provider和consumer中增加zookeeper客户端jar包依赖:
<!--引入zookeeper服务对应版本的zookeeper jar -->
<dependency>
<groupld>org.apache.zookeeper</groupld>
<artifactld>zookeeper</artifactld>
<version>3.4.11</version>
</dependency>
  1. Dubbo支持zkclient和curator两种zk客户端实现
1<dubbo:registry address="zookeeper://10.20.153.10:2181" 2  client= "zkclient" />34<dubbo:registry protocol"zookeeper" address=" 5  10.20.153.10:2181" client-"curator" />67dubbo.registry.client=zkclient

Dubbo2.6.6默认用curator

  1. 使用zkclient zookeeper客户端
  2. 使用curator zookeeper客户端
1<!-- 默认使用的是第三方 zookeeper 客户端 curator -->2<dependency>3  <groupld>org.apache.curator</groupld>4  <artifactld>curator- framework</artifactld>5  <version>4.2.0</version> 6  <!-- 如使用zk服务版本不是3.5,请排除自动依赖,再单独引入zk依赖 -->7  <exclusions>8    <exclusion>9      <groupld>org.apache.zookeeper</groupld>10      <artifactld> zookeeper</artifactld>11    </exclusion>12  </exclusions>13</dependency>
  1. zookeepe集群配置
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" />

惊喜!Dubbo注册中心全新升级,助力业务快速扩张!_zookeeper_02

➢ 服务提供者启动时:向/dubbo/com.foo.BarService/providers目录下写入自己的URL地址

➢ 服务消费者启动时:订阅/dubbo/com.foo.BarService/providers目录下的提供者URL地址。 并向/dubbo/com.foo.BarService/consumers目录下写入自己的URL地址

➢ 监控中心启动时:订阅/dubbo/com.foo.BarService目录下的所有提供者和消费者URL地址。

Zookeeper注册中心支持功能

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

同一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" />

特点 ➢ 适合作为Dubbo服务的注册中心,工业强度较高,可用于生产环境,推荐使用。

2 支持多注册中心

Dubbo支持同一服各向多注册中心同吋注册,或者不同服努分別注册到不同的注册中心上去,甚至可以同吋引用注册在不同注册中心上的同名服务器。

向多个注册中心注册

1<dubbo:application name="world" />2<!--多注册中心配置-->3<dubbo:registry id="hangzhouRegistry" address=" 10.20.141.150:9090" /> 4<dubbo:registry id="qingdaoRegistry" address-" 10.20.141.151:9010" default="false" />5<!--向多个注册中心注册-->6<dubbo:service interface="com.alibaba.hello.api.HelloService" version=" 1.0.0" ref-"helloService"7registry="hangzhouRegistry,qingdaoRegistry" >

如:中文站有些服各来不及在青島部署,只在杭州部署,而青島的其它座用需要引用此服 各,就可以将服努同吋注册到兩个注册中心。

不同服务使用不同注册中心

1<dubbo:application name="world" />2<!--多注册中心配置-->3<dubbo:registry id="chinaRegistry" address=" 10.20.141.150:9090" /> 4<dubbo:registry id="intlRegistry" address="10.20.154. 177:9010" default="false" />5<!--向中文站注册中心注册-->6<dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService"7registry="chinaRegistry" />8<!--向国际站注册中心注册-->9<dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService"10registry="intlRegistry" />

如CRM有些服务是专门为国际站设计的,有些服务是专门为中文站设计的。

多注册中心引用

1<dubbo:application name="world" />2<!--多注册中心配置-->3<dubbo:registry id="chinaRegistry" address=" 10.20.141.150:9090" />4<dubbo:registry id="intlRegistry" address=" 10.20.154.177:9010" default="false" />5<!--引用中文站服务-->-6<dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0"7registry="chinaRegistry" /> 8<!--引用国际站站服务-->9<dubbo:referenfce id="intlHelloService" interface= "com.alibaba.hello.api.HelloService" version="1.0.0"10registry= "intlRegistry" />

比如: CRM需同时调用中文站和国际站的PC2服务,PC2 在中文站和国际站均有部署,接 口及版本号都一样,但连的数据库不一样。

多注册中心引用

如果只是測武坏境恪吋需要達接丙个不同注册中心,使用竪号分隔多个不同注册中心地址:

1<dubbo:application name="world"2<!--多注册中心配置,竪号分隔表示同吋達接多个不同注册中心,同一-注册中心的多个集群地址用逗号分隔-->3<dubbo:registry address=" 10.20.141.150:909010.20.154.177:9010" >4<!--引用服努-->5<dubbo:reference id="helloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" />

3 支持多种使用场景

为方便开发、测试、人工管理,注册中心提供多种使用场景支持: ➢只订阅 ➢只注册 ➢直连提供者 ➢静态服务

详细用法配置参见官方文档