Fix/fix local springmvc client cant register success (#12)
* fix: the variable 'isDiscoveryLocalMode' always false in AbstractontextRefreshedEventListener cause local springmvc client can't register success
* fix: fix test and checkStyle
---------
Co-authored-by: Liming Deng <liming.d.pro@gmail.com>
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 @@
private ApplicationContext context;
- private final Boolean isDiscoveryLocalMode;
-
/**
* multiple namespace support.
*/
@@ -139,7 +137,6 @@
this.ipAndPort = props.getProperty(ShenyuClientConstants.IP_PORT);
this.host = props.getProperty(ShenyuClientConstants.HOST);
this.port = props.getProperty(ShenyuClientConstants.PORT);
- this.isDiscoveryLocalMode = Boolean.valueOf(props.getProperty(ShenyuClientConstants.DISCOVERY_LOCAL_MODE_KEY));
publisher.start(shenyuClientRegisterRepository);
}
@@ -153,6 +150,9 @@
if (!registered.compareAndSet(false, true)) {
return;
}
+ String discoveryMode = context.getEnvironment()
+ .getProperty("shenyu.discovery.type", ShenyuClientConstants.DISCOVERY_LOCAL_MODE);
+ boolean isDiscoveryLocalMode = ShenyuClientConstants.DISCOVERY_LOCAL_MODE.equals(discoveryMode);
if (isDiscoveryLocalMode) {
List<String> namespaceIds = this.getNamespace();
namespaceIds.forEach(namespaceId -> publisher.publishEvent(buildURIRegisterDTO(context, beans, namespaceId)));
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 @@
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 @@
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 @@
@Mock
private ApplicationContext applicationContext;
+ @Mock
+ private Environment env;
+
private ContextRefreshedEvent contextRefreshedEvent;
@BeforeEach
@@ -77,6 +82,8 @@
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);