zookeeper curator 基本使用
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,下面,我们将通过一个简单的示例演示curator最基本的crud功能:maven依赖: org.apache.zookeeper zookeeper 3.4.12
千家信息网最后更新 2026年11月03日zookeeper curator 基本使用
下面,我们将通过一个简单的示例演示curator最基本的crud功能:
maven依赖:
org.apache.zookeeper zookeeper 3.4.12 org.apache.curator curator-recipes 2.12.0 junit junit 4.12 test 测试类:
package per.ym.zookeeper;import java.util.List;import org.apache.curator.RetryPolicy;import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.BoundedExponentialBackoffRetry;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.curator.retry.RetryForever;import org.apache.curator.retry.RetryNTimes;import org.apache.curator.retry.RetryOneTime;import org.apache.curator.retry.RetryUntilElapsed;import org.apache.zookeeper.data.Stat;import org.junit.After;import org.junit.Before;import org.junit.Test;public class CuratorBasicAPI { private CuratorFramework client; private String connectString = "192.168.61.131:2184"; private int sessionTimeoutMs = 60000; private int connectionTimeoutMs = 60000; private String path = "/test"; private String rootPath = "/"; private byte[] data = "data".getBytes(); private byte[] newData = "newData".getBytes(); private int baseSleepTimeMs = 1000; private int maxRetries = 3; @Before public void connect() { //baseSleepTimeMs:基础睡眠时间;maxRetries:最大重试次数;maxSleepMs:两次重试之间最大睡眠时间 //其睡眠时间计算公式为:sleepMs = baseSleepTimeMs * Math.max(1, random.nextInt(1 << (retryCount + 1))); RetryPolicy retryPolicy1 = new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries); //ExponentialBackoffRetry的子类,重写了父类的getSleepTimeMs方法 //其睡眠时间计算公式为:Math.min(maxSleepTimeMs, super.getSleepTimeMs(retryCount, elapsedTimeMs)); RetryPolicy retryPolicy2 = new BoundedExponentialBackoffRetry(baseSleepTimeMs, 50000, maxRetries); //最大重试3次,每次相隔时间为5000毫秒 RetryPolicy retryPolicy3 = new RetryNTimes(3, 5000); //只重试一次,RetryNTimes的子类,就是将次数指定为1 RetryPolicy retryPolicy4 = new RetryOneTime(3000); //一直重试,重试间隔为3000毫秒 RetryPolicy retryPolicy5 = new RetryForever(3000); //最大重试时间为20000毫秒,若超过就不重试了,每次间隔3000毫秒 RetryPolicy retryPolicy6 = new RetryUntilElapsed(20000, 3000); //client = CuratorFrameworkFactory.newClient(connectString, sessionTimeoutMs, connectionTimeoutMs, retryPolicy); //流式 client = CuratorFrameworkFactory.builder() .connectString(connectString) //服务器列表,格式host1:port1,host2:port2,… .sessionTimeoutMs(sessionTimeoutMs) //会话超时时间,单位毫秒,默认60000ms .connectionTimeoutMs(connectionTimeoutMs) //连接创建超时时间,单位毫秒,默认60000ms .retryPolicy(retryPolicy1) .build(); //启动 client.start(); } @Test public void showBasicAPI() throws Exception { exist(); create(); exist(); getData(); setData(); getData(); getChildren(); delete(); getChildren(); } @After public void close() { //关闭连接 client.close(); } private void create() throws Exception { //创建一个节点 //client.create().forPath(path); //创建一个节点附带数据 client.create().forPath(path, data); System.out.println("创建节点:" + path); //创建一个临时节点,默认不指定是持久的,另外还有持久带序列和临时带序列PERSISTENT_SEQUENTIAL、EPHEMERAL_SEQUENTIAL //client.create().withMode(CreateMode.EPHEMERAL).forPath(path); //如果没有父目录则一起创建 //client.create().creatingParentsIfNeeded().forPath(path); //后天模式,还可以传入自定义CallBack和线程池 //client.create().inBackground().forPath(path); //带权限的 //List aclList = new ArrayList(); //acls.add(new ACL(Perms.ALL, new Id("digest", "520:1314"))); //client.create().withACL(aclList).forPath(path); //当然你也这可这样 //client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).withACL(aclList).inBackground().forPath(path, data); } private void exist() throws Exception { Stat stat = client.checkExists().forPath(path); if (stat == null) { System.out.println("节点" + path + "不存在。"); } else { System.out.println("节点" + path + "存在。"); } } private void getData() throws Exception { byte[] byteData = client.getData().forPath(path); //client.getData().inBackground().forPath(path); //Stat stat = new Stat(); //client.getData().storingStatIn(stat).forPath("path"); System.out.println("节点" + path + "的数据为:" + new String(byteData)); } private void setData() throws Exception { client.setData().forPath(path, newData); System.out.println("设置节点" + path + "数据为:" + new String(newData)); //client.setData().inBackground().forPath(path, newData); //指定版本,若版本错误将抛出异常bad version //client.setData().withVersion(520).forPath(path,newData); } private void delete() throws Exception { client.delete().forPath(path); System.out.println("删除节点:" + path); //client.delete().withVersion(1314).forPath(path); //只要会话有效将一直尝试删除,直到删除成功 //client.delete().guaranteed().forPath(path); //同 //client.delete().deletingChildrenIfNeeded().forPath(path); //client.delete().inBackground().forPath(path); //client.delete().guaranteed().deletingChildrenIfNeeded().withVersion(1314).inBackground().forPath(path); } private void getChildren() throws Exception { List names = client.getChildren().forPath(rootPath); System.out.println("根目录下有节点:" + names); //client.getChildren().inBackground().forPath(rootPath); }} 运行结果:节点/test不存在。创建节点:/test节点/test存在。节点/test的数据为:data设置节点/test数据为:newData节点/test的数据为:newData根目录下有节点:[zookeeper, test]删除节点:/test根目录下有节点:[zookeeper]
节点
时间
数据
最大
睡眠
下有
根目录
公式
单位
子类
序列
次数
版本
有效
成功
之间
功能
基础
就是
方法
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
手磨咖啡
广东猪兼强互联网科技有限公司
互联网科技小贷
南京欣网互联网科技
亿账通互联网科技
互联网 视角下的科技创新
互联网科技素材视频竖屏
太原新华互联网科技学校怎么样
科技与互联网哪个更好
互联网时代科技改变生活
银川新华互联网科技学校联系电话
华夏恒生互联网科技013171
科技赋能视角下互联网保险发展
中和天地互联网科技有限公司
南通创西澳互联网科技有限公司
工业互联网科技创业
吉林省码上互联网科技电话
长沙渺峰互联网科技有限公司
网互联网络科技
六子台科技互联网展会
深圳恒合互联网络科技有限公司
科技互联网龙头
盈实互联网科技
中资互联网科技
深圳科技互联网
互联网金融 金融科技
齐家互联网科技
华中科技大学互联网
郑州华宇互联网科技
996高科技互联网公司