From 7e732d0b8498571dd200638110edf0fed6b14881 Mon Sep 17 00:00:00 2001 From: ascrutae Date: Sat, 12 Nov 2016 10:41:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E9=80=92=E5=BD=92=E5=88=9B=E5=BB=BAPa?= =?UTF-8?q?th=E6=94=B9=E4=B8=BAfor=E5=BE=AA=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zookeeper/ZookeeperRegistryCenter.java | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/skywalking-registry/src/main/java/com/a/eye/skywalking/registry/impl/zookeeper/ZookeeperRegistryCenter.java b/skywalking-registry/src/main/java/com/a/eye/skywalking/registry/impl/zookeeper/ZookeeperRegistryCenter.java index 7173ae51b..ee5ed9e98 100644 --- a/skywalking-registry/src/main/java/com/a/eye/skywalking/registry/impl/zookeeper/ZookeeperRegistryCenter.java +++ b/skywalking-registry/src/main/java/com/a/eye/skywalking/registry/impl/zookeeper/ZookeeperRegistryCenter.java @@ -23,36 +23,32 @@ public class ZookeeperRegistryCenter implements RegistryCenter { createPath = "/" + createPath; } - mkdirs(createPath, 0); + mkdirs(createPath); } - /** - * @param path - * @param index - */ - private void mkdirs(String path, int index) { - //TODO: 修改成循环创建 + private void mkdirs(String path) { try { - int next = path.indexOf("/", index + 1); - CreateMode createMode = CreateMode.EPHEMERAL; - if (next != -1) { - createMode = CreateMode.PERSISTENT; - path = path.substring(0, next); + String[] pathArray = path.split("/"); + if (pathArray.length == 0) { + return; } - if (client.exists(path, false) == null) - client.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode); - - if (next != -1) { - mkdirs(path, next); + for (int i = 0; i < pathArray.length - 1; i++) { + String pathSegment = pathArray[i]; + if (pathSegment.length() == 0) { + continue; + } + client.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } + + client.create(pathArray[pathArray.length - 1], null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); + logger.info("register path[{}] success", path); } catch (Exception e) { logger.error("Failed to create path[{}]", path, e); } } - @Override public void subscribe(final String path, final NotifyListener listener) { try {