ZooKeeper节点类型:
ZooKeeper 节点是有生命周期的,这取决于节点的类型。在 ZooKeeper 中,节点类型可以分为持久节点(PERSISTENT )、临时节点(EPHEMERAL),以及时序节点(SEQUENTIAL ),具体在节点创建过程中,一般是组合使用,可以生成以下 4 种节点类型。
- 持久节点(PERSISTENT)
- 持久顺序节点(PERSISTENT_SEQUENTIAL)
- 临时节点(EPHEMERAL)
- 临时顺序节点(EPHEMERAL_SEQUENTIAL)
持久节点(PERSISTENT)
所谓持久节点,是指在节点创建后,就一直存在,直到有删除操作来主动清除这个节点——不会因为创建该节点的客户端会话失效而消失。
代码示例:
代码示例:
public static void main(String[] args) throws KeeperException, InterruptedException {
MyZooKeeper zooKeeper = new MyZooKeeper();
zooKeeper.connect("127.0.0.1:2181");
ZooKeeper zk = MyZooKeeper.zooKeeper;
//持久节点
String root = "/ephemeral";
String createdPath = zk.create(root, root.getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("createdPath = " + createdPath);
Thread.sleep(1000 * 20); // 等待20秒关闭ZooKeeper连接
zooKeeper.close(); // 关闭连接后创建的临时节点将自动删除
}
注意:这里提到的是会话失效,而非连接断开。另外,在临时节点下面不能创建子节点。
持久顺序节点(PERSISTENT_SEQUENTIAL)
这类节点的基本特性和上面的节点类型是一致的。额外的特性是,在ZK中,每个父节点会为他的第一级子节点维护一份时序,会记录每个子节点创建的先后顺序。基于这个特性,在创建子节点的时候,可以设置这个属性,那么在创建节点过程中,ZK会自动为给定节点名加上一个数字后缀,作为新的节点名。这个数字后缀的范围是整型的最大值。
示例:
public static void main(String[] args) throws KeeperException, InterruptedException {
MyZooKeeper zooKeeper = new MyZooKeeper();
zooKeeper.connect("127.0.0.1:2181");
ZooKeeper zk = MyZooKeeper.zooKeeper;
String root = "/computer";
String createdPath = zk.create(root, root.getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("createdPath = " + createdPath);
//持久顺序节点
for (int i=0; i<5; i++) {
String path = "/computer/node";
String createdPath1 = zk.create(path, path.getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
System.out.println("createdPath = " + createdPath);
}
zk.close();
Thread.sleep(1000 * 20); // 等待20秒关闭ZooKeeper连接
zooKeeper.close(); // 关闭连接后创建的临时节点将自动删除
}
运行结果:
createdPath = /computer
createdPath = /computer/node0000000000
createdPath = /computer/node0000000001
createdPath = /computer/node0000000002
createdPath = /computer/node0000000003
createdPath = /computer/node0000000004
结果中的0000000000~0000000004都是自动添加的序列号
临时节点(EPHEMERAL)
和持久节点不同的是,临时节点的生命周期和客户端会话绑定。也就是说,如果客户端会话失效,那么这个节点就会自动被清除掉。注意,这里提到的是会话失效,而非连接断开。另外,在临时节点下面不能创建子节点。
示例:
public static void main(String[] args) throws KeeperException, InterruptedException {
MyZooKeeper zooKeeper = new MyZooKeeper();
zooKeeper.connect("127.0.0.1:2181");
ZooKeeper zk = MyZooKeeper.zooKeeper;
String path = "/ephemeral/test01" ;
String createdPath = zk.create(path, path.getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("createdPath = " + createdPath);
Thread.sleep(1000 * 20); // 等待20秒关闭ZooKeeper连接
zooKeeper.close(); // 关闭连接后创建的临时节点将自动删除
}
注意:这里提到的是会话失效,而非连接断开。另外,在临时节点下面不能创建子节点。
临时顺序节点(EPHEMERAL_SEQUENTIAL)
临时节点的生命周期和客户端会话绑定。也就是说,如果客户端会话失效,那么这个节点就会自动被清除掉。注意创建的节点会自动加上编号。实例:
public static void main(String[] args) throws KeeperException, InterruptedException {
MyZooKeeper zooKeeper = new MyZooKeeper();
zooKeeper.connect("127.0.0.1:2181");
ZooKeeper zk = MyZooKeeper.zooKeeper;
String root = "/ephemeral";
String createdPath = zk.create(root, root.getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("createdPath = " + createdPath);
//临时顺序节点
String path = "/ephemeral/test01" ;
createdPath = zk.create(path, path.getBytes(),
Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("createdPath = " + createdPath);
Thread.sleep(1000 * 20); // 等待20秒关闭ZooKeeper连接
zooKeeper.close(); // 关闭连接后创建的临时节点将自动删除
}
输出结果:
type = None
createdPath = /ephemeral/test0000000003
createdPath = /ephemeral/test0000000004
createdPath = /ephemeral/test0000000005
createdPath = /ephemeral/test0000000006
注意点:
- znode 中的数据可以有多个版本,在查询该 znode 数据时就需要带上版本信息。 (setpath version / delete path version)
- znode 可以是临时 znode(create -e 生成的节点),一旦创建这个 znode 的 client 与server 断开连接,该 znode 将被自动删除。client 和 server 之间通过 heartbeat 来确认连接正常,这种状态称之为 session,断开连接后 session 失效。
- 临时 znode 不能有子 znode。
- znode 可以自动编号(create -s 生成的节点),例如在 create -s /app/node 已存在时,将会生成 /app/node00***001 节点。
- znode 可以被监控,该目录下某些信息的修改,例如节点数据、子节点变化等,可以主动通知监控注册的 client。事实上,通过这个特性,可以完成许多重要应用,例如配置管理、信息同步、分布式锁等等。