watcher概念
zookeeper提供了数据的发布/订阅功能,多个订阅者可同时监听某一特定主题对象,当该主题对象的自身状态发生变化时(例如节点内容改变、节点下的子节点列表改变等),会实时、主动通知所有订阅者。
zookeeper采用了Watcher机制实现数据的发布/订阅功能。该机制在被订阅对象发生变化时会异步通知客户端,因此客户端不必在Watcher注册后轮询阻塞,从而减轻了客户端压力。 watcher机制实际上与观察者模式类似,也可看作是一种观察者模式在分布式场景下的实现方式。
watcher架构
Watcher实现由三个部分组成:
- zookeeper服务端
- zookeeper客户端
- 客户端的ZKWatchManager对象
客户端首先将Watcher注册到服务端,同时将Watcher对象保存到客户端的WatchManager中。当zooKeeper服务端监听的数据状态发生变化时,服务端会主动通知客户端,接着客户端的WatchManager会触发相关Watcher来回调相应处理逻辑,从而完成整体的数据发布/订阅流程。
watcher特性
特性 | 说明 |
---|---|
一次性 | watcher是一次性的,一旦被触发就会移除,再次使用时需要重新注册。 |
客户端顺序回调 | watcher回调是顺序串行化执行的,只有回调后客户端才能看到最新的数 据状态。一个watcher回调逻辑不应该太多,以免影响别的watcher执行。 |
轻量级 | WatchEvent是最小的通信单元,结构上只包含通知状态、事件类型和节点路径,但不会告诉数据节点变化前后的具体内容。 |
时效性 | watcher只有在当前session彻底失效时才会无效,若在session有效期内 快速重连成功,则watcher依然存在,仍可接收到通知。 |
watcher接口设计
Watcher是一个接口,任何实现了Watcher接口的类就是一个新的Watcher。 Watcher内部包含了三个枚举类:KeeperState、EventType、WatcherType。
Watcher通知状态(KeeperState)
KeeperState是客户端与服务端连接状态发生变化时对应的通知类型。是一个枚举类,其枚举属性如下:
枚举属性 | 说明 |
---|---|
Disconnected | 客户端与服务器断开连接时。 |
SyncConnected | 客户端与服务器正常连接时(异步)。 |
NoSyncConnected | 已过时 |
AuthFailed | 客户端身份认证失败时 |
ConnectedReadOnly | 客户端以只读的方式连接服务器时。 |
SaslAuthenticated | 客户端用SASL权限验证时。 |
Expired | 客户端会话session失效时。 |
Closed | 客户端与服务端连接关闭时。 |
Unknown | 已过时 |
Watcher事件类型(EventType)
EventType是数据节点(znode)发生变化时对应的通知类型。EventType变化时KeeperState永远处于SyncConnected通知状态下。当KeeperState发生变化时,EventType永远为None。EventType是一个枚举类,枚举属性如下:
枚举属性 | 说明 |
---|---|
None | 无。 |
NodeCreated | Watcher监听的数据节点被创建时。 |
NodeDeleted | Watcher监听的数据节点被删除时。 |
NodeDataChanged | Watcher监听的数据节点内容发生变更时(无论内容数据 是否变化)。 |
NodeChildrenChanged | Watcher监听的数据节点的子节点列表发生变更时。 |
DataWatchRemoved | 客户端用SASL权限验证时。 |
ChildWatchRemoved | 客户端会话session失效时。 |
PersistentWatchRemoved | 客户端与服务端连接关闭时。 |
注册事件方式
在zookeeper中采用exists(path, watch)、getData(path, watcher, stat) 、getChildren(path, watch)这样的方式为某个znode注册监听。
注册方式 | Created | ChildrenChanged | Changed | Deleted |
---|---|---|---|---|
exists | 可监控 | 可监控 | 可监控 | |
getData | 可监控 | 可监控 | ||
getChildren | 可监控 | 可监控 |
用法
监听连接状态
package com.huazai.zookeeper.zkexample.config;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.util.concurrent.CountDownLatch;
/**
* @author pyh
* @date 2021/5/19 0:20
*/
public class ZookeeperConnection {
public static void main(String[] args) {
connect();
}
public static ZooKeeper zooKeeper;
public static ZooKeeper connect() {
try {
// 由于连接zookeeper服务器是异步连接,需要CountDownLatch阻塞主线程,等待子线程连接结果后反馈给主线程
CountDownLatch countDownLatch = new CountDownLatch(1);
/*
connectString:服务器的ip和端口
sessionTimeout:客户端与服务器之间的会话超时时间,以毫秒为单位的
watcher:监视器对象
*/
zooKeeper = new ZooKeeper("192.168.64.129:2181", 5000, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
System.out.println("zookeeper异步连接成功");
} else if (watchedEvent.getState() == Event.KeeperState.Disconnected) {
System.out.println("断开连接");
} else if (watchedEvent.getState() == Event.KeeperState.Expired) {
System.out.println("会话超时");
} else if (watchedEvent.getState() == Event.KeeperState.AuthFailed) {
System.out.println("认证失败");
} else if (watchedEvent.getState() == Event.KeeperState.Closed) {
System.out.println("连接关闭");
}
countDownLatch.countDown();
}
});
// 主线程阻塞等待连接对象的创建成功
countDownLatch.await();
// 会话编号
System.out.println("客户端sessionId:" + zooKeeper.getSessionId());
return zooKeeper;
} catch (Exception e) {
System.out.println("zookeeper连接异常");
e.printStackTrace();
}
return null;
}
/**
* 关闭zookeeper会话
*/
public static void close() {
if (zooKeeper != null) {
try {
zooKeeper.close();
System.out.println("zookeeper关闭成功");
} catch (InterruptedException e) {
System.out.println("zookeeper关闭失败");
e.printStackTrace();
}
}
}
}
监听节点是否存在
public Stat exists(String path, boolean watch);
public Stat exists(final String path, Watcher watcher);
- path:znode路径。
- watch:是否使用连接对象中注册的监视器。
- watcher:监视器对象。
package com.huazai.zookeeper.zkexample.watcher;
import com.huazai.zookeeper.zkexample.config.ZookeeperConnection;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author pyh
* @date 2021/6/1 0:05
*/
public class ExistsWatch {
private ZooKeeper zooKeeper;
@Before
public void before() {
zooKeeper = ZookeeperConnection.connect();
}
@Test
public void watchExists() throws Exception {
/*
watch为true,则默认使用ZooKeeper构造函数的监听器
*/
zooKeeper.exists("/watcher1", true);
Thread.sleep(50000);
System.out.println("结束");
}
@Test
public void watchExists1() throws Exception {
zooKeeper.exists("/watcher1", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
}
});
Thread.sleep(50000);
System.out.println("结束");
}
/**
* 由于监听器一次性特性,监听器一旦触发就会被移除,再次使用时需要重新注册。
* 可以在触发监听回调的时候紧接着注册监听器,由此伪装性的达到一次监听多次触发的效果
*
* @throws Exception
*/
@Test
public void watchExists2() throws Exception {
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
try {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
// 监听回调中再次注册监听器,以此反复,伪装性的达到一次监听多次触发的效果
zooKeeper.exists("/watcher1", this);
} catch (Exception e) {
e.printStackTrace();
}
}
};
// 第一次监听
zooKeeper.exists("/watcher1", watcher);
Thread.sleep(500000);
System.out.println("结束");
}
/**
* 一个节点可以注册多个监听器,由于监听器特性可知,多个监听器执行是顺序串行回调的
*
* @throws Exception
*/
@Test
public void watchExists3() throws Exception {
for (int i = 0; i < 10; i++) {
zooKeeper.exists("/watcher1", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
}
});
}
Thread.sleep(50000);
System.out.println("结束");
}
@After
public void after() {
ZookeeperConnection.close();
}
}
监听节点数据变化
public byte[] getData(String path, boolean watch, Stat stat);
public byte[] getData(final String path, Watcher watcher, Stat stat);
- path:znode路径。
- watch:是否使用连接对象中注册的监视器。
- watcher:监视器对象。
- stat:返回znode的元数据。
package com.huazai.zookeeper.zkexample.watcher;
import com.huazai.zookeeper.zkexample.config.ZookeeperConnection;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author pyh
* @date 2021/6/1 0:42
*/
public class GetDataWatch {
private ZooKeeper zooKeeper;
@Before
public void before() {
zooKeeper = ZookeeperConnection.connect();
}
@Test
public void watchGetData() throws Exception {
/*
watch为true,则默认使用ZooKeeper构造函数的监听器
*/
Stat stat = new Stat();
zooKeeper.getData("/watcher1", true, stat);
System.out.println(stat);
Thread.sleep(50000);
System.out.println("结束");
}
@Test
public void watchGetData1() throws Exception {
Stat stat = new Stat();
zooKeeper.getData("/watcher1", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
}
}, stat);
Thread.sleep(50000);
System.out.println("结束");
}
/**
* 由于监听器一次性特性,监听器一旦触发就会被移除,再次使用时需要重新注册。
* 可以在触发监听回调的时候紧接着注册监听器,由此伪装性的达到一次监听多次触发的效果
*
* @throws Exception
*/
@Test
public void watchGetData2() throws Exception {
Stat stat = new Stat();
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
try {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
// 监听回调中再次注册监听器,以此反复,伪装性的达到一次监听多次触发的效果
zooKeeper.getData("/watcher1", this, stat);
} catch (Exception e) {
e.printStackTrace();
}
}
};
// 第一次监听
zooKeeper.getData("/watcher1", watcher, stat);
Thread.sleep(500000);
System.out.println("结束");
}
/**
* 一个节点可以注册多个监听器,由于监听器特性可知,多个监听器执行是顺序串行回调的
*
* @throws Exception
*/
@Test
public void watchGetData3() throws Exception {
Stat stat = new Stat();
for (int i = 0; i < 10; i++) {
zooKeeper.getData("/watcher1", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
}
}, stat);
}
Thread.sleep(50000);
System.out.println("结束");
}
@After
public void after() {
ZookeeperConnection.close();
}
}
监听子节点列表
public List<String> getChildren(String path, boolean watch);
public List<String> getChildren(final String path, Watcher watcher);
- path:znode路径。
- watch:是否使用连接对象中注册的监视器。
- watcher:监视器对象。
package com.huazai.zookeeper.zkexample.watcher;
import com.huazai.zookeeper.zkexample.config.ZookeeperConnection;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* @author pyh
* @date 2021/6/2 22:22
*/
public class GetChildrenWatch {
private ZooKeeper zooKeeper;
@Before
public void before() {
zooKeeper = ZookeeperConnection.connect();
}
@Test
public void watchGetChildren() throws Exception {
/*
watch为true,则默认使用ZooKeeper构造函数的监听器
*/
zooKeeper.getChildren("/watcher1", true);
Thread.sleep(50000);
System.out.println("结束");
}
@Test
public void watchGetChildren1() throws Exception {
zooKeeper.getChildren("/watcher1", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
}
});
Thread.sleep(50000);
System.out.println("结束");
}
/**
* 由于监听器一次性特性,监听器一旦触发就会被移除,再次使用时需要重新注册。
* 可以在触发监听回调的时候紧接着注册监听器,由此伪装性的达到一次监听多次触发的效果
*
* @throws Exception
*/
@Test
public void watchGetChildren2() throws Exception {
Watcher watcher = new Watcher() {
@Override
public void process(WatchedEvent event) {
try {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
// 监听回调中再次注册监听器,以此反复,伪装性的达到一次监听多次触发的效果
zooKeeper.getChildren("/watcher1", this);
} catch (Exception e) {
e.printStackTrace();
}
}
};
// 第一次监听
zooKeeper.getChildren("/watcher1", watcher);
Thread.sleep(500000);
System.out.println("结束");
}
/**
* 一个节点可以注册多个监听器,由于监听器特性可知,多个监听器执行是顺序串行回调的
*
* @throws Exception
*/
@Test
public void watchGetChildren3() throws Exception {
for (int i = 0; i < 10; i++) {
zooKeeper.getChildren("/watcher1", new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("自定义watcher");
System.out.println("path=" + event.getPath());
System.out.println("eventType=" + event.getType());
}
});
}
Thread.sleep(50000);
System.out.println("结束");
}
@After
public void after() {
ZookeeperConnection.close();
}
}