Skip to content

Commit

Permalink
refactor(registry-etcd): Refactor the EtcdDiscovery class and update …
Browse files Browse the repository at this point in the history
…related components

- Refactor the findServiceInstances method and use SingletonManager to manage watchers
- Optimize the output format of exception information in log records
- Update the Etcd image version in the test environment
- Removed redundant information in README.md - Added SingletonManager class for managing singleton objects
  • Loading branch information
SweetWuXiaoMei committed Oct 23, 2024
1 parent 2c64e42 commit cf55f27
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 11 deletions.
3 changes: 1 addition & 2 deletions demo/demo-etcd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion demo/demo-etcd/test-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
<configuration>
<images>
<image>
<name>etcd:3.5.9</name>
<!-- <name>etcd:3.5.9</name>-->
<name>bitnami/etcd:latest</name>
<alias>etcd</alias>
<run>
<namingStrategy>alias</namingStrategy>
Expand Down
2 changes: 1 addition & 1 deletion service-registry/registry-etcd/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
</dependency>
</dependencies>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public boolean enabled(String application, String serviceName) {
public List<EtcdDiscoveryInstance> 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(),
Expand All @@ -94,11 +94,11 @@ public List<EtcdDiscoveryInstance> 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<KeyValue> endpointKv = getValuesByPrefix(prefixPath);
return convertServiceInstanceList(endpointKv);
}
Expand All @@ -110,7 +110,7 @@ public List<KeyValue> 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<EtcdDiscoveryInstance> convertServiceInstanceList(List<KeyValue> keyValueList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public <T, R> R executeFunction(FunctionWithException<T, R> 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;
}
}
Expand All @@ -73,7 +73,7 @@ public <T> T executeCompletableFuture(CompletableFuture<T> 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;
}
}
Expand All @@ -82,7 +82,7 @@ public <T1, T2, R> R executeFunctionWithDoubleParam(FunctionWithDoubleParam<T1,
try {
return function.apply(t1, t2);
} catch (Exception e) {
LOGGER.error(getLogMessage("execute FunctionWithDoubleParam failure..."),customMessageParams, e);
LOGGER.error(getLogMessage("execute FunctionWithDoubleParam failure..."), customMessageParams, e);
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.servicecomb.registry.etcd;

import java.util.function.Function;

import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;

public class SingletonManager {

private static SingletonManager instance;

private ConcurrentHashMapEx<String, Object> singletons = new ConcurrentHashMapEx<>();

private SingletonManager() {
}

public static synchronized SingletonManager getInstance() {
if (instance == null) {
instance = new SingletonManager();
}
return instance;
}

public <T> T getSingleton(String key, Function<? super String, ? extends T> mappingFunction) {
return (T) singletons.computeIfAbsent(key, mappingFunction);
}

public void destroy() {
singletons.clear();
instance = null;
}
}

0 comments on commit cf55f27

Please sign in to comment.