一、Zookeeper的原生API使用
zookeeper 提供了java与C两种语言的客户端。我们学习的java的客户端使用,引入maven的jar包依赖。
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.8</version> </dependency>
客户端类:org.apache.zookeeper.ZooKeeper
1、初始连接
实例化 Zookeeper 类之后将会自动与集群建立连接。构造方法的参数:
- connectString(String):连接串,包括ip+端口 ,集群模式下用逗号隔开;
- sessionTimeout(int):会话超时时间,该值不能超过服务端所设置的minSessionTimeout 和maxSessionTimeout;
- watcher(Watcher):会话监听器,服务端事件将会触该监听;
- sessionId(long):自定义会话ID;
- sessionPasswd(byte[]):会话密码;
- canBeReadOnly(boolean):该连接是否为只读的;
- hostProvider(HostProvider):服务端地址提供者,指示客户端如何选择某个服务来调用,默认采用StaticHostProvider实现;
ZooKeeper zooKeeper = new ZooKeeper("192.168.1.1:2181", 4000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
}
});
2、创建节点 create方法
create的参数说明:
- path(String):节点路径;
- data(btye[]):节点存储的数据;
- acl(List<ACL>):节点的权限;
- createMode(CreateMode):节点的类型(持久、持久序号、临时、临时序号)
- cb(StringCallback):
- ctx(Object):
public void create() throws KeeperException, InterruptedException {
int perm = ZooDefs.Perms.ADMIN | ZooDefs.Perms.READ; // ra
List<ACL> aclList = new ArrayList<>();
aclList.add(new ACL(perm, new Id("world", "anyone")));
aclList.add(new ACL(perm, new Id("ip", "127.0.0.1")));
String s = zooKeeper.create("/china/xi‘an", "Hel".getBytes(), aclList, CreateMode.PERSISTENT);
System.out.println(s);
}
3、查看节点 getData 方法
getData 方法的参数:
- path(String):节点的路径;
- watch(boolean):是否监听;若为 true,当前事件触发之后会调用连接到zookeeper时构造函数中定义的 Watcher.process() 方法。
- watcher(Watcher) :会话监听器;自定义监听;
- cb(DataCallback):数据回调
- ctx(Object):
注:所有的监听都是一次性的,如果要持续监听需要触发后在添加一次监听。
public void getData3() throws KeeperException, InterruptedException {
Stat stat = new Stat();
//获取节点数据时添加监听;当监听事件出发之后,再次去添加该监听
byte[] data = zooKeeper.getData("/china", new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
//再次添加监听
zooKeeper.getData(watchedEvent.getPath(), this, null);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("listener: " + watchedEvent);
}
}, stat);
System.out.println(new String(data));
System.out.println(stat);
Thread.sleep(Long.MAX_VALUE);
}
4、查看子节点 getChildren 方法
getChildren 方法的参数:
- path(String):节点的路径;
- watch(boolean):是否监听;
- watcher(Watcher) :会话监听器;
- cb(Children2Callback):
- ctx(Object):
public void getChild() throws KeeperException, InterruptedException {
List<String> children = zooKeeper.getChildren("/china", false, null);
children.stream().forEach(System.out::println);
}
二、使用 ZkClient 客户端
ZkClient是由Datameer的工程师开发的开源客户端,它是在zookeeper客户端基础之上封装的,同时在内部实现了诸如session超时重连、watcher反复注册等功能,使用上更加方便;主要变化:
- 可以设置持久监听,或删除某个监听;
- 可以插入JAVA对象,自动进行序列化和反序列化;
- 简化了基本的增删改查操作。
添加maven的jar包依赖:
<!--zkClient-->
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
zkClient链接:
https://www.jianshu.com/p/d6de2d21d744
https://www.cnblogs.com/rouqinglangzi/p/11152580.html
https://www.cnblogs.com/xbq8080/p/6622606.html
path |
String |
|
watch |
boolean |
|
watcher |
Watcher |
|
cb |
Children2Callback |
|
ctx |
Object |
|