用ZOOKEEPER客户端向zookeeper的某一个节点添加子节点时出现以下错误:

org.apache.zookeeper.KeeperException$NoChildrenForEphemeralsException: KeeperErrorCode = NoChildrenForEphemerals for /xxxxx/xxxx/
at org.apache.zookeeper.KeeperException.create(KeeperException.java:117)
at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783)
at org.apache.curator.framework.imps.CreateBuilderImpl$11.call(CreateBuilderImpl.java:721)
at org.apache.curator.framework.imps.CreateBuilderImpl$11.call(CreateBuilderImpl.java:704)
at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:108)
at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:701)
at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:477)
at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:467)
at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:44)
at com.xxxx.utils.zookeeper.crawler.Main.main(Main1.java:14)

从报错可以看出是因为临时节点下不能创建子节点的原因:

所以解决方法是把要创建子节点的节点创建为永久性节点:

如用原生的zookeeper客户端如下:

zk.create(seqPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

用curator客户端代码如下:

client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(xxxxxx);

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐