public class TestZookeeper {
public static void main(String[] args) {
/*
1.将zookeeper的相关配置从数据库中获取出来
/
String zkStr = “”;
int retryInterval = 0;
int retryIntervalCelling = 0;
int retryTimes = 0;
/
2.根据zookeeper创建curator客户端
/
ZookeeperConnect zookeeperConnect = new ZookeeperConnect();
zookeeperConnect.setZkStr(zkStr);
zookeeperConnect.setRetryInterval(retryInterval);
zookeeperConnect.setRetryIntervalCelling(retryIntervalCelling);
zookeeperConnect.setRetryTimes(retryTimes);
CuratorFramework curatorFramework = ZkUtils.newCurator(zookeeperConnect);
/
3.根据预先设定好要更新情况的路径,看节点是否存在.如果节点不存在即创建节点,并将信息发到放进去。
若节点存在,则将更新的信息更新进去。
CreateMode的四种方案:
PERSISTENT,持久化目录节点,会话结束存储数据不会丢失。
PERSISTENT_SEQUENTIAL:顺序自动编号持久化目录节点,会根据已存在的节点数自动加1,持久化
EPHEMERAL:临时目录节点
EPHEMERAL_SEQUENTIAL:临时自动编号节点
*/
String zkPath = “”;
String info = “”;
try {
if (curatorFramework.checkExists().forPath(zkPath) == null){
curatorFramework.create()
.creatingParentsIfNeeded()
.withMode(CreateMode.PERSISTENT)
.forPath(zkPath, info.getBytes());
}else {
curatorFramework.setData().forPath(zkPath, info.getBytes());
}
}catch (Exception e){
System.out.println(“zookeeper节点联系不上,请检查代码的zookeeper和服务端的zookeeper的版本是否对应”);
更新zookeeper节点信息
最新推荐文章于 2023-08-20 13:56:07 发布