(原) 2.2 ZkClient使用

本文为原创文章,转载请注明出处,谢谢

ZkClient使用

1、jar包引入,演示版本为0.8,非maven项目,可以下载jar包导入到项目中

 

<dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.8</version>
</dependency>

 

 

2、创建Zookeeper连接

 示例:ZkClient zc = new ZkClient("192.168.117.128:2181",10000,10000,new SerializableSerializer());

 API:ZkClient(java.lang.String zkServers, int sessionTimeout, int connectionTimeout, org.I0Itec.zkclient.serialize.ZkSerializer zkSerializer)

  • zkServers:Zookeeper服务器地址
  • sessionTimeout:session超时时间
  • connectionTimeout:连接超时时间
  • zkSerializer:序列化器,ZkClient提供2种
    • SerializableSerializer:对象序列化,可转换对象
    • BytesPushThroughSerializer:字节数组序列化

3、创建节点

   示例:

 @Test
    public void createNode() {
        createSession();
        User user = new User();
        user.setId(1l);
        user.setName("scot");
        String path = zc.create("/zkClient_01/02",user, CreateMode.PERSISTENT);
        System.out.println("path :" + path);
    }

 API:

public java.lang.String create(java.lang.String path, java.lang.Object data, org.apache.zookeeper.CreateMode mode) public java.lang.String create(java.lang.String path, java.lang.Object data, java.util.List<org.apache.zookeeper.data.ACL> acl, org.apache.zookeeper.CreateMode mode) 

示例用的为第一种,没有设置访问权限列表(List<org.apache.zookeeper.data.ACL> acl)

4、节点是否存在

  boolean exists(java.lang.String path);

5、获取节点信息

 示例:

 @Test
    public void getNode() {
        createSession();
        User user = zc.readData("/zkClient_01");
        System.out.println(user.getName());

        Stat stat = new Stat();
        User user1 = zc.readData("/zkClient_01",stat);
        System.out.println(user.getName());
        System.out.println(stat);
    }

API:

   public <T> T readData(java.lang.String path)public <T> T readData(java.lang.String path, boolean returnNullIfPathNotExists)public <T> T readData(java.lang.String path, org.apache.zookeeper.data.Stat stat)
  • path:节点路径。
  • returnnullIfPathNotExists:同字面意思,节点不存在返回null
  • stat:节点详细信息。传递stat对象到readData方法,方法内部会填充stat数据

6、获取子节点

  示例:

@Test
    public void getChild() {
        createSession();
        String path = "/node_scot";
        boolean b = this.exists(path);
        if(b) {
            List<String> children =zc.getChildren(path);
            System.out.println(children.size());
        }else {
            System.out.println("do not have this node");
        }
    }

 API:

   public java.util.List<java.lang.String> getChildren(java.lang.String path) 

 

7、删除节点

  示例:   

 @Test
    public void del() {
        createSession();
        String path = "/zkClient_01/01";
        if(this.exists(path)) {
            //zc.delete(path);//删除当前节点,有子节点无法删除
            zc.deleteRecursive(path);//删除非子节点
        }
    }

 API:

public boolean delete(java.lang.String path)

public boolean delete(java.lang.String path, int version) 

public boolean deleteRecursive(java.lang.String path) 
  • version:节点的版本。如果版本不符无法删除

 8、更新节点信息

  示例:

@Test
    public void writeNode() {
        createSession();
        User user = new User();
        user.setId(11l);
        user.setName("sksujer002");
        zc.writeData("/zkClient_01",user);
    }

 

9、监控子节点改变(当前节点不存在也可以设置监控)

 示例: 

    @Test
    public void subscribeChildChange() throws InterruptedException {
        createSession();
        zc.subscribeChildChanges("/zkClient_01",new MyZkChildListener());
        Thread.sleep(Integer.MAX_VALUE);
    }

    static class MyZkChildListener implements IZkChildListener {

        @Override
        public void handleChildChange(String s, List<String> strings) throws Exception {
            System.out.println("s:"+s);
            System.out.println("Strings:" + strings);
        }
    }

 

API:

public java.util.List<java.lang.String> subscribeChildChanges(java.lang.String path, org.I0Itec.zkclient.IZkChildListener listener)

public interface IZkChildListener {
  void handleChildChange(java.lang.String parentPath, java.util.List<java.lang.String> currentChilds) throws java.lang.Exception;
}
  • subscribeChildChanges:注册子节点改变监控
    • path:路径
    • IZkChildListener listener:子节点监控接口
  • IZkChildListener - handChildChange:子节点列表发生改变触发此方法
    • parentPath:监控节点路径
    • currentChilds:子节点列表

10、监控节点数据

 示例:

 @Test
    public void subscribeDataChange () throws InterruptedException {
        createSession();
        zc.subscribeDataChanges("/zkClient_01",new MyZkDataListener());
        Thread.sleep(Integer.MAX_VALUE);
    }

    static class MyZkDataListener implements IZkDataListener {

        @Override
        public void handleDataChange(String s, Object o) throws Exception {
            System.out.println("节点信息改变");
            System.out.println("s:"+s);
            System.out.println("o:"+o);
        }

        @Override
        public void handleDataDeleted(String s) throws Exception {
            System.out.println("节点被删除:"+s);
        }
    }

API:

 public void subscribeDataChanges(java.lang.String path, org.I0Itec.zkclient.IZkDataListener listener)


public interface IZkDataListener {
void handleDataChange(java.lang.String dataPath, java.lang.Object object) throws java.lang.Exception;

void handleDataDeleted(java.lang.String dataPath) throws java.lang.Exception;
}
  • subscribeDataChanges:注册节点数据监控
    • IZkDataListener listener:数据改变监控接口
  • IZkDataListener - handleDataChange:节点数据改变触发此方法
    • dataPath:监控节点路径
    • object:节点的新数据
  • IZkDataListener - handleDataDeleted:节点被删除触发此方法
    • dataPath:监控节点路径

 下一节:2.3Curator使用

posted @ 2016-07-04 09:33  lives0815  阅读(4444)  评论(0编辑  收藏  举报