1、启动ZK
2、连接 Zookeeper
// 添加Maven依赖
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
// 创建CountDownLatch对象,防止zk对象未创建完,就执行zk操作
private final CountDownLatch countDownLatch = new CountDownLatch(1)
// 连接ZK 需要 IP + 端口号、超时时间 Watcher的实现
ZooKeeper zk = new ZooKeeper("127.0.0.1:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent event) {
// 1、获取事件状态
KeeperState state = event.getState();
// 2、判断为连接状态
if (KeeperState.SyncConnected == state) {
// 3、获取事件类型
EventType type = event.getType();
if (type == EventType.None) {
// countDownLach 的值减一
countDownLatch.countDown();
System.out.println("zk开始启动连接啦!!");
}
}
}
});
// countDownLatch 的值不为0 就阻塞
countDownLatch.await();
//TODO 执行后续操作
3、创建节点
// 持久节点
String result1 = zk.create("/zktest", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("持久节点创建信息:" + result1)
// 临时节点-zk关闭自动删除,如需测试看效果,可以在关闭前sleep 主线程(CreateMode.EPHEMERAL)
String result2 = zk.create("/zktemp", "temp001".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("临时节点创建信息:" + result2)
// 关闭zk
zk.close();
4、修改节点
zk.setData("/zktest/test", "0001".getBytes(), -1);
5、获取节点
List<String> children = zk.getChildren("/zktest", false);
System.out.println(Arrays.toString(children.toArray()));
6、删除节点
zk.delete("/zktest/test",-1);
封装:
public class ZKConnectionUtils implements Watcher {
// 地址
private String ADDRESS = null;
// 端口号
private String HOST = null;
// 超时时间
private Integer SESSION_OUTTIME = null;
// 保证节点创建成功然后继续操作
private final CountDownLatch countDownLatch = new CountDownLatch(1);
// zk 对象
private ZooKeeper zk = null;
public ZKConnectionUtils(String ADDRESS, String HOST, Integer SESSION_OUTTIME) {
this.ADDRESS = ADDRESS;
this.HOST = HOST;
this.SESSION_OUTTIME = SESSION_OUTTIME;
}
/**
* 连接ZK
*/
private void connection() {
try {
zk = new ZooKeeper(ADDRESS + ":" + HOST, SESSION_OUTTIME, this);
countDownLatch.countDown();
System.out.println("###zk开始启动连接....###");
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean createNode(String path, String data) {
try {
if (zk == null) {
connection();
exists(path, true);
}
String result = this.zk.create(path, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("新节点创建成功,path: " + path + ",data: " + data);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
this.closeZK();
}
return true;
}
/**
* 修改节点信息
* @param path
* @param data
* @return
*/
public boolean changeNode(String path, String data) {
try {
if (zk == null) {
connection();
exists(path, true);
}
this.zk.setData(path, data.getBytes(), -1);
System.out.println("节点修改成功,path: " + path + ",data: " + data);
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
this.closeZK();
}
return true;
}
private void closeZK(){
try {
if (zk != null){
this.zk.close();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 删除信息
* @param path
* @return
*/
public boolean deleteNode(String path){
try {
if (zk == null){
connection();
exists(path,true);
}
this.zk.delete(path,-1);
System.out.println("节点删除成功,path: " + path);
} catch (Exception e) {
e.printStackTrace();
return false;
}finally {
this.closeZK();
}
return true;
}
/**
* 设置是否开始消息推送
*
* @param path
* @param needWatch
* @return
*/
private Stat exists(String path, boolean needWatch) {
try {
return zk.exists(path, needWatch);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public void process(WatchedEvent event) {
// 1、获取事件状态
Event.KeeperState state = event.getState();
// 2、获取节点路径
String path = event.getPath();
// 3、获取事件类型
Event.EventType type = event.getType();
if (state == Event.KeeperState.SyncConnected) {
if (type == Event.EventType.None) {
countDownLatch.countDown();
System.out.println("###zk开始启动连接....###");
} else if (type == Event.EventType.NodeCreated) {
System.out.println("获取到事件通知,当前节点node:" + path + "---创建成功");
} else if (type == Event.EventType.NodeDataChanged) {
System.out.println("获取到事件通知,当前节点node:" + path + "---修改成功");
} else if (type == Event.EventType.NodeDeleted) {
System.out.println("获取到事件通知,当前节点node:" + path + "---删除成功");
}
}
}
public static void main(String[] args) {
ZKConnectionUtils zk = new ZKConnectionUtils("127.0.0.1","2181",5000);
zk.createNode("/zktest/test6","006");
// zk.changeNode("/zktest/test6","0006");
// zk.deleteNode("/zktest/test6");
}
}