根据百度传课上的课程进行学习的zookeeper,记录一下zkclient的使用方法
(1)连接zookeeper
package com.lihao.zkClientTest;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
public class CreateSession {
public static void main(String[] args) {
// 建立连接
ZkClient zkClient = new ZkClient("10.10.8.161:2188", 10000, 10000, new SerializableSerializer());
System.out.println("connect ok");
}
}
(2)创建节点znode
package com.lihao.zkClientTest;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.CreateMode;
public class CreateNode {
public static void main(String[] args) {
// 建立连接
ZkClient zkClient = new ZkClient("10.10.8.161:2188", 10000, 10000, new SerializableSerializer());
System.out.println("connect ok");
User user = new User();
user.setId(1);
user.setName("lihao");
String path = zkClient.create("/jike5", user, CreateMode.PERSISTENT);
System.out.println("create path" + path);
}
}
还有User必须实现Serializable 序列化接口
package com.lihao.zkClientTest;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(3)获取数据
package com.lihao.zkClientTest;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.data.Stat;
public class GetData {
public static void main(String[] args) {
// 建立连接
ZkClient zkClient = new ZkClient("10.10.8.161:2188", 10000, 10000, new SerializableSerializer());
System.out.println("connect ok");
Stat stat = new Stat();
User user = zkClient.readData("/jike5", stat);
System.out.println("id:" + user.getId());
System.out.println("name:" + user.getName());
System.out.println(stat);
}
}
(4)获取子节点
package com.lihao.zkClientTest;
import java.util.List;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
public class GetChildren {
public static void main(String[] args) {
// 建立连接
ZkClient zkClient = new ZkClient("10.10.8.161:2188", 10000, 10000, new SerializableSerializer());
System.out.println("connect ok");
List<String> cList = zkClient.getChildren("/jike5");
System.out.println(cList.toString());
}
}
(5)判断节点是否存在
package com.lihao.zkClientTest;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
public class NodeExists {
public static void main(String[] args) {
// 建立连接
ZkClient zkClient = new ZkClient("10.10.8.161:2188", 10000, 10000, new SerializableSerializer());
System.out.println("connect ok");
boolean is = zkClient.exists("/jike5");
System.out.println(is);
}
}
(6)删除节点
package com.lihao.zkClientTest;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
public class DeleteNode {
public static void main(String[] args) {
// 建立连接
ZkClient zkClient = new ZkClient("10.10.8.161:2188", 10000, 10000, new SerializableSerializer());
System.out.println("connect ok");
boolean is = zkClient.delete("/jike5");
System.out.println("是否删除?" + is);
}
}
(7)订阅消息
一、当节点子列表发生改变时
package com.lihao.zkClientTest;
import java.util.List;
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
/*
* 订阅功能:子节点列表发生变化(增加,删除,创建,修改)时
*/
public class SubscribeChildChanges {
private static class ZKChildListener implements IZkChildListener {
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println(parentPath);
System.out.println(currentChilds.toString());
}
}
public static void main(String[] args) throws InterruptedException {
// 建立连接
ZkClient zkClient = new ZkClient("10.10.8.161:2188", 10000, 10000, new SerializableSerializer());
System.out.println("connect ok");
zkClient.subscribeChildChanges("/jike5", new ZKChildListener());
Thread.sleep(Integer.MAX_VALUE);
}
}
二、当节点的数据发生变化时
package com.lihao.zkClientTest;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.BytesPushThroughSerializer;
/*
* 订阅功能:节点数据发生变化时
*/
public class SubscribeDataChanges {
private static class ZKDataListener implements IZkDataListener {
public void handleDataChange(String dataPath, Object data) throws Exception {
// TODO Auto-generated method stub
System.out.println(dataPath + ":" + data);
}
public void handleDataDeleted(String dataPath) throws Exception {
// TODO Auto-generated method stub
System.out.println("delete:" + dataPath);
}
}
public static void main(String[] args) throws InterruptedException {
// 建立连接 实现另一种序列化方式
ZkClient zkClient = new ZkClient("10.10.8.161:2188", 10000, 10000, new BytesPushThroughSerializer());
System.out.println("connect ok");
zkClient.subscribeDataChanges("/jike5", new ZKDataListener());
Thread.sleep(Integer.MAX_VALUE);
}
}