ZooKeeper

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.

zookeeper 3.4 documentation

Locks 分布式锁

lock操作过程
  1. Call create( ) with a pathname of "locknode/lock-" and the sequence and ephemeral flags set.

  2. Call getChildren( ) on the lock node without setting the watch flag (this is important to avoid the herd effect).

  3. If the pathname created in step 1 has the lowest sequence number suffix, the client has the lock and the client exits the protocol.

  4. The client calls exists( ) with the watch flag set on the path in the lock directory with the next lowest sequence number.

  5. 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值