这个watch和redis中的watch是十分相似的。客户端注册监听它关心的节点目录,目录一旦发生变化,zk就会通知客户端。打个比方:你在看电视剧,中途插播广告了,你不想看广告,就出去玩了,并且你跟你妈妈说广告播完了就通知你。在这里,你就是客户端,你妈妈就是watch,在那里监控着电视里播的内容,一旦发现广告播完了,就会告诉你。所以watch就是异步 + 通知 + 触发机制。getData()、getChildren()和exist()都可以设置watcher。
1、对watch的理解:
触发:触发分为一次性触发和永久触发。一次性触发就是zk观察一个节点,当发生变化就通知客户端,然后通知完就完事了,这个节点再次发生变化它也不管了。而永久触发就是它一直在监控着,只要有变化就会通知。
为数据设置watch:
时序性和一致性:zk在通知客户端的时候,可以保证不同客户端看到变化的顺序是一致的。
变化类型:变化分为三种,节点变化、数据变化或者两者都变化。
2、数据变化之一次性触发demo:
public String getZnode(String path) throws KeeperException, InterruptedException {
String result = null;
byte[] bytes = zooKeeper.getData(path, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
getNewData(path);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
}, new Stat());
result = new String(bytes);
return result;
}
private String getNewData(String path) throws KeeperException, InterruptedException {
String result = null;
byte[] bytes = zooKeeper.getData(path, false, new Stat());
result = new String(bytes);
System.out.println("监控到值有变化,新值:" + result);
return result;
}
public static void main(String[] args) throws Exception {
Watches watches = new Watches();
ZooKeeper zooKeeper = watches.startZk();
watches.setZooKeeper(zooKeeper);
if (watches.getZooKeeper().exists(PATH,false) == null){
watches.createZnode(PATH,"hello");
String returnInfo = watches.getZnode(PATH);
System.out.println("第一次拿到的值:" + returnInfo);
System.in.read();
}
}
首先在getZnode方法里的getData方法里面的参数Watcher不是false,而是new了一个,重写其方法,在里面再次调用获取数据的方法。在main方法里面先将节点设置"hello",会打印出“第一次拿到的值为hello”。由于有System.in.read(),所以main线程不会结束,此时我们在Linux中启动zkCli.sh,将节点值设置为“niubi”,控制台就会立即打印出新的值。但是再次更改,不会再监控。
3、数据变化之永久触发demo:
第一次设置的值是"xixi",然后就监控"xixi",然后改成"haha",发现值变了,那么就会通知,这时再监控"heihei",如果再次修改,又会触发通知。也就是说每次都是监控最新值。
// 定义全局变量存储从zk中拿到的值
private String oldValue = null;
// 获取zk节点值的方法
public String getZnode(String path) throws KeeperException, InterruptedException {
String result = null;
byte[] bytes = zooKeeper.getData(path, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
// 获取新值
getNewData(path);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
}, new Stat());
result = new String(bytes);
// 将本次获取到的值存起来
oldValue = result;
return result;
}
// 获取新值的方法
private boolean getNewData(String path) throws KeeperException, InterruptedException {
String result = null;
// 获取新值的时候再new 一个 watcher,再对当前获取到的值进行监控
byte[] bytes = zooKeeper.getData(path, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
try {
// 在这里再次调用自己本身,实现长效监控,相当于递归
getNewData(path);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
}, new Stat());
result = new String(bytes);
String newValue = result;
if (oldValue.equals(newValue)){
System.out.println("监控到值没有变化,新旧值都为:" + result);
return false;
}else {
System.out.println("监控到值有变化,旧值为:" + oldValue +
", 新值为:" + newValue);
// 新值变成了老值,继续下一次的监控
oldValue = newValue;
return true;
}
}
4、子节点变化demo:
监控子节点变化也就说我们监控一个父节点,当发现这个父节点下面有子节点的增删时,就会触发通知。更多细节请看下面的代码以及注释。
// 1. 获得zk实例
public ZooKeeper startZk() throws IOException {
return new ZooKeeper(CONNECTURL, SESSION_TIMEOUT, new Watcher() {
@Override
public void process(WatchedEvent watchedEvent) {
// 一开始if条件不会成立,因为还没有获取子节点,所以没有子节点变化
if (watchedEvent.getType() == Event.EventType.NodeChildrenChanged && watchedEvent.getPath().equals(PATH)){
// 如果path父节点下的子节点有变化,就打印出这些节点
printChildNode(PATH);
}else {
// 第一次执行会进入这个else,会获取path下所有的子节点
aquireParentNode(PATH);
}
}
});
}
// 获取需要监控的父节点下的初始子节点, path就是要监控的父节点
private void aquireParentNode(String path) {
List<String> childNodes = null;
try {
childNodes = zooKeeper.getChildren(path, true);
System.out.println(path + " 下的初始子节点有: " + childNodes );
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
// 如果path节点下的子节点有变化,就打印出这些子节点
private void printChildNode(String path) {
List<String> childNodes = null;
try {
childNodes = zooKeeper.getChildren(path, true);
System.out.println("监控到 " + path + " 下的子节点发生变化,变化后的子节点列表为:" + childNodes );
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
六、zookeeper集群