Skip to content

Commit

Permalink
Merge pull request #1264 from TangLeDaily/issue1241_1.0.x
Browse files Browse the repository at this point in the history
修复springcloud双注册场景,使用zk注册中心有可能无法去重的bug
  • Loading branch information
Sherlockhan authored Jul 29, 2023
2 parents db90caa + 404c4d7 commit b360faf
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.huawei.registry.context.RegisterContext;
import com.huawei.registry.support.InstanceInterceptorSupport;
import com.huawei.registry.utils.HostUtils;

import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext;

Expand All @@ -26,7 +27,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -56,7 +56,7 @@ private List<ServiceInstance> getMergedInstances(ExecuteContext context) {
// 全量服务实例
final Object allServiceInstances = context.getArguments()[0];
final List<ServiceInstance> serviceInstances = filterDiscoveryServiceInstance(
(List<ServiceInstance>) allServiceInstances);
(List<ServiceInstance>) allServiceInstances);
if (originServiceInstances instanceof List) {
return convertAndMerge(serviceInstances, (List<ServiceInstance>) originServiceInstances);
}
Expand All @@ -72,17 +72,23 @@ private List<ServiceInstance> getMergedInstances(ExecuteContext context) {
*/
private List<ServiceInstance> filterDiscoveryServiceInstance(List<ServiceInstance> serviceInstances) {
return serviceInstances.stream()
.filter(serviceInstance -> SERVICE_INSTANCE_CLASS_NAME.equals(serviceInstance.getClass().getName()))
.collect(Collectors.toList());
.filter(serviceInstance -> SERVICE_INSTANCE_CLASS_NAME.equals(serviceInstance.getClass().getName()))
.collect(Collectors.toList());
}

private List<ServiceInstance> convertAndMerge(List<ServiceInstance> microServiceInstances,
List<ServiceInstance> originServiceInstances) {
List<ServiceInstance> result = new ArrayList<>(microServiceInstances);
List<ServiceInstance> originServiceInstances) {
List<ServiceInstance> result = new ArrayList<>();
if (isOpenMigration() && RegisterContext.INSTANCE.isAvailable()) {
result.addAll(originServiceInstances);
}
return result.stream().distinct().filter(Objects::nonNull).collect(Collectors.toList());
for (ServiceInstance microServiceInstance : microServiceInstances) {
result.removeIf(originServiceInstance ->
HostUtils.isSameInstance(originServiceInstance.getHost(), originServiceInstance.getPort(),
microServiceInstance.getHost(), microServiceInstance.getPort()));
result.add(microServiceInstance);
}
return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public void doAfter() throws NoSuchMethodException {
Assert.assertTrue(result instanceof List);
List<ServiceInstance> instances = (List<ServiceInstance>) result;
assertEquals(instances.size(), (scInstances.size() + originInstances.size()));

// 测试去重
scInstances.clear();
originInstances.clear();
allInstances.clear();
final ExecuteContext contextRepeat = interceptor.doAfter(buildContextRepeatedly());
final Object resultRepeat = contextRepeat.getResult();
Assert.assertTrue(resultRepeat instanceof List);
List<ServiceInstance> instancesRepeat = (List<ServiceInstance>) resultRepeat;
assertEquals(instancesRepeat.size(), (scInstances.size() + originInstances.size() - 1));
RegisterContext.INSTANCE.setAvailable(false);
}

Expand All @@ -102,6 +112,24 @@ private ExecuteContext buildContext() throws NoSuchMethodException {
return context;
}

private ExecuteContext buildContextRepeatedly() throws NoSuchMethodException {
scInstances.add(new DiscoveryServiceInstance(buildInstance(8001), serviceName));
scInstances.add(new DiscoveryServiceInstance(buildInstance(8002), serviceName));
scInstances.add(new DiscoveryServiceInstance(buildInstance(8003), serviceName));
scInstances.add(new DiscoveryServiceInstance(buildInstance(8004), serviceName));
scInstances.add(new DiscoveryServiceInstance(buildInstance(8005), serviceName));
originInstances.add(new ZookeeperServiceInstance(serviceName, buildZkInstance(8005)));
originInstances.add(new ZookeeperServiceInstance(serviceName, buildZkInstance(8006)));
originInstances.add(new ZookeeperServiceInstance(serviceName, buildZkInstance(8007)));
originInstances.add(new ZookeeperServiceInstance(serviceName, buildZkInstance(8008)));
allInstances.addAll(originInstances);
allInstances.addAll(scInstances);
final ExecuteContext context = ExecuteContext.forMemberMethod(this, String.class.getDeclaredMethod("trim"),
new Object[]{allInstances}, null, null);
context.changeResult(originInstances);
return context;
}

/**
* 构建实例
*
Expand All @@ -111,9 +139,10 @@ private ExecuteContext buildContext() throws NoSuchMethodException {
public org.apache.curator.x.discovery.ServiceInstance buildZkInstance(int port) {
final org.apache.curator.x.discovery.ServiceInstance serviceInstance = Mockito
.mock(org.apache.curator.x.discovery.ServiceInstance.class);
Mockito.when(serviceInstance.getSslPort()).thenReturn(port);
Mockito.when(serviceInstance.getPort()).thenReturn(port);
Mockito.when(serviceInstance.getAddress()).thenReturn("localhost");
Mockito.when(serviceInstance.buildUriSpec()).thenReturn("http://localhost:" + port);
Mockito.when(serviceInstance.getAddress()).thenReturn("127.0.0.1");
Mockito.when(serviceInstance.buildUriSpec()).thenReturn("http://127.0.0.1:" + port);
return serviceInstance;
}

Expand Down

0 comments on commit b360faf

Please sign in to comment.