一 简介
Zkclient是Github上一个开源的Zookeeper客户端。Zkclient在Zookeeper原生 API接口之上进行了包装,是一个更加易用的Zookeeper客户端。同时Zkclient在内部实现了诸如Session超时重连,Watcher反复注册等功能。
二 搭建开发环境
1 eclipse中创建maven项目zookerper
2 编辑pom.xml
1 创建自定义序列化类 MyZkSerializer
conneted ok!
3 新建CreateNode类
conneted ok!
Create path/test5
4 新建GetData类
conneted ok!
id=test
38654705708,38654705708,1515232000388,1515232000388,0,0,0,0,4,0,38654705708
5 新建GetChild类
conneted ok!
[]
6 新建NodeExists类
Zkclient是Github上一个开源的Zookeeper客户端。Zkclient在Zookeeper原生 API接口之上进行了包装,是一个更加易用的Zookeeper客户端。同时Zkclient在内部实现了诸如Session超时重连,Watcher反复注册等功能。
二 搭建开发环境
1 eclipse中创建maven项目zookerper
2 编辑pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">;
<modelVersion>4.0.0</modelVersion>
<groupId>com.cakin</groupId>
<artifactId>zookerper</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>zookerper</name>
<url>http://maven.apache.org</url>;
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
三 编辑代码
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<String> 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
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