(四).dubbo注册中心

1、注册中心类型

根据Dubbo官方文档,Dubbo支持多注册中。

  • ZooKeeper注册中心,也是官方建议使用的注册方式;
  • Multicast注册中心;
  • Redis注册中心;
  • Simple注册中心

1.1、ZooKeeper注册中心

流程说明:

  • 服务提供者启动时
    • 向/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="*" />,可订阅服务的所有分组和所有版本的提供者。

官方建议使用ZooKeeper注册中,后面继续重点描述ZooKeeper注册中心。

1.2、Muticast注册中心

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

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

配置方式:

  • <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>

1.3、redis注册中心

优点:
           Redis说明:Redis是一个高效的KV存储服务器,参见:http://redis.io
           Redis安装:安装方式参见: Redis安装手册,只需搭一个原生的Redis服务器,并将Quick Start中Provider和Consumer里的conf/dubbo.properties中的dubbo.registry.addrss的值改为redis://127.0.0.1:6379即可使用
缺点:
          1.Redis过期数据:通过心跳的方式检测脏数据,服务器时间必须相同,并且对服务器有一定压力。
          2.可靠性声明:阿里内部并没有采用Redis做为注册中心,而是使用自己实现的基于数据库的注册中心,即:Redis注册中心并没有在阿里内部长时间运行的可靠性保障,此Redis桥接实现只为开源版本提供,其可靠性依赖于Redis本身的可靠性。
         3. 从2.1.0版本开始支持
数据结构:
使用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/*,会收到所有服务的所有变更事件。

调用过程:

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

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

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

 

1.4、Simple注册中心

注册中心本身就是一个普通的Dubbo服务,可以减少第三方依赖,使整体通讯方式一致。
此SimpleRegistryService只是简单实现,不支持集群,可作为自定义注册中心的参考,但不适合直接用于生产环境。

配置:

<?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-2.5.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <!-- 当前应用信息配置 -->
    <dubbo:application name="simple-registry" />
 
    <!-- 暴露服务协议配置 -->
    <dubbo:protocol port="9090" />
 
    <!-- 暴露服务配置 -->
    <dubbo:service interface="com.alibaba.dubbo.registry.RegistryService" ref="registryService" registry="N/A" ondisconnect="disconnect" callbacks="1000">
        <dubbo:method name="subscribe"><dubbo:argument index="1" callback="true" /></dubbo:method>
        <dubbo:method name="unsubscribe"><dubbo:argument index="1" callback="false" /></dubbo:method>
    </dubbo:service>
 
    <!-- 简单注册中心实现,可自行扩展实现集群和状态同步 -->
    <bean id="registryService" class="com.alibaba.dubbo.registry.simple.SimpleRegistryService" />
 
</beans>

引用:

<dubbo:registry address="127.0.0.1:9090" />

<dubbo:service interface="com.alibaba.dubbo.registry.RegistryService" group="simple" version="1.0.0" ... >

<dubbo:registry address="127.0.0.1:9090" group="simple" version="1.0.0" />

2、Zookeeper注册中心

因为官方建议使用Zookeeper注册中心,主要是ZooKeeper的注册中心的配置形式

2.1、单机配置

最简单的一种配置,如下两种方式进行配置:

  • <dubbo:registry address="zookeeper://10.20.153.10:2181" />
  • <dubbo:registry protocol="zookeeper" address="10.20.153.10:2181" />

2.2、ZooKeeper集群配置

就是将ZooKeeper集群配置到Dubbo配置文件中,主要为了某个ZooKeeper挂了,不影响Dubbo的正常使用。也有两种配置方式如下:

  • <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" />

2.3、多注册中心

2.3.1多注册中心注册

应用场景:
     中文站有些服务来不及在青岛部署,只在杭州部署,而青岛的其它应用需要引用此服务,就可以将服务同时注册到两个注册中心。

配置:

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <dubbo:application name="world"  />
 
    <!-- 多注册中心配置 -->
    <dubbo:registry id="hangzhouRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="qingdaoRegistry" address="10.20.141.151:9010" default="false" />
 
    <!-- 向多个注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="hangzhouRegistry,qingdaoRegistry" />
 
</beans>

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

应用场景:
    CRM有些服务是专门为国际站设计的,有些服务是专门为中文站设计的。
配置:

   

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <dubbo:application name="world"  />
 
    <!-- 多注册中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
 
    <!-- 向中文站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.HelloService" version="1.0.0" ref="helloService" registry="chinaRegistry" />
 
    <!-- 向国际站注册中心注册 -->
    <dubbo:service interface="com.alibaba.hello.api.DemoService" version="1.0.0" ref="demoService" registry="intlRegistry" />
 
</beans>

2.3.3多注册中心引用

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

   

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://code.alibabatech.com/schema/dubbohttp://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <dubbo:application name="world"  />
 
    <!-- 多注册中心配置 -->
    <dubbo:registry id="chinaRegistry" address="10.20.141.150:9090" />
    <dubbo:registry id="intlRegistry" address="10.20.154.177:9010" default="false" />
 
    <!-- 引用中文站服务 -->
    <dubbo:reference id="chinaHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="chinaRegistry" />
 
    <!-- 引用国际站站服务 -->
    <dubbo:reference id="intlHelloService" interface="com.alibaba.hello.api.HelloService" version="1.0.0" registry="intlRegistry" />
 
</beans>

 

3.<dubbo:registry/>

dubbo注册的其他属性,暂时都没有使用到,贴出官方的属性的用法。

注册中心配置:

配置类:com.alibaba.dubbo.config.RegistryConfig
说明:如果有多个不同的注册中心,可以声明多个<dubbo:registry>标签,并在<dubbo:service>或<dubbo:reference>的registry属性指定使用的注册中心。

标签 属性 对应URL参数 类型 是否必填 缺省值 作用 描述 兼容性
<dubbo:registry> id   string 可选   配置关联 注册中心引用BeanId,可以在<dubbo:service registry="">或<dubbo:reference registry="">中引用此ID 1.0.16以上版本
<dubbo:registry> address <host:port> string 必填   服务发现 注册中心服务器地址,如果地址没有端口缺省为9090,同一集群内的多个地址用逗号分隔,如:ip:port,ip:port,不同集群的注册中心,请配置多个<dubbo:registry>标签 1.0.16以上版本
<dubbo:registry> protocol <protocol> string 可选 dubbo 服务发现 注同中心地址协议,支持dubbo, http, local三种协议,分别表示,dubbo地址,http地址,本地注册中心 2.0.0以上版本
<dubbo:registry> port <port> int 可选 9090 服务发现 注册中心缺省端口,当address没有带端口时使用此端口做为缺省值 2.0.0以上版本
<dubbo:registry> username <username> string 可选   服务治理 登录注册中心用户名,如果注册中心不需要验证可不填 2.0.0以上版本
<dubbo:registry> password <password> string 可选   服务治理 登录注册中心密码,如果注册中心不需要验证可不填 2.0.0以上版本
<dubbo:registry> transport registry.transporter string 可选 netty 性能调优 网络传输方式,可选mina,netty 2.0.0以上版本
<dubbo:registry> timeout registry.timeout int 可选 5000 性能调优 注册中心请求超时时间(毫秒) 2.0.0以上版本
<dubbo:registry> session registry.session int 可选 60000 性能调优 注册中心会话超时时间(毫秒),用于检测提供者非正常断线后的脏数据,比如用心跳检测的实现,此时间就是心跳间隔,不同注册中心实现不一样。 2.1.0以上版本
<dubbo:registry> file registry.file string 可选   服务治理 使用文件缓存注册中心地址列表及服务提供者列表,应用重启时将基于此文件恢复,注意:两个注册中心不能使用同一文件存储 2.0.0以上版本
<dubbo:registry> wait registry.wait int 可选 0 性能调优 停止时等待通知完成时间(毫秒) 2.0.0以上版本
<dubbo:registry> check check boolean 可选 true 服务治理 注册中心不存在时,是否报错 2.0.0以上版本
<dubbo:registry> register register boolean 可选 true 服务治理 是否向此注册中心注册服务,如果设为false,将只订阅,不注册 2.0.5以上版本
<dubbo:registry> subscribe subscribe boolean 可选 true 服务治理 是否向此注册中心订阅服务,如果设为false,将只注册,不订阅 2.0.5以上版本
<dubbo:registry> dynamic dynamic boolean 可选 true 服务治理 服务是否动态注册,如果设为false,注册后将显示后disable状态,需人工启用,并且服务提供者停止时,也不会自动取消册,需人工禁用。 2.0.5以上版本

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章