;
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
三 编辑代码
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!");
}
}
运行结果
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);
}
}
运行结果
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);
}
}
运行结果
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());
}
}
运行结果:
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
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