依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.7</version>
</dependency>
基本操作
zookeeper 客户端创建
public static ZooKeeper getZookeeperConn(String address) throws IOException {
ZooKeeper zooKeeper = new ZooKeeper(address, 5000, watchedEvent -> System.out.println("type:" + watchedEvent.getType() + "keepstat:" + watchedEvent.getState().getIntValue()));
System.out.println("创建连接成功"+zooKeeper.getSessionId());
return zooKeeper;
}
创建节点
public static void zKCreateOp() throws IOException, KeeperException, InterruptedException {
ZooKeeper zookeeper = getZookeeperConn("127.0.0.1:2181");
if (zookeeper != null) {
/**
* 创建临时节点
* Zookeeper ZooDefs.Ids
* OPEN_ACL_UNSAFE : 完全开放的ACL,任何连接的客户端都可以操作该属性znode
* CREATOR_ALL_ACL : 只有创建者才有ACL权限
* READ_ACL_UNSAFE:只能读取ACL
*/
// System.out.println(zookeeper.create("/mydata/dev1", "zl-100".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL));
//重复创建节点(KeeperException$NodeExistsException: KeeperErrorCode = NodeExists for /mydata/dev1)
// System.out.println(zookeeper.create("/mydata/dev1","zl-100".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL));
//路径不存在的节点
// System.out.println(zookeeper.create("/father", "new node".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL));
System.out.println(zookeeper.create("/lock/seq_node", "zl-sequential".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL));
//重复节点
System.out.println(zookeeper.create("/lock/seq_node", "zl-sequential".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL));
List<String> children = zookeeper.getChildren("/lock", false);
children.stream().forEach(System.out::println);
}
}
获取数据
public static void getOp() throws IOException, KeeperException, InterruptedException {
ZooKeeper zookeeper = getZookeeperConn("127.0.0.1:2181");
//获取子节点
List<String> children = zookeeper.getChildren("/", false);
System.out.println("所有的字节点:");
children.stream().forEach(System.out::println);
//获取指定节点的权限信息
List<ACL> acl = zookeeper.getACL("/test", new Stat());
System.out.println("节点权限信息:");
acl.stream().forEach(System.out::println);
//获取节点数据
System.out.println("节点数据;");
byte[] data = zookeeper.getData("/test", false, new Stat());
System.out.println(new String(data));
//获取客户端状态
ZooKeeper.States state = zookeeper.getState();
System.out.println("zk stats :" + state);
}
set 数据
public static void setOp() throws IOException, KeeperException, InterruptedException {
ZooKeeper zookeeper = getZookeeperConn("127.0.0.1:2181");
zookeeper.setData("/test","mydata-set".getBytes(),-1);
System.out.println(zookeeper.getData("/test",false,new Stat()));
}