
java
package zkcase2;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class DistributedLock {
private final ZooKeeper zooKeeper;
private final String connection="hadoop102:2181,hadoop103:2181,hadoop104:2181"; //对应你服务器里面安装zk的节点,注意不可以有空格出现
private final int sessiontimeout=2000;
private CountDownLatch connect=new CountDownLatch(1);
private CountDownLatch waitlath=new CountDownLatch(1);
private String waitpath;
private String currentnode;
public DistributedLock() throws IOException, InterruptedException, KeeperException {
//获取zk
zooKeeper = new ZooKeeper(connection, sessiontimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
//connect表示如果连接到zk 可以释放
if (watchedEvent.getState()==Event.KeeperState.SyncConnected){
connect.countDown();
}
//waitlath需要释放
if (watchedEvent.getType()==Event.EventType.NodeDeleted && watchedEvent.getPath().equals(waitpath)){
waitlath.countDown();
}
}
});
//等待zk正常连接之后往下进行
connect.await();
//判断根节点/locks是否存在
Stat exists = zooKeeper.exists("/locks", false);
if (exists==null){
//如果状态为空说明根节点不存在,那就创建根节点
zooKeeper.create("/locks","locks".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);
}
}
//创建zk锁
public void zklock() throws InterruptedException {
//创建对应的临时带序号节点
try {
currentnode = zooKeeper.create("/locks/" + "seq-", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
//判断当前节点是不是最小的序号节点,如果是获取到锁,如果不是监听序号前一个节点
List<String> children = zooKeeper.getChildren("/locks", false);
//如果childern只有一个值那么就获取锁,如果有多个节点,判断谁最小
if (children.size()==1){
return;
}else {
Collections.sort(children);
//获取节点名称seq-000000000000
String thisnode = currentnode.substring("/locks/".length());
//通过seq-000000000获取当前节点在childen当中的位置
int indexOf = children.indexOf(thisnode);
//判断
if (indexOf==-1){
System.out.println("数据异常");
} else if (indexOf==0) {
//只有一个节点直接获取锁
return;
}else {
//不是只有一个节点所以需要监听他前一个节点
waitpath="/locks/"+children.get(indexOf-1);
zooKeeper.getData(waitpath,true,null);
//等待监听
waitlath.await();
}
}
} catch (KeeperException e) {
throw new RuntimeException(e);
}
}
//解锁
public void unzklock(){
//删除节点
try {
zooKeeper.delete(currentnode,-1);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (KeeperException e) {
throw new RuntimeException(e);
}
}
}
java
package zkcase2;
import org.apache.zookeeper.KeeperException;
import java.io.IOException;
public class DistributedLockTest {
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
final DistributedLock distributedLock1 = new DistributedLock();
final DistributedLock distributedLock2 = new DistributedLock();
new Thread(new Runnable() {
@Override
public void run() {
try {
distributedLock1.zklock();
System.out.println("线程1启动,获取到锁");
Thread.sleep(5*1000);
distributedLock1.unzklock();
System.out.println("线程1释放锁");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
try {
distributedLock2.zklock();
System.out.println("线程2启动,获取到锁");
Thread.sleep(5*1000);
distributedLock2.unzklock();
System.out.println("线程2释放锁");
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}).start();
}
}