From b9d0304de194100b60f6f6a20ef0190f5cdb37b6 Mon Sep 17 00:00:00 2001 From: hengyu_laptop <1183660933@qq.com> Date: Wed, 24 Jun 2026 16:07:37 +0800 Subject: [PATCH 1/2] fix: the variable 'isDiscoveryLocalMode' always false in AbstractontextRefreshedEventListener cause local springmvc client can't register success --- .../core/client/AbstractContextRefreshedEventListener.java | 6 +++--- .../springmvc/ShenyuSpringMvcClientConfiguration.java | 2 -- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java b/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java index 0d9ac05..063ac52 100644 --- a/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java +++ b/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java @@ -103,8 +103,6 @@ public abstract class AbstractContextRefreshedEventListener namespaceIds = this.getNamespace(); namespaceIds.forEach(namespaceId -> publisher.publishEvent(buildURIRegisterDTO(context, beans, namespaceId))); diff --git a/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java b/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java index 187eab5..d6d3293 100644 --- a/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java +++ b/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java @@ -70,7 +70,6 @@ public SpringMvcClientEventListener springHttpClientEventListener(final ShenyuCl ClientPropertiesConfig clientPropertiesConfig = clientConfig.getClient().get(RpcTypeEnum.HTTP.getName()); Properties props = Optional.ofNullable(clientPropertiesConfig).map(ClientPropertiesConfig::getProps).orElse(null); String applicationName = env.getProperty("spring.application.name"); - String discoveryMode = env.getProperty("shenyu.discovery.type", ShenyuClientConstants.DISCOVERY_LOCAL_MODE); if (Objects.nonNull(props)) { String appName = props.getProperty(ShenyuClientConstants.APP_NAME); if (StringUtils.isBlank(appName) && StringUtils.isBlank(applicationName)) { @@ -83,7 +82,6 @@ public SpringMvcClientEventListener springHttpClientEventListener(final ShenyuCl if (StringUtils.isBlank(contextPath)) { props.setProperty(ShenyuClientConstants.CONTEXT_PATH, String.format("/%s", applicationName)); } - props.setProperty(ShenyuClientConstants.DISCOVERY_LOCAL_MODE_KEY, Boolean.valueOf(ShenyuClientConstants.DISCOVERY_LOCAL_MODE.equals(discoveryMode)).toString()); } return new SpringMvcClientEventListener(clientConfig, shenyuClientRegisterRepository, env); } From b833b79b63d8b35d97e815b80c6cb2b1bf497267 Mon Sep 17 00:00:00 2001 From: hengyuss <1183660933@qq.com> Date: Wed, 24 Jun 2026 17:10:12 +0800 Subject: [PATCH 2/2] fix: fix test and checkStyle --- .../springmvc/init/SpringMvcClientEventListenerTest.java | 6 ++++-- .../client/tars/TarsServiceBeanPostProcessorTest.java | 7 +++++++ .../springmvc/ShenyuSpringMvcClientConfiguration.java | 2 ++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java b/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java index fa6aa47..7a2e1a4 100644 --- a/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java +++ b/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/init/SpringMvcClientEventListenerTest.java @@ -93,6 +93,8 @@ private void init() { results.put("springMvcClientTestBean3", springMvcClientTestBean3); results.put("springMvcClientTestBean4", springMvcClientTestBean4); when(applicationContext.getBeansWithAnnotation(any())).thenReturn(results); + when(applicationContext.getEnvironment()).thenReturn(env); + when(env.getProperty("shenyu.discovery.type", ShenyuClientConstants.DISCOVERY_LOCAL_MODE)).thenReturn("local"); contextRefreshedEvent = new ContextRefreshedEvent(applicationContext); ShenyuClientConfig shenyuClientConfig = mock(ShenyuClientConfig.class); Assert.assertThrows(ShenyuClientIllegalArgumentException.class, () -> new SpringMvcClientEventListener(shenyuClientConfig, mock(ShenyuClientRegisterRepository.class), env)); @@ -182,12 +184,12 @@ public void testOnBuildApiSuperPath() { SpringMvcClientEventListener springMvcClientEventListener = buildSpringMvcClientEventListener(false, false); Assertions.assertEquals("/order", springMvcClientEventListener.buildApiSuperPath( - SpringMvcClientTestBean.class, AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, ShenyuSpringMvcClient.class)), "super-path"); + SpringMvcClientTestBean.class, AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, ShenyuSpringMvcClient.class)), "super-path"); when(env.getProperty("spring.mvc.servlet.path")).thenReturn("/servlet-path"); when(env.getProperty("server.servlet.context-path")).thenReturn("/servlet-context-path"); Assertions.assertEquals("/servlet-context-path/servlet-path/order", springMvcClientEventListener.buildApiSuperPath( - SpringMvcClientTestBean.class, AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, ShenyuSpringMvcClient.class)), "super-path"); + SpringMvcClientTestBean.class, AnnotatedElementUtils.findMergedAnnotation(SpringMvcClientTestBean.class, ShenyuSpringMvcClient.class)), "super-path"); registerUtilsMockedStatic.close(); } diff --git a/shenyu-client-tars/src/test/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessorTest.java b/shenyu-client-tars/src/test/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessorTest.java index a81395e..e50e6a1 100644 --- a/shenyu-client-tars/src/test/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessorTest.java +++ b/shenyu-client-tars/src/test/java/org/apache/shenyu/client/tars/TarsServiceBeanPostProcessorTest.java @@ -17,6 +17,7 @@ package org.apache.shenyu.client.tars; +import org.apache.shenyu.client.core.constant.ShenyuClientConstants; import org.apache.shenyu.client.core.enums.RpcTypeEnum; import org.apache.shenyu.client.core.exception.ShenyuClientIllegalArgumentException; import org.apache.shenyu.client.core.register.ShenyuClientRegisterRepository; @@ -38,6 +39,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.context.ApplicationContext; import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.core.env.Environment; import java.util.LinkedHashMap; import java.util.Map; @@ -68,6 +70,9 @@ public final class TarsServiceBeanPostProcessorTest { @Mock private ApplicationContext applicationContext; + @Mock + private Environment env; + private ContextRefreshedEvent contextRefreshedEvent; @BeforeEach @@ -77,6 +82,8 @@ public void init() { results.put("tarsDemoService2", tarsDemoService2); results.put("tarsDemoService3", tarsDemoService3); when(applicationContext.getBeansWithAnnotation(any())).thenReturn(results); + when(applicationContext.getEnvironment()).thenReturn(env); + when(env.getProperty("shenyu.discovery.type", ShenyuClientConstants.DISCOVERY_LOCAL_MODE)).thenReturn("local"); contextRefreshedEvent = new ContextRefreshedEvent(applicationContext); ShenyuClientConfig shenyuClientConfig = mock(ShenyuClientConfig.class); diff --git a/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java b/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java index d6d3293..187eab5 100644 --- a/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java +++ b/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-springmvc/src/main/java/org/apache/shenyu/springboot/starter/client/springmvc/ShenyuSpringMvcClientConfiguration.java @@ -70,6 +70,7 @@ public SpringMvcClientEventListener springHttpClientEventListener(final ShenyuCl ClientPropertiesConfig clientPropertiesConfig = clientConfig.getClient().get(RpcTypeEnum.HTTP.getName()); Properties props = Optional.ofNullable(clientPropertiesConfig).map(ClientPropertiesConfig::getProps).orElse(null); String applicationName = env.getProperty("spring.application.name"); + String discoveryMode = env.getProperty("shenyu.discovery.type", ShenyuClientConstants.DISCOVERY_LOCAL_MODE); if (Objects.nonNull(props)) { String appName = props.getProperty(ShenyuClientConstants.APP_NAME); if (StringUtils.isBlank(appName) && StringUtils.isBlank(applicationName)) { @@ -82,6 +83,7 @@ public SpringMvcClientEventListener springHttpClientEventListener(final ShenyuCl if (StringUtils.isBlank(contextPath)) { props.setProperty(ShenyuClientConstants.CONTEXT_PATH, String.format("/%s", applicationName)); } + props.setProperty(ShenyuClientConstants.DISCOVERY_LOCAL_MODE_KEY, Boolean.valueOf(ShenyuClientConstants.DISCOVERY_LOCAL_MODE.equals(discoveryMode)).toString()); } return new SpringMvcClientEventListener(clientConfig, shenyuClientRegisterRepository, env); }