ZooKeeper是一個開源的致力於分佈式協調服務的服務器。
Apache ZooKeeper is an effort to develop and maintain an open-source server which enables highly reliable distributed coordination.
ZooKeeper爲分佈式應用提供了配置信息管理、命名服務、分佈式鎖等基礎服務。在解決分佈式數據一致性方面,並沒有直接採用Paxos算法,而是採用ZAB(ZooKeeper Atomic Broadcast)的分佈式數據一致性協議。
ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them, which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.
ZooKeeper可以保證如下的分佈式一致性特性。
ZooKeeper使用ZNode結構存儲數據。
ZNode是ZooKeeper數據的最小單元,大小應該小於1MB。除了可以存儲數據,還可以掛載子節點。
多個數據節點按照層次化結構進行組織,形成了樹。
The ZooKeeper client and the server implementations have sanity checks to ensure that znodes have less than 1M of data, but the data should be much less than that on average.
節點特性
節點類型有:持久節點、臨時節點、順序節點、容器節點(3.5.3版本)、TTL節點(3.5.3版本)五種類型。
五種類型的節點可以組合成多種類型的節點。
狀態信息
每個數據節點除了存儲數據內容之外,還會存儲節點本身的狀態信息。
可以通過get或者stat命令獲取節點的狀態信息。(get命令額外輸出節點的內容)
服務器角色分爲:Leader、Follower、Observer。
Leader
事務請求的唯一調度者和處理者。保證集羣事務處理的順序性。
集羣內部各服務器的調度者。
Follower
處理客戶端的非事務請求,轉發事務請求給Leader。
參與事務請求Proposal的投票。
參與Leader選舉投票。
Observer
處理客戶端的非事務請求,轉發事務請求給Leader。