gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:27:51 26 4
gpt4 key购买 nike

我打算从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$11.call(CreateBuilderImpl.java:682)
at org.apache.curator.framework.imps.CreateBuilderImpl$11.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.access$100(LeaderSelector.java:64)
at org.apache.curator.framework.recipes.leader.LeaderSelector$2.call(LeaderSelector.java:241)
at org.apache.curator.framework.recipes.leader.LeaderSelector$2.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]);

效果非常好!

关于java - 无法使用 java 从根创建 Zookeeper 节点(尚不存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29752971/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com