结论:在使用CuratorFramework的监听机制时,
如果使用NodeCache,对于多级路径,即使不创建该路径,设置监听后CuratorFramework会主动创建从第一级路径到倒数第二级的路径,比如要监听路径 "/a/b/c" 的NodeCache,CuratorFramework会自动替你创建路径 "/a/b";
如果使用PathChildrenCache,对于多级路径,即使不创建该路径,设置监听后CuratorFramework会主动创建整个完整路径,比如要监听路径 "/A/B/C" 的PathChildrenCache,CuratorFramework会自动替你创建路径 "/A/B/C"。
测试代码:
启动程序前,ZooKeeper的节点情况:
[zk: localhost:2181(CONNECTED) 2] ls /
[cluster, brokers, zookeeper, admin, isr_change_notification, log_dir_event_notification, controller_epoch, name, ha, consumers, latest_producer_id_block, config]
节点中并不包含"/A"和"/a"这两个。
启动程序后,ZooKeeper的节点情况:
[zk: localhost:2181(CONNECTED) 3] ls /
[cluster, a, A, brokers, zookeeper, admin, isr_change_notification, log_dir_event_notification, controller_epoch, name, ha, consumers, latest_producer_id_block, config]
再查看其具体路径:
[zk: localhost:2181(CONNECTED) 4] get /a/b
cZxid = 0x125b
ctime = Sun Sep 09 12:43:02 GMT+08:00 2018
mZxid = 0x125b
mtime = Sun Sep 09 12:43:02 GMT+08:00 2018
pZxid = 0x125b
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 0
[zk: localhost:2181(CONNECTED) 5] get /a/b/c
Node does not exist: /a/b/c
[zk: localhost:2181(CONNECTED) 6] get /A/B/C
cZxid = 0x125e
ctime = Sun Sep 09 12:43:02 GMT+08:00 2018
mZxid = 0x125e
mtime = Sun Sep 09 12:43:02 GMT+08:00 2018
pZxid = 0x125e
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 0
numChildren = 0
可以看出,确实和结论一样。