目录
ZkClient介绍
因为Zookeeper API比较复杂,使用并不方便,所以出现了ZkClient,ZkClient对Zookeeper API进行了封装,利用ZkClient可以更加方便地对Zookeeper进行操作。
导入jar包依赖
在pom.xml中添加如下内容:
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.14</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.11</version> </dependency>
简单使用样例
package cn.ganlixin.test; import org.I0Itec.zkclient.ZkClient; import org.apache.zookeeper.CreateMode; public class ZkClinentAPI { public static void main(String[] args) { // 连接串 String serverString = "127.0.0.1:2181"; // 连接超时时间 int timeout = 2000; ZkClient zk = new ZkClient(serverString, timeout); zk.create("/demo", "hello demo", CreateMode.PERSISTENT); Object obj = zk.readData("/demo"); System.out.println(obj); // 关闭连接 zk.close(); } }