使用zookeeper实现分布式共享锁

分布式系统中经常需要协调多进程,多个jvm,或者多台机器之间的同步问题,得益于zookeeper,实现了一个分布式的共享锁,方便在多台服务器之间竞争资源时,来协调各系统之间的协作和同步。

package com.concurrent;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

/**
DistributedLock lock = null;
try {
lock = new DistributedLock("127.0.0.1:2182","test");
lock.lock();
//do something...
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(lock != null)
lock.unlock();
}
* @author xueliang
*
*/
public class DistributedLock implements Lock, Watcher{
private ZooKeeper zk;
private String root = "/locks";//根
private String lockName;//竞争资源的标志
private String waitNode;//等待前一个锁
private String myZnode;//当前锁
private CountDownLatch latch;//计数器
private int sessionTimeout = 30000;
private List<Exception> exception = new ArrayList<Exception>();

/**
* 创建分布式锁,使用前请确认config配置的zookeeper服务可用
* @param config 127.0.0.1:2181
* @param lockName 竞争资源标志,lockName中不能包含单词lock
*/
public DistributedLock(String config, String lockName){
this.lockName = lockName;
// 创建一个与服务器的连接
try {
zk = new ZooKeeper(config, sessionTimeout, this);
Stat stat = zk.exists(root, false);
if(stat == null){
// 创建根节点
zk.create(root, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
}
} catch (IOException e) {
exception.add(e);
} catch (KeeperException e) {
exception.add(e);
} catch (InterruptedException e) {
exception.add(e);
}
}

/**
* zookeeper节点的监视器
*/
public void process(WatchedEvent event) {
if(this.latch != null) {
this.latch.countDown();
}
}

public void lock() {
if(exception.size() > 0){
throw new LockException(exception.get(0));
}
try {
if(this.tryLock()){
System.out.println("Thread " + Thread.currentThread().getId() + " " +myZnode + " get lock true");
return;
}
else{
waitForLock(waitNode, sessionTimeout);//等待锁
}
} catch (KeeperException e) {
throw new LockException(e);
} catch (InterruptedException e) {
throw new LockException(e);
}
}

public boolean tryLock() {
try {
String splitStr = "_lock_";
if(lockName.contains(splitStr))
throw new LockException("lockName can not contains \\u000B");
//创建临时子节点
myZnode = zk.create(root + "/" + lockName + splitStr, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(myZnode + " is created ");
//取出所有子节点
List<String> subNodes = zk.getChildren(root, false);
//取出所有lockName的锁
List<String> lockObjNodes = new ArrayList<String>();
for (String node : subNodes) {
String _node = node.split(splitStr)[0];
if(_node.equals(lockName)){
lockObjNodes.add(node);
}
}
Collections.sort(lockObjNodes);
System.out.println(myZnode + "==" + lockObjNodes.get(0));
if(myZnode.equals(root+"/"+lockObjNodes.get(0))){
//如果是最小的节点,则表示取得锁
return true;
}
//如果不是最小的节点,找到比自己小1的节点
String subMyZnode = myZnode.substring(myZnode.lastIndexOf("/") + 1);
waitNode = lockObjNodes.get(Collections.binarySearch(lockObjNodes, subMyZnode) - 1);
} catch (KeeperException e) {
throw new LockException(e);
} catch (InterruptedException e) {
throw new LockException(e);
}
return false;
}

public boolean tryLock(long time, TimeUnit unit) {
try {
if(this.tryLock()){
return true;
}
return waitForLock(waitNode,time);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

private boolean waitForLock(String lower, long waitTime) throws InterruptedException, KeeperException {
Stat stat = zk.exists(root + "/" + lower,true);
//判断比自己小一个数的节点是否存在,如果不存在则无需等待锁,同时注册监听
if(stat != null){
System.out.println("Thread " + Thread.currentThread().getId() + " waiting for " + root + "/" + lower);
this.latch = new CountDownLatch(1);
this.latch.await(waitTime, TimeUnit.MILLISECONDS);
this.latch = null;
}
return true;
}

public void unlock() {
try {
System.out.println("unlock " + myZnode);
zk.delete(myZnode,-1);
myZnode = null;
zk.close();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
}
}

public void lockInterruptibly() throws InterruptedException {
this.lock();
}

public Condition newCondition() {
return null;
}

public class LockException extends RuntimeException {
private static final long serialVersionUID = 1L;
public LockException(String e){
super(e);
}
public LockException(Exception e){
super(e);
}
}

}

多线程的并发测试要复杂很多,下面是一个使用CountDownLatch实现的并发测试工具,可以简单模拟一些并发场景

package com.concurrent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

/**
ConcurrentTask[] task = new ConcurrentTask[5];
for(int i=0;i<task.length;i++){
task[i] = new ConcurrentTask(){
public void run() {
System.out.println("==============");

}};
}
new ConcurrentTest(task);
* @author xueliang
*
*/
public class ConcurrentTest {
private CountDownLatch startSignal = new CountDownLatch(1);//开始阀门
private CountDownLatch doneSignal = null;//结束阀门
private CopyOnWriteArrayList<Long> list = new CopyOnWriteArrayList<Long>();
private AtomicInteger err = new AtomicInteger();//原子递增
private ConcurrentTask[] task = null;

public ConcurrentTest(ConcurrentTask... task){
this.task = task;
if(task == null){
System.out.println("task can not null");
System.exit(1);
}
doneSignal = new CountDownLatch(task.length);
start();
}
/**
* @param args
* @throws ClassNotFoundException
*/
private void start(){
//创建线程,并将所有线程等待在阀门处
createThread();
//打开阀门
startSignal.countDown();//递减锁存器的计数,如果计数到达零,则释放所有等待的线程
try {
doneSignal.await();//等待所有线程都执行完毕
} catch (InterruptedException e) {
e.printStackTrace();
}
//计算执行时间
getExeTime();
}
/**
* 初始化所有线程,并在阀门处等待
*/
private void createThread() {
long len = doneSignal.getCount();
for (int i = 0; i < len; i++) {
final int j = i;
new Thread(new Runnable(){
public void run() {
try {
startSignal.await();//使当前线程在锁存器倒计数至零之前一直等待
long start = System.currentTimeMillis();
task[j].run();
long end = (System.currentTimeMillis() - start);
list.add(end);
} catch (Exception e) {
err.getAndIncrement();//相当于err++
}
doneSignal.countDown();
}
}).start();
}
}
/**
* 计算平均响应时间
*/
private void getExeTime() {
int size = list.size();
List<Long> _list = new ArrayList<Long>(size);
_list.addAll(list);
Collections.sort(_list);
long min = _list.get(0);
long max = _list.get(size-1);
long sum = 0L;
for (Long t : _list) {
sum += t;
}
long avg = sum/size;
System.out.println("min: " + min);
System.out.println("max: " + max);
System.out.println("avg: " + avg);
System.out.println("err: " + err.get());
}

public interface ConcurrentTask {
void run();
}

}

下面使用这个工具来测试一下我们的分布式共享锁

package com.concurrent;

import com.concurrent.ConcurrentTest.ConcurrentTask;

public class ZkTest {
public static void main(String[] args) {
Runnable task1 = new Runnable(){
public void run() {
DistributedLock lock = null;
try {
lock = new DistributedLock("127.0.0.1:2182","test1");
//lock = new DistributedLock("127.0.0.1:2182","test2");
lock.lock();
Thread.sleep(3000);
System.out.println("===Thread " + Thread.currentThread().getId() + " running");
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(lock != null)
lock.unlock();
}

}

};
new Thread(task1).start();
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
ConcurrentTask[] tasks = new ConcurrentTask[10];
for(int i=0;i<tasks.length;i++){
ConcurrentTask task3 = new ConcurrentTask(){
public void run() {
DistributedLock lock = null;
try {
lock = new DistributedLock("127.0.0.1:2183","test2");
lock.lock();
System.out.println("Thread " + Thread.currentThread().getId() + " running");
} catch (Exception e) {
e.printStackTrace();
}
finally {
lock.unlock();
}

}
};
tasks[i] = task3;
}
new ConcurrentTest(tasks);
}
}

测试结果:
[INFO][08-26 15:05:58.472][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
[INFO][08-26 15:05:58.474][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:host.name=zhouguofeng-PC
[INFO][08-26 15:05:58.474][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:java.version=1.8.0_101
[INFO][08-26 15:05:58.474][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:java.vendor=Oracle Corporation
[INFO][08-26 15:05:58.475][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:java.home=D:\Program Files\Java\jre8
[INFO][08-26 15:05:58.475][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:java.class.path=D:\xgWorkspase\Test\target\classes;D:\xgWorkspase\Test\lib\jmagick.jar;D:\xgWorkspase\Test\lib\QRCode.jar;D:\xgWorkspase\Test\lib\Qrcodeen.jar;E:\maven\repository\log4j\log4j\1.2.12\log4j-1.2.12.jar;E:\maven\repository\commons-codec\commons-codec\1.6\commons-codec-1.6.jar;E:\maven\repository\commons-lang\commons-lang\2.5\commons-lang-2.5.jar;E:\maven\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar;E:\maven\repository\junit\junit\4.9\junit-4.9.jar;E:\maven\repository\org\hamcrest\hamcrest-core\1.1\hamcrest-core-1.1.jar;E:\maven\repository\commons-io\commons-io\1.3.2\commons-io-1.3.2.jar;E:\maven\repository\org\apache\commons\commons-pool2\2.2\commons-pool2-2.2.jar;E:\maven\repository\org\csource\fastdfs\1.24\fastdfs-1.24.jar;E:\maven\repository\jmagick\jmagick\6.6.9\jmagick-6.6.9.jar;E:\maven\repository\org\im4java\im4java\1.4.0\im4java-1.4.0.jar;E:\maven\repository\org\apache\shiro\shiro-core\1.2.3\shiro-core-1.2.3.jar;E:\maven\repository\org\slf4j\slf4j-api\1.6.4\slf4j-api-1.6.4.jar;E:\maven\repository\commons-beanutils\commons-beanutils\1.8.3\commons-beanutils-1.8.3.jar;E:\maven\repository\com\google\zxing\core\2.0\core-2.0.jar;E:\maven\repository\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;E:\maven\repository\org\apache\httpcomponents\httpcore\4.4.4\httpcore-4.4.4.jar;E:\maven\repository\org\apache\zookeeper\zookeeper\3.4.8\zookeeper-3.4.8.jar;E:\maven\repository\org\slf4j\slf4j-log4j12\1.6.1\slf4j-log4j12-1.6.1.jar;E:\maven\repository\jline\jline\0.9.94\jline-0.9.94.jar;E:\maven\repository\io\netty\netty\3.7.0.Final\netty-3.7.0.Final.jar;E:\maven\repository\com\netflix\curator\curator-framework\1.2.3\curator-framework-1.2.3.jar;E:\maven\repository\com\netflix\curator\curator-client\1.2.3\curator-client-1.2.3.jar;E:\maven\repository\com\google\guava\guava\11.0.1\guava-11.0.1.jar;E:\maven\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;E:\maven\repository\com\netflix\curator\curator-recipes\1.2.3\curator-recipes-1.2.3.jar;E:\maven\repository\com\netflix\curator\curator-test\1.2.3\curator-test-1.2.3.jar;E:\maven\repository\org\javassist\javassist\3.15.0-GA\javassist-3.15.0-GA.jar;E:\maven\repository\org\apache\commons\commons-math\2.2\commons-math-2.2.jar;E:\maven\repository\com\netflix\curator\curator-x-discovery\1.2.3\curator-x-discovery-1.2.3.jar;E:\maven\repository\org\codehaus\jackson\jackson-mapper-asl\1.9.2\jackson-mapper-asl-1.9.2.jar;E:\maven\repository\org\codehaus\jackson\jackson-core-asl\1.9.2\jackson-core-asl-1.9.2.jar
[INFO][08-26 15:05:58.475][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:java.library.path=D:\Program Files\Java\jre8\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/Program Files/Java/jre8/bin/server;D:/Program Files/Java/jre8/bin;D:/Program Files/Java/jre8/lib/amd64;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Program Files\Java\jdk1.8.0_101\bin;D:\Program Files\Java\jdk1.8.0_101\jre\bin;D:\JAVA\mysql-5.6.17\bin;D:\Program Files\nodejs\;D:\Program Files\TortoiseSVN\bin;C:\Users\zhouguofeng\AppData\Roaming\npm;d:\Program Files (x86)\SSH Communications Security\SSH Secure Shell;C:\Users\zhouguofeng\Desktop;;.
[INFO][08-26 15:05:58.475][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:java.io.tmpdir=C:\Users\ZHOUGU~1\AppData\Local\Temp\
[INFO][08-26 15:05:58.475][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:java.compiler=<NA>
[INFO][08-26 15:05:58.475][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:os.name=Windows 7
[INFO][08-26 15:05:58.475][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:os.arch=amd64
[INFO][08-26 15:05:58.476][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:os.version=6.1
[INFO][08-26 15:05:58.476][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:user.name=zhouguofeng
[INFO][08-26 15:05:58.476][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:user.home=C:\Users\zhouguofeng
[INFO][08-26 15:05:58.476][Thread-0][org.apache.zookeeper.ZooKeeper:100]: Client environment:user.dir=D:\xgWorkspase\Test
[INFO][08-26 15:05:58.477][Thread-0][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@5563d6e7
[INFO][08-26 15:05:58.602][Thread-0-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:58.603][Thread-0-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:58.613][Thread-0-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900e3, negotiated timeout = 40000
/locks/test1_lock_0000000192 is created
/locks/test1_lock_0000000192==test1_lock_0000000192
Thread 10 /locks/test1_lock_0000000192 get lock true
[INFO][08-26 15:05:59.371][Thread-1][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@7e7181b2
[INFO][08-26 15:05:59.372][Thread-6][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@772ba4ed
[INFO][08-26 15:05:59.372][Thread-9][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@60988475
[INFO][08-26 15:05:59.380][Thread-6-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.381][Thread-6-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.372][Thread-5][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@68004e4f
[INFO][08-26 15:05:59.371][Thread-4][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@679c1ee8
[INFO][08-26 15:05:59.371][Thread-3][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@62aaf848
[INFO][08-26 15:05:59.371][Thread-2][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@5d6f03ec
[INFO][08-26 15:05:59.371][Thread-10][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@270e8fc8
[INFO][08-26 15:05:59.382][Thread-1-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.386][Thread-1-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.387][Thread-9-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.390][Thread-9-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.391][Thread-5-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.372][Thread-8][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@67eec7d2
[INFO][08-26 15:05:59.393][Thread-3-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.393][Thread-10-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.372][Thread-7][org.apache.zookeeper.ZooKeeper:438]: Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=300000 watcher=com.concurrent.DistributedLock@7837a6ba
[INFO][08-26 15:05:59.394][Thread-6-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900e4, negotiated timeout = 40000
[INFO][08-26 15:05:59.396][Thread-8-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.397][Thread-8-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.394][Thread-3-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.399][Thread-10-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.393][Thread-5-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.392][Thread-2-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.388][Thread-4-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.400][Thread-7-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1032]: Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[INFO][08-26 15:05:59.401][Thread-2-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.401][Thread-4-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.402][Thread-7-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:876]: Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[INFO][08-26 15:05:59.402][Thread-1-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900e5, negotiated timeout = 40000
[INFO][08-26 15:05:59.404][Thread-9-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900e6, negotiated timeout = 40000
[INFO][08-26 15:05:59.406][Thread-5-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900e7, negotiated timeout = 40000
/locks/test1_lock_0000000193 is created
[INFO][08-26 15:05:59.407][Thread-3-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900e8, negotiated timeout = 40000
[INFO][08-26 15:05:59.409][Thread-8-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900e9, negotiated timeout = 40000
/locks/test1_lock_0000000193==test1_lock_0000000192
等待锁-----test1_lock_0000000192
/locks/test1_lock_0000000194 is created
[INFO][08-26 15:05:59.411][Thread-10-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900ea, negotiated timeout = 40000
Thread 18 waiting for /locks/test1_lock_0000000192
/locks/test1_lock_0000000194==test1_lock_0000000192
等待锁-----test1_lock_0000000193
[INFO][08-26 15:05:59.413][Thread-2-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900eb, negotiated timeout = 40000
/locks/test1_lock_0000000196 is created
/locks/test1_lock_0000000195 is created
[INFO][08-26 15:05:59.414][Thread-4-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900ec, negotiated timeout = 40000
/locks/test1_lock_0000000197 is created
Thread 13 waiting for /locks/test1_lock_0000000193
[INFO][08-26 15:05:59.415][Thread-7-SendThread(127.0.0.1:2181)][org.apache.zookeeper.ClientCnxn:1299]: Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x156c53eab3900ed, negotiated timeout = 40000
/locks/test1_lock_0000000198 is created
/locks/test1_lock_0000000199 is created
/locks/test1_lock_0000000200 is created
/locks/test1_lock_0000000197==test1_lock_0000000192
等待锁-----test1_lock_0000000196
/locks/test1_lock_0000000196==test1_lock_0000000192
等待锁-----test1_lock_0000000195
/locks/test1_lock_0000000201 is created
/locks/test1_lock_0000000202 is created
/locks/test1_lock_0000000199==test1_lock_0000000192
等待锁-----test1_lock_0000000198
/locks/test1_lock_0000000198==test1_lock_0000000192
等待锁-----test1_lock_0000000197
/locks/test1_lock_0000000201==test1_lock_0000000192
等待锁-----test1_lock_0000000200
/locks/test1_lock_0000000200==test1_lock_0000000192
等待锁-----test1_lock_0000000199
Thread 15 waiting for /locks/test1_lock_0000000195
Thread 20 waiting for /locks/test1_lock_0000000198
Thread 17 waiting for /locks/test1_lock_0000000196
Thread 14 waiting for /locks/test1_lock_0000000199
Thread 16 waiting for /locks/test1_lock_0000000200
/locks/test1_lock_0000000202==test1_lock_0000000192
等待锁-----test1_lock_0000000201
Thread 22 waiting for /locks/test1_lock_0000000197
/locks/test1_lock_0000000195==test1_lock_0000000192
等待锁-----test1_lock_0000000194
Thread 19 waiting for /locks/test1_lock_0000000201
Thread 21 waiting for /locks/test1_lock_0000000194
===Thread 10 running
释放锁--------------------------unlock /locks/test1_lock_0000000192
[INFO][08-26 15:06:01.635][Thread-0][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900e3 closed
[INFO][08-26 15:06:01.638][Thread-0-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900e3
Thread 18 running
释放锁--------------------------unlock /locks/test1_lock_0000000193
[INFO][08-26 15:06:02.641][Thread-6][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900e4 closed
[INFO][08-26 15:06:02.642][Thread-6-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900e4
Thread 13 running
释放锁--------------------------unlock /locks/test1_lock_0000000194
[INFO][08-26 15:06:03.651][Thread-1][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900e5 closed
[INFO][08-26 15:06:03.653][Thread-1-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900e5
Thread 21 running
释放锁--------------------------unlock /locks/test1_lock_0000000195
[INFO][08-26 15:06:04.657][Thread-9][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900e6 closed
[INFO][08-26 15:06:04.657][Thread-9-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900e6
Thread 15 running
释放锁--------------------------unlock /locks/test1_lock_0000000196
[INFO][08-26 15:06:05.661][Thread-3][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900e8 closed
[INFO][08-26 15:06:05.661][Thread-3-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900e8
Thread 17 running
释放锁--------------------------unlock /locks/test1_lock_0000000197
[INFO][08-26 15:06:06.664][Thread-5][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900e7 closed
[INFO][08-26 15:06:06.664][Thread-5-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900e7
Thread 22 running
释放锁--------------------------unlock /locks/test1_lock_0000000198
[INFO][08-26 15:06:07.677][Thread-10][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900ea closed
[INFO][08-26 15:06:07.677][Thread-10-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900ea
Thread 20 running
释放锁--------------------------unlock /locks/test1_lock_0000000199
[INFO][08-26 15:06:08.682][Thread-8][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900e9 closed
[INFO][08-26 15:06:08.682][Thread-8-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900e9
Thread 14 running
释放锁--------------------------unlock /locks/test1_lock_0000000200
[INFO][08-26 15:06:09.690][Thread-2][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900eb closed
[INFO][08-26 15:06:09.690][Thread-2-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900eb
Thread 16 running
释放锁--------------------------unlock /locks/test1_lock_0000000201
[INFO][08-26 15:06:10.694][Thread-4][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900ec closed
[INFO][08-26 15:06:10.695][Thread-4-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900ec
Thread 19 running
释放锁--------------------------unlock /locks/test1_lock_0000000202
[INFO][08-26 15:06:11.701][Thread-7][org.apache.zookeeper.ZooKeeper:684]: Session: 0x156c53eab3900ed closed
min: 3270
max: 12330
avg: 7800
[INFO][08-26 15:06:11.701][Thread-7-EventThread][org.apache.zookeeper.ClientCnxn:519]: EventThread shut down for session: 0x156c53eab3900ed
err: 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值