/**
* connectString: zk的server地址,多个server之间用英文逗号分隔开
* connectionTimeoutMs: 连接超时时间,默认是 15s
* sessionTimeoutMs: 会话超时时间,默认是 60s
* retryPolicy: 失败重试策略
* ExponentialBackoffRetry: 构造器含有三个参数 ExponentialBackoffRetry(int baseSleepTimeMs,
* maxRetries,int maxSleepMs)
* baseSleepTimeMs:初始的sleep事件,用于计算之后的每次重试的sleep事件
* 计算公式:当前sleep时间=baseSleepTimeMs*Math.max(1,random.nextInt(1<<(retryCount+1)))
* maxRetries:最大重试次数
* maxSleepMs:最大sleep时间,如果上述的当前sleep计算出来比这个大,那么sleep这个时间,默认的最大时间
* 是 Integer.MAX_VALUE 毫秒
* start() 完成会话的创建
*/
public class CuratorUtil {
private CuratorFramework client;
private RetryPolicy retryPolicy = new ExponentialBackoffRetry(5000, 10);
@Before
public void before(){
client = CuratorFrameworkFactory.builder()
.connectString("101.132.167.18:2181")
.sessionTimeoutMs(50000)
.connectionTimeoutMs(30000)
.retryPolicy(retryPolicy)
.namespace("base") //独立的命名空间
.build();
client.start();
System.out.println("会话2被创建了......");
}
@Test
public void test(){
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("101.132.167.18:2181",retryPolicy);
curatorFramework.start();
System.out.println("会话被建立了....");
//使用fluent变成风格
CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("101.132.167.18:2181")
.sessionTimeoutMs(50000)
.connectionTimeoutMs(30000)
.retryPolicy(retryPolicy)
.namespace("base") //独立的命名空间
.build();
client.start();
System.out.println("会话2被创建了......");
}
/**
* creatingParentsIfNeeded这个接口非常有用,在使用zookeeper的过程中,开发人员经常
* 在创建子节点时抛出异常,其中一个可能的原因就是试图对一个不存在的父节点创建子节点。
* 因此,开发人员不得不在每次创建节点之前,都判断一下该父节点是否存在--这个处理通常比较
* 麻烦。在使用Curator之后,通过调用creatingParentsIfNeeded接口,Curator就能够自动地
* 递归创建所有需要的节点
*/
@Test
public void create() throws Exception {
client.create().forPath("/curator1","1234".getBytes());
//client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/curatorEp/ep/c1");
}
@Test
public void delete() throws Exception {
client.delete().forPath("/curator1");
client.delete().deletingChildrenIfNeeded().forPath("/curatorEp");
//client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/curatorEp/ep/c1");
//根据指定版本删除
client.delete().withVersion(1).forPath("/curator");
//强制删除一个节点
client.delete().guaranteed().forPath("/curator");
}
@Test
public void deleteInfo() throws Exception {
client.create().forPath("/delete-go");
client.delete().deletingChildrenIfNeeded().withVersion(-1).forPath("/delete-go");
System.out.println("删除成功!");
client.delete().forPath("/curator1");
}
@Test
public void get() throws Exception {
//普通查询
byte[] bytes = client.getData().forPath("/curator1");
System.out.println("节点数据中的内容:" + new String(bytes));
//包含状态查询
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/curator1");
System.out.println("节点的状态信息:" + stat.getVersion());
}
@Test
public void update() throws Exception {
//普通查询
byte[] bytes = client.getData().forPath("/curator1");
System.out.println("节点数据中的内容:" + new String(bytes));
//包含状态查询
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/curator1");
System.out.println("节点的状态信息:" + stat.getVersion());
//更新节点内容
int version = client.setData().withVersion(stat.getVersion()).forPath("/curator1", "修改内容1".getBytes()).getVersion();
System.out.println("当时的最新版本是:" + version);
byte[] bytes1 = client.getData().forPath("/curator1");
System.out.println("修改后的节点内容:" + new String(bytes1));
//不是 stat.getVersion() 不是最新版本抛异常,-1 代表最新版本
client.setData().withVersion(stat.getVersion()).forPath("/curator1","修改内容2".getBytes());
}
}
curator-api 简单操作
最新推荐文章于 2024-06-16 09:46:44 发布