Curator实现分布式锁的基本原理-createsTheLock
生活随笔
收集整理的這篇文章主要介紹了
Curator实现分布式锁的基本原理-createsTheLock
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// From StandardLockInternalsDriver
// 在Zookeeper中創(chuàng)建臨時(shí)順序節(jié)點(diǎn)
public String createsTheLock(CuratorFramework client, String path, byte[] lockNodeBytes) throws Exception{ String ourPath; // lockNodeBytes不為null則作為數(shù)據(jù)節(jié)點(diǎn)內(nèi)容,否則采用默認(rèn)內(nèi)容(IP地址) if ( lockNodeBytes != null ){ // 下面對(duì)CuratorFramework的一些細(xì)節(jié)做解釋,不影響對(duì)分布式鎖主邏輯的解釋,可跳過 // creatingParentContainersIfNeeded:用于創(chuàng)建父節(jié)點(diǎn),如果不支持CreateMode.CONTAINER // 那么將采用CreateMode.PERSISTENT // withProtection:臨時(shí)子節(jié)點(diǎn)會(huì)添加GUID前綴 ourPath = client.create().creatingParentContainersIfNeeded() // CreateMode.EPHEMERAL_SEQUENTIAL:臨時(shí)順序節(jié)點(diǎn),Zookeeper能保證在節(jié)點(diǎn)產(chǎn)生的順序性 // 依據(jù)順序來激活分布式鎖,從而也實(shí)現(xiàn)了分布式鎖的公平性,后續(xù)繼續(xù)分析 .withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path, lockNodeBytes); } else { ourPath = client.create().creatingParentContainersIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path); } return ourPath;
}
?
總結(jié)
以上是生活随笔為你收集整理的Curator实现分布式锁的基本原理-createsTheLock的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Curator实现分布式锁的基本原理-L
- 下一篇: Curator实现分布式锁的基本原理-L