dubbo注册中心

  官方推荐的是zookeeper注册中心。

1.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>

 测试代码:

provider.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hello-world-app">
        <!-- 关闭QOS:Quality of Service -->
        <dubbo:parameter key="qos.enable" value="false" />
        <dubbo:parameter key="qos.accept.foreign.ip" value="true" />
        <dubbo:parameter key="qos.port" value="2222" />
    </dubbo:application>
        
    <dubbo:registry address="multicast://224.5.6.7:1234" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" dispatcher="message" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="dubbo.DubboService" ref="demoService"
        timeout="500000" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="dubbo.DubboServiceImpl" />
</beans>

 consumer.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="consumer-of-helloworld-app">
        <!-- 关闭QOS:Quality of Service -->
        <dubbo:parameter key="qos.enable" value="false" />
    </dubbo:application>

    <!-- 使用multicast广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="multicast://224.5.6.7:1234" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="dubboService" interface="dubbo.DubboService" />
</beans>

 2.zookeeper 注册中心

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

 

流程说明:

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

 使用:

 1.   pom.xml增加zookeeper依赖以及zkclient依赖(从 2.2.0 版本开始缺省为 zkclient 实现,另外还有curator常用的API)

 2.单机版配置如下:

<dubbo:registry address="zookeeper://127.0.0.1:2181" />

或者:

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

集群方式如下:

    <dubbo:registry protocol="zookeeper"
        address="127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183" />

或者:

    <dubbo:registry
        address="zookeeper://127.0.0.1:2181?backup=127.0.0.1:2182,127.0.0.1:2183" />

  前提是安装好zookeeper或者对zookeeper进行集群。

3.  Redis 注册中心

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

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


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

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

配置方式

单机版:

<dubbo:registry address="redis://127.0.0.1:6379" />

或者:

    <dubbo:registry protocol="redis" address="127.0.0.1:6379" />

集群:

<dubbo:registry address="redis://10.20.153.10:6379?backup=10.20.153.11:6379,10.20.153.12:6379" />

或者:

<dubbo:registry protocol="redis" address="10.20.153.10:6379,10.20.153.11:6379,10.20.153.12:6379" />

结果: (URL作为key存储)

 

4. Simple 注册中心

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

配置 

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

<?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://dubbo.apache.org/schema/dubbo"
    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">
    <!-- 当前应用信息配置 -->
    <dubbo:application name="simple-registry" />
    <!-- 暴露服务协议配置 -->
    <dubbo:protocol port="9090" />
    <!-- 暴露服务配置 -->
    <dubbo:service interface="org.apache.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="org.apache.dubbo.registry.simple.SimpleRegistryService" />
</beans>

引用 Simple Registry 服务:

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

或者:

<dubbo:service interface="org.apache.dubbo.registry.RegistryService" group="simple" version="1.0.0" ... >

或者:

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

适用性说明:

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

 

  这个Simple我没有测试过,上面三种经过测试是可用的。而且在自己的SOA中用的zookeeper做注册中心来使用。。。

原文地址:https://www.cnblogs.com/qlqwjy/p/10751482.html