private CuratorFramework client;
@Before
public void testConnect(){
RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,10);
client = CuratorFrameworkFactory.builder().connectString("127.0.0.1:2181")
.sessionTimeoutMs(60 * 100)
.connectionTimeoutMs(15 * 1000)
.retryPolicy(retryPolicy).namespace("it").build();
client.start();
}
@Test
public void testCreate() throws Exception {
client.create().creatingParentContainersIfNeeded().forPath("/it/app1");//同时创建父节点和子节点
String path = client.create().forPath("/app1");
System.out.println(path);
}
@Test
public void testGet() throws Exception {
byte[] bytes = client.getData().forPath("/app1");//当前节点信息
System.out.println(new String(bytes));
List<String> strings = client.getChildren().forPath("/");//查询子节点
System.out.println(strings);
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/app1");//将节点所有信息记录到stat
System.out.println(stat);
}
@Test
public void testSet() throws Exception {
Stat stat = new Stat();
client.getData().storingStatIn(stat).forPath("/app1");
int version = stat.getVersion();
client.setData().withVersion(version).forPath("/","hhh".getBytes(StandardCharsets.UTF_8));//根据版本修改若版本不对这修改失败
}
@Test
public void testDelete() throws Exception {
client.delete().forPath("/app1");
client.delete().deletingChildrenIfNeeded().forPath("/it");//有子节点也删除
client.delete().guaranteed().forPath("/app1"); //guaranteed为保证被删除(反复尝试)
//返回被删除节点信息
client.delete().guaranteed().inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
System.out.println(curatorEvent);
}
}).forPath("/app1");
}
@After
public void closer(){
client.close();
}
zookeeper的增删改查
最新推荐文章于 2024-05-04 20:20:12 发布