一、Znode分类
1、persistent_node 持久化节点
是默认类型,该类型节点,并不会随着客户端断开就会自动删除;zookeeper服务重启该节点也不会被删除,只有手动删除的时候才会删除;可以创建子节点;
# create a persistent_node
[zkshell: 7] create /persistent_nodeCreated /persistent_node
2、persistent-sequential node 持久化顺序节点
该类型节点,并不会随着客户端断开就会自动删除;zookeeper服务重启该节点也不会被删除,只有手动删除的时候才会删除;可以创建子节点;创建该节点的时候,会自动在节点名后加10位数字序列后缀,后缀就是父节点的cversion,代表子节点的版本号
if (createMode.isSequential()) {path = path + String.format(Locale.ENGLISH, "%010d", parentCVersion);
}
# create the persistent-sequential node
[zkshell: 9] create -s /persistent_sequential_node mydataCreated /persistent_sequential_node0000000176
[zk: localhost:2181(CONNECTED) 38] ls -s /helo
[baobao, lichf, liubb, world]
cZxid = 0x184
ctime = Tue Mar 26 15:44:15 CST 2024
mZxid = 0x193
mtime = Tue Mar 26 18:30:18 CST 2024
pZxid = 0x18b
cversion = 4
dataVersion = 5
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 5
numChildren = 4
[zk: localhost:2181(CONNECTED) 39] create -s /helo/queue
# 0000000004 是父节点的cversion
Created /helo/queue0000000004
[zk: localhost:2181(CONNECTED) 40]
3、Ephemeral Nodes 临时节点
一个session连接可以创建一个临时节点,该临时节点的生命周期就是该session的生命周期,当该连接断开的时候,该临时节点也会被删除,因为此特性,临时节点不允许有子节点
# create a ephemeral node
[zkshell: 8] create -e /ephemeral_node mydataCreated /ephemeral_node
4、ephemeral-sequential_node 临时序列节点
临时序列节点就是临时节点后缀加上序列号,方式同持久化顺序节点
# create the ephemeral-sequential_node
[zkshell: 10] create -s -e /ephemeral_sequential_node mydataCreated /ephemeral_sequential_node0000000174
5、container node 容器节点 Added in 3.5.3(在3.5.3版本中新增)
容器节点的特性和持久节点是一样的,但是区别是,当ZK 服务端启动后,会有一个单独的线程去扫描所有的容器节点,当发现容器节点的子节点数量为 0 时,会自动删除该节点;常用于leader、lock等场景
# create the container node.When the last child of a container is deleted,the container becomes to be deleted
[zkshell: 14] create -c /container_node mydataCreated /container_node
[zkshell: 15] create -c /container_node/child_1 mydataCreated /container_node/child_1
[zkshell: 16] create -c /container_node/child_2 mydataCreated /container_node/child_2
[zkshell: 17] delete /container_node/child_1
[zkshell: 18] delete /container_node/child_2
[zkshell: 19] get /container_nodeorg.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /container_node
6、TTL Nodes Added in 3.5.3
可以在创建persistent_node和persistent-sequential node 这两类节点的之后指定TTL,单位毫秒,如果该znode在TTL时间内没有被修改同时又没有子节点,则该节点将在将来的某个时间点被服务器删除。
# create the ttl node.
# set zookeeper.extendedTypesEnabled=true
# Otherwise:KeeperErrorCode = Unimplemented for /ttl_node
[zkshell: 20] create -t 3000 /ttl_node mydataCreated /ttl_node
# after 3s later
[zkshell: 21] get /ttl_nodeorg.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /ttl_node
注意TTL node 如果要启用需要通过设置系统参数来启用,默认情况下是禁用的,如果不设置对应参数,创建的时候会报KeeperException.UnimplementedException.异常;
[zk: localhost:2181(CONNECTED) 44] create -t 3000 /helo/test
KeeperErrorCode = Unimplemented for /helo/test
[zk: localhost:2181(CONNECTED) 45]
可以做相关设置如下:
3.5.3版本
-Dzookeeper.emulate353TTLNodes=true -Dzookeeper.extendedTypesEnabled=true
其他版本只需要一个参数就可以
-Dzookeeper.extendedTypesEnabled=true