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。