【ZooKeeper系列】2.用Java实现ZooKeeper API的调用
温馨提示
:在这里我再次提个小要求,希望大家能习惯看官方文档,文档虽然是英文但用词都比较简单,基本都能看懂文档表达的意思。授之以鱼不如授之以渔的道理相信大家都明白,也希望通过猿人谷的这个ZooKeeper系列,让大家入门、到熟悉,举一反三后能精通ZooKeeper。
在前一篇我们介绍了ZooKeeper单机版、伪集群和集群环境搭建,通过命令行的方式做了节点的创建、删除、更新、获取节点信息的测试。Zookeeper 的目的是为客户端构建复杂的协调功能提供简单、高效的核心 API,这一篇我们用Java通过ZooKeeper提供的API接口来实现这些增删改查的功能。
1 简介
org.apache.zookeeper.Zookeeper
是ZooKeeper客户端的主类,在官方文档中已明确说明(This is the main class of ZooKeeper client library.)。
This is the main class of ZooKeeper client library. To use a ZooKeeper service, an application must first instantiate an object of ZooKeeper class. All the iterations will be done by calling the methods of ZooKeeper class. The methods of this class are thread-safe unless otherwise noted.
Once a connection to a server is established, a session ID is assigned to the client. The client will send heart beats to the server periodically to keep the session valid.
创建一个ZooKeeper的实例来使用org.apache.zookeeper.Zookeeper
里的方法,官方文档已经指出没有特别声明的话,ZooKeeper类里的方法是线程安全
的。客户端连接到ZooKeeper服务的时候,会给客户端分配一个会话ID(session ID),客户端与服务端会通过心跳来保持会话有效。
org.apache.zookeeper.Zookeeper
里的方法非常多,就不一一列举了,只列几个增删改查的。
|Method | Description |
|--|--|
| create(String path, byte[] data, List
| create(String path, byte[] data, List
| create(String path, byte[] data, List
| delete(String path, int version) | Delete the node with the given path.(删除指定路径的节点) |
| delete(String path, int version, AsyncCallback.VoidCallback cb, Object ctx) | The asynchronous version of delete.(异步删除指定路径的节点) |
| exists(String path, boolean watch) | Return the stat of the node of the given path.(返回指定路径的节点状态信息) |
| getChildren(String path, boolean watch) | Return the list of the children of the node of the given path.(返回指定路径的所有子节点状态信息) |
| getData(String path, boolean watch, Stat stat) | Return the data and the stat of the node of the given path.(返回指定路径的节点数据和状态信息) |
| setData(String path, byte[] data, int version) | Set the data for the node of the given path if such a node exists and the given version matches the version of the node (if the given version is -1, it matches any node's versions).(给指定路径和版本的节点设置新值,如版本为-1,即给所有版本设置值) |
2 测试环境搭建
这里新建一个Spring Boot的项目来进行测试,新建Spring Boot项目的过程很简单,也不是这里的重点,就不做介绍了。
项目里会需要额外引入两个包来进行测试:
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.5.5</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.5.2</version> </dependency>
3 API测试
完整测试代码如下:
/** * 简单测试示例 * @author 猿人谷 * @date 2019/12/16 */ public class ZooKeeperDemo { private static final Logger LOGGER = LoggerFactory.getLogger(ZooKeeperDemo.class); private static final int SESSION_TIME_OUT = 10000; // ZooKeeper服务的地址,如为集群,多个地址用逗号分隔 private static final String CONNECT_STRING = "127.0.0.1:2181"; private static final String ZNODE_PATH = "/zk_demo"; private static final String ZNODE_PATH_PARENT = "/app1"; private static final String ZNODE_PATH_CHILDREN = "/app1/app1_1"; private ZooKeeper zk = null; @Before