为什么推荐Zookeeper作注册中心
Zookeeper的数据模型很简单,有一系列被称为ZNode的数据节点组成,与传统的磁盘文件系统不同的是,zk将全量数据存储在内存中,可谓是高性能,而且支持集群,可谓高可用,另外支持事件监听。这些特点决定了zk特别适合作为注册中心(数据发布/订阅)。
Zookeeper注册中心
![]() |
建议使用dubbo-2.3.3以上版本的zookeeper注册中心客户端 |
![]() |
Zookeeper说明 Zookeeper是Apacahe Hadoop的子项目,是一个树型的目录服务,支持变更推送,适合作为Dubbo服务的注册中心,工业强度较高,可用于生产环境,并推荐使用,参见:http://zookeeper.apache.org |
![]() |
Zookeeper安装 安装方式参见: Zookeeper安装手册,只需搭一个原生的Zookeeper服务器,并将Quick Start中Provider和Consumer里的conf/dubbo.properties中的dubbo.registry.addrss的值改为zookeeper://127.0.0.1:2181即可使用 |
![]() |
可靠性声明 阿里内部并没有采用Zookeeper做为注册中心,而是使用自己实现的基于数据库的注册中心,即:Zookeeper注册中心并没有在阿里内部长时间运行的可靠性保障,此Zookeeper桥接实现只为开源版本提供,其可靠性依赖于Zookeeper本身的可靠性。 |
![]() |
兼容性声明 因2.0.8最初设计的zookeeper存储结构不能扩充不同类型的数据,2.0.9版本做了调整,所以不兼容,需全部改用2.0.9版本才行,以后的版本会保持兼容2.0.9。 2.2.0版本改为基于zkclient实现,需增加zkclient的依赖包,2.3.0版本增加了基于curator的实现,作为可选实现策略。 |

流程说明:
- 服务提供者启动时
- 向/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客户端的健状性。
![]() |
ZKClient是Datameer开源的一个Zookeeper客户端实现,开源比较早,参见:https://github.com/sgroschupf/zkclient |
缺省配置:
<dubbo:registry ... client="zkclient" />
|
或:
dubbo.registry.client=zkclient |
或:
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是Netflix开源的一个Zookeeper客户端实现,比较活跃,参见:https://github.com/Netflix/curator |
如果需要改为curator实现,请配置:
<dubbo:registry ... client="curator" />
|
或:
dubbo.registry.client=curator |
或:
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说明 Redis是一个高效的KV存储服务器,参见:http://redis.io |
![]() |
Redis安装 安装方式参见: Redis安装手册,只需搭一个原生的Redis服务器,并将Quick Start中Provider和Consumer里的conf/dubbo.properties中的dubbo.registry.addrss的值改为redis://127.0.0.1:6379即可使用 |
![]() |
Redis过期数据 通过心跳的方式检测脏数据,服务器时间必须相同,并且对服务器有一定压力。 |
![]() |
可靠性声明 阿里内部并没有采用Redis做为注册中心,而是使用自己实现的基于数据库的注册中心,即:Redis注册中心并没有在阿里内部长时间运行的可靠性保障,此Redis桥接实现只为开源版本提供,其可靠性依赖于Redis本身的可靠性。 |
![]() |
从2.1.0版本开始支持 |

数据结构:
- 使用Redis的Key/Map结构存储数据。
- 主Key为服务名和类型。
- Map中的Key为URL地址。
- Map中的Value为过期时间,用于判断脏数据,脏数据由监控中心删除。(注意:服务器时间必需同步,否则过期检测会不准确)
- 使用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/providers下,添加当前消费者的地址。
- 服务消费方收到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 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注册中心
![]() |
Dogfooding 注册中心本身就是一个普通的Dubbo服务,可以减少第三方依赖,使整体通讯方式一致。 |
![]() |
适用性说明 此SimpleRegistryService只是简单实现,不支持集群,可作为自定义注册中心的参考,但不适合直接用于生产环境。 |
Export simple registry service:
<?xml version="1.0" encoding="UTF-8"?>
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>
|
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" />
|
为什么推荐Zookeeper作注册中心的更多相关文章
- Dubbo原理解析-注册中心之Zookeeper协议注册中心
下面我们来看下开源dubbo推荐的业界成熟的zookeeper做为注册中心, zookeeper是hadoop的一个子项目是分布式系统的可靠协调者,他提供了配置维护,名字服务,分布式同步等服务.对于z ...
- springboot整合dubbo\zookeeper做注册中心
springboot整合dubbo发布服务,zookeeper做注册中心.前期的安装zookeeper以及启动zookeeper集群就不说了. dubbo-admin-2.5.4.war:dubbo服 ...
- 以zookeeper为注册中心搭建spring cloud环境
在spring cloud体系中,有多种手段实现注册中心,本例中采用zookeeper作为注册中心的角色.服务提供者向zookeeper注册,服务消费者从zookeeper中发现服务提供者的相关信息, ...
- Zookeeper用作注册中心的原理
RPC框架中有3个重要的角色: 注册中心 :保存所有服务的名字,服务提供者的ip列表,服务消费者的IP列表: 服务提供者: 提供跨进程服务: 服务消费者: 寻找到指定命名的服务并消费. 一:Zooke ...
- zookeeper作配置中心(存储支付信息)
zookeeper作配置中心(存储敏感信息) 前提:最近在项目中需要用到支付接口,支付宝或者微信支付,根据官方文档,需要配置一些诸如notify-url或者app-private-key等信息,这些信 ...
- SpringCloud之使用Zookeeper作为注册中心
SpringCloud之使用Zookeeper作为注册中心 linux安装zookeeper 安装zookeeper 关闭linux防火墙 启动zookeeper 1 创建项目导入依赖和配置文件 &l ...
- 面试题:Dubbo中zookeeper做注册中心,如果注册中心集群全都挂掉,发布者和订阅者之间还能通信么?
1.[提供者]在[启动]时,向注册中心zk [注册]自己提供的服务. 2.[消费者]在[启动]时,向注册中心zk [订阅]自己所需的服务. 可以的,消费者在启动时,消费者会从zk拉取注册的生产者的 ...
- Linux上搭建zookeeper服务注册中心
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- Zookeeper注册中心底层实现小记
内容摘自微信公众号,程序员小灰.推荐-ing Zookeeper的数据模型 Zookeeper的数据模型是什么样子呢?它很像数据结构当中的树,也很像文件系统的目录. 树是由节点所组成,Zookeepe ...
随机推荐
- angular中的cookie读写
AngularJS中对cookie的操作封装了一个单独的模块,模块名为ngCookies,若想使用需在页面中先引入angular-cookies.js: <script src="js ...
- Lua 数据类型和 Redis 数据类型之间转换
当 Lua 通过 call() 或 pcall() 函数执行 Redis 命令的时候,命令的返回值会被转换成 Lua 数据结构. 同样地,当 Lua 脚本在 Redis 内置的解释器里运行时,Lua ...
- javascriptDOM对象之scrollTo()方法,滚动到页面指定位置
scrollTo是一个神奇的方法,常用于篇幅过长的页面,制作一个回顶部的按钮,我这里简单的实现以下 当然没有一个过渡的js效果 scrollTo(xpos,ypos) 参数 描述 xpos 必需.要在 ...
- 【IIS】windows2008 ii7 设置访问网站提示帐号密码登录
3个步骤: 1.添加windows身份验证: windows2008默认是不启用的,需要我们自己去启动,在管理工具 - 服务器管理- 角色 ,拉下去,下面有个[添加角色服务],安全性- Windows ...
- Java Socket编程基础(1)
参考资料: <Java网络编程精解> 孙卫琴 一.socket通信简介 什么是socket,简单来说,在linux系统上,进程与进程之间的通信称为IPC,在同一台计算机中,进程与进程之间通 ...
- HDU 1040 As Easy As A+B(排序)
As Easy As A+B Problem Description These days, I am thinking about a question, how can I get a probl ...
- iOS参考工具和资源
图片: Glyphish(图标资源) 资源: SwiftGuide:这份指南汇集了Swift语言主流学习资源,并以开发者的视角整理编排. 27款iOS开源库,让你的开发溜到飞起 创业者的新春礼包—优秀 ...
- Visual Studio 2010 使用外部代码格式化工具 AStyle
其中AStyle.exe 最好自己编译,放置到 Vistual Studio 安装目录\\Common7\\Tools 直接上配置截图: 单文件: 一次性格式化整个工程:
- HTML5历史管理
边看视频边做的练习,随机显示数字,分别使用history和hash来实现历史管理 <!doctype html> <html> <head> <meta ch ...
- Byte Array to Hexadecimal String
Lookup Text: 23,879.41 (20.8X faster) Sentence: 1.15 (23.9X faster) /// <summary> /// Hex stri ...

