ZkClient的使用

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、源码下载


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值