Skip to content

Commit be965bf

Browse files
authored
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
1 parent 209aecf commit be965bf

File tree

5 files changed

+71
-23
lines changed

5 files changed

+71
-23
lines changed

dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistry.java

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public AbstractRegistry(URL url) {
8383
setUrl(url);
8484
// 启动文件保存定时器
8585
syncSaveFile = url.getParameter(Constants.REGISTRY_FILESAVE_SYNC_KEY, false);
86-
String filename = url.getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/.dubbo/dubbo-registry-" + url.getHost() + ".cache");
86+
String filename = url.getParameter(Constants.FILE_KEY, System.getProperty("user.home") + "/.dubbo/dubbo-registry-" + url.getParameter(Constants.APPLICATION_KEY) + "-" + url.getAddress() + ".cache");
8787
File file = null;
8888
if (ConfigUtils.isNotEmpty(filename)) {
8989
file = new File(filename);
@@ -149,28 +149,8 @@ public void doSaveProperties(long version) {
149149
if (file == null) {
150150
return;
151151
}
152-
Properties newProperties = new Properties();
153-
// 保存之前先读取一遍,防止多个注册中心之间冲突
154-
InputStream in = null;
155-
try {
156-
if (file.exists()) {
157-
in = new FileInputStream(file);
158-
newProperties.load(in);
159-
}
160-
} catch (Throwable e) {
161-
logger.warn("Failed to load registry store file, cause: " + e.getMessage(), e);
162-
} finally {
163-
if (in != null) {
164-
try {
165-
in.close();
166-
} catch (IOException e) {
167-
logger.warn(e.getMessage(), e);
168-
}
169-
}
170-
}
171152
// 保存
172153
try {
173-
newProperties.putAll(properties);
174154
File lockfile = new File(file.getAbsolutePath() + ".lock");
175155
if (!lockfile.exists()) {
176156
lockfile.createNewFile();
@@ -190,7 +170,7 @@ public void doSaveProperties(long version) {
190170
}
191171
FileOutputStream outputFile = new FileOutputStream(file);
192172
try {
193-
newProperties.store(outputFile, "Dubbo Registry Cache");
173+
properties.store(outputFile, "Dubbo Registry Cache");
194174
} finally {
195175
outputFile.close();
196176
}

dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/curator/CuratorZookeeperClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ public List<String> getChildren(String path) {
8888
}
8989
}
9090

91+
public boolean checkExists(String path) {
92+
try {
93+
if (client.checkExists().forPath(path) != null) {
94+
return true;
95+
}
96+
} catch (Exception e) {
97+
}
98+
return false;
99+
}
91100
public boolean isConnected() {
92101
return client.getZookeeperClient().isConnected();
93102
}

dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/support/AbstractZookeeperClient.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public URL getUrl() {
3636
public void create(String path, boolean ephemeral) {
3737
int i = path.lastIndexOf('/');
3838
if (i > 0) {
39-
create(path.substring(0, i), false);
39+
String parentPath = path.substring(0, i);
40+
if (!checkExists(parentPath)) {
41+
create(parentPath, false);
42+
}
4043
}
4144
if (ephemeral) {
4245
createEphemeral(path);
@@ -105,6 +108,8 @@ public void close() {
105108

106109
protected abstract void createEphemeral(String path);
107110

111+
protected abstract boolean checkExists(String path);
112+
108113
protected abstract TargetChildListener createTargetChildListener(String path, ChildListener listener);
109114

110115
protected abstract List<String> addTargetChildListener(String path, TargetChildListener listener);

dubbo-remoting/dubbo-remoting-zookeeper/src/main/java/com/alibaba/dubbo/remoting/zookeeper/zkclient/ZkclientZookeeperClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public List<String> getChildren(String path) {
6868
}
6969
}
7070

71+
public boolean checkExists(String path) {
72+
try {
73+
return client.exists(path);
74+
} catch (Throwable t) {
75+
}
76+
return false;
77+
}
78+
7179
public boolean isConnected() {
7280
return state == KeeperState.SyncConnected;
7381
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.alibaba.dubbo.remoting.zookeeper.curator;
2+
3+
import com.alibaba.dubbo.common.URL;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
/**
9+
* @author ken.lj
10+
* @date 2017/10/16
11+
*/
12+
public class CuratorZookeeperClientTest {
13+
14+
@Test
15+
public void testCheckExists() {
16+
CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService"));
17+
String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
18+
curatorClient.create(path, false);
19+
Assert.assertTrue(curatorClient.checkExists(path));
20+
Assert.assertFalse(curatorClient.checkExists(path + "/noneexits"));
21+
}
22+
23+
/**
24+
* create checkExists 性能測試
25+
*/
26+
@Test
27+
public void testCreate() {
28+
CuratorZookeeperClient curatorClient = new CuratorZookeeperClient(URL.valueOf("zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService"));
29+
String path = "/dubbo/com.alibaba.dubbo.demo.DemoService/providers";
30+
curatorClient.create(path, false);
31+
32+
// 重复create 100次,耗时
33+
long startTime = System.nanoTime();
34+
for (int i = 0; i < 100; i++) {
35+
curatorClient.create(path, true);
36+
}
37+
System.out.println("create cost: " + (System.nanoTime() - startTime) / 1000 / 1000);
38+
39+
// 判断100次,耗时
40+
startTime = System.nanoTime();
41+
for (int i = 0; i < 100; i++) {
42+
curatorClient.checkExists(path);
43+
}
44+
System.out.println("judge cost: " + (System.nanoTime() - startTime) / 1000 / 1000);
45+
}
46+
}

0 commit comments

Comments
 (0)