curator操作zookeeper
使用zookeeper原生API实现一些复杂的东西比较麻烦。所以,出现了两款比较好的开源客户端,对zookeeper的原生API进行了包装:zkClient和curator。后者是Netflix出版的,必属精品,也是最好用的zk的开源客户端。
一 curator基本API使用
推荐博客:https://www.cnblogs.com/java-zhao/p/7350945.html
https://www.cnblogs.com/nevermorewang/p/5815602.html 强烈推荐
pom
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.</version>
</dependency>
创建客户端
private static CuratorFramework client = CuratorFrameworkFactory.builder()
.authorization("digest","admin:123".getBytes()) //创建客户端的时候授权
.connectString("ip:2181")
.sessionTimeoutMs()
.connectionTimeoutMs()
.retryPolicy(new ExponentialBackoffRetry(, )).build();
创建节点
/**
* 创建会话
*/
client.start(); /**
* 创建节点
* 注意:
* 1 除非指明创建节点的类型,默认是持久节点
* 2 ZooKeeper规定:所有非叶子节点都是持久节点,所以递归创建出来的节点,只有最后的数据节点才是指定类型的节点,其父节点是持久节点
*/
client.create().forPath("/China");//创建一个初始内容为空的节点
client.create().forPath("/America", "zhangsan".getBytes());
client.create().withMode(CreateMode.EPHEMERAL).forPath("/France");//创建一个初始内容为空的临时节点
client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/Russia/car", "haha".getBytes());//递归创建,/Russia是持久节点 /**
* 异步创建节点
* 注意:如果自己指定了线程池,那么相应的操作就会在线程池中执行,如果没有指定,那么就会使用Zookeeper的EventThread线程对事件进行串行处理
*/
client.create().withMode(CreateMode.EPHEMERAL).inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println("当前线程:" + Thread.currentThread().getName() + ",code:" + event.getResultCode()
+ ",type:" + event.getType());
}
}, Executors.newFixedThreadPool(10)).forPath("/async-curator-my"); client.create().withMode(CreateMode.EPHEMERAL).inBackground(new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
System.out.println("当前线程:" + Thread.currentThread().getName() + ",code:" + event.getResultCode()
+ ",type:" + event.getType());
}
}).forPath("/async-curator-zookeeper");
获取节点数据
Stat stat = new Stat();
byte[] bytes = zk.curator.getData().storingStatIn(stat).forPath("/liuzhonghua");
System.out.println("路径/liuzhonghua的数据是:"+new String(bytes));
System.out.println("路径/liuzhonghua的数据的版本是:"+stat.getVersion());
路径/liuzhonghua的数据是:lzh
路径/liuzhonghua的数据的版本是:0
更新节点
client.setData().withVersion(4).forPath("/America", "lisi".getBytes());
删除节点
/**
* 删除节点
*/
client.delete().forPath("/China");//只能删除叶子节点
client.delete().deletingChildrenIfNeeded().forPath("/Russia");//删除一个节点,并递归删除其所有子节点
client.delete().withVersion(5).forPath("/America");//强制指定版本进行删除
client.delete().guaranteed().forPath("/America");//注意:由于一些网络原因,上述的删除操作有可能失败,使用guaranteed(),如果删除失败,会记录下来,只要会话有效,就会不断的重试,直到删除成功为止
创建Acl权限
ArrayList<ACL> acls = new ArrayList<ACL>();
Id id1=new Id("digest", DigestAuthenticationProvider.generateDigest("admin1:123"));
Id id2=new Id("digest", DigestAuthenticationProvider.generateDigest("admin2:123"));
acls.add(new ACL(ZooDefs.Perms.ADMIN,id1));
acls.add(new ACL(ZooDefs.Perms.CREATE,id2));
acls.add(new ACL(ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ,id2));
zk.curator.create().creatingParentsIfNeeded().withACL(acls).forPath("/liuzhonghua001/003","lzh".getBytes()); 结果:
[zk: localhost:2181(CONNECTED) 5] getAcl /liuzhonghua001/003
'digest,'admin1:CC9El/l/qyakTrK/mNREkrSikQ0=
: a
'digest,'admin2:iZICqg+4cn1ouEHqGPuzGrop/M4=
: c
'digest,'admin2:iZICqg+4cn1ouEHqGPuzGrop/M4=
: ra
[zk: localhost:2181(CONNECTED) 6]
设置Acl权限
ArrayList<ACL> acls = new ArrayList<ACL>();
Id id1=new Id("digest", DigestAuthenticationProvider.generateDigest("admin1:123"));
Id id2=new Id("digest", DigestAuthenticationProvider.generateDigest("admin2:123"));
acls.add(new ACL(ZooDefs.Perms.ADMIN,id1));
acls.add(new ACL(ZooDefs.Perms.CREATE,id2));
acls.add(new ACL(ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ,id2)); zk.curator.setACL().withACL(acls).forPath("/liuzhonghua001/003");
curator实现事件监听
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
对指定节点进行监听(监听指定节点本身的变化,包括节点本身的创建和节点本身数据的变化)
NodeCache nodeCache = new NodeCache(client,"/lzh");
nodeCache.getListenable().addListener(new NodeCacheListener() {
@Override
public void nodeChanged() throws Exception {
System.out.println("新的节点数据:" + new String(nodeCache.getCurrentData().getData()));
}
});
nodeCache.start(true);
监听子节点变化情况(1 新增子节点,2删除子节点,3数据改变)
/**
* 监听子节点变化情况
* 1 新增子节点
* 2 删除子节点
* 3 子节点数据变更
*/
PathChildrenCache pathChildrenCache = new PathChildrenCache(client,"/lzh",true);
pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
switch (event.getType()){
case CHILD_ADDED:
System.out.println("新增子节点:" + event.getData().getPath());
break;
case CHILD_UPDATED:
System.out.println("子节点数据变化:" + event.getData().getPath());
break;
case CHILD_REMOVED:
System.out.println("删除子节点:" + event.getData().getPath());
break;
default:
break;
}
}
});
pathChildrenCache.start();
- PathChildrenCache只会监听指定节点的一级子节点,不会监听节点本身(例如:“/book13”),也不会监听子节点的子节点(例如,“/book13/car/color”)
curator操作zookeeper的更多相关文章
- Java curator操作zookeeper获取kafka
Java curator操作zookeeper获取kafka Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更 ...
- 使用Curator操作ZooKeeper
Curator是Netflix公司开源的一个ZooKeeper client library,用于简化ZooKeeper客户端编程.它包含如下模块: Framework:Framework是ZooKe ...
- 通过Curator操作Zookeeper的简单例子代码
Curator主要解决了三类问题: 一个是ZooKeeper client与ZooKeeper server之间的连接处理; 一个是提供了一套Fluent风格的操作API; 一个是ZooKeeper各 ...
- 使用curator框架简单操作zookeeper 学习笔记
Curator 操作是zookeeper的优秀api(相对于原生api),满足大部分需求.而且是Fluent流式api风格. 参考文献:https://www.jianshu.com/p/70151f ...
- 15. 使用Apache Curator管理ZooKeeper
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
- Java操作zookeeper
Java操作zookeeper总共有三种方式: 1.原生的Java API 2.zkclient 3.curator 第一种实现代码: pom.xml <dependency> <g ...
- 15. 使用Apache Curator装饰ZooKeeper
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
- storm操作zookeeper源码分析-cluster.clj
storm操作zookeeper的主要函数都定义在命名空间backtype.storm.cluster中(即cluster.clj文件中).backtype.storm.cluster定义了两个重要p ...
- 使用Apache Curator管理ZooKeeper(转)
Apache ZooKeeper是为了帮助解决复杂问题的软件工具,它可以帮助用户从复杂的实现中解救出来. 然而,ZooKeeper只暴露了原语,这取决于用户如何使用这些原语来解决应用程序中的协调问题. ...
随机推荐
- myclipse里有感叹号的问题,希望可以帮到各位
今天,我在myeclipse中导入一个项目的时候就发现一个问题:项目有红色感叹号,并且项目有红色错误提示.我首先看了这个红色错误提示的地方,发现这个根本不应该报错,想必是这个红色感叹号的原因. 于 ...
- 读取CSV到DataTable
using System; using System.Collections.Generic; using System.Data; using System.Data.OleDb; using Sy ...
- HDU1800 字典树写法
题意:高级魔法师可以教低级魔法师 魔法扫把技能,同时教会了的低级魔法师又可以教比他更低级是,是传递的关系 同时如果教会了的话,他们可以同时坐一个扫把 问最少需要多少个扫把 思路:就是判断相同的数字最多 ...
- 爬虫_豆瓣全部正在热映电影 (xpath)
单纯地练习一下xpath import requests from lxml import etree def get_url(url): html = requests.get(url) retur ...
- Picture POJ - 1177 (扫描线)
扫描线求周长,可以看成两条线,一条扫x轴,一条扫y轴,然后这两天线扫过去的 周长加起来,就是周长了 #include<map> #include<set> #include&l ...
- kvm虚拟化管理
虚拟化 KVM (kernel-based virtual machine) 常见的一些虚拟化的软件xen kvm vmware esx openVZ Oracle VM VirtualBox vsp ...
- BZOJ2288 生日礼物
本题是数据备份的进阶版. 首先去掉所有0,把连续的正数/负数连起来. 计算所有正数段的个数与总和. 然后考虑数据备份,有一点区别: 如果我们在数列中选出一个负数,相当于把它左右连起来. 选出一个正数, ...
- P1966 火柴排队
这道题需要小小的思考一波 (然而我思考了两节课) 好,我们先得出一个结论:a中第k大的与b中第k大的一定要排在一起,才能保证最小. 然后发现:挪a,b其实没有区别,故我们固定a,挪b. 然后我们就思考 ...
- 跟着 underscore 学节流
更多内容请参考:我的新博客 在上一篇文章中,我们了解了为什么要限制事件的频繁触发,以及如何做限制: debounce 防抖 throttle 节流 上次已经说过防抖的实现了,今天主要来说一下节流的实现 ...
- (五)Oracle 的 oracle 表查询
http://www.hechaku.com/Oracle/oracle_tables_chack.html 通过scott用户下的表来演示如何使用select语句,接下来对emp.dept.salg ...