diff --git a/demo/demo-etcd/README.md b/demo/demo-etcd/README.md index d25539da93..046bec6c96 100644 --- a/demo/demo-etcd/README.md +++ b/demo/demo-etcd/README.md @@ -2,5 +2,4 @@ This integration tests is designed for Etcd registry and configuration. And extra test cases include: -* Test cases related to SpringMVC annotations that demo-springmvc can not cover. - +* Test cases related to SpringMVC annotations that demo-springmvc can not cover. diff --git a/demo/demo-etcd/test-client/pom.xml b/demo/demo-etcd/test-client/pom.xml index 288d4dcd87..461df7e4ac 100644 --- a/demo/demo-etcd/test-client/pom.xml +++ b/demo/demo-etcd/test-client/pom.xml @@ -60,7 +60,8 @@ - etcd:3.5.9 + + bitnami/etcd:latest etcd alias diff --git a/service-registry/registry-etcd/pom.xml b/service-registry/registry-etcd/pom.xml index 52681b6078..cf48811d34 100644 --- a/service-registry/registry-etcd/pom.xml +++ b/service-registry/registry-etcd/pom.xml @@ -49,4 +49,4 @@ - \ No newline at end of file + diff --git a/service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/EtcdDiscovery.java b/service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/EtcdDiscovery.java index b3ce9f94bd..0537ebf615 100644 --- a/service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/EtcdDiscovery.java +++ b/service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/EtcdDiscovery.java @@ -85,7 +85,7 @@ public boolean enabled(String application, String serviceName) { public List findServiceInstances(String application, String serviceName) { String prefixPath = basePath + "/" + application + "/" + serviceName; - if (!isWatch) { + SingletonManager.getInstance().getSingleton(prefixPath, serName -> { Watch watchClient = client.getWatchClient(); try { watchClient.watch(ByteSequence.from(prefixPath, Charset.defaultCharset()), WatchOption.builder().build(), @@ -94,11 +94,11 @@ public List findServiceInstances(String application, Stri instanceChangedListener.onInstanceChanged(name(), application, serviceName, convertServiceInstanceList(keyValueList)); }); - isWatch = true; } catch (Exception e) { LOGGER.error("add watch failure", e); } - } + return watchClient; + }); List endpointKv = getValuesByPrefix(prefixPath); return convertServiceInstanceList(endpointKv); } @@ -110,7 +110,7 @@ public List getValuesByPrefix(String prefix) { GetOption.builder().withPrefix(ByteSequence.from(prefix, StandardCharsets.UTF_8)).build()); GetResponse response = MuteExceptionUtil.builder().withLog("get kv by prefix error") .executeCompletableFuture(getFuture); - return response.getKvs(); // 返回所有匹配前缀的键值对 + return response.getKvs(); } private List convertServiceInstanceList(List keyValueList) { diff --git a/service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/MuteExceptionUtil.java b/service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/MuteExceptionUtil.java index 21260bfaef..83041cacfb 100644 --- a/service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/MuteExceptionUtil.java +++ b/service-registry/registry-etcd/src/main/java/org/apache/servicecomb/registry/etcd/MuteExceptionUtil.java @@ -55,7 +55,7 @@ public R executeFunction(FunctionWithException function, T t) { try { return function.apply(t); } catch (Exception e) { - LOGGER.error(getLogMessage("execute Function failure..."), customMessageParams, e); + LOGGER.error(getLogMessage("execute Function failure..."), customMessageParams, e); return null; } } @@ -73,7 +73,7 @@ public T executeCompletableFuture(CompletableFuture completableFuture) { try { return completableFuture.get(); } catch (Exception e) { - LOGGER.error(getLogMessage("execute CompletableFuture failure..."),customMessageParams, e); + LOGGER.error(getLogMessage("execute CompletableFuture failure..."), customMessageParams, e); return null; } } @@ -82,7 +82,7 @@ public R executeFunctionWithDoubleParam(FunctionWithDoubleParam singletons = new ConcurrentHashMapEx<>(); + + private SingletonManager() { + } + + public static synchronized SingletonManager getInstance() { + if (instance == null) { + instance = new SingletonManager(); + } + return instance; + } + + public T getSingleton(String key, Function mappingFunction) { + return (T) singletons.computeIfAbsent(key, mappingFunction); + } + + public void destroy() { + singletons.clear(); + instance = null; + } +}