# ZooKeeper

## 分布式协调

A 系统发送个请求到 mq，然后 B 消息消费之后处理了。那 A 系统如何知道 B 系统的处理结果？

**解决方式**：A 系统发送请求之后可以在 zk 上对某个节点的值注册个监听器，一旦 B 系统处理完了就修改 zk 那个节点的值，A 立马就可以收到通知。

![](/files/-LbDZfMDk9HVTXADuH3H)

## 分布式锁

1. 节点 A 尝试创建**临时** znode，创建成功则表示获取了这个锁；
2. 此时节点 B 创建锁会失败，转而注册个**监听器**监听这个锁。
3. 节点 A 释放锁就是删除这个 znode，删除后 zk 会通知节点 B，节点 B 可以再次加锁。

## 元数据/配置信息管理

Kafka、Storm、Dubbo 等很多分布式系统都会选用 zk 来做一些元数据、配置信息的管理。

## HA&#x20;

Hadoop、HDFS、Yarn 等很多大数据系统，都选择基于 zk 来开发 HA 高可用机制。原理：一个重要进程一般会做主备两个，主进程挂了立马通过 zk 感知到切换到备用进程。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://yunzhao.gitbook.io/notes/database/zookeeper-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
