Zookeeper实例Curator API-使用Curator更新数据内容

来源:互联网 发布:电子发票软件 编辑:程序博客网 时间:2024/05/17 08:41


import org.apache.curator.framework.CuratorFramework;import org.apache.curator.framework.CuratorFrameworkFactory;import org.apache.curator.retry.ExponentialBackoffRetry;import org.apache.zookeeper.CreateMode;import org.apache.zookeeper.data.Stat;/** *  * @ClassName: Set_Data_Sample * @Description: TODO(使用Curator更新数据内容) * @author RongShu * @date 2017年6月17日 上午10:06:07 * */public class Set_Data_Sample {static String path = "/zk-book";static CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").sessionTimeoutMs(5000).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();public static void main(String[] args) throws Exception {client.start();client.delete().deletingChildrenIfNeeded().forPath(path);client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path, "init".getBytes());Stat stat = new Stat();client.getData().storingStatIn(stat).forPath(path);System.out.println("Success set node for : " + path + ", new version: "+ client.setData().withVersion(stat.getVersion()).forPath(path).getVersion());try {client.setData().withVersion(stat.getVersion()).forPath(path);} catch (Exception e) {System.out.println("Fail set node due to " + e.getMessage());}}}输出Success set node for : /zk-book, new version: 1Fail set node due to KeeperErrorCode = BadVersion for /zk-book


参考

1.《从Paxos到Zookeeper:分布式一致性原理与实践》

2.https://zookeeper.apache.org/doc/r3.5.3-beta/javaExample.html




阅读全文
0 0