本地搭建
在这里https://zookeeper.apache.org/releases.html下载这个包,解压后,运行bin目录的对应cmd文件就可以了
代码连接服务端
将服务端的lib目录下的jar包拷贝到客户端的jar目录中即可,main文件路径:D:\java\AIAS-main\AIAS-main\1_image_sdks\onlyTest\src\main\ZooKeeperPathMonitor.java
package main;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
/**
* ZooKeeper 节点监控主类
* 负责连接管理、数据操作,使用独立的Watcher处理事件
*/
public class ZooKeeperPathMonitor {
// 同步计数器:用于等待连接建立完成
private static final CountDownLatch connectedSignal = new CountDownLatch(1);
// ZooKeeper 客户端实例
private ZooKeeper zk;
// 待监控的节点路径
private String monitoredPath;
// 独立的事件监听器实例
private final ZooKeeperWatcher watcher;
// 构造方法:初始化监听器
public ZooKeeperPathMonitor() {
this.watcher = new ZooKeeperWatcher(this, connectedSignal);
}
/**
* 连接 ZooKeeper 服务端,并初始化监控
*/
public void connect(String host, int sessionTimeout, String path) throws IOException, InterruptedException, KeeperException {
this.monitoredPath = path;
// 创建 ZooKeeper 实例时使用独立的 watcher
zk = new ZooKeeper(host, sessionTimeout, watcher);
connectedSignal.await(); // 等待连接建立
// 初始读取数据并设置监控
byte[] data = zk.getData(path, watcher, new Stat());
System.out.println("初始数据: " + new String(data));
// 修改节点数据
zk.setData(path, "3424".getBytes(), -1);
System.out.println("初始数据1: " + new String(zk.getData(path, watcher, new Stat())));
}
// 提供给 Watcher 访问 ZooKeeper 实例的方法
public ZooKeeper getZk() {
return zk;
}
// 提供给 Watcher 访问监控路径的方法
public String getMonitoredPath() {
return monitoredPath;
}
/**
* 关闭连接
*/
public void close() throws InterruptedException {
if (zk != null) {
zk.close();
}
}
public static void main(String[] args) {
String host = "localhost:2181";
int sessionTimeout = 5000;
String pathToMonitor = "/testNode";
ZooKeeperPathMonitor monitor = new ZooKeeperPathMonitor();
try {
monitor.connect(host, sessionTimeout, pathToMonitor);
Thread.sleep(Long.MAX_VALUE); // 保持程序运行
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
monitor.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
ZooKeeperWatcher
package main;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import java.util.concurrent.CountDownLatch;
/**
* 独立的 ZooKeeper 事件监听器
* 专门处理所有 ZooKeeper 服务端推送的事件
*/
public class ZooKeeperWatcher implements Watcher {
// 持有主监控类的引用,用于获取连接实例和监控路径
private final ZooKeeperPathMonitor monitor;
// 连接同步计数器(从主类传入)
private final CountDownLatch connectedSignal;
/**
* 构造方法
* @param monitor 主监控类实例
* @param connectedSignal 连接同步计数器
*/
public ZooKeeperWatcher(ZooKeeperPathMonitor monitor, CountDownLatch connectedSignal) {
this.monitor = monitor;
this.connectedSignal = connectedSignal;
}
/**
* 事件处理核心方法
*/
@Override
public void process(WatchedEvent event) {
// 处理连接建立事件
if (event.getState() == Event.KeeperState.SyncConnected) {
if (connectedSignal.getCount() > 0) {
connectedSignal.countDown();
System.out.println("成功连接到ZooKeeper服务端");
}
}
// 处理节点数据变化事件
if (event.getType() == Event.EventType.NodeDataChanged &&
event.getPath() != null && event.getPath().equals(monitor.getMonitoredPath())) {
try {
// 从主类获取ZooKeeper实例和监控路径
ZooKeeper zk = monitor.getZk();
String path = monitor.getMonitoredPath();
// 重新获取数据并注册监听(保持监控)
byte[] data = zk.getData(path, this, new Stat());
System.out.println("路径 " + path + " 数据已更新: " + new String(data));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
客户端打印日志控制
根据路径D:\java\AIAS-main\AIAS-main\1_image_sdks\onlyTest\src\logback.xml
,新建一个logback.xml,即可控制,具体内容如下
<configuration>
<!-- 设置 ZooKeeper 相关日志级别为 WARN -->
<logger name="org.apache.zookeeper" level="WARN"/>
<!-- 根日志级别 -->
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
</configuration>
输出
成功连接到ZooKeeper服务端
初始数据: 3424
路径 /testNode 数据已更新: 3424
初始数据1: 3424