提交 a18c0542 编写于 作者: 武汉红喜's avatar 武汉红喜

CuratorTest

上级 cddabfe2
package org.hongxi.whatsmars.zk.curator;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
/**
* Created by shenhongxi on 2018/10/27.
*/
public class CuratorTest {
public static void main(String[] args) throws Exception {
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new RetryNTimes(10, 5000));
client.start();
System.out.println("zk client started!");
String path1 = "/test";
Stat stat = client.checkExists().forPath(path1);
if (stat == null) {
client.inTransaction().create().forPath(path1, "hongxi.org".getBytes())
.and().setData().forPath(path1, "org.hongxi".getBytes())
.and().commit();
}
System.out.println(new String(client.getData().forPath(path1)));
client.delete().deletingChildrenIfNeeded().forPath(path1);
System.out.println(client.getChildren().forPath("/"));
String path2 = "/dubbo/org.hongxi.service.OrderService/providers";
client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path2, "A".getBytes());
stat = client.checkExists().forPath("/dubbo/org.hongxi.service.OrderService");
if (stat != null) System.out.println(stat.getCzxid());
stat = client.checkExists().forPath("/dubbo/org.hongxi.service.OrderService/providers");
if (stat != null) System.out.println(stat.getMzxid());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册