package com.atguigu.case2;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
public class DistributedLock {
private final String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private final int sessionTimeout = 2000;
private final ZooKeeper zk;
private CountDownLatch connectLatch = new CountDownLatch(1);
private CountDownLatch waitLatch = new CountDownLatch(1);
private String waitPath;
private String currentMode;
public DistributedLock() throws IOException, InterruptedException, KeeperException {
// 获取连接
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
// connectLatch 如果连接上zk 可以释放
if (watchedEvent.getState() == Event.KeeperState.SyncConnected){
connectLatch.countDown();
}
// waitLatch 需要释放
if (watchedEvent.getType()== Event.EventType.NodeDeleted && watchedEvent.getPath().equals(waitPath)){
waitLatch.countDown();
}
}
});
// 等待zk正常连接后,往下走程序
connectLatch.await();
// 判断根节点/locks是否存在
Stat stat = zk.exists("/locks", false);
if (stat == null) {
// 创建一下根节点
zk.create("/locks", "locks".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
// 对zk加锁
public void zklock() {
// 创建对应的临时带序号节点
try {
currentMode = zk.create("/locks/" + "seq-", null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
// wait一小会, 让结果更清晰一些
Thread.sleep(10);
// 判断创建的节点是否是最小的序号节点,如果是获取到锁;如果不是,监听他序号前一个节点
List<String> children = zk.getChildren("/locks", false);
// 如果children 只有一个值,那就直接获取锁; 如果有多个节点,需要判断,谁最小
if (children.size() == 1) {
return;
} else {
Collections.sort(children);
// 获取节点名称 seq-00000000
String thisNode = currentMode.substring("/locks/".length());
// 通过seq-00000000获取该节点在children集合的位置
int index = children.indexOf(thisNode);
// 判断
if (index == -1) {
System.out.println("数据异常");
} else if (index == 0) {
// 就一个节点,可以获取锁了
return;
} else {
// 需要监听 他前一个节点变化
waitPath = "/locks/" + children.get(index - 1);
zk.getData(waitPath,true,new Stat());
// 等待监听
waitLatch.await();
return;
}
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// 解锁
public void unZkLock() {
// 删除节点
try {
zk.delete(this.currentMode,-1);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (KeeperException e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
zookeeper从入门到精通的代码.rar
共35个文件
class:14个
xml:9个
java:6个
需积分: 2 1 下载量 37 浏览量
2021-09-25
19:52:50
上传
评论
收藏 27KB RAR 举报
温馨提示
主要是一些zookeeper的代码 可结合博主的zookeeper从入门到精通进行阅读 详情可配合我的博客进行学习 https://blog.csdn.net/weixin_47872288/article/details/120444546
资源推荐
资源详情
资源评论
收起资源包目录
zookeeper从入门到精通的代码.rar (35个子文件)
zookeeper
zookeeper.iml 81B
src
test
java
main
resources
log4j.properties 459B
java
com
atguigu
case2
DistributedLock.java 4KB
DistributedLockTest.java 1KB
zk
zkClient.java 2KB
case3
CuratorLockTest.java 3KB
case1
DistributeServer.java 1KB
DistributeClient.java 2KB
target
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 572B
inputFiles.lst 578B
generated-sources
annotations
classes
com
atguigu
case2
DistributedLock$1.class 2KB
DistributedLockTest$2.class 1KB
DistributedLock.class 4KB
DistributedLockTest$1.class 1KB
DistributedLockTest.class 1021B
zk
zkClient$1.class 691B
zkClient.class 3KB
case3
CuratorLockTest$2.class 1KB
CuratorLockTest$1.class 1KB
CuratorLockTest.class 2KB
case1
DistributeServer.class 2KB
DistributeClient.class 3KB
DistributeServer$1.class 760B
DistributeClient$1.class 1KB
log4j.properties 459B
.idea
runConfigurations.xml 346B
misc.xml 526B
compiler.xml 542B
workspace.xml 8KB
encodings.xml 267B
codeStyles
Project.xml 269B
codeStyleConfig.xml 153B
.gitignore 227B
jarRepositories.xml 1KB
pom.xml 2KB
共 35 条
- 1
资源评论
码农研究僧
- 粉丝: 21w+
- 资源: 46
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功