Zookeeper3.5.7版本——客户端 API 操作(代码示例)
Posted 小志的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Zookeeper3.5.7版本——客户端 API 操作(代码示例)相关的知识,希望对你有一定的参考价值。
目录
一、IDEA环境搭建
1.1、创建一个springboot项目,pom 文件引入依赖包
-
pom 文件引入依赖包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.7</version> </dependency> </dependencies>
1.2、resources目录下添加log4j.properties
-
log4j.properties文件内容如下
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=target/spring.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
二、创建 ZooKeeper 客户端代码示例
-
创建ZooKeeper 客户端代码示例
package com.xz.zk; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.Test; import java.io.IOException; import java.util.List; public class zkClient // 注意:如果是zk集群,逗号左右不能有空格 //private String connectString = "192.168.136.27:2181,192.168.136.28:2181,192.168.136.29:2181"; private String connectString = "192.168.136.26:2181"; private int sessionTimeout = 2000; private ZooKeeper zkClient; @Test public void init() throws IOException zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() @Override public void process(WatchedEvent watchedEvent) );
-
运行init方法,控制台输出结果如下:表示客户端连接成功。
三、创建子节点 代码示例
-
创建子节点代码示例
package com.xz.zk; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.List; public class zkClient // 注意:如果是zk集群,逗号左右不能有空格 //private String connectString = "192.168.136.27:2181,192.168.136.28:2181,192.168.136.29:2181"; private String connectString = "192.168.136.26:2181"; private int sessionTimeout = 20000; private ZooKeeper zkClient; @Before public void init() throws IOException zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() @Override public void process(WatchedEvent watchedEvent) ); @Test public void create() throws KeeperException, InterruptedException /** * @param path 要创建的节点的路径 * @param data 节点数据 * @param acl 节点权限 * @param createMode 节点的类型:是持久节点(有序号 or 无序号)还是临时节点(有序号 or 无序号) * */ String nodeCreated = zkClient.create("/test3", "zhangsan".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
-
运行create方法,控制台输出如下:
-
在服务器的 zk 客户端上查看创建节点情况,如下图:
四、获取子节点并监听节点 代码示例(只监听一次)
-
获取子节点并监听节点代码
package com.xz.zk; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.List; public class zkClient // 注意:如果是zk集群,逗号左右不能有空格 //private String connectString = "192.168.136.27:2181,192.168.136.28:2181,192.168.136.29:2181"; private String connectString = "192.168.136.26:2181"; private int sessionTimeout = 20000; private ZooKeeper zkClient; @Before public void init() throws IOException zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() @Override public void process(WatchedEvent watchedEvent) ); @Test public void getChildren() throws KeeperException, InterruptedException //监听根目录节点,返回根目录下所有节点 List<String> children = zkClient.getChildren("/", true); //遍历根目录下所有节点 for (String child : children) System.out.println(child); // 延时阻塞 Thread.sleep(Long.MAX_VALUE);
-
运行getChildren方法,控制台输出如下:
-
在服务器的 zk 客户端上创建 test4 节点,如下图:
-
再次查看控制台监听节点情况,如下图:
-
由上图输出结果可知:新创建的节点没有被监听到,说明节点的路径变化,也是注册一次,生效一次。
五、获取子节点并监听节点 代码示例(多次监听)
-
获取子节点并监听节点代码示例
package com.xz.zk; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.List; public class zkClient // 注意:如果是zk集群,逗号左右不能有空格 //private String connectString = "192.168.136.27:2181,192.168.136.28:2181,192.168.136.29:2181"; private String connectString = "192.168.136.26:2181"; private int sessionTimeout = 20000; private ZooKeeper zkClient; @Before public void init() throws IOException zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() @Override public void process(WatchedEvent watchedEvent) // 收到事件通知后的回调函数(用户的业务逻辑) System.out.println("======================="); // 监听根目录节点,返回根目录下所有节点 List<String> children = null; // 再次启动监听 try children = zkClient.getChildren("/", true); //遍历根目录下所有节点 for (String child : children) System.out.println("====:"+child); catch (KeeperException e) e.printStackTrace(); catch (InterruptedException e) e.printStackTrace(); ); @Test public void getChildren() throws KeeperException, InterruptedException //监听根目录节点,返回根目录下所有节点 List<String> children = zkClient.getChildren("/", true); //遍历根目录下所有节点 for (String child : children) System.out.println("----:"+child); // 延时阻塞 Thread.sleep(Long.MAX_VALUE);
-
运行getChildren方法,控制台输出如下:
-
在服务器的 zk 客户端上创建 test5 节点,如下图:
-
再次查看控制台监听节点情况,如下图:
-
由上图输出结果可知:新创建的节点被监听到,说明节点的路径变化,可以被多次监听。
六、判断节点是否存在 代码示例
-
判断节点是否存在代码示例
package com.xz.zk; import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat; import org.junit.Before; import org.junit.Test; import java.io.IOException; import java.util.List; public class zkClient // 注意:如果是zk集群,逗号左右不能有空格 //private String connectString = "192.168.136.27:2181,192.168.136.28:2181,192.168.136.29:2181"; private String connectString = "192.168.136.26:2181"; private int sessionTimeout = 20000; private ZooKeeper zkClient; @Before public void init() throws IOException zkClient = new ZooKeeper(connectString, sessionTimeout, new Watcher() @Override public void process(WatchedEvent watchedEvent) ); @Test public void exist() throws KeeperException, InterruptedException Stat stat = zkClient.exists("/test3", false); System.out.println(stat==null? "not exist " : "exist");
-
运行exist方法,控制台输出如下:
以上是关于Zookeeper3.5.7版本——客户端 API 操作(代码示例)的主要内容,如果未能解决你的问题,请参考以下文章
Zookeeper3.5.7版本——客户端命令行操作(节点删除与查看)
Zookeeper3.5.7版本——客户端命令行操作(命令行语法)
Zookeeper3.5.7版本——客户端命令行操作(监听器原理)