curator简介

Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更高,提供了各种应用场景的实现封装,fluent风格的API增加了代码的优雅性,简化了Zookeeper客户端的开发量。

public class CuratorCreateSessionDemo {private final static String CONNECTSTRING="192.168.74.131:2181,192.168.74.132:2181,192.168.74.133:2181";public static void main(String[] args) {//创建会话的两种方式CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(CONNECTSTRING, 5000,5000,new ExponentialBackoffRetry(1000, 3));//ExponentialBackoffRetry 衰减重试curatorFramework.start();//fluent风格CuratorFramework curatorFramework2 = CuratorFrameworkFactory.builder().connectString(CONNECTSTRING).sessionTimeoutMs(5000).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();curatorFramework2.start();System.out.println("success");}
}

curator创建 删除  查询 更新  异步   事务(事务是curator独有的)代码:

public class CuratorOperatorDemo {public static void main(String[] args) throws Exception {CuratorFramework curatorFramework = CuratorClientUtils.getInstance();System.out.println("连接成功....");// flunt风格/*** 创建节点 可以递归创建*//** try { String result =* curatorFramework.create().creatingParentsIfNeeded().withMode(* CreateMode.PERSISTENT).forPath("/c/cc/ccc", "123".getBytes()); }* catch (Exception e) { // TODO: handle exception }*//*** 删除节点 可以递归删除*//** try {* curatorFramework.delete().deletingChildrenIfNeeded().forPath("/c"); }* catch (Exception e) { // TODO: handle exception }*//*** 查询*//** try { Stat stat = new Stat(); byte[]* bytes=curatorFramework.getData().storingStatIn(stat).forPath(* "/zkclient"); System.out.println(new String(bytes)+"-->stat:"+stat);* } catch (Exception e) { }*//*** 更新*//** try { Stat stat = curatorFramework.setData().forPath("/zookeeper",* "666".getBytes()); byte[]* bytes=curatorFramework.getData().storingStatIn(stat).forPath(* "/zkclient"); System.out.println(new String(bytes));* System.out.println("stat:"+stat); } catch (Exception e) { // TODO:* handle exception }*//** * 异步操作   创建节点与set值异步进行*//*ExecutorService service = Executors.newFixedThreadPool(1);final CountDownLatch countDownLatch = new CountDownLatch(1);try {System.out.println(1);curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).inBackground(new BackgroundCallback() {public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent)throws Exception {// TODO Auto-generated method stubSystem.out.println(Thread.currentThread().getName() + "->resultCode:"+ curatorEvent.getResultCode() + "->" + curatorEvent.getType());countDownLatch.countDown();}},service).forPath("/b","111".getBytes());} catch (Exception e) {}countDownLatch.await();service.shutdown();*//*** 事务操作*//*try {Collection<CuratorTransactionResult> resultCollections=curatorFramework.inTransaction().create().forPath("/trans","111".getBytes()).and().setData().forPath("/bzy","111".getBytes()).and().commit();for (CuratorTransactionResult result:resultCollections){System.out.println(result.getForPath()+"->"+result.getType());}} catch (Exception e) {e.printStackTrace();}*/}
}

curator创建监听

public class CuratorEventDemo {/*** 三种watcher来做节点监听* pathcache 监视一个路径下子节点的创建 删除 数据更新* nodecache 监视一个节点的创建 更新 删除* treecache pathcache+nodecache的合体(监视路径下的创建 更新 删除)* @throws Exception */public static void main(String[] args) throws Exception {CuratorFramework curatorFramework = CuratorClientUtils.getInstance();/*** 节点变化nodecache*/NodeCache nodeCache = new NodeCache(curatorFramework,"/zkclient",false);nodeCache.start(true);nodeCache.getListenable().addListener(()-> System.out.println("节点数据发生变化,变化后的结果" +":"+new String(nodeCache.getCurrentData().getData())));curatorFramework.setData().forPath("/zkclient","bzy666".getBytes());/*** pathcache 子节点数据变化*/PathChildrenCache cache=new PathChildrenCache(curatorFramework,"/zkclient",true);cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);// Normal / BUILD_INITIAL_CACHE /POST_INITIALIZED_EVENTcache.getListenable().addListener((curatorFramework1,pathChildrenCacheEvent)->{switch (pathChildrenCacheEvent.getType()){case CHILD_ADDED:System.out.println("增加子节点");break;case CHILD_REMOVED:System.out.println("删除子节点");break;case CHILD_UPDATED:System.out.println("更新子节点");break;default:break;}});curatorFramework.create().withMode(CreateMode.PERSISTENT).forPath("/zkclient","zkclient".getBytes());TimeUnit.SECONDS.sleep(1);System.out.println("1");curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath("/zkclient/zkclient1","1".getBytes());TimeUnit.SECONDS.sleep(1);System.out.println("2");curatorFramework.setData().forPath("/zkclient/zkclient1","222".getBytes());TimeUnit.SECONDS.sleep(1);System.out.println("3");curatorFramework.delete().forPath("/zkclient/zkclient1");System.out.println("4");System.in.read();}
}

zookeeper客户端 curator的使用相关推荐

  1. Zookeeper客户端Curator使用详解

    http://www.jianshu.com/p/70151fc0ef5d Zookeeper客户端Curator使用详解 简介 Curator是Netflix公司开源的一套zookeeper客户端框 ...

  2. ZooKeeper客户端Curator的基本使用

    前提:ZooKeeper版本:3.4.14      Curator版本:2.13.0 1.什么是Curator Curator是Netflix公司开源的一套zookeeper客户端框架,解决了很多Z ...

  3. 【高级篇】详解Zookeeper客户端Curator

    一.序言 之前分享过一篇关于Curotor的基本应用[基础篇]详解Zookeeper客户端Curator,如果对Curator没有了解的可以看看,本文分享关于Curator的一些高级特性,在监听和le ...

  4. 【基础篇】详解Zookeeper客户端Curator

    一.前言 Zookeeper被广泛应用于分布式环境下各种应用程序的协调,而Curator无疑是Zookeeper客户端中的瑞士军刀,解决了很多Zookeeper客户端非常底层的细节开发工作,包括连接重 ...

  5. Zookeeper客户端Curator详解

    一.Curator 客户端使用 Curator是 Netflix公司开源的一套ZooKeeper客户端框架,和 ZkClient一样它解决了非常底层的细节开发工作,包括连接.重连.反复注册Watche ...

  6. 2.ZooKeeper客户端Curator「第三章 ZooKeeper Java客户端」「架构之路ZooKeeper理论和实战」

    前言 上一篇文章 介绍了zookeeper原生API的使用,使用过原生API不得不说,有很多的问题,比如:不能递归创建和删除节点.Watcher只能使用一次.还有很多可以解决分布式应用问题的api(比 ...

  7. 聊聊、Zookeeper 客户端 Curator

    [Curator]   和 ZkClient 一样,Curator 也是开源客户端,Curator 是 Netflix 公司开源的一套框架. <dependency><groupId ...

  8. Zookeeper客户端Curator Framework使用

    Curator是Zookeeper开源的客户端框架,封装了很多API,使用起来非常的方便,直接进入正题,讲解如何使用. 一.客户端创建 使用静态工厂方式进行创建,connectionInfo为Zook ...

  9. ZooKeeper客户端Curator使用一 创建连接

    如何创建一个ZK连接 工厂方法newClient() public static void main(String[] args) {final String connectString = &quo ...

最新文章

  1. PHP时间戳 strtotime()使用方法和技巧
  2. 类的const和非const成员函数的重载
  3. MySQL事务控制语句
  4. Queue:poll、offer、element、peek的区别
  5. MATLAB用递归法求解集合子集,用递归法求一个集合的子集c语言,急!!!
  6. iloc,ix和loc有何不同?
  7. php读取doc pdf文件,PHP读取创建txt,doc,xls,pdf类型文件
  8. arm poky linux,opencv移植在4412和imx6(yocto 3.14.28 arm-poky-linux-gnueabi )上
  9. oracle管理员的作用,ORACLE数据库管理员的职责
  10. OpenCV — Otsu 算法
  11. Python 语感训练100题
  12. docker学习之docker hub寻宝游戏
  13. 应急响应--windows主机入侵排查思路
  14. (E1)ENVI-met介绍及下载
  15. 余淼杰老师 经济学原理复习笔记(微观)
  16. 知乎上的48条神回复,句句都是人生哲理
  17. 关于模型的评估指标(超详细)
  18. gpg生成秘钥时卡死
  19. (附源码)计算机毕业设计SSM制造型企业仓储管理系统
  20. mysql 存储表情符号

热门文章

  1. 今日头条是怎么挣钱的
  2. 华为mate20 pro 专业模式拍照
  3. 芭比娃娃缘何泪洒上海滩?
  4. 一篇让你弄明白C语言指针传参和数组传参~
  5. oracle的switch+case语句吗,2.7 switch 语句中的 case 范围
  6. 【快进来,这不是毒鸡汤,只是有毒而已】
  7. Color类 设置字体颜色、背景颜色
  8. 圣诞节,描述京东 Merry Christmas
  9. 利用MATLAB画传递函数的奈奎斯特曲线
  10. 虚幻4引擎开发的手游_虚幻4引擎开发 《神佑》手游首次公开