From f0b0e2281609d3816fdf792ed795e4ceb5883ed0 Mon Sep 17 00:00:00 2001 From: "ken.lj" Date: Sat, 28 Oct 2017 17:39:11 +0800 Subject: [PATCH] Merge pull request #762 from chickenlj:bugfix#685_registrycache * Optimize(AbstractRegistry) registry cache, one registry file per 'registry & application', avoid reload (#685). * Fixed #692 optimize zookeeper operation in service export/reference process: check already exists before create --- .../registry/support/AbstractRegistry.java | 24 +--------- .../curator/CuratorZookeeperClient.java | 9 ++++ .../support/AbstractZookeeperClient.java | 7 ++- .../zkclient/ZkclientZookeeperClient.java | 8 ++++ .../curator/CuratorZookeeperClientTest.java | 46 +++++++++++++++++++ 5 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java b/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java index face7ca7e32..a347c411e7c 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java @@ -83,7 +83,7 @@ public AbstractRegistry(URL url) { setUrl(url); // 启动文件保存定时器 syncSaveFile = url.getParameter(Constants.REGISTRY_FILESAVE_SYNC_KEY, false); - String filename = url.getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/.dubbo/dubbo-registry-" + url.getHost() + ".cache"); + String filename = url.getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/.dubbo/dubbo-registry-" + url.getParameter(Constants.APPLICATION_KEY) + "-" + url.getAddress() + ".cache"); File file = null; if (ConfigUtils.isNotEmpty(filename)) { file = new File(filename); @@ -149,28 +149,8 @@ public void doSaveProperties(long version) { if (file == null) { return; } - Properties newProperties = new Properties(); - // 保存之前先读取一遍,防止多个注册中心之间冲突 - InputStream in = null; - try { - if (file.exists()) { - in = new FileInputStream(file); - newProperties.load(in); - } - } catch (Throwable e) { - logger.warn("Failed to load registry store file, cause: " + e.getMessage(), e); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - logger.warn(e.getMessage(), e); - } - } - } // 保存 try { - newProperties.putAll(properties); File lockfile = new File(file.getAbsolutePath() + ".lock"); if (!lockfile.exists()) { lockfile.createNewFile(); @@ -190,7 +170,7 @@ public void doSaveProperties(long version) { } FileOutputStream outputFile = new FileOutputStream(file); try { - newProperties.store(outputFile, "Dubbo Registry Cache"); + properties.store(outputFile, "Dubbo Registry Cache"); } finally { outputFile.close(); } diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java index 4f20ae76b17..de3b3fbff3e 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java @@ -88,6 +88,15 @@ public List getChildren(String path) { } } + public boolean checkExists(String path) { + try { + if (client.checkExists().forPath(path) != null) { + return true; + } + } catch (Exception e) { + } + return false; + } public boolean isConnected() { return client.getZookeeperClient().isConnected(); } diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java index 8b89efb1891..f9ff9639dd4 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java @@ -36,7 +36,10 @@ public URL getUrl() { public void create(String path, boolean ephemeral) { int i = path.lastIndexOf('/'); if (i > 0) { - create(path.substring(0, i), false); + String parentPath = path.substring(0, i); + if (!checkExists(parentPath)) { + create(parentPath, false); + } } if (ephemeral) { createEphemeral(path); @@ -105,6 +108,8 @@ public void close() { protected abstract void createEphemeral(String path); + protected abstract boolean checkExists(String path); + protected abstract TargetChildListener createTargetChildListener(String path, ChildListener listener); protected abstract List addTargetChildListener(String path, TargetChildListener listener); diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java index 8109b753f66..dec4da12542 100644 --- a/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java @@ -68,6 +68,14 @@ public List getChildren(String path) { } } + public boolean checkExists(String path) { + try { + return client.exists(path); + } catch (Throwable t) { + } + return false; + } + public boolean isConnected() { return state == KeeperState.SyncConnected; } diff --git a/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java new file mode 100644 index 00000000000..dc26596c691 --- /dev/null +++ b/dubbo-remoting/dubbo-remoting-zookeeper/src/test/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClientTest.java @@ -0,0 +1,46 @@ +package com.alibaba.dubbo.remoting.zookeeper.curator; + +import com.alibaba.dubbo.common.URL; + +import org.junit.Assert; +import org.junit.Test; + +/** + * @author ken.lj + * @date 2017/10/16 + */ +public class CuratorZookeeperClientTest { + + @Test + public void testCheckExists() { + CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService")); + String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; + curatorClient.create(path, false); + Assert.assertTrue(curatorClient.checkExists(path)); + Assert.assertFalse(curatorClient.checkExists(path + "/noneexits")); + } + + /** + * create checkExists 性能測試 + */ + @Test + public void testCreate() { + CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService")); + String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers"; + curatorClient.create(path, false); + + // 重复create 100次,耗时 + long startTime = System.nanoTime(); + for (int i = 0; i < 100; i++) { + curatorClient.create(path, true); + } + System.out.println("create cost: " + (System.nanoTime() - startTime) / 1000 / 1000); + + // 判断100次,耗时 + startTime = System.nanoTime(); + for (int i = 0; i < 100; i++) { + curatorClient.checkExists(path); + } + System.out.println("judge cost: " + (System.nanoTime() - startTime) / 1000 / 1000); + } +}