使用 Apache Curator 时,为什么创建 zNode 会导致 NoNodeException

When using Apache Curator, why does creating a zNode cause NoNodeException

我正在尝试在 Zookeeper 中创建一个“目录”,如下所示:

curatorFramework = CuratorFrameworkFactory.newClient(
    "ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181",
    zkInfo.getSessionTimeoutMs(),
    zkInfo.getConnectionTimeoutMs(),
    new RetryNTimes(zkInfo.getRetryAttempts(), 
    zkInfo.getRetryIntervalMs())
);
curatorFramework.start();

byte[] byteArray = new byte[1];
byteArray[0] = (byte) 7;

curatorFramework.create()
    .withMode(CreateMode.PERSISTENT)
    .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)
    .forPath("/my_node", byteArray); 

令人费解的是,它在我尝试创建的节点上出现了“NoNodeException”。

Caused by: org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /my_node
        at org.apache.zookeeper.KeeperException.create(KeeperException.java:111) ~[stormjar.jar:?]
        at org.apache.zookeeper.KeeperException.create(KeeperException.java:51) ~[stormjar.jar:?]
        at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl.call(CreateBuilderImpl.java:1176) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl.call(CreateBuilderImpl.java:1156) ~[stormjar.jar:?]
        at org.apache.curator.connection.StandardConnectionHandlingPolicy.callWithRetry(StandardConnectionHandlingPolicy.java:64) ~[stormjar.jar:?]
        at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:100) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:1153) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:607) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:597) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:362) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:310) ~[stormjar.jar:?]

请注意,我可以连接到 Zookeeper:

Socket connection established to ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181, initiating session
Session establishment complete on server ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181, sessionid = 0x100000363b13354, negotiated timeout = 20000        

请注意,Zookeeper 服务器在远程机器上并且 ip ("111.11.111.1") 已在此 post 中更改。

似乎与 ACL 相关,只是为了确定,您可以手动创建它。

找到您的本地 zk 二进制文件(不需要在远程主机上)并启动指向您的服务器的客户端 (zkCli)。连接后,创建新的 znode:

bin/zkCli.sh -server 111.11.111.1:2181

[zkshell:x] create /my_node
>>Created /mynode

shell应该输出最后一句,以保证节点已经创建。完成后,再次启动 Curator 进程。

查看 here 了解有关 zk 客户端的更多详细信息。

所以我尝试连接 zkCli,发现问题出在我的 connectString 上:

/opt/zookeeper-3.6.1/bin/zkCli.sh -server ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181
Welcome to ZooKeeper!
[myid:ip-111-11-111-1.us-west-2.compute.internal:2181] - INFO  [main-SendThread(ip-111-11-111-1.us-west-2.compute.internal:2181):ClientCnxn$SendThread@1154] - Opening socket connection to server ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181.
[myid:ip-111-11-111-1.us-west-2.compute.internal:2181] - INFO  [main-SendThread(ip-111-11-111-1.us-west-2.compute.internal:2181):ClientCnxn$SendThread@986] - Socket connection established, initiating session, client: /111.11.111.2:43736, server: ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181
[myid:ip-111-11-111-1.us-west-2.compute.internal:2181] - INFO  [main-SendThread(ip-111-11-111-1.us-west-2.compute.internal:2181):ClientCnxn$SendThread@1420] - Session establishment complete on server ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181, session id = 0x100000363b1d2e8, negotiated timeout = 30000

[zk: ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181(CONNECTED) 0] ls /
Node does not exist: /
[zk: ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181(CONNECTED) 1] create /my_node
Node does not exist: /my_node

如您所见,尝试创建节点会在 zkCli 中出现 NoNode 错误。

事实证明 ip-111-11-111-1.us-west-2.compute.internal/111.11.111.1:2181 不是正确的连接字符串,但我很困惑,因为 zkCli 允许我连接到它。