import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.CuratorCache;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
@Slf4j
public class ZookeeperService {
@Autowired
private CuratorFramework client;
@Autowired
private InterProcessMutex lock;
/**
* 创建永久Zookeeper节点
*
* @param nodePath 节点路径(如果父节点不存在则会自动创建父节点),如:/curator
* @param nodeValue 节点数据
* @return 返回创建成功的节点路径
*/
public String createPersistentNode(String nodePath, String nodeValue) {
try {
return client.create().creatingParentsIfNeeded().forPath(nodePath, nodeValue.getBytes());
} catch (Exception e) {
log.error("创建永久Zookeeper节点失败,nodePath:{},nodeValue:{}", nodePath, nodeValue, e);
}
return null;
}
/**
* 创建永久有序Zookeeper节点
*
* @param nodePath 节点路径(如果父节点不存在则会自动创建父节点),如:/curator
* @param nodeValue 节点数据
* @return 返回创建成功的节点路径
*/
public String createSequentialPersistentNode(String nodePath, String nodeValue) {
try {
return client.create().creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT_SEQUENTIAL)
.forPath(nodePath, nodeValue.getBytes());
} catch (Exception e) {
log.error("创建永久有序Zookeeper节点失败,nodePath:{},nodeValue:{}", nodePath, nodeValue, e);
}
return null;
}
/**
* 创建临时Zookeeper节点
*
* @param nodePath 节点路径(如果父节点不存在则会自动创建父节点),如:/curator
* @param nodeValue 节点数据
* @return 返回创建成功的节点路径
*/
public String createEphemeralNode(String nodePath, String nodeValue) {
try {
return client.create().creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL)
.forPath(nodePath, nodeValue.getBytes());
} catch (Exception e) {
log.error("创建临时Zookeeper节点失败,nodePath:{},nodeValue:{}", nodePath, nodeValue, e);
}
return null;
}
/**
* 创建临时有序Zookeeper节点
*
* @param nodePath 节点路径(如果父节点不存在则会自动创建父节点),如:/curator
* @param nodeValue 节点数据
* @return 返回创建成功的节点路径
* @since 1.0.0
*/
public String createSequentialEphemeralNode(String nodePath, String nodeValue) {
try {
return client.create().creatingParentsIfNeeded()
.withMode(CreateMode.EPHEMERAL_SEQUENTIAL)
.forPath(nodePath, nodeValue.getBytes());
} catch (Exception e) {
log.error("创建临时有序Zookeeper节点失败,nodePath:{},nodeValue:{}", nodePath, nodeValue, e);
}
return null;
}
/**
* 检查Zookeeper节点是否存在
*
* @param nodePath 节点路径
* @return boolean 如果存在则返回true
*/
public boolean checkExists(String nodePath) {
try {
Stat stat = client.checkExists().forPath(nodePath);
return stat != null;
} catch (Exception e) {
log.error("检查Zookeeper节点是否存在出现异常,nodePath:{}", nodePath, e);
}
return false;
}
/**
* 获取某个Zookeeper节点的所有子节点
*
* @param nodePath 节点路径
* @return 返回所有子节点的节点名
*/
public List<String> getChildren(String nodePath) {
try {
return client.getChildren().forPath(nodePath);
} catch (Exception e) {
log.error("获取某个Zookeeper节点的所有子节点出现异常,nodePath:{}", nodePath, e);
}
return null;
}
/**
* 获取某个Zookeeper节点的数据
*
* @param nodePath 节点路径
* @return 节点存储的数据
*/
public String getData(String nodePath) {
try {
return new String(client.getData().forPath(nodePath));
} catch (Exception e) {
log.error("获取某个Zookeeper节点的数据出现异常,nodePath:{}", nodePath, e);
}
return null;
}
/**
* 设置某个Zookeeper节点的数据
*
* @param nodePath 节点路径
*/
public void setData(String nodePath, String newNodeValue) {
try {
client.setData().forPath(nodePath, newNodeValue.getBytes());
} catch (Exception e) {
log.error("设置某个Zookeeper节点的数据出现异常,nodePath:{}", nodePath, e);
}
}
/**
* 删除某个Zookeeper节点
*
* @param nodePath 节点路径
*/
public void delete(String nodePath) {
try {
client.delete().guaranteed().forPath(nodePath);
} catch (Exception e) {
log.error("删除某个Zookeeper节点出现异常,nodePath:{}", nodePath, e);
}
}
/**
* 级联删除某个Zookeeper节点及其子节点
*
* @param nodePath 节点路径
*/
public void deleteChildrenIfNeeded(String nodePath) {
try {
client.delete().guaranteed().deletingChildrenIfNeeded().forPath(nodePath);
} catch (Exception e) {
log.error("级联删除某个Zookeeper节点及其子节点出现异常,nodePath:{}", nodePath, e);
}
}
/**
* 监听节点变化
*
* @param nodePath 节点路径
*/
public void cacheListener(String nodePath) {
//1.创建curatorCache对象
CuratorCache curatorCache = CuratorCache.build(client, nodePath);
//2.注册监听器
curatorCache.listenable().addListener(new ZKNodeEventListener());
//3.开启监听:true加载缓冲数据
curatorCache.start();
}
/**
* 分布式锁-加锁,一直等待,直到加上为止
*/
public boolean getLock() {
try {
lock.acquire();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 分布式锁-尝试加锁
*
* @param waitTime 等待时间
* @param timeUnit 时间单位
*/
public boolean tryLock(long waitTime, TimeUnit timeUnit) {
try {
return lock.acquire(waitTime, timeUnit);
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 分布式锁-释放锁
*/
public boolean releaseLock() {
try {
if (lock.isOwnedByCurrentThread()) {
lock.release();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
16.Zookeeper工具类
于 2022-01-25 09:49:11 首次发布