Zookeeper:JavaAPI watcher机制
1.watcher架构
watcher由三个部分组成:
zookeeper服务端、zookeeper客户端、客户端的ZKWatchManager对象。
接口设计:
Watcher接口中含有一个内部接口Event,其中含有两个枚举KeeperStat(通知状态)和EventType(事件类型)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | public enum KeeperState { /** Unused, this state is never generated by the server */ @Deprecated Unknown (- 1 ), /** The client is in the disconnected state - it is not connected * to any server in the ensemble. */ Disconnected ( 0 ), /** Unused, this state is never generated by the server */ @Deprecated NoSyncConnected ( 1 ), /** The client is in the connected state - it is connected * to a server in the ensemble (one of the servers specified * in the host connection parameter during ZooKeeper client * creation). */ SyncConnected ( 3 ), /** * Auth failed state */ AuthFailed ( 4 ), /** * The client is connected to a read-only server, that is the * server which is not currently connected to the majority. * The only operations allowed after receiving this state is * read operations. * This state is generated for read-only clients only since * read/write clients aren't allowed to connect to r/o servers. */ ConnectedReadOnly ( 5 ), /** * SaslAuthenticated: used to notify clients that they are SASL-authenticated, * so that they can perform Zookeeper actions with their SASL-authorized permissions. */ SaslAuthenticated( 6 ), /** The serving cluster has expired this session. The ZooKeeper * client connection (the session) is no longer valid. You must * create a new client connection (instantiate a new ZooKeeper * instance) if you with to access the ensemble. */ Expired (- 112 ); private final int intValue; // Integer representation of value // for sending over wire KeeperState( int intValue) { this .intValue = intValue; } public int getIntValue() { return intValue; } public static KeeperState fromInt( int intValue) { switch (intValue) { case - 1 : return KeeperState.Unknown; case 0 : return KeeperState.Disconnected; case 1 : return KeeperState.NoSyncConnected; case 3 : return KeeperState.SyncConnected; case 4 : return KeeperState.AuthFailed; case 5 : return KeeperState.ConnectedReadOnly; case 6 : return KeeperState.SaslAuthenticated; case - 112 : return KeeperState.Expired; default : throw new RuntimeException( "Invalid integer value for conversion to KeeperState" ); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public enum EventType { None (- 1 ), NodeCreated ( 1 ), NodeDeleted ( 2 ), NodeDataChanged ( 3 ), NodeChildrenChanged ( 4 ); private final int intValue; // Integer representation of value // for sending over wire EventType( int intValue) { this .intValue = intValue; } public int getIntValue() { return intValue; } public static EventType fromInt( int intValue) { switch (intValue) { case - 1 : return EventType.None; case 1 : return EventType.NodeCreated; case 2 : return EventType.NodeDeleted; case 3 : return EventType.NodeDataChanged; case 4 : return EventType.NodeChildrenChanged; default : throw new RuntimeException( "Invalid integer value for conversion to EventType" ); } } } |
以node节点为例,说明调用的注册方法和可监听时间间的关系:
注册方式 | Created | ChildrenChanged | Changed | Deleted |
zk.exists("/node",watcher) | 可监控 | 可监控 | 可监控 | |
zk.getData("/node",watcher) | 可监控 | 可监控 | ||
zk.getChildren("/node",watcher) | 可监控 | 可监控 |
2.注册Watcher
KeeperState 通知状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class ZKWatcher implements Watcher{ static CountDownLatch latch = new CountDownLatch( 1 ); static ZooKeeper zooKeeper; public static void main(String[] args) throws Exception{ Watcher watcher; zooKeeper = new ZooKeeper( "192.168.10.132:2181" , 5000 , new ZKWatcher()); latch.await(); } public void process(WatchedEvent event) { if (event.getType() == Event.EventType.None){ if (event.getState() == Event.KeeperState.SyncConnected){ System.out.println( "连接成功" ); latch.countDown(); } else if (event.getState() == Event.KeeperState.Disconnected){ System.out.println( "断开连接" ); } else if (event.getState() == Event.KeeperState.Expired){ System.out.println( "会话超时" ); } else if (event.getState() == Event.KeeperState.AuthFailed){ System.out.println( "认证失败" ); } } } } |
3.检查节点是否存在
1 2 3 4 5 6 7 | //使用链接对象的监视器 exists(String path, boolean b) //自定义监视器 exists(String path,Watacher watcher) //NodeCreated:节点创建 //NodeDeleted:节点删除 //NodeDataChanged:节点内容发生变化 |
4.查看节点
1 2 3 4 5 6 7 | //使用连接对象的监视器 getData(String path, boolean b,Stat stat) //自定义监视器 getData(String path,Watcher w,Stat stat) //NodeDeleted:节点删除 //NodeDataChanged:节点内容变化 |
5.查看子节点
1 2 3 4 5 6 7 | //使用连接对象的监视器 getChildren(String path, boolean b) //自定义监视器 getChildren(String path,Watcher w) //NodeDeleted:节点删除 //NodeChildrenChanged:子节点内容变化 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix