From 410a24197f1663576058442fd19faf2d528f9e3b Mon Sep 17 00:00:00 2001 From: TLPC <470193496@qq.com> Date: Mon, 31 Jul 2023 17:46:19 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90fix=E3=80=91=E4=BC=98=E5=8C=96springcl?= =?UTF-8?q?oud=E5=8F=8C=E6=B3=A8=E5=86=8C=E5=9C=BA=E6=99=AF=EF=BC=8CDiscov?= =?UTF-8?q?eryClientInterceptor/DiscoveryClientServiceInterceptor=E7=9A=84?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DiscoveryClientInterceptor.java | 88 ++++++++++--------- .../DiscoveryClientServiceInterceptor.java | 51 +++++------ .../DynamicServerListInterceptor.java | 5 +- .../support/InstanceInterceptorSupport.java | 35 +------- .../interceptors/BaseRegistryTest.java | 21 ++++- .../DiscoveryClientInterceptorTest.java | 60 ++++++++++--- ...DiscoveryClientServiceInterceptorTest.java | 18 ++-- 7 files changed, 153 insertions(+), 125 deletions(-) diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DiscoveryClientInterceptor.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DiscoveryClientInterceptor.java index d67ce75b82..68836791f8 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DiscoveryClientInterceptor.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DiscoveryClientInterceptor.java @@ -23,23 +23,18 @@ import com.huawei.registry.support.InstanceInterceptorSupport; import com.huawei.registry.utils.HostUtils; -import com.huaweicloud.sermant.core.common.LoggerFactory; import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; -import com.huaweicloud.sermant.core.service.ServiceManager; import reactor.core.publisher.Flux; import org.springframework.cloud.client.ServiceInstance; -import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient; import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.util.Locale; import java.util.Objects; -import java.util.logging.Logger; import java.util.stream.Collectors; /** @@ -49,41 +44,56 @@ * @since 2021-12-13 */ public class DiscoveryClientInterceptor extends InstanceInterceptorSupport { - private static final Logger LOGGER = LoggerFactory.getLogger(); + private static final String SERVICE_ID = "serviceId"; + + private static final String MICRO_SERVICE_INSTANCES = "microServiceInstances"; @Override public ExecuteContext doBefore(ExecuteContext context) { - if (isMarked()) { + String serviceId = (String) context.getArguments()[0]; + final RegisterCenterService service = PluginServiceManager.getPluginService(RegisterCenterService.class); + final List microServiceInstances = service.getServerList(serviceId); + if (microServiceInstances.isEmpty()) { return context; } - try { - mark(); - String serviceId = (String) context.getArguments()[0]; - final RegisterCenterService service = PluginServiceManager.getPluginService(RegisterCenterService.class); - final List microServiceInstances = service.getServerList(serviceId); + context.setLocalFieldValue(SERVICE_ID, serviceId); + context.setLocalFieldValue(MICRO_SERVICE_INSTANCES, microServiceInstances); + if (RegisterContext.INSTANCE.isAvailable() + && !RegisterDynamicConfig.INSTANCE.isNeedCloseOriginRegisterCenter()) { + return context; + } + final Object target = context.getObject(); + context.skip(isWebfLux(target) ? Flux.fromIterable(Collections.emptyList()) + : Collections.emptyList()); + return context; + } + + @Override + public ExecuteContext doAfter(ExecuteContext context) { + final String serviceId = (String) context.getLocalFieldValue(SERVICE_ID); + final List microServiceInstances = + (List) context.getLocalFieldValue(MICRO_SERVICE_INSTANCES); + if (microServiceInstances != null && !microServiceInstances.isEmpty()) { final Object target = context.getObject(); - if (!microServiceInstances.isEmpty()) { - context.skip(isWebfLux(target) ? convertAndMergeWithFlux(microServiceInstances, serviceId, target) - : convertAndMerge(microServiceInstances, serviceId, target)); - } - } finally { - unMark(); + final Object contextResult = context.getResult(); + context.changeResult( + isWebfLux(target) ? convertAndMergeWithFlux(microServiceInstances, serviceId, target, contextResult) + : convertAndMerge(microServiceInstances, serviceId, target, contextResult)); } return context; } - private Flux convertAndMergeWithFlux(List microServiceInstances, - String serviceId, Object target) { - return Flux.fromIterable(convertAndMerge(microServiceInstances, serviceId, target)); + private Flux convertAndMergeWithFlux( + List microServiceInstances, + String serviceId, Object target, Object contextResult) { + return Flux.fromIterable(convertAndMerge(microServiceInstances, serviceId, target, contextResult)); } - private List convertAndMerge(List microServiceInstances, String serviceId, - Object target) { + private List convertAndMerge(List microServiceInstances, + String serviceId, + Object target, Object contextResult) { List result = new ArrayList<>(microServiceInstances.size()); - if (RegisterContext.INSTANCE.isAvailable() - && !RegisterDynamicConfig.INSTANCE.isNeedCloseOriginRegisterCenter()) { - result.addAll(queryOriginInstances(target, serviceId)); - } + result.addAll(queryOriginInstances(target, contextResult)); for (MicroServiceInstance microServiceInstance : microServiceInstances) { result.removeIf(originServiceInstance -> HostUtils.isSameInstance(originServiceInstance.getHost(), originServiceInstance.getPort(), @@ -94,21 +104,15 @@ private List convertAndMerge(List microSe return result.stream().filter(Objects::nonNull).collect(Collectors.toList()); } - private List queryOriginInstances(Object target, String serviceId) { - try { - if (target instanceof CompositeDiscoveryClient) { - final CompositeDiscoveryClient discoveryClient = (CompositeDiscoveryClient) target; - return discoveryClient.getInstances(serviceId); - } - if (isWebfLux(target)) { - ReactiveDiscoveryClient reactiveDiscoveryClient = (ReactiveDiscoveryClient) target; - final Flux instances = reactiveDiscoveryClient.getInstances(serviceId); - return instances.collectList().block(); - } - } catch (Exception exception) { - LOGGER.warning(String.format(Locale.ENGLISH, - "Query Instances from origin register center failed, may be it is not available! reason: %s", - exception.getMessage())); + private List queryOriginInstances(Object target, Object contextResult) { + if (target instanceof CompositeDiscoveryClient) { + return contextResult == null ? Collections.emptyList() : (List) contextResult; + } + if (isWebfLux(target)) { + List resultList = new ArrayList<>(); + final Flux instances = (Flux) contextResult; + instances.collectList().subscribe(resultList::addAll); + return resultList; } return Collections.emptyList(); } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DiscoveryClientServiceInterceptor.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DiscoveryClientServiceInterceptor.java index cce454700b..d89fcaac32 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DiscoveryClientServiceInterceptor.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DiscoveryClientServiceInterceptor.java @@ -23,14 +23,11 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; -import com.huaweicloud.sermant.core.service.ServiceManager; import reactor.core.publisher.Flux; -import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient; - import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.stream.Collectors; @@ -43,43 +40,43 @@ public class DiscoveryClientServiceInterceptor extends InstanceInterceptorSupport { @Override public ExecuteContext doBefore(ExecuteContext context) { - if (isMarked()) { + if (RegisterContext.INSTANCE.isAvailable() + && !RegisterDynamicConfig.INSTANCE.isNeedCloseOriginRegisterCenter()) { return context; } - try { - mark(); - final RegisterCenterService service = PluginServiceManager.getPluginService(RegisterCenterService.class); - final List services = new ArrayList<>(service.getServices()); - final Object target = context.getObject(); - context.skip(isWebfLux(target) ? getServicesWithFlux(services, target) : getServices(services, target)); - } finally { - unMark(); - } + final Object target = context.getObject(); + context.skip(isWebfLux(target) ? Flux.fromIterable(Collections.emptyList()) : Collections.emptyList()); return context; } - private Flux getServicesWithFlux(List services, Object target) { - return Flux.fromIterable(getServices(services, target)); + @Override + public ExecuteContext doAfter(ExecuteContext context) { + final RegisterCenterService service = PluginServiceManager.getPluginService(RegisterCenterService.class); + final List services = new ArrayList<>(service.getServices()); + final Object target = context.getObject(); + final Object contextResult = context.getResult(); + context.changeResult( + isWebfLux(target) ? getServicesWithFlux(services, target, contextResult) : getServices(services, + target, contextResult)); + return context; } - private List getServices(List services, Object target) { - if (!RegisterContext.INSTANCE.isAvailable() - || RegisterDynamicConfig.INSTANCE.isNeedCloseOriginRegisterCenter()) { - return services; - } + private Flux getServicesWithFlux(List services, Object target, Object contextResult) { + return Flux.fromIterable(getServices(services, target, contextResult)); + } + private List getServices(List services, Object target, Object contextResult) { // 合并两个注册中心 if (isWebfLux(target)) { - ReactiveDiscoveryClient reactiveDiscoveryClient = (ReactiveDiscoveryClient) target; - final Flux originServicesFlux = reactiveDiscoveryClient.getServices(); - final List originServices = originServicesFlux.collectList().block(); - if (originServices == null) { + final Flux originServicesFlux = (Flux) contextResult; + final List originServices = new ArrayList<>(); + originServicesFlux.collectList().subscribe(originServices::addAll); + if (originServices.size() == 0) { return services; } services.addAll(originServices); } else { - final DiscoveryClient discoveryClient = (DiscoveryClient) target; - services.addAll(discoveryClient.getServices()); + services.addAll((List) contextResult); } return services.stream().distinct().collect(Collectors.toList()); } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DynamicServerListInterceptor.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DynamicServerListInterceptor.java index ae19249f67..ea97081e2b 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DynamicServerListInterceptor.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/interceptors/DynamicServerListInterceptor.java @@ -26,7 +26,6 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; -import com.huaweicloud.sermant.core.service.ServiceManager; import com.huaweicloud.sermant.core.utils.ReflectUtils; import com.netflix.loadbalancer.DynamicServerListLoadBalancer; @@ -69,8 +68,8 @@ private List convertAndMerge(DynamicServerListLoadBalancer serve } for (MicroServiceInstance microServiceInstance : microServiceInstances) { result.removeIf(originServiceInstance -> - HostUtils.isSameInstance(originServiceInstance.getHost(), originServiceInstance.getPort(), - microServiceInstance.getHost(), microServiceInstance.getPort())); + HostUtils.isSameInstance(originServiceInstance.getHost(), originServiceInstance.getPort(), + microServiceInstance.getHost(), microServiceInstance.getPort())); result.add(new ScServer(microServiceInstance, serverListLoadBalancer.getName())); } return result; diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/support/InstanceInterceptorSupport.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/support/InstanceInterceptorSupport.java index ecf78cad4d..57fb393dfa 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/support/InstanceInterceptorSupport.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/main/java/com/huawei/registry/support/InstanceInterceptorSupport.java @@ -38,8 +38,6 @@ * @since 2022-02-22 */ public abstract class InstanceInterceptorSupport extends RegisterSwitchSupport { - private final ThreadLocal threadLocal = new ThreadLocal<>(); - /** * 类缓存, 避免多次调用loadClass */ @@ -47,31 +45,6 @@ public abstract class InstanceInterceptorSupport extends RegisterSwitchSupport { private RegisterConfig config; - /** - * 标记当前线程方法调用 - *

- * 默认标记, 确保下游调用不会存在再次mark的场景 - */ - protected final void mark() { - threadLocal.set(Boolean.TRUE); - } - - /** - * 默认去除标记 - */ - protected final void unMark() { - threadLocal.remove(); - } - - /** - * 判断是否被标记 - * - * @return 是否被标记 - */ - protected final boolean isMarked() { - return threadLocal.get() != null; - } - /** * 是否开启注册中心迁移,双注册 * @@ -100,7 +73,7 @@ protected final Class getInstanceClass(String className) { Class result = null; try { result = ClassLoaderUtils.defineClass(className, contextClassLoader, - ClassLoaderUtils.getClassResource(this.getClass().getClassLoader(), className)); + ClassLoaderUtils.getClassResource(this.getClass().getClassLoader(), className)); } catch (InvocationTargetException | IllegalAccessException | NoSuchMethodException | IOException e) { // 有可能已经加载过了,直接用contextClassLoader.loadClass加载 try { @@ -117,17 +90,17 @@ protected final Class getInstanceClass(String className) { * 构建实例 由子类自行转换 * * @param microServiceInstance 实例信息 - * @param serviceName 服务名 + * @param serviceName 服务名 * @return Object */ protected final Optional buildInstance(MicroServiceInstance microServiceInstance, String serviceName) { final Class serverClass = getInstanceClass(getInstanceClassName()); try { Constructor declaredConstructor = serverClass - .getDeclaredConstructor(MicroServiceInstance.class, String.class); + .getDeclaredConstructor(MicroServiceInstance.class, String.class); return Optional.of(declaredConstructor.newInstance(microServiceInstance, serviceName)); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException - | InvocationTargetException ignored) { + | InvocationTargetException ignored) { return Optional.empty(); } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/BaseRegistryTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/BaseRegistryTest.java index db2b4bb0c6..5b7aff7728 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/BaseRegistryTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/BaseRegistryTest.java @@ -64,7 +64,6 @@ public static void init() { RegisterServiceCommonConfig.class)).thenReturn(COMMON_CONFIG); pluginServiceManagerMockedStatic = Mockito.mockStatic(PluginServiceManager.class); serviceManagerMockedStatic = Mockito.mockStatic(ServiceManager.class); - } @AfterClass @@ -79,7 +78,6 @@ public static void clear() { } /** - * * @return 测试拦截器 */ protected abstract T getInterceptor(); @@ -98,7 +96,7 @@ protected ExecuteContext buildContext() throws NoSuchMethodException { * 构建基本的context * * @param arguments 参数 - * @param target 对象 + * @param target 对象 * @return context * @throws NoSuchMethodException 不会抛出 */ @@ -106,4 +104,21 @@ protected ExecuteContext buildContext(Object target, Object[] arguments) throws return ExecuteContext.forMemberMethod(target, String.class.getDeclaredMethod("trim"), arguments, null, null); } + + /** + * 构建基本的context + * + * @param arguments 参数 + * @param target 对象 + * @param result ExecuteContext的result + * @return context + * @throws NoSuchMethodException 不会抛出 + */ + protected ExecuteContext buildContext(Object target, Object[] arguments, Object result) + throws NoSuchMethodException { + ExecuteContext context = ExecuteContext.forMemberMethod(target, String.class.getDeclaredMethod("trim"), + arguments, null, null); + context.changeResult(result); + return context; + } } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientInterceptorTest.java index 988a8b42cd..51b07860a4 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientInterceptorTest.java @@ -25,6 +25,7 @@ import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.service.PluginServiceManager; import com.huaweicloud.sermant.core.service.ServiceManager; +import com.huaweicloud.sermant.core.utils.CollectionUtils; import org.junit.Assert; import org.junit.Before; @@ -36,9 +37,12 @@ import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient; import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClient; import org.springframework.cloud.zookeeper.discovery.ZookeeperServiceInstance; + import reactor.core.publisher.Flux; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.List; /** @@ -49,6 +53,7 @@ */ public class DiscoveryClientInterceptorTest extends BaseRegistryTest { private final List instanceList = new ArrayList<>(); + private final List zkInstanceList = new ArrayList<>(); private final String serviceName = "test"; @@ -72,29 +77,60 @@ public void setUp() throws Exception { instanceList.add(test.buildInstance(8002)); zkInstanceList.add(new ZookeeperServiceInstance(serviceName, test.buildZkInstance(8003))); zkInstanceList.add(new ZookeeperServiceInstance(serviceName, test.buildZkInstance(8004))); - Mockito.when(registerCenterService.getServerList(serviceName)).thenReturn(instanceList); - Mockito.when(client.getInstances(serviceName)).thenReturn(zkInstanceList); - Mockito.when(reactiveDiscoveryClient.getInstances(serviceName)).thenReturn(Flux.fromIterable(zkInstanceList)); + zkInstanceList.add(new ZookeeperServiceInstance(serviceName, test.buildZkInstance(8005))); } @Test public void doBefore() throws NoSuchMethodException { + Mockito.when(registerCenterService.getServerList(serviceName)).thenReturn(instanceList); + + // isEmpty为false,isAvailable为true的普通场景 RegisterContext.INSTANCE.setAvailable(true); REGISTER_CONFIG.setEnableSpringRegister(true); REGISTER_CONFIG.setOpenMigration(true); RegisterDynamicConfig.INSTANCE.setClose(false); - final ExecuteContext context = interceptor.doBefore(buildContext(client, new Object[]{serviceName})); - Assert.assertTrue(context.isSkip()); - Assert.assertTrue(context.getResult() instanceof List); - Assert.assertEquals(((List) context.getResult()).size(), zkInstanceList.size() + instanceList.size()); - - final ExecuteContext fluxContext = interceptor.doBefore(buildContext(reactiveDiscoveryClient, new Object[]{serviceName})); - Assert.assertTrue(fluxContext.isSkip()); - Assert.assertTrue(fluxContext.getResult() instanceof Flux); - final List block = ((Flux) fluxContext.getResult()).collectList().block(); + final ExecuteContext context = interceptor.doBefore( + buildContext(client, new Object[]{serviceName}, zkInstanceList)); + final ExecuteContext contextResult = interceptor.doAfter(context); + Assert.assertTrue(contextResult.getResult() instanceof List); + Assert.assertEquals(((List) contextResult.getResult()).size(), zkInstanceList.size() + instanceList.size()); + + // isEmpty为false,isAvailable为true的isWebfLux场景 + final ExecuteContext fluxContext = interceptor.doBefore(buildContext(reactiveDiscoveryClient, + new Object[]{serviceName}, Flux.fromIterable(zkInstanceList))); + final ExecuteContext fluxContextResult = interceptor.doAfter(fluxContext); + Assert.assertTrue(fluxContextResult.getResult() instanceof Flux); + final List block = ((Flux) fluxContextResult.getResult()).collectList().block(); Assert.assertNotNull(block); Assert.assertEquals(block.size(), zkInstanceList.size() + instanceList.size()); + + // isEmpty为false,isAvailable为false场景 RegisterContext.INSTANCE.setAvailable(false); + final ExecuteContext notAvailableContext = interceptor.doBefore( + buildContext(client, new Object[]{serviceName}, zkInstanceList)); + Assert.assertTrue(notAvailableContext.isSkip()); + Assert.assertTrue(CollectionUtils.isEmpty((Collection) notAvailableContext.getResult())); + final ExecuteContext notAvailableContextResult = interceptor.doAfter(notAvailableContext); + Assert.assertTrue(notAvailableContextResult.getResult() instanceof List); + Assert.assertEquals(((List) notAvailableContextResult.getResult()).size(), instanceList.size()); + + // isEmpty为true,isAvailable为true场景 + RegisterContext.INSTANCE.setAvailable(true); + Mockito.when(registerCenterService.getServerList(serviceName)).thenReturn(Collections.emptyList()); + final ExecuteContext contextWithEmptyList = interceptor.doBefore( + buildContext(client, new Object[]{serviceName}, zkInstanceList)); + final ExecuteContext contextWithEmptyListResult = interceptor.doAfter(contextWithEmptyList); + Assert.assertTrue(contextWithEmptyListResult.getResult() instanceof List); + Assert.assertEquals(((List) contextWithEmptyListResult.getResult()).size(), zkInstanceList.size()); + + // isEmpty为true,isAvailable为false场景 + Mockito.when(registerCenterService.getServerList(serviceName)).thenReturn(Collections.emptyList()); + final ExecuteContext contextWithEmptyListx = interceptor.doBefore( + buildContext(client, new Object[]{serviceName}, zkInstanceList)); + final ExecuteContext contextWithEmptyListResultx = interceptor.doAfter(contextWithEmptyListx); + Assert.assertTrue(contextWithEmptyListResultx.getResult() instanceof List); + Assert.assertEquals(((List) contextWithEmptyListResultx.getResult()).size(), zkInstanceList.size()); + REGISTER_CONFIG.setEnableSpringRegister(false); REGISTER_CONFIG.setOpenMigration(false); } diff --git a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientServiceInterceptorTest.java b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientServiceInterceptorTest.java index 1fcd79451e..865c275d43 100644 --- a/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientServiceInterceptorTest.java +++ b/sermant-plugins/sermant-service-registry/spring-cloud-registry-plugin/src/test/java/com/huawei/registry/interceptors/DiscoveryClientServiceInterceptorTest.java @@ -34,6 +34,7 @@ import org.mockito.MockitoAnnotations; import org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient; import org.springframework.cloud.client.discovery.composite.reactive.ReactiveCompositeDiscoveryClient; + import reactor.core.publisher.Flux; import java.util.ArrayList; @@ -67,8 +68,6 @@ public void setUp() throws Exception { services.add("test1"); services.add("test2"); Mockito.when(registerCenterService.getServices()).thenReturn(services); - Mockito.when(client.getServices()).thenReturn(originServices); - Mockito.when(reactiveCompositeDiscoveryClient.getServices()).thenReturn(Flux.fromIterable(originServices)); } @After @@ -82,22 +81,27 @@ protected DiscoveryClientServiceInterceptor getInterceptor() { @Test public void doBefore() throws NoSuchMethodException { + // isAvailable为true的普通场景 RegisterContext.INSTANCE.setAvailable(true); REGISTER_CONFIG.setEnableSpringRegister(true); REGISTER_CONFIG.setOpenMigration(true); - final ExecuteContext context = interceptor.doBefore(buildContext(client, null)); - Assert.assertTrue(context.isSkip()); + final ExecuteContext context = interceptor.doAfter(buildContext(client, null, originServices)); Assert.assertTrue(context.getResult() instanceof List); Assert.assertEquals(((List) context.getResult()).size(), originServices.size() + services.size()); - // 测试flux - final ExecuteContext fluxContext = interceptor.doBefore(buildContext(reactiveCompositeDiscoveryClient, null)); - Assert.assertTrue(fluxContext.isSkip()); + // isAvailable为true的isWebfLux场景 + final ExecuteContext fluxContext = interceptor.doAfter( + buildContext(reactiveCompositeDiscoveryClient, null, Flux.fromIterable(originServices))); Assert.assertTrue(fluxContext.getResult() instanceof Flux); final List block = ((Flux) fluxContext.getResult()).collectList().block(); Assert.assertNotNull(block); Assert.assertEquals(block.size(), originServices.size() + services.size()); + + // isAvailable为false的普通场景 RegisterContext.INSTANCE.setAvailable(false); + final ExecuteContext NotAvailableContext = interceptor.doBefore( + buildContext(client, null)); + Assert.assertTrue(NotAvailableContext.isSkip()); REGISTER_CONFIG.setEnableSpringRegister(false); REGISTER_CONFIG.setOpenMigration(false); }