ZooKeeper is a high-performance coordination service for distributed applications. It exposes common services - such as naming, configuration management, synchronization, and group services - in a simple interface so you don't have to write them from scratch. You can use it off-the-shelf to implement consensus, group management, leader election, and presence protocols. And you can build on it for your own, specific needs.
Locks 分布式锁
lock操作过程
Call create( ) with a pathname of "locknode/lock-" and the sequence and ephemeral flags set.
Call getChildren( ) on the lock node without setting the watch flag (this is important to avoid the herd effect).
If the pathname created in step 1 has the lowest sequence number suffix, the client has the lock and the client exits the protocol.
The client calls exists( ) with the watch flag set on the path in the lock directory with the next lowest sequence number.
if exists( ) returns false, go to step 2. Otherwise, wait for a notification for the pathname from the previous step before going to step 2.
总结一下
客户端去创建一个根节点 如上的"locknode/lock-"节点, 如果根节点存在,则在根节点下创建自增长瞬时节点, 取出根节点下的所有节点,取出最小的序号作为锁的持有者,此时如果自己的节点id与此序号相等则获得锁,反之,watch住序号比自己靠前的一个锁的释放操作.
unlock操作过程
The unlock protocol is very simple: clients wishing to release a lock simply delete the node they created in step 1.