ZkClient使用

来源:互联网 发布:新疆有网络吗 编辑:程序博客网 时间:2024/06/08 02:37

ZkClient是一个开源的ZooKeeper客户端,是在原生的ZooKeeper API接口之上进行包装,是一个更易使用的ZooKeeper客户端。ZkClient在内部实现了Session超时重连、Watcher反复注册等功能,使得ZooKeeper客户端的繁琐细节对开发人员透明。 
接下来,我们将从创建会话、创建节点、读取数据、更新数据、删除节点、检测节点等方面介绍ZkClient的使用。

1.会话创建、节点创建、获取子节点、删除节点

public class ZkClientTest {    public static void main(String[] args) throws IOException,InterruptedException {        //创建会话        ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000);        String path = "/zk-book";        //监测子节点变化        zkClient.subscribeChildChanges(path, new IZkChildListener() {               @Override            public void handleChildChange(String parentPath, List<String> currentChild)                    throws Exception {                System.out.println(parentPath + " 's child changed ,currentChilds: " + currentChild);            }        });        //创建节点        zkClient.createPersistent(path);        Thread.sleep(1000);        System.out.println(zkClient.getChildren(path));        Thread.sleep(1000);        zkClient.createPersistent(path + "/c1");        Thread.sleep(1000);        System.out.println(zkClient.getChildren(path));        Thread.sleep(1000);        zkClient.createPersistent(path + "/c2","123");        Thread.sleep(1000);        System.out.println(zkClient.getChildren(path));        //删除节点        Thread.sleep(1000);        zkClient.delete(path + "/c1");        Thread.sleep(1000);        System.out.println(zkClient.getChildren(path));        zkClient.delete(path + "/c2");        System.out.println(zkClient.getChildren(path));         Thread.sleep(1000);         }}   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

输出结果:

/zk-book 's child changed ,currentChilds: [][]/zk-book 's child changed ,currentChilds: [c1][c1]/zk-book 's child changed ,currentChilds: [c1, c2][c1, c2]/zk-book 's child changed ,currentChilds: [c2][c2][]/zk-book 's child changed ,currentChilds: []
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

2.改变节点数据、检测节点是否存在

public class ZkClientTest {    public static void main(String[] args) throws InterruptedException   {        //创建会话        ZkClient zkClient = new ZkClient("127.0.0.1:2181", 5000);        //创建节点        String path = "/zk-book";        zkClient.createPersistent(path,"123");        //监测节点数据变化        zkClient.subscribeDataChanges(path, new IZkDataListener() {            @Override            public void handleDataDeleted(String dataPath) throws Exception {                System.out.println("Node " + dataPath + " deleted.");            }            @Override            public void handleDataChange(String dataPath, Object data) throws Exception {                System.out.println("Node " + dataPath + " changed, new data: " +data);            }        });        //读取节点数据        System.out.println(zkClient.readData(path));        zkClient.writeData(path, "456");        Thread.sleep(1000);        System.out.println("Node exists :" + zkClient.exists(path));        zkClient.delete(path);        System.out.println("Node exists :" + zkClient.exists(path));    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

输出结果:

123Node /zk-book changed, new data: 456Node exists :trueNode exists :false
1 0