List of typical application scenarios of zookeeper

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.
  • Some configuration information used in the application is placed on the ZK for centralized management. This kind of scenario is usually like this: the application will take the initiative to obtain a configuration when it starts, and at the same time, register a Watcher on the node, so that every time the configuration is updated in the future, it will notify the subscribed client in real time. Always achieve the purpose of obtaining the latest configuration information.
  • In the distributed search service, the meta information of the index and the node status of the server cluster machine are stored in some designated nodes of ZK for each client to subscribe.
  • Distributed log collection system. The core job of this system is to collect logs distributed across different machines. The collector usually allocates collection task units according to the application, so it is necessary to create a node P with the application name as the path on the ZK, and register all the machine IPs of this application on the node P in the form of child nodes, so that a When the machine changes, the collector can be notified in real time to adjust the task allocation.
  • Some information in the system needs to be dynamically obtained, and there will be questions about manually modifying this information manually. Usually an interface is exposed, such as a JMX interface, to obtain some runtime information. After the introduction of ZK, there is no need to implement a set of solutions by yourself, as long as the information is stored on the designated ZK node.

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:

  • Each partition mounts only one consumer for the same group.
  • If the number of consumers in the same group is greater than the number of partitions, the excess consumers will not participate in consumption.
  • If the number of consumers in the same group is less than the number of partitions, some consumers need to undertake additional consumption tasks.

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,那么另一个系统能够收到通知,并作出相应处理
  • 另一种心跳检测机制:检测系统和被检测系统之间并不直接关联起来,而是通过zk上某个节点关联,大大减少系统耦合。
  • 另一种系统调度模式:某系统有控制台和推送系统两部分组成,控制台的职责是控制推送系统进行相应的推送工作。管理人员在控制台作的一些操作,实际上是修改了ZK上某些节点的状态,而ZK就把这些变化通知给他们注册Watcher的客户端,即推送系统,于是,作出相应的推送任务。
  • 另一种工作汇报模式:一些类似于任务分发系统,子任务启动后,到zk来注册一个临时节点,并且定时将自己的进度进行汇报(将进度写回这个临时节点),这样任务管理者就能够实时知道任务进度。

总之,使用zookeeper来进行分布式通知和协调能够大大降低系统之间的耦合

集群管理与Master选举
  • 集群机器监控:这通常用于那种对集群中机器状态,机器在线率有较高要求的场景,能够快速对集群中机器变化作出响应。这样的场景中,往往有一个监控系统,实时检测集群机器是否存活。过去的做法通常是:监控系统通过某种手段(比如ping)定时检测每个机器,或者每个机器自己定时向监控系统汇报“我还活着”。 这种做法可行,但是存在两个比较明显的问题:
  1. 集群中机器有变动的时候,牵连修改的东西比较多。
  2. 有一定的延时。

利用ZooKeeper有两个特性,就可以实时另一种集群机器存活性监控系统:

  1. 客户端在节点 x 上注册一个Watcher,那么如果 x?的子节点变化了,会通知该客户端。
  2. 创建EPHEMERAL类型的节点,一旦客户端和服务器的会话结束或过期,那么该节点就会消失。

例如,监控系统在 /clusterServers 节点上注册一个Watcher,以后每动态加机器,那么就往 /clusterServers 下创建一个 EPHEMERAL类型的节点:/clusterServers/{hostname}. 这样,监控系统就能够实时知道机器的增减情况,至于后续处理就是监控系统的业务了。

  • Master选举则是zookeeper中最为经典的应用场景了。

在分布式环境中,相同的业务应用分布在不同的机器上,有些业务逻辑(例如一些耗时的计算,网络I/O处理),往往只需要让整个集群中的某一台机器进行执行,其余机器可以共享这个结果,这样可以大大减少重复劳动,提高性能,于是这个master选举便是这种场景下的碰到的主要问题。

利用ZooKeeper的强一致性,能够保证在分布式高并发情况下节点创建的全局唯一性,即:同时有多个客户端请求创建 /currentMaster 节点,最终一定只有一个客户端请求能够创建成功。利用这个特性,就能很轻易的在分布式环境中进行集群选取了。

另外,这种场景演化一下,就是动态Master选举。这就要用到?EPHEMERAL_SEQUENTIAL类型节点的特性了。

上文中提到,所有客户端创建请求,最终只有一个能够创建成功。在这里稍微变化下,就是允许所有请求都能够创建成功,但是得有个创建顺序,于是所有的请求最终在ZK上创建结果的一种可能情况是这样: /currentMaster/{sessionId}-1 ,?/currentMaster/{sessionId}-2 ,?/currentMaster/{sessionId}-3 ….. 每次选取序列号最小的那个机器作为Master,如果这个机器挂了,由于他创建的节点会马上小时,那么之后最小的那个机器就是Master了。

  • 在搜索系统中,如果集群中每个机器都生成一份全量索引,不仅耗时,而且不能保证彼此之间索引数据一致。因此让集群中的Master来进行全量索引的生成,然后同步到集群中其它机器。另外,Master选举的容灾措施是,可以随时进行手动指定master,就是说应用在zk在无法获取master信息时,可以通过比如http方式,向一个地方获取master。
  • 在Hbase中,也是使用ZooKeeper来实现动态HMaster的选举。在Hbase实现中,会在ZK上存储一些ROOT表的地址和HMaster的地址,HRegionServer也会把自己以临时节点(Ephemeral)的方式注册到Zookeeper中,使得HMaster可以随时感知到各个HRegionServer的存活状态,同时,一旦HMaster出现问题,会重新选举出一个HMaster来运行,从而避免了HMaster的单点问题
分布式锁
分布式锁,这个主要得益于ZooKeeper为我们保证了数据的强一致性。锁服务可以分为两类,一个是保持独占,另一个是控制时序

 

  • 所谓保持独占,就是所有试图来获取这个锁的客户端,最终只有一个可以成功获得这把锁。通常的做法是把zk上的一个znode看作是一把锁,通过create znode的方式来实现。所有客户端都去创建 /distribute_lock 节点,最终成功创建的那个客户端也即拥有了这把锁。
  • 控制时序,就是所有视图来获取这个锁的客户端,最终都是会被安排执行,只是有个全局时序了。做法和上面基本类似,只是这里 /distribute_lock 已经预先存在,客户端在它下面创建临时有序节点(这个可以通过节点的属性控制:CreateMode.EPHEMERAL_SEQUENTIAL来指定)。Zk的父节点(/distribute_lock)维持一份sequence,保证子节点创建的时序性,从而也形成了每个客户端的全局时序。
分布式队列
队列方面,简单地讲有两种,一种是常规的先进先出队列,另一种是要等到队列成员聚齐之后的才统一按序执行。对于第一种先进先出队列,和分布式锁服务中的控制时序场景基本原理一致,这里不再赘述。

 

第二种队列其实是在FIFO队列的基础上作了一个增强。通常可以在 /queue 这个znode下预先建立一个/queue/num 节点,并且赋值为n(或者直接给/queue赋值n),表示队列大小,之后每次有队列成员加入后,就判断下是否已经到达队列大小,决定是否可以开始执行了。这种用法的典型场景是,分布式环境中,一个大任务Task A,需要在很多子任务完成(或条件就绪)情况下才能进行。这个时候,凡是其中一个子任务完成(就绪),那么就去 /taskList 下建立自己的临时时序节点(CreateMode.EPHEMERAL_SEQUENTIAL),当 /taskList 发现自己下面的子节点满足指定个数,就可以进行下一步按序进行处理了。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326442309&siteId=291194637