摘要
在分布式解决方案中,分布式锁是一个很重要的部分。之前有写过一篇基于redis实现的分布式锁,为了实现在工作项目中生产环境多节点的定时任务抢占触发。前段时间又接手了一个社区项目的模块改版任务,实现需求是,用户通过APP端报名参加茶会活动,需要在报名接口增加一个分布式锁,以保证在并发请求下活动的名额能够正确的扣减。出于这个项目正好用到了zookeeper做服务注册发现中心。于是尝试搞了一下利用zookeeper的临时有序节点实现分布式锁机制。
实现原理
首先,我们知道zookeeper的数据存储结构就像一棵树,由很多节点(Znode)组成。zookeeper的节点共有四种类型,
1、持久节点(PERSISTENT)
2、持久顺序节点(PERSISTENT_SEQUENTIAL)
3、临时节点(EPHEMERAL)
4、临时顺序节点(EPHEMERAL_SEQUENTIAL)
zookeeper分布式锁正好用到的就是临时顺序节点的特性实现的。首先,先创建一个父节点/lock,请求进来创建一个临时顺序节点,然后获取所有子节点,
遍历判断自己是不是序号最小的那个,如果是则获得锁成功。否则就会监控上一个节点,直至上一个节点释放锁删除节点后才会获得锁。
代码实现
1、先定义一个锁接口
/**
* @author v_liuwen
* @date 2019-05-30
*/
public interface ILock {
/**
* 获取锁
* @throws ConcurrencyException
*/
public void acquireLock() throws ConcurrencyException;
/**
* 等待获取锁
* @throws ConcurrencyException
*/
public void acquireLockWait() throws ConcurrencyException;
/**
* 释放锁
*/
public void releaseLock() ;
}
2、实现锁接口,事先具体逻辑
/**
* zookeeper 分布式抢占锁
* @author v_liuwen
* @date 2019-05-30
*/
public class ZkDistributeLock implements ILock, Watcher {
/**
* 日志打印
*/
private static Logger logger = LoggerFactory.getLogger(ZkDistributeLock.class);
//计数器
private CountDownLatch countDownLatch;
//根节点
private String ROOT_LOCK = "/lock";
//等待的前一个锁
private String WAIT_LOCK;
//当前锁
private String CURRENT_LOCK;
//锁资源
private String lockName;
private ZooKeeper zooKeeper = null;
private int sessionTimeOut = 30000;
private List<Exception> exceptionList = Lists.newArrayList();
private static final String ZOOKEEPER_PROPERTITY_PATH = "/application.properties";
/**
* 配置分布式锁
* @param lockName 锁资源
*/
public ZkDistributeLock(String lockName){
this.lockName = lockName;
try{
//连接zookeeeper
String config = PropertiesUtils.getProperty("spring.zookeeper.address", ZOOKEEPER_PROPERTITY_PATH);
zooKeeper = new ZooKeeper(config,sessionTimeOut,this);
Stat stat = zooKeeper.exists(ROOT_LOCK,false);
if(stat == null){
//如果根节点不存在,则创建根节点
zooKeeper.create(ROOT_LOCK,new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}catch (IOException | KeeperException | InterruptedException e){
logger.error(e.getMessage());
}
}
@Override
public void acquireLock() throws ConcurrencyException {
if(exceptionList.size() > 0){
throw new LockException(exceptionList.get(0));
}
try{
if(this.tryLock()){
System.out.println(Thread.currentThread().getName()+" "+lockName+"获得了锁");
logger.debug(Thread.currentThread().getName()+" "+lockName+"获得了锁");
return;
}
throw new LockException("获取锁失败!");
}catch (LockException e){
logger.error(e.getMessage());
throw e;
}
}
public boolean tryLock() {
try{
String splitStr = "_lock_";
if(lockName.contains(splitStr)){
throw new LockException("锁名错误");
}
//创建临时有序节点
CURRENT_LOCK = zooKeeper.create(ROOT_LOCK+"/"+lockName+splitStr,new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(CURRENT_LOCK+"已经创建");
logger.debug(CURRENT_LOCK+"已经创建");
//获取所有子节点
List<String> subNodes = zooKeeper.getChildren(ROOT_LOCK,false);
//取出所有locakName的锁
List<String> lockObjects = Lists.newArrayList();
subNodes.forEach(s -> {
String _node = s.split(splitStr)[0];
if(_node.equalsIgnoreCase(lockName)){
lockObjects.add(s);
}
});
Collections.sort(lockObjects);
System.out.println(Thread.currentThread().getName()+" 的锁是"+CURRENT_LOCK);
logger.debug(Thread.currentThread().getName()+" 的锁是"+CURRENT_LOCK);
//若当前节点为最小节点,则获得锁成功
if(CURRENT_LOCK.equalsIgnoreCase(ROOT_LOCK+"/"+lockObjects.get(0))){
return true;
}
//若不是最小节点,则找到自己的前一个节点
String preNode = CURRENT_LOCK.substring(CURRENT_LOCK.lastIndexOf("/")+1);
WAIT_LOCK = lockObjects.get(Collections.binarySearch(lockObjects,preNode)-1);
}catch (InterruptedException | KeeperException e){
logger.error(e.getMessage());
}
return false;
}
/**
* 等待获取锁
* @param prev
* @param waitTime
* @return
* @throws KeeperException
* @throws InterruptedException
*/
private boolean waitForLock(String prev,long waitTime) throws KeeperException, InterruptedException {
Stat stat = zooKeeper.exists(ROOT_LOCK+"/"+prev,true);
if(stat != null){
System.out.println(Thread.currentThread().getName()+"等待锁"+ROOT_LOCK+"/"+prev);
logger.debug(Thread.currentThread().getName()+"等待锁"+ROOT_LOCK+"/"+prev);
this.countDownLatch = new CountDownLatch(1);
//计数等待,若等到前一个节点消失,则precess中进行countDown,停止等待,获取锁
this.countDownLatch = null;
System.out.println(Thread.currentThread().getName()+"等到了锁");
logger.debug(Thread.currentThread().getName()+"等到了锁");
}
return true;
}
@Override
public void acquireLockWait() throws ConcurrencyException {
if(exceptionList.size() > 0){
throw new LockException(exceptionList.get(0));
}
try{
if(this.tryLock()){
System.out.println(Thread.currentThread().getName()+" "+lockName+"获得了锁");
logger.debug(Thread.currentThread().getName()+" "+lockName+"获得了锁");
return;
}else {
//等待锁
waitForLock(WAIT_LOCK,sessionTimeOut);
}
}catch (InterruptedException | KeeperException e){
logger.error(e.getMessage());
}
}
@Override
public void releaseLock() {
try{
logger.debug("释放锁"+CURRENT_LOCK);
System.out.println("释放锁"+CURRENT_LOCK);
zooKeeper.delete(CURRENT_LOCK,-1);
CURRENT_LOCK = null;
zooKeeper.close();
}catch (InterruptedException | KeeperException e){
logger.error(e.getMessage());
}
}
/**
* 节点监视器
* @param watchedEvent
*/
@Override
public void process(WatchedEvent watchedEvent) {
if(this.countDownLatch != null){
this.countDownLatch.countDown();
}
}
}
3、测试
/**
* @author v_liuwen
* @date 2019-05-25
*/
public class Test1 {
static int n = 500;
public static void secsKill(){
System.out.println(--n);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
ZkDistributeLock lock = null;
try{
lock = new ZkDistributeLock("activity");
lock.acquireLockWait();
secsKill();
System.out.println(Thread.currentThread().getName()+"正在运行");
}finally {
if(lock != null){
lock.releaseLock();
}
}
}
};
for(int i = 0;i<10;i++){
Thread t = new Thread(runnable);
t.start();
}
}
}
测试打印部分输出
18:38:50.929 [Thread-1-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950009, packet:: clientPath:null serverPath:null finished:false header:: 1,3 replyHeader:: 1,148,0 request:: '/lock,F response:: s{20,20,1558774399619,1558774399619,0,60,0,0,0,0,129}
18:38:50.930 [Thread-3-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950001, packet:: clientPath:null serverPath:null finished:false header:: 1,3 replyHeader:: 1,148,0 request:: '/lock,F response:: s{20,20,1558774399619,1558774399619,0,60,0,0,0,0,129}
18:38:50.930 [Thread-8-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950005, packet:: clientPath:null serverPath:null finished:false header:: 1,3 replyHeader:: 1,148,0 request:: '/lock,F response:: s{20,20,1558774399619,1558774399619,0,60,0,0,0,0,129}
18:38:50.952 [Thread-1-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950009, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,149,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000030
/lock/activity_lock_0000000030已经创建
18:38:50.955 [Thread-1] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000030已经创建
18:38:50.957 [Thread-4-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950003, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,150,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000031
18:38:50.958 [Thread-6-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950006, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,151,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000032
/lock/activity_lock_0000000031已经创建
18:38:50.959 [Thread-3-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950001, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,152,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000033
18:38:50.958 [Thread-4] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000031已经创建
/lock/activity_lock_0000000032已经创建
18:38:50.962 [Thread-6] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000032已经创建
18:38:50.965 [Thread-8-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950005, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,153,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000034
18:38:50.971 [Thread-5-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950002, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,154,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000035
18:38:50.973 [Thread-2-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950004, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,155,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000036
18:38:50.974 [Thread-0-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950007, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,156,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000037
18:38:50.975 [Thread-9-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950000, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,157,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000038
18:38:50.976 [Thread-7-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950008, packet:: clientPath:null serverPath:null finished:false header:: 2,1 replyHeader:: 2,158,0 request:: '/lock/activity_lock_,,v{s{31,s{'world,'anyone}}},3 response:: '/lock/activity_lock_0000000039
/lock/activity_lock_0000000033已经创建
18:38:50.983 [Thread-3] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000033已经创建
/lock/activity_lock_0000000034已经创建
18:38:50.983 [Thread-8] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000034已经创建
/lock/activity_lock_0000000035已经创建
/lock/activity_lock_0000000036已经创建
/lock/activity_lock_0000000037已经创建
/lock/activity_lock_0000000038已经创建
18:38:50.987 [Thread-5] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000035已经创建
/lock/activity_lock_0000000039已经创建
18:38:50.988 [Thread-7] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000039已经创建
18:38:50.988 [Thread-9] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000038已经创建
18:38:50.988 [Thread-0] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000037已经创建
18:38:50.988 [Thread-2] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - /lock/activity_lock_0000000036已经创建
18:38:50.996 [Thread-6-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950006, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:50.996 [Thread-1-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950009, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:50.996 [Thread-8-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950005, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:50.996 [Thread-4-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950003, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:50.997 [Thread-3-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950001, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:50.997 [Thread-5-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950002, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:50.999 [Thread-7-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950008, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:50.999 [Thread-9-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950000, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:50.999 [Thread-0-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950007, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
18:38:51.000 [Thread-2-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950004, packet:: clientPath:null serverPath:null finished:false header:: 3,8 replyHeader:: 3,158,0 request:: '/lock,F response:: v{'activity_lock_0000000030,'activity_lock_0000000032,'activity_lock_0000000031,'activity_lock_0000000034,'activity_lock_0000000033,'activity_lock_0000000036,'activity_lock_0000000035,'activity_lock_0000000038,'activity_lock_0000000037,'activity_lock_0000000039}
Thread-0 的锁是/lock/activity_lock_0000000037
Thread-2 的锁是/lock/activity_lock_0000000036
18:38:51.002 [Thread-0] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-0 的锁是/lock/activity_lock_0000000037
18:38:51.002 [Thread-2] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-2 的锁是/lock/activity_lock_0000000036
Thread-6 的锁是/lock/activity_lock_0000000032
18:38:51.002 [Thread-6] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-6 的锁是/lock/activity_lock_0000000032
Thread-3 的锁是/lock/activity_lock_0000000033
18:38:51.002 [Thread-3] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-3 的锁是/lock/activity_lock_0000000033
Thread-1 的锁是/lock/activity_lock_0000000030
18:38:51.003 [Thread-1] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-1 的锁是/lock/activity_lock_0000000030
Thread-7 的锁是/lock/activity_lock_0000000039
Thread-1 activity获得了锁
Thread-4 的锁是/lock/activity_lock_0000000031
18:38:51.004 [Thread-4] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-4 的锁是/lock/activity_lock_0000000031
18:38:51.005 [Thread-0-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950007, packet:: clientPath:null serverPath:null finished:false header:: 4,3 replyHeader:: 4,158,0 request:: '/lock/activity_lock_0000000036,T response:: s{155,155,1562582330950,1562582330950,0,0,0,72069033391095812,0,0,155}
18:38:51.005 [Thread-6-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950006, packet:: clientPath:null serverPath:null finished:false header:: 4,3 replyHeader:: 4,158,0 request:: '/lock/activity_lock_0000000031,T response:: s{150,150,1562582330949,1562582330949,0,0,0,72069033391095811,0,0,150}
18:38:51.006 [Thread-3-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950001, packet:: clientPath:null serverPath:null finished:false header:: 4,3 replyHeader:: 4,158,0 request:: '/lock/activity_lock_0000000032,T response:: s{151,151,1562582330949,1562582330949,0,0,0,72069033391095814,0,0,151}
18:38:51.003 [Thread-7] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-7 的锁是/lock/activity_lock_0000000039
Thread-9 的锁是/lock/activity_lock_0000000038
18:38:51.006 [Thread-9] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-9 的锁是/lock/activity_lock_0000000038
Thread-8 的锁是/lock/activity_lock_0000000034
18:38:51.006 [Thread-8] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-8 的锁是/lock/activity_lock_0000000034
Thread-0等待锁/lock/activity_lock_0000000036
18:38:51.007 [Thread-0] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-0等待锁/lock/activity_lock_0000000036
Thread-6等待锁/lock/activity_lock_0000000031
18:38:51.007 [Thread-6] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-6等待锁/lock/activity_lock_0000000031
Thread-3等待锁/lock/activity_lock_0000000032
18:38:51.007 [Thread-3] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-3等待锁/lock/activity_lock_0000000032
18:38:51.004 [Thread-1] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-1 activity获得了锁
499
Thread-1正在运行
Thread-0等到了锁
Thread-5 的锁是/lock/activity_lock_0000000035
Thread-3等到了锁
Thread-6等到了锁
18:38:51.008 [Thread-2-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950004, packet:: clientPath:null serverPath:null finished:false header:: 4,3 replyHeader:: 4,158,0 request:: '/lock/activity_lock_0000000035,T response:: s{154,154,1562582330949,1562582330949,0,0,0,72069033391095810,0,0,154}
18:38:51.008 [Thread-4-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950003, packet:: clientPath:null serverPath:null finished:false header:: 4,3 replyHeader:: 4,158,0 request:: '/lock/activity_lock_0000000030,T response:: s{149,149,1562582330939,1562582330939,0,0,0,72069033391095817,0,0,149}
18:38:51.009 [Thread-6] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-6等到了锁
498
Thread-6正在运行
18:38:51.009 [Thread-6] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000032
释放锁/lock/activity_lock_0000000032
18:38:51.010 [Thread-7-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950008, packet:: clientPath:null serverPath:null finished:false header:: 4,3 replyHeader:: 4,158,0 request:: '/lock/activity_lock_0000000038,T response:: s{157,157,1562582330950,1562582330950,0,0,0,72069033391095808,0,0,157}
18:38:51.011 [Thread-8-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950005, packet:: clientPath:null serverPath:null finished:false header:: 4,3 replyHeader:: 4,158,0 request:: '/lock/activity_lock_0000000033,T response:: s{152,152,1562582330949,1562582330949,0,0,0,72069033391095809,0,0,152}
18:38:51.011 [Thread-9-SendThread(127.0.0.1:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x1000a676e950000, packet:: clientPath:null serverPath:null finished:false header:: 4,3 replyHeader:: 4,158,0 request:: '/lock/activity_lock_0000000037,T response:: s{156,156,1562582330950,1562582330950,0,0,0,72069033391095815,0,0,156}
Thread-2等待锁/lock/activity_lock_0000000035
18:38:51.008 [Thread-1] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000030
释放锁/lock/activity_lock_0000000030
18:38:51.012 [Thread-2] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-2等待锁/lock/activity_lock_0000000035
Thread-2等到了锁
18:38:51.012 [Thread-2] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-2等到了锁
497
Thread-2正在运行
Thread-7等待锁/lock/activity_lock_0000000038
18:38:51.012 [Thread-2] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000036
释放锁/lock/activity_lock_0000000036
18:38:51.012 [Thread-7] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-7等待锁/lock/activity_lock_0000000038
Thread-7等到了锁
18:38:51.012 [Thread-7] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-7等到了锁
496
Thread-7正在运行
18:38:51.012 [Thread-7] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000039
释放锁/lock/activity_lock_0000000039
Thread-8等待锁/lock/activity_lock_0000000033
18:38:51.012 [Thread-8] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-8等待锁/lock/activity_lock_0000000033
Thread-8等到了锁
18:38:51.012 [Thread-8] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-8等到了锁
495
Thread-8正在运行
18:38:51.012 [Thread-8] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000034
释放锁/lock/activity_lock_0000000034
Thread-9等待锁/lock/activity_lock_0000000037
18:38:51.012 [Thread-9] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-9等待锁/lock/activity_lock_0000000037
Thread-9等到了锁
18:38:51.013 [Thread-9] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-9等到了锁
494
Thread-9正在运行
18:38:51.013 [Thread-9] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000038
释放锁/lock/activity_lock_0000000038
18:38:51.008 [Thread-0] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-0等到了锁
493
Thread-0正在运行
18:38:51.013 [Thread-0] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000037
释放锁/lock/activity_lock_0000000037
Thread-4等待锁/lock/activity_lock_0000000030
18:38:51.013 [Thread-4] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-4等待锁/lock/activity_lock_0000000030
Thread-4等到了锁
18:38:51.013 [Thread-4] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-4等到了锁
492
Thread-4正在运行
18:38:51.013 [Thread-4] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000031
释放锁/lock/activity_lock_0000000031
18:38:51.009 [Thread-5] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-5 的锁是/lock/activity_lock_0000000035
18:38:51.009 [Thread-3] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - Thread-3等到了锁
491
Thread-3正在运行
18:38:51.014 [Thread-3] DEBUG top.qrainly.zookeeper.customlock.ZkDistributeLock - 释放锁/lock/activity_lock_0000000033
释放锁/lock/activity_lock_0000000033
4、搞定
持续更新中…