zookeeper模拟监控服务节点宕机
/**
* 模拟监控服务节点宕机
* 思路:
* 节点上线的时候,往/watch下创建一个节点,然后监控该节点,记录事件类型,判断节点是否宕机
* @throws Exception
*/
public static void watch() throws Exception {
while(true) {
final ZooKeeper zkClient = new ZooKeeper("192.168.1.231,192.168.1.232,192.168.1.233", 50000, null);
String path = zkClient.create("/watch/", "".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
zkClient.exists(path, new Watcher() {
public void process(WatchedEvent event) {
if(event.getType().getIntValue() == EventType.NodeDeleted.getIntValue()) {
System.err.println("节点销毁 ====================== " + event);
} else {
System.err.println("================ " + event);
}
try {
zkClient.getChildren(event.getPath(), this);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
zkClient.close();
}
}