首页  

zookeeper watch 机制     所属分类 zookeeper 浏览量 1507
事件 订阅发布  watch  推拉结合
服务端感知到变化,只会发送事件类型和节点信息给关注的客户端,
不会包括具体的变更内容,所以事件本身是轻量级的 ,
收到变更通知的客户端需要自己去拉变更的数据 


zookeeper Watcher机制特点

一次性的触发器(one-time trigger) 只发送一次 触发之后 重新注册
送给客户端(Sent to the client)  服务端推送给客户端
设置Watch的数据 数据监视和子节点监视(data watches and child watches)

可以注册watcher的方法 getData exists getChildren
可以触发watcher的方法 create delete setData

setData() 当前的数据监视 
create()  当前节点的数据监视  父节点的子节点监视
delete()  当前节点的数据监视  子节点监视事件  父节点的child watch



New ZooKeeper时注册的watcher叫default watcher,它不是一次性的

Watcher 接口

public interface Watcher {
    abstract public void process(WatchedEvent event);
}

// 事件状态 
Event.KeeperState keeperState = watchedEvent.getState();
// 事件类型
Event.EventType eventType = watchedEvent.getType();
// zk 路径
String path = watchedEvent.getPath();


process watchedEvent,WatchedEvent state:SyncConnected type:NodeCreated path:/watchertest
process watchedEvent,WatchedEvent state:SyncConnected type:NodeDataChanged path:/watchertest
process watchedEvent,WatchedEvent state:SyncConnected type:NodeCreated path:/watchertest/child
process watchedEvent,WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/watchertest
process watchedEvent,WatchedEvent state:SyncConnected type:NodeDeleted path:/watchertest/child
process watchedEvent,WatchedEvent state:SyncConnected type:NodeDeleted path:/watchertest
process watchedEvent,WatchedEvent state:SyncConnected type:NodeCreated path:/watchertest
process watchedEvent,WatchedEvent state:SyncConnected type:NodeDataChanged path:/watchertest


例子代码
https://gitee.com/dyyx/cdhdemo/blob/master/zk/src/main/java/dyyx/WatcherTest.java

上一篇     下一篇
网站架构演变过程

redis消息发布与订阅

Redis客户端 Jedis Redisson Lettuce 对比

zookeeper Java 客户端

服务注册与发现组件比较

mybatis动态更新自动去掉多余逗号