zookeeper中watch监听事件如图
主要是KeeperState.SyncConnected 中几种事件类型:
- EventType.NodeCreated : 节点创建事件类型
- EventType.NodeDeleted : 节点被删除
- EventType.NodeDataChanged : 节点被修改
- EventType.None : 客户端与服务器成功建立会话
- EventType.NodeChildrenChanged : 子节点列表发生变更
下面就通过程序来演示每一个事件的发生条件
EventType.None : 客户端与服务器成功建立会话
先看代码
package com.fancy;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
public class ZookeeperClient implements Watcher{
private static final String host = "127.0.0.1:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws IOException {
ZooKeeper zooKeeper =
new ZooKeeper(host, 5000,
new ZookeeperClient());
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent watchedEvent) {
System.out.println("receive watched event: " + watchedEvent);
System.out.println("watchedEvent Type : " + watchedEvent.getType());
if (Event.KeeperState.SyncConnected == watchedEvent.getState()) {
System.out.println("连接到zookeeper");
}
countDownLatch.countDown();
}
}
程序比较简单,在运行程序之前需要下载zookeeper,然后部署一下,由于我是部署在本机,所以ip地址就是本机地址,还有就是需要CountDownLatch来阻塞主线程,因为zookeeper创建连接是异步,如果不阻塞主线程的话,在运行完 new Zookeeper之后便会结束,这样就看不到process函数被回调。
运行结果:
receive watched event: WatchedEvent state:SyncConnected type:None path:null
watchedEvent Type : None
连接到zookeeper
EventType.NodeDeleted : 节点被删除
先看代码
package com.fancy;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
class ZookeeperClient implements Watcher{
private static final String host = "127.0.0.1:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws IOException {
ZooKeeper zooKeeper =
new ZooKeeper(host, 3000,
new ZookeeperClient());
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
zooKeeper.exists("/test1", true);
zooKeeper.create("/test1", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent watchedEvent) {
countDownLatch.countDown();
System.out.println(watchedEvent.getType());
}
}
首先是通过zookeeper.exits来设置监听我们要创建的节点,随后去创建节点
结果如下:
None
NodeCreated
第一行是与zookeeper连接成功的watch通知,第二行是创建我们监听的节点的watch通知。
其中 zooKeeper.exists(“/test1”, true); 第二个参数true的意思是使用默认的watch监听,也就是我们创建zookeeper时设置的监听器。
EventType.NodeDeleted : 节点被删除
package com.fancy;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
class ZookeeperClient implements Watcher{
private static final String host = "127.0.0.1:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws IOException {
ZooKeeper zooKeeper =
new ZooKeeper(host, 3000,
new ZookeeperClient());
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
zooKeeper.exists("/test1", true);
zooKeeper.create("/test1", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
//重新设置watch,zookeeper中watch被调用之后需要重新设置
zooKeeper.exists("/test1", true);
zooKeeper.delete("/test1", -1);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent watchedEvent) {
countDownLatch.countDown();
System.out.println(watchedEvent.getType());
}
}
zookeeper delete方法也有同步和异步,这篇文章所演示的方法都是同步方法,注意的是,delete方法的第二个参数version在这里设置成-1,更新操作中的版本参数如果为-1,则表示更新操作针对任何版本均可。当更新版本不为-1,且不等于节点的目前版本,则更新失败。 version值在zookeeper原子性操作中起着重要的作用,后续可能去介绍一下version的使用。
上面程序的结果是:先是连接,然后创建节点,最好删除节点。
None
NodeCreated
NodeDeleted
EventType.NodeDataChanged : 节点被修改
package com.fancy;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
class ZookeeperClient implements Watcher{
private static final String host = "127.0.0.1:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws IOException {
ZooKeeper zooKeeper =
new ZooKeeper(host, 3000,
new ZookeeperClient());
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
zooKeeper.exists("/test1", true);
zooKeeper.create("/test1", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
zooKeeper.exists("/test1", true);
zooKeeper.setData("/test1", "123".getBytes(), -1);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent watchedEvent) {
countDownLatch.countDown();
System.out.println(watchedEvent.getType());
}
}
同样的setData第三个参数version设置成-1,代表无视版本,这里需要注明一下的是:节点修改不管内容有没有变化或者version版本号的变更都会触发watch通知,上面的代码中创建的test1节点内容是123,随后修改后的内容也是123,修改前后的内容并没有变化,但是都会引起watch通知。
结果:
None
NodeCreated
NodeDataChanged
EventType.NodeChildrenChanged : 子节点列表发生变更
package com.fancy;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
class ZookeeperClient implements Watcher {
private static final String host = "127.0.0.1:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws IOException {
ZooKeeper zooKeeper =
new ZooKeeper(host, 3000,
new ZookeeperClient());
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
zooKeeper.exists("/test1", true);
zooKeeper.create("/test1", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zooKeeper.getChildren("/test1", true);
zooKeeper.create("/test1/child-1", "123".getBytes(), ZooDefs.Ids
.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent watchedEvent) {
countDownLatch.countDown();
System.out.println(watchedEvent.getType());
}
}
结果是:
None
NodeCreated
NodeChildrenChanged
这里需要注意几点是:
- 要关注子节点列表变更不能通过exists函数来设置watch,需要通过getChildren来设置watch
- 只能在持久节点下创建子节点,临时节点下是不能创建子节点的
- 子节点变更只是子节点列表个数发生变化,子节点内容发生变化是不会触发watch通知,可以通过下面的代码来验证
package com.fancy;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
class ZookeeperClient implements Watcher {
private static final String host = "127.0.0.1:2181";
private static CountDownLatch countDownLatch = new CountDownLatch(1);
public static void main(String[] args) throws IOException {
ZooKeeper zooKeeper =
new ZooKeeper(host, 3000,
new ZookeeperClient());
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
zooKeeper.exists("/test1", true);
zooKeeper.create("/test1", "123".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
zooKeeper.getChildren("/test1", true);
zooKeeper.create("/test1/child-1", "123".getBytes(), ZooDefs.Ids
.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
zooKeeper.getChildren("/test1", true);
zooKeeper.setData("/test1/child-1", "234".getBytes(), -1);
} catch (KeeperException | InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void process(WatchedEvent watchedEvent) {
countDownLatch.countDown();
System.out.println(watchedEvent.getType());
}
}
运行结果和上面的运行结果是一样的,在修改child-1子节点的值之后,并不会有watch通知。
小结
上面就是常用的KeeperState.SyncConnected一些使用场景和简单的java API操作,使用的方法都是同步方法,异步方法也很简单,在方法中加上一个回调接口即可。
参考
《从paxos到zookeeper》