我打算从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节点?
答案 0 :(得分:2)
我用以下代码解决了这个问题。
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).withACL(Ids.OPEN_ACL_UNSAFE).forPath(MUTEX_LEADER_PATH, new byte[0]);
它完美无缺!