List of typical application scenarios of ZooKeeper |
Data publishing and subscription (configuration center) |
The publish and subscribe model, the so-called configuration center, as the name implies, is that the publisher publishes data to the ZK node for the subscriber to dynamically obtain the data, and realizes the centralized management and dynamic update of the configuration information. For example, global configuration information, service address list of service-based service framework, etc. are very suitable for use. |
Note : In the application scenarios mentioned above, there is a default premise: the amount of data is small, but the data update may be faster. |
load balancing |
The load balancing mentioned here refers to soft load balancing. In a distributed environment, in order to ensure high availability, usually the same application or the same service provider will deploy multiple copies to achieve peer-to-peer services. Consumers need to choose one of these peer servers to execute related business logic, among which is the typical producer and consumer load balancing in message middleware. |
For the load balancing of publishers and subscribers in message middleware, linkedin's open source KafkaMQ and Alibaba's open source metaq both use zookeeper to achieve load balancing between producers and consumers. Here is an example of metaq: Producer load balancing : When metaq sends a message, the producer must select a partition on a broker to send the message, so metaq will transfer all brokers during the running process. And the corresponding partition information is all registered on the designated node of ZK. The default strategy is a round-robin process. After the producer obtains the partition list through ZK, it will be organized into an ordered partition list according to the order of brokerId and partition. , when sending, select a partition to send the message in a circular way from beginning to end.
Consumption load balancing: During the consumption process, a consumer will consume messages in one or more partitions, but a partition will only be consumed by one consumer. MetaQ's consumption strategy is:
In the event of a consumer failure or restart, other consumers will perceive the change (watch the consumer list through zookeeper), and then re-balance the load to ensure that all partitions have consumers for consumption. |
Naming Service |
Naming services are also a common type of scenario in distributed systems. In a distributed system, by using the naming service, the client application can obtain the address, provider and other information of the resource or service according to the specified name. The named entity can usually be a machine in the cluster, the address of the service provided, the remote object, etc. - these we can collectively call them the name (Name). One of the more common ones is the list of service addresses in some distributed service frameworks. By calling the API for creating nodes provided by ZK, it is easy to create a globally unique path, which can be used as a name. |
阿里巴巴集团开源的分布式服务框架Dubbo中使用ZooKeeper来作为其命名服务,维护全局的服务地址列表,点击这里查看Dubbo开源项目。在Dubbo实现中:
服务提供者在启动的时候,向ZK上的指定节点/dubbo/${serviceName}/providers目录下写入自己的URL地址,这个操作就完成了服务的发布。 服务消费者启动的时候,订阅/dubbo/${serviceName}/providers目录下的提供者URL地址, 并向/dubbo/${serviceName} /consumers目录下写入自己的URL地址。 注意,所有向ZK上注册的地址都是临时节点,这样就能够保证服务提供者和消费者能够自动感应资源的变化。 另外,Dubbo还有针对服务粒度的监控,方法是订阅/dubbo/${serviceName}目录下所有提供者和消费者的信息。 |
分布式通知/协调 |
ZooKeeper中特有watcher注册与异步通知机制,能够很好的实现分布式环境下不同系统之间的通知与协调,实现对数据变更的实时处理。使用方法通常是不同系统都对ZK上同一个znode进行注册,监听znode的变化(包括znode本身内容及子节点的),其中一个系统update了znode,那么另一个系统能够收到通知,并作出相应处理 |
总之,使用zookeeper来进行分布式通知和协调能够大大降低系统之间的耦合 |
集群管理与Master选举 |
利用ZooKeeper有两个特性,就可以实时另一种集群机器存活性监控系统:
例如,监控系统在 /clusterServers 节点上注册一个Watcher,以后每动态加机器,那么就往 /clusterServers 下创建一个 EPHEMERAL类型的节点:/clusterServers/{hostname}. 这样,监控系统就能够实时知道机器的增减情况,至于后续处理就是监控系统的业务了。
在分布式环境中,相同的业务应用分布在不同的机器上,有些业务逻辑(例如一些耗时的计算,网络I/O处理),往往只需要让整个集群中的某一台机器进行执行,其余机器可以共享这个结果,这样可以大大减少重复劳动,提高性能,于是这个master选举便是这种场景下的碰到的主要问题。 利用ZooKeeper的强一致性,能够保证在分布式高并发情况下节点创建的全局唯一性,即:同时有多个客户端请求创建 /currentMaster 节点,最终一定只有一个客户端请求能够创建成功。利用这个特性,就能很轻易的在分布式环境中进行集群选取了。 另外,这种场景演化一下,就是动态Master选举。这就要用到?EPHEMERAL_SEQUENTIAL类型节点的特性了。 上文中提到,所有客户端创建请求,最终只有一个能够创建成功。在这里稍微变化下,就是允许所有请求都能够创建成功,但是得有个创建顺序,于是所有的请求最终在ZK上创建结果的一种可能情况是这样: /currentMaster/{sessionId}-1 ,?/currentMaster/{sessionId}-2 ,?/currentMaster/{sessionId}-3 ….. 每次选取序列号最小的那个机器作为Master,如果这个机器挂了,由于他创建的节点会马上小时,那么之后最小的那个机器就是Master了。 |
|
分布式锁 |
分布式锁,这个主要得益于ZooKeeper为我们保证了数据的强一致性。锁服务可以分为两类,一个是保持独占,另一个是控制时序。
|
分布式队列 |
队列方面,简单地讲有两种,一种是常规的先进先出队列,另一种是要等到队列成员聚齐之后的才统一按序执行。对于第一种先进先出队列,和分布式锁服务中的控制时序场景基本原理一致,这里不再赘述。
第二种队列其实是在FIFO队列的基础上作了一个增强。通常可以在 /queue 这个znode下预先建立一个/queue/num 节点,并且赋值为n(或者直接给/queue赋值n),表示队列大小,之后每次有队列成员加入后,就判断下是否已经到达队列大小,决定是否可以开始执行了。这种用法的典型场景是,分布式环境中,一个大任务Task A,需要在很多子任务完成(或条件就绪)情况下才能进行。这个时候,凡是其中一个子任务完成(就绪),那么就去 /taskList 下建立自己的临时时序节点(CreateMode.EPHEMERAL_SEQUENTIAL),当 /taskList 发现自己下面的子节点满足指定个数,就可以进行下一步按序进行处理了。 |