1、ZkClient的基本用法
public class ZkClientBase {
static final String CONNECT_ADDR = "192.168.2.101:2181";
static final int SESSION_OUTTIME = 5000;// ms
private static final String PARENT_PATH = "/testWatch";
private static final String CHILDREN_PATH = "/testWatch/children";
public static void main(String[] args) {
ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000);
// 创建节点
zkc.createPersistent(PARENT_PATH, "1234");
// 读取节点数据,并且输出
String data = zkc.readData(PARENT_PATH);
System.out.println(data);
// 更新节点数据
zkc.writeData(PARENT_PATH, "4567");
String data2 = zkc.readData(PARENT_PATH);
System.out.println(data2);
// 创建子节点
zkc.createPersistent(CHILDREN_PATH, "children");
String data3 = zkc.readData(CHILDREN_PATH);
System.out.println(data3);
// 判断节点是否存在
boolean exists = zkc.exists(PARENT_PATH);
System.out.println(exists);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 递归的删除节点
zkc.deleteRecursive(PARENT_PATH);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
zkc.close();
}
}
2、subscribeDataChanges的用法
public class ZkClientWatcherOne {
static final String CONNECT_ADDR = "192.168.2.101:2181";
static final int SESSION_OUTTIME = 5000;// ms
private static final String PARENT_PATH = "/testWatch";
public static void main(String[] args) {
ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000);
// 注册节点改变的信息变化
zkc.subscribeDataChanges(PARENT_PATH, new IZkDataListener() {
@Override
public void handleDataDeleted(String path) throws Exception {
System.out.println("subscribeDataChanges handleDataDeleted");
System.out.println("删除的节点为:" + path);
}
@Override
public void handleDataChange(String path, Object data)
throws Exception {
System.out.println("subscribeDataChanges handleDataChange");
System.out.println("变更的节点为:" + path + ", 变更内容为:" + data);
}
});
// 创建节点
zkc.createPersistent(PARENT_PATH, "1234");
// 读取节点数据,并且输出
String data = zkc.readData(PARENT_PATH);
System.out.println(data);
// 更新节点数据
zkc.writeData(PARENT_PATH, "4567");
String data2 = zkc.readData(PARENT_PATH);
System.out.println(data2);
// 删除节点
zkc.deleteRecursive(PARENT_PATH);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
zkc.close();
}
}
3、subscribeChildChanges的用法
public class ZkClientWatcherTwo {
static final String CONNECT_ADDR = "192.168.2.101:2181";
static final int SESSION_OUTTIME = 5000;// ms
private static final String PARENT_PATH = "/testWatch";
private static final String CHILDREN_PATH = "/testWatch/children";
public static void main(String[] args) {
ZkClient zkc = new ZkClient(new ZkConnection(CONNECT_ADDR), 5000);
// 注册节点孩子信息变化
zkc.subscribeChildChanges(PARENT_PATH, new IZkChildListener() {
@Override
public void handleChildChange(String parentPath,
List<String> currentChilds) throws Exception {
System.out.println("subscribeChildChanges handleChildChange");
System.out.println("parentPath: " + parentPath);
System.out.println("currentChilds: " + currentChilds);
}
});
// 创建节点
zkc.createPersistent(PARENT_PATH, "1234");
// 创建子节点
zkc.createPersistent(CHILDREN_PATH, "children");
// 修改子节点的数据(不会触发subscribeChildChanges)
zkc.writeData(CHILDREN_PATH, "hahaha");
// 删除子节点
zkc.deleteRecursive(CHILDREN_PATH);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 删除节点
zkc.deleteRecursive(PARENT_PATH);
zkc.close();
}
}
4、源码下载