curator操作zk节点

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
import com.alibaba.dubbo.registry.Registry;
import com.alibaba.dubbo.registry.RegistryFactory;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

CuratorFramework client = CuratorFrameworkFactory.builder(). connectString("127.0.0.1:2181"). // 若配置集群则是 ip1:port, ip2:post, ip3:port(,隔开) sessionTimeoutMs(5000). // 超时,也是心跳时间 retryPolicy(new ExponentialBackoffRetry(1000, 3)). // 重试策略,初试时间为1s 重试3次 build(); client.start(); // 启动 注:服务器防火墙端口要开放 //新建节点,节点存在时不能新建 //client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/data/program", "test".getBytes()); List<String> routers = client.getChildren().forPath("/dubbo/com.example.springboot_demo.service.UserService/routers"); String res = URL.decode(URL.decode(routers.get(0))); Stat stat = client.checkExists().forPath("/dubbo/com.example.springboot_demo.service.UserService/routers"); //不存在则返回null if(stat!=null){ //删除节点 client.delete().guaranteed().deletingChildrenIfNeeded().forPath("/dubbo/com.example.springboot_demo.service.UserService/routers/"+routers.get(0)); //client.delete().forPath("/dubbo/com.example.springboot_demo.service.UserService/routers/"+routers.get(0)); } //修改子节点 //client.setData().forPath("/super/c2", "修改c2内容".getBytes());

 

posted @ 2023-08-06 21:50  Mr_sven  阅读(36)  评论(0编辑  收藏  举报