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;
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;
}
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;
}
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;
}
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;
}
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;
}
public List getChildren(String nodePath) {
try {
return client.getChildren().forPath(nodePath);
} catch (Exception e) {
log.error("获取某个Zookeeper节点的所有子节点出现异常,nodePath:{}", nodePath, e);
}
return null;
}
public String getData(String nodePath) {
try {
return new String(client.getData().forPath(nodePath));
} catch (Exception e) {
log.error("获取某个Zookeeper节点的数据出现异常,nodePath:{}", nodePath, e);
}
return null;
}
public void setData(String nodePath, String newNodevalue) {
try {
client.setData().forPath(nodePath, newNodevalue.getBytes());
} catch (Exception e) {
log.error("设置某个Zookeeper节点的数据出现异常,nodePath:{}", nodePath, e);
}
}
public void delete(String nodePath) {
try {
client.delete().guaranteed().forPath(nodePath);
} catch (Exception e) {
log.error("删除某个Zookeeper节点出现异常,nodePath:{}", nodePath, e);
}
}
public void deleteChildrenIfNeeded(String nodePath) {
try {
client.delete().guaranteed().deletingChildrenIfNeeded().forPath(nodePath);
} catch (Exception e) {
log.error("级联删除某个Zookeeper节点及其子节点出现异常,nodePath:{}", nodePath, e);
}
}
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;
}
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;
}
}