Maven工程方式导入ZkClient API
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
create session 创建和zookeeper集群间的连接
package com.timer.zkClientApi;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
/**
* Created with IntelliJ IDEA.
*
* @author: zhubo
* @description:
*
1)和zookeeper原生API不同,通过zkclient API创建会话,需要提供session timout, connection timeout两个定时器
2)同时要提供1个序列化器实例,原因在于后续创建znode节点时,写入的数据(java对象)会自动通过序列化器来转换为byte[]
3)同理,读取出的byte[]的数据,也会自动通过序列化器直接转换为Java对象
* @time: 2018年06月04日
* @modifytime:
*/
public class CreateSession {
public static void main(String[] args) {
ZkClient zkClient = new ZkClient("192.168.20.157:2181",5000,5000,new SerializableSerializer());
System.out.println("Connection OK!");
}
}
创建znode节点
package com.timer.zkClientApi;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.CreateMode;
/**
* Created with IntelliJ IDEA.
*
* @author: zhubo
* @description: 添加数据节点
* @time: 2018年06月04日
* @modifytime:
*/
public class CreateNode {
public static void main(String[] args) {
ZkClient zc = new ZkClient("192.168.20.157:2181", 5000, 5000, new SerializableSerializer());
User u = new User();
String actual_path = zc.create("/node_zkclient/ddd", u, CreateMode.PERSISTENT); //直接将实例u写入,自动通过序列化为byte[]
System.out.println(actual_path);
}
}
修改节点数据
package com.timer.zkClientApi;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.CreateMode;
/**
* Created with IntelliJ IDEA.
*
* @author: zhubo
* @description: 修改数据节点
* @time: 2018年06月04日
* @modifytime:
*/
public class NodeSetData {
public static void main(String[] args) {
ZkClient zc = new ZkClient("192.168.20.157:2181", 5000, 5000, new SerializableSerializer());
User u = new User();
u.setId(1L);
u.setName("shayzhang");
if(!zc.exists("/node_zkclient")){
zc.create("/node_zkclient",u, CreateMode.PERSISTENT);
}
u.setId(2L);
u.setName("zhangjin");
zc.writeData("/node_zkclient",u);//直接写入实例,自动通过连接创建时创建的序列化实例,转换为byte[]
}
}
获取节点数据
package com.timer.zkClientApi;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;
/**
* Created with IntelliJ IDEA.
*
* @author: zhubo
* @description:
* @time: 2018年06月04日
* @modifytime:
*/
public class NodeGetData {
public static void main(String[] args) {
ZkClient zc = new ZkClient("192.168.20.157:2181", 5000, 5000, new SerializableSerializer());
User u = new User();
u.setId(1L);
u.setName("shayzhang");
String actual_path = zc.create("/node_zkclient", u, CreateMode.PERSISTENT);
System.out.println("Create path is: " + actual_path);
Stat stat = new Stat();
zc.readData("/node_zkclient",stat);
if(u == null){
System.out.println("Node doesn't exist!");
}else {
System.out.println(u.getId());
System.out.println(stat);
}
}
}
获取子节点列表
package com.timer.zkClientApi;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.CreateMode;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @author: zhubo
* @description: 获取子节点列表
* @time: 2018年06月04日
* @modifytime:
*/
public class NodeGetChildren {
public static void main(String[] args) {
ZkClient zc = new ZkClient("192.168.20.157:2181", 5000, 5000, new SerializableSerializer());
if(!zc.exists("/node_zkclient")){
User u = new User();
u.setId(1L);
u.setName("shayzhang");
String actual_path = zc.create("/node_zkclient", u, CreateMode.PERSISTENT);
System.out.println("Create path is: " + actual_path);
}
List<String> children_list = zc.getChildren("/node_zkclient");
System.out.println("Children list of /node_zkclient is : " + children_list.toString()); //打印子节点列表
}
}
删除节点
package com.timer.zkClientApi;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
/**
* Created with IntelliJ IDEA.
*
* @author: zhubo
* @description: 删除节点
* @time: 2018年06月04日
* @modifytime:
*/
public class NodeDeleteNode {
public static void main(String[] args) {
ZkClient zc = new ZkClient("192.168.20.157:2181", 5000, 5000, new SerializableSerializer());
String delete_node = "/node_zkclient/abc";
if(zc.exists(delete_node)){
//delete node without children
boolean e1 = zc.delete(delete_node);
System.out.println("Delete node without children: " + e1);
}
// delete node and all children
boolean e2 = zc.deleteRecursive(delete_node);
System.out.println("Delete node and children: " + e2);
}
}
订阅子节点列表发生变化
package com.timer.zkClientApi;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import java.util.List;
/**
* Created with IntelliJ IDEA.
*
* @author: zhubo
* @description: 订阅子节点列表发生变化
* @time: 2018年06月04日
* @modifytime:
*/
public class SubscribeChildren {
public static void main(String[] args) {
ZkClient zc = new ZkClient("192.168.20.157:2181", 5000, 5000, new SerializableSerializer());
System.out.println("Connected to zk server!");
// subscribe children change event, multiple subscribe
List<String> results = zc.subscribeChildChanges("/node_zkclient", new ZkChildListener());
try{
Thread.sleep(Long.MAX_VALUE);
}catch (InterruptedException e){
e.printStackTrace();
}
}
private static class ZkChildListener implements IZkChildListener{
@Override
public void handleChildChange(String s, List<String> list) throws Exception {
//print parent path
System.out.println("Parent path: " + s);
//print current children
System.out.println("Current children: " + list.toString());
}
}
}
订阅数据变化
package com.timer.zkClientApi;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
/**
* Created with IntelliJ IDEA.
*
* @author: zhubo
* @description: 订阅数据发生变化
* @time: 2018年06月04日
* @modifytime:
*/
public class SubscribeData {
public static void main(String[] args) {
ZkClient zc = new ZkClient("192.168.20.157:2181", 5000, 5000, new SerializableSerializer());
System.out.println("Connected to zk server!");
// subscribe data change event, multiple subscribe
zc.subscribeDataChanges("/node_zkclient", new ZkDataListener());
try{
Thread.sleep(Long.MAX_VALUE);
}catch (InterruptedException e){
e.printStackTrace();
}
}
private static class ZkDataListener implements IZkDataListener {
@Override
public void handleDataChange(String s, Object o) throws Exception {
System.out.println("Path Data Changed: " + s);
System.out.println("Current Data: " + o.toString());
}
@Override
public void handleDataDeleted(String s) throws Exception {
System.out.println("Data Deleted: " + s);
}
}
}