This repository has been archived on 2025-09-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
galaxy-tsg-olap-storm-log-s…/src/main/java/com/zdjizhi/utils/zookeeper/ZookeeperUtils.java

140 lines
4.1 KiB
Java
Raw Normal View History

2021-03-16 14:48:07 +08:00
package com.zdjizhi.utils.zookeeper;
2021-03-25 14:27:41 +08:00
import cn.hutool.core.util.StrUtil;
2021-03-16 14:48:07 +08:00
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* @author qidaijie
2020-12-25 17:32:54 +08:00
* @Package cn.ac.iie.utils.zookeeper
* @Description:
* @date 2020/11/1411:28
*/
public class ZookeeperUtils implements Watcher {
2021-03-16 14:48:07 +08:00
private static final Log logger = LogFactory.get();
private ZooKeeper zookeeper;
private static final int SESSION_TIME_OUT = 20000;
private CountDownLatch countDownLatch = new CountDownLatch(1);
@Override
public void process(WatchedEvent event) {
2020-12-25 17:32:54 +08:00
if (event.getState() == Event.KeeperState.SyncConnected) {
countDownLatch.countDown();
}
}
/**
* 修改节点信息
*
* @param path 节点路径
*/
2020-12-25 17:32:54 +08:00
public int modifyNode(String path, String zookeeperIp) {
createNode(path, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, zookeeperIp);
int workerId = 0;
try {
2020-12-25 17:32:54 +08:00
connectZookeeper(zookeeperIp);
Stat stat = zookeeper.exists(path, true);
workerId = Integer.parseInt(getNodeDate(path));
2020-12-25 17:32:54 +08:00
if (workerId > 63) {
workerId = 0;
zookeeper.setData(path, "1".getBytes(), stat.getVersion());
} else {
String result = String.valueOf(workerId + 1);
if (stat != null) {
zookeeper.setData(path, result.getBytes(), stat.getVersion());
} else {
logger.error("Node does not exist!,Can't modify");
}
}
} catch (KeeperException | InterruptedException e) {
2021-03-25 14:27:41 +08:00
logger.error("modify error Can't modify," + e);
} finally {
closeConn();
}
2020-12-25 17:32:54 +08:00
logger.warn("workerID is" + workerId);
return workerId;
}
/**
* 连接zookeeper
*
2020-12-25 17:32:54 +08:00
* @param host 地址
*/
2020-12-25 17:32:54 +08:00
public void connectZookeeper(String host) {
try {
2020-12-25 17:32:54 +08:00
zookeeper = new ZooKeeper(host, SESSION_TIME_OUT, this);
countDownLatch.await();
} catch (IOException | InterruptedException e) {
2021-03-25 14:27:41 +08:00
logger.error("Connection to the Zookeeper Exception! message:" + e);
}
}
/**
* 关闭连接
*/
2020-12-25 17:32:54 +08:00
public void closeConn() {
try {
if (zookeeper != null) {
zookeeper.close();
}
} catch (InterruptedException e) {
2021-03-25 14:27:41 +08:00
logger.error("Close the Zookeeper connection Exception! message:" + e);
}
}
/**
* 获取节点内容
*
* @param path 节点路径
* @return 内容/异常null
*/
2020-12-25 17:32:54 +08:00
public String getNodeDate(String path) {
String result = null;
Stat stat = new Stat();
try {
byte[] resByte = zookeeper.getData(path, true, stat);
2021-03-25 14:27:41 +08:00
result = StrUtil.str(resByte, "UTF-8");
} catch (KeeperException | InterruptedException e) {
2021-03-25 14:27:41 +08:00
logger.error("Get node information exception" + e);
}
return result;
}
/**
* @param path 节点创建的路径
* @param date 节点所存储的数据的byte[]
* @param acls 控制权限策略
*/
2020-12-25 17:32:54 +08:00
public void createNode(String path, byte[] date, List<ACL> acls, String zookeeperIp) {
try {
2020-12-25 17:32:54 +08:00
connectZookeeper(zookeeperIp);
Stat exists = zookeeper.exists(path, true);
if (exists == null) {
2020-12-25 17:32:54 +08:00
Stat existsSnowflakeld = zookeeper.exists("/Snowflake", true);
if (existsSnowflakeld == null) {
zookeeper.create("/Snowflake", null, acls, CreateMode.PERSISTENT);
}
zookeeper.create(path, date, acls, CreateMode.PERSISTENT);
} else {
2020-12-25 17:32:54 +08:00
logger.warn("Node already exists ! Don't need to create");
}
} catch (KeeperException | InterruptedException e) {
2021-03-25 14:27:41 +08:00
logger.error(e);
} finally {
closeConn();
}
}
}