首页 > 解决方案 > 使用 Apache Curator 时,为什么创建 zNode 会导致 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$17.call(CreateBuilderImpl.java:1176) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl$17.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$3.forPath(CreateBuilderImpl.java:362) ~[stormjar.jar:?]
        at org.apache.curator.framework.imps.CreateBuilderImpl$3.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”)已更改。

标签: javaapache-zookeeperapache-curator

解决方案


所以我尝试用 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 允许我连接到它。


推荐阅读