前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dubbo入门学习--Dubbo注册及监控中心(3)

Dubbo入门学习--Dubbo注册及监控中心(3)

作者头像
Java帮帮
发布2018-03-19 18:04:06
1K0
发布2018-03-19 18:04:06
举报

Dubbo入门学习--Dubbo注册及监控中心

Dubbo提供广播、Zookeeper、Redis和Simple四种注册中心类型。

Multicast注册中心

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

<dubbo:registry address="multicast://224.5.6.7:1234" />

Or:

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

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

<dubbo:registry address="multicast://224.5.6.7:1234?unicast=false" />

Or:

<dubbo:registry protocol="multicast" address="224.5.6.7:1234">

<dubbo:parameter key="unicast" value="false" />

</dubbo:registry>

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

在provider和consumer中增加zookeeper客户端jar包依赖:

<dependency>

<groupId>org.apache.zookeeper</groupId>

<artifactId>zookeeper</artifactId>

<version>3.3.3</version>

</dependency>

或直接下载:http://repo1.maven.org/maven2/org/apache/zookeeper/zookeeper

支持zkclient和curator两种Zookeeper客户端实现:

ZKClient Zookeeper Registry

从2.2.0版本开始缺省为zkclient实现,以提升zookeeper客户端的健状性。

缺省配置:

<dubbo:registry ... client="zkclient" />

Or:

dubbo.registry.client=zkclient

Or:

zookeeper://10.20.153.10:2181?client=zkclient

需依赖:

<dependency>

<groupId>com.github.sgroschupf</groupId>

<artifactId>zkclient</artifactId>

<version>0.1</version>

</dependency>

或直接下载:http://repo1.maven.org/maven2/com/github/sgroschupf/zkclient

Curator Zookeeper Registry

从2.3.0版本开始支持可选curator实现。

如果需要改为curator实现,请配置:

<dubbo:registry ... client="curator" />

Or:

dubbo.registry.client=curator

Or:

zookeeper://10.20.153.10:2181?client=curator

需依赖

<dependency>

<groupId>com.netflix.curator</groupId>

<artifactId>curator-framework</artifactId>

<version>1.1.10</version>

</dependency>

或直接下载:http://repo1.maven.org/maven2/com/netflix/curator/curator-framework

Zookeeper单机配置:

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

Or:

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

Or:

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

Redis注册中心

数据结构:

  • 使用Redis的Key/Map结构存储数据。
    • 主Key为服务名和类型。
    • Map中的Key为URL地址。
    • Map中的Value为过期时间,用于判断脏数据,脏数据由监控中心删除。(注意:服务器时间必需同步,否则过期检测会不准确)
  • 使用Redis的Publish/Subscribe事件通知数据变更。
    • 通过事件的值区分事件类型:register, unregister, subscribe, unsubscribe。
    • 普通消费者直接订阅指定服务提供者的Key,只会收到指定服务的register, unregister事件。
    • 监控中心通过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: 在客户端同时写入所有服务器,只读取单台,服务器端不需要同步,注册中心集群增大,性能压力也会更大。

Config redis registry:

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

Or:

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

Or:

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

Or:

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

Simple注册中心

Export simple registry service:

<!-- 当前应用信息配置 -->

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

Reference the simple registry service:

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

Or:

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

Simple监控中心

1.1 暴露一个简单监控中心服务到注册中心: (如果是用安装包,不需要自己写这个配置,如果是自己实现监控中心,则需要)

<!-- 当前应用信息配置 -->

<dubbo:application name="simple-monitor" />

<!-- 连接注册中心配置 -->

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

<!-- 暴露服务协议配置 -->

<dubbo:protocol port="7070" />

<!-- 暴露服务配置 -->

<dubbo:service interface="com.alibaba.dubbo.monitor.MonitorService" ref="monitorService" />

<bean id="monitorService" class="com.alibaba.dubbo.monitor.simple.SimpleMonitorService" />

1.2 通过注册中心发现监控中心服务:

<dubbo:monitor protocol="registry" />

Or:

dubbo.monitor.protocol=registry

2.1 暴露一个简单监控中心服务,但不注册到注册中心: (如果是用安装包,不需要自己写这个配置,如果是自己实现监控中心,则需要)

<!-- 当前应用信息配置 -->

<dubbo:application name="simple-monitor" />

<!-- 暴露服务协议配置 -->

<dubbo:protocol port="7070" />

<!-- 暴露服务配置 -->

<dubbo:service interface="com.alibaba.dubbo.monitor.MonitorService" ref="monitorService" registry="N/A" />

<bean id="monitorService" class="com.alibaba.dubbo.monitor.simple.SimpleMonitorService" />

2.2 直连监控中心服务:

<dubbo:monitor address="dubbo://127.0.0.1:7070/com.alibaba.dubbo.monitor.MonitorService" />

Or:

<dubbo:monitor address="127.0.0.1:7070" />

Or:

dubbo.monitor.address=127.0.0.1:7070

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-06-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java帮帮 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Zookeeper注册中心
    • ZKClient Zookeeper Registry
      • Curator Zookeeper Registry
      • Redis注册中心
      • Simple注册中心
      • Simple监控中心
      相关产品与服务
      微服务引擎 TSE
      微服务引擎(Tencent Cloud Service Engine)提供开箱即用的云上全场景微服务解决方案。支持开源增强的云原生注册配置中心(Zookeeper、Nacos 和 Apollo),北极星网格(腾讯自研并开源的 PolarisMesh)、云原生 API 网关(Kong)以及微服务应用托管的弹性微服务平台。微服务引擎完全兼容开源版本的使用方式,在功能、可用性和可运维性等多个方面进行增强。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档