开源客户端ZkClient的使用

一 简介
Zkclient是Github上一个开源的Zookeeper客户端。Zkclient在Zookeeper原生 API接口之上进行了包装,是一个更加易用的Zookeeper客户端。同时Zkclient在内部实现了诸如Session超时重连,Watcher反复注册等功能。

二 搭建开发环境
1 eclipse中创建maven项目zookerper
2 编辑pom.xml
;
  4.0.0
  com.cakin
  zookerper
  0.0.1-SNAPSHOT
  jar
  zookerper
  http://maven.apache.org;
  
    UTF-8
  
  
    
        org.apache.zookeeper
        zookeeper
        3.4.6
    
    
        com.101tec
        zkclient
        0.5
    
    
      junit
      junit
      3.8.1
      test
    
  
三 编辑代码
1 创建自定义序列化类 MyZkSerializer
package com.cakin.zookerper;
import java.io.UnsupportedEncodingException;
import org.I0Itec.zkclient.exception.ZkMarshallingError;
import org.I0Itec.zkclient.serialize.ZkSerializer;
public class MyZkSerializer implements ZkSerializer{
    public byte[] serialize(Object data) throws ZkMarshallingError {
        try {
            return String.valueOf(data).getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    public Object deserialize(byte[] bytes) throws ZkMarshallingError {
        try {
            return new String(bytes, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
2 新建CreateSession类
package com.cakin.zookerper;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
public class CreateSession {
    public static void main(String[] args) {
        ZkClient zc = new ZkClient("192.168.0.110:2181",10000,10000,new SerializableSerializer());
        System.out.println("conneted ok!");
    }
}
运行结果
conneted ok!

3 新建CreateNode类
package com.cakin.zookerper;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;
public class CreateNode {
     public static void main(String[] args) {
           ZkClient zc = new ZkClient("192.168.0.110:2181",10000,10000);
           zc.setZkSerializer(new MyZkSerializer());
           System.out.println("conneted ok!");
           String path = zc.create("/test5", "test", CreateMode.PERSISTENT);
           System.out.println("Create path" + path);
     }
}
运行结果
conneted ok!
Create path/test5

4 新建GetData类
package com.cakin.zookerper;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.data.Stat;
public class GetData {
     public static void main(String[] args) {
           ZkClient zc = new ZkClient("192.168.0.110:2181",10000,10000);
           zc.setZkSerializer(new MyZkSerializer());
           System.out.println("conneted ok!");
           
           Stat stat = new Stat();
           String test = zc.readData("/test5",stat);
           
           System.out.println("id="+test);
           System.out.println(stat);
     }
}
运行结果
conneted ok!
id=test
38654705708,38654705708,1515232000388,1515232000388,0,0,0,0,4,0,38654705708

5 新建GetChild类
package com.cakin.zookerper;
import java.util.List;
import org.I0Itec.zkclient.ZkClient;
public class GetChild {
     public static void main(String[] args) {
           ZkClient zc = new ZkClient("192.168.0.110:2181",10000,10000);
           zc.setZkSerializer(new MyZkSerializer());
           System.out.println("conneted ok!");
           
           List cList = zc.getChildren("/test5");
           System.out.println(cList.toString());
     }
}
运行结果:
conneted ok!
[]

6 新建NodeExists类
package com.cakin.zookerper;
import org.I0Itec.zkclient.ZkClient;
public class NodeExists {
     public static void main(String[] args) {
           ZkClient zc = new ZkClient("192.168.0.110:2181",10000,10000);
           zc.setZkSerializer(new MyZkSerializer());
           System.out.println("conneted ok!");
           
           boolean e = zc.exists("/test5");
           System.out.println(e);
     }
}

运行结果

conneted ok!
true

7 新建NodeDel类
package com.cakin.zookerper;
import org.I0Itec.zkclient.ZkClient;
public class NodeDel {
     public static void main(String[] args) {
           ZkClient zc = new ZkClient("192.168.0.110:2181",10000,10000);
           zc.setZkSerializer(new MyZkSerializer());
           System.out.println("conneted ok!");
           
           boolean e1 = zc.delete("/test5");
           boolean e2 = zc.deleteRecursive("/test5");
           
           System.out.println(e1);
           System.out.println(e2);
           
     }
}

运行结果

conneted ok!
true
true

四 参考

http://www.jikexueyuan.com/course/2063_1.html?ss=1
https://www.cnblogs.com/xbq8080/p/6622606.html




你可能感兴趣的:(zookeeper)