无法使用 java 从根创建 zookeeper 节点(尚不存在)

Could not create zookeeper node (which does not already exist) from the root using java

我打算从 java 创建一个 zookeeper 节点。我最初创建了 curator 客户端,我用它检索了 zookeeper 对象并创建了失败的节点。

我做什么?

        //client is the CuratorFramework object
        String MUTEX_LEADER_PATH = "/leader/jobadmin";
        client.getZookeeperClient().getZooKeeper().create(MUTEX_LEADER_PATH, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

我得到了什么?

    [2015-04-20 10:53:17,992] [ERROR] [org.apache.curator.framework.recipes.leader.LeaderSelector] [LeaderSelector-0] mutex.acquire() threw an exception
org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /leader
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:111)
    at org.apache.zookeeper.KeeperException.create(KeeperException.java:51)
    at org.apache.zookeeper.ZooKeeper.create(ZooKeeper.java:783)
    at org.apache.curator.utils.ZKPaths.mkdirs(ZKPaths.java:199)
    at org.apache.curator.framework.imps.CreateBuilderImpl.call(CreateBuilderImpl.java:682)
    at org.apache.curator.framework.imps.CreateBuilderImpl.call(CreateBuilderImpl.java:660)
    at org.apache.curator.RetryLoop.callWithRetry(RetryLoop.java:107)
    at org.apache.curator.framework.imps.CreateBuilderImpl.pathInForeground(CreateBuilderImpl.java:656)
    at org.apache.curator.framework.imps.CreateBuilderImpl.protectedPathInForeground(CreateBuilderImpl.java:441)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:431)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:411)
    at org.apache.curator.framework.imps.CreateBuilderImpl.forPath(CreateBuilderImpl.java:44)
    at org.apache.curator.framework.recipes.locks.LockInternals.attemptLock(LockInternals.java:224)
    at org.apache.curator.framework.recipes.locks.InterProcessMutex.internalLock(InterProcessMutex.java:221)
    at org.apache.curator.framework.recipes.locks.InterProcessMutex.acquire(InterProcessMutex.java:77)
    at org.apache.curator.framework.recipes.leader.LeaderSelector.doWork(LeaderSelector.java:378)
    at org.apache.curator.framework.recipes.leader.LeaderSelector.doWorkLoop(LeaderSelector.java:436)
    at org.apache.curator.framework.recipes.leader.LeaderSelector.access0(LeaderSelector.java:64)
    at org.apache.curator.framework.recipes.leader.LeaderSelector.call(LeaderSelector.java:241)
    at org.apache.curator.framework.recipes.leader.LeaderSelector.call(LeaderSelector.java:235)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

尝试了不同的创建模式,但结果是一样的。

使用以下代码创建客户端:

private synchronized CuratorFramework createClient() {
    if (client != null) {
        return client;
    }

    RetryPolicy retryPolicy = new RetryUntilElapsed(SESSION_TIMEOUT, 1000);
    client = CuratorFrameworkFactory.newClient(MUTEX_LEADER_PATH, SESSION_TIMEOUT, 100, retryPolicy);
    client.start();

    return client;
}

如何创建根节点不存在的 zookeeper 节点?

我用下面的代码解决了这个问题。

 client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(Ids.OPEN_ACL_UNSAFE).forPath(MUTEX_LEADER_PATH, new byte[0]);

效果很好!