From a2b158d67d8dca368370e9d1a348cf5d2fbd21ce Mon Sep 17 00:00:00 2001 From: flystar32 Date: Mon, 29 Oct 2018 23:01:30 +0800 Subject: [PATCH] put all nacos config metadata to bootstrap --- .../nacos-config-example/readme-zh.md | 9 +- .../nacos-config-example/readme.md | 9 +- .../alibaba/cloud/examples/Application.java | 40 +++---- .../src/main/resources/application.properties | 1 - .../src/main/resources/bootstrap.properties | 1 + .../nacos/NacosConfigAutoConfiguration.java | 16 +-- .../NacosConfigBootstrapConfiguration.java | 2 + .../alibaba/nacos/NacosConfigProperties.java | 105 +++++++++++------ .../client/NacosPropertySourceBuilder.java | 1 - .../client/NacosPropertySourceLocator.java | 56 ++------- .../NacosConfigEndpointAutoConfiguration.java | 12 +- .../NacosConfigAutoConfigurationTests.java | 111 ++++++++---------- ...acosConfigBootstrapConfigurationTests.java | 11 +- 13 files changed, 169 insertions(+), 205 deletions(-) diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md index b9ec18f02..e7339058c 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md @@ -19,9 +19,10 @@ spring-cloud-starter-alibaba-nacos-config -2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 地址 +2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 元数据 - spring.cloud.nacos.config.server-addr=127.0.0.1:8848 + spring.application.name=nacos-config-example + spring.cloud.nacos.config.server-addr=127.0.0.1:8848 3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能 @@ -70,8 +71,8 @@ 1. 增加配置,在应用的 /src/main/resources/application.properties 中添加基本配置信息 - spring.application.name=nacos-config-example - server.port=18084 + server.port=18084 + management.endpoints.web.exposure.include=* 2. 启动应用,支持 IDE 直接启动和编译打包后启动。 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md index a2f8f3e8f..58b499aef 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md @@ -19,9 +19,10 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl spring-cloud-starter-alibaba-nacos-config -2. Add Nacos server address configurations to file /src/main/resources/bootstrap.properties +2. Add Nacos config metadata configurations to file /src/main/resources/bootstrap.properties - spring.cloud.nacos.config.server-addr=127.0.0.1:8848 + spring.application.name=nacos-config-example + spring.cloud.nacos.config.server-addr=127.0.0.1:8848 3. After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh . @RefreshScope @@ -70,8 +71,8 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl 1. Add necessary configurations to file /src/main/resources/application.properties - spring.application.name=nacos-config-example - server.port=18084 + server.port=18084 + management.endpoints.web.exposure.include=* 2. Start the application in IDE or by building a fatjar. diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/Application.java b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/Application.java index b11681077..1818a2e02 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/Application.java +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/Application.java @@ -16,39 +16,39 @@ import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class Application { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } } @Component class SampleRunner implements ApplicationRunner { - @Value("${user.name}") - String userName; + @Value("${user.name}") + String userName; - @Value("${user.age}") - int userAge; + @Value("${user.age}") + int userAge; - @Override - public void run(ApplicationArguments args) throws Exception { - System.out.println(userName); - System.out.println(userAge); - } + @Override + public void run(ApplicationArguments args) throws Exception { + System.out.println(userName); + System.out.println(userAge); + } } @RestController @RefreshScope class SampleController { - @Value("${user.name}") - String userName; + @Value("${user.name}") + String userName; - @Value("${user.age}") - int age; + @Value("${user.age}") + int age; - @RequestMapping("/user") - public String simple() { - return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!"; - } + @RequestMapping("/user") + public String simple() { + return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!"; + } } \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/application.properties index a0e934e2a..c6e216ad7 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/application.properties @@ -1,3 +1,2 @@ -spring.application.name=nacos-config-example server.port=18084 management.endpoints.web.exposure.include=* \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties index 8d94d562d..6adb653ca 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties @@ -1 +1,2 @@ +spring.application.name=nacos-config-example spring.cloud.nacos.config.server-addr=127.0.0.1:8848 \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java index f08969121..5dbdb50af 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.cloud.alibaba.nacos; -import com.alibaba.nacos.api.config.ConfigService; - import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher; @@ -43,14 +41,6 @@ public class NacosConfigAutoConfiguration implements ApplicationContextAware { @Autowired private NacosRefreshProperties nacosRefreshProperties; - @Autowired - private ConfigService configService; - - @Bean - public NacosConfigProperties nacosConfigProperties() { - return new NacosConfigProperties(); - } - @Bean public NacosPropertySourceRepository nacosPropertySourceRepository() { return new NacosPropertySourceRepository(applicationContext); @@ -69,10 +59,10 @@ public class NacosConfigAutoConfiguration implements ApplicationContextAware { @Bean public NacosContextRefresher nacosContextRefresher(ContextRefresher contextRefresher, NacosRefreshHistory refreshHistory, - NacosPropertySourceRepository propertySourceRepository, - ConfigService configService) { + NacosPropertySourceRepository propertySourceRepository) { return new NacosContextRefresher(contextRefresher, nacosConfigProperties, - nacosRefreshProperties, refreshHistory, propertySourceRepository,configService); + nacosRefreshProperties, refreshHistory, propertySourceRepository, + nacosConfigProperties.configServiceInstance()); } @Override diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java index d5e3bd3b8..e643d35a8 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.cloud.alibaba.nacos; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -32,6 +33,7 @@ public class NacosConfigBootstrapConfiguration { } @Bean + @ConditionalOnMissingBean public NacosConfigProperties nacosConfigProperties() { return new NacosConfigProperties(); } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java index 446483d69..e279d9cbe 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java @@ -16,9 +16,30 @@ package org.springframework.cloud.alibaba.nacos; +import java.util.Arrays; +import java.util.Objects; +import java.util.Properties; + +import javax.annotation.PostConstruct; + +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.config.ConfigService; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.env.Environment; -import org.springframework.util.StringUtils; + +import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY; +import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME; +import static com.alibaba.nacos.api.PropertyKeyConst.CONTEXT_PATH; +import static com.alibaba.nacos.api.PropertyKeyConst.ENCODE; +import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT; +import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE; +import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY; +import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR; /** * nacos properties @@ -29,6 +50,9 @@ import org.springframework.util.StringUtils; @ConfigurationProperties("spring.cloud.nacos.config") public class NacosConfigProperties { + private static final Logger LOGGER = LoggerFactory + .getLogger(NacosConfigProperties.class); + /** * nacos config server address */ @@ -89,6 +113,21 @@ public class NacosConfigProperties { */ private String clusterName; + @Value("${spring.application.name}") + private String name; + + private String[] activeProfiles; + + private ConfigService configService; + + @Autowired + private Environment environment; + + @PostConstruct + public void init() { + this.activeProfiles = environment.getActiveProfiles(); + } + // todo sts support public String getServerAddr() { @@ -187,6 +226,14 @@ public class NacosConfigProperties { this.clusterName = clusterName; } + public String getName() { + return name; + } + + public String[] getActiveProfiles() { + return activeProfiles; + } + @Override public String toString() { return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\'' @@ -195,46 +242,32 @@ public class NacosConfigProperties { + ", timeout=" + timeout + ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\'' + ", secretKey='" + secretKey + '\'' + ", contextPath='" + contextPath - + '\'' + ", clusterName='" + clusterName + '\'' + '}'; + + '\'' + ", clusterName='" + clusterName + '\'' + ", name='" + name + '\'' + + ", activeProfiles=" + Arrays.toString(activeProfiles) + '}'; } - public void overrideFromEnv(Environment env) { + public ConfigService configServiceInstance() { - if (StringUtils.isEmpty(this.getServerAddr())) { - this.setServerAddr( - env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}")); - } - if (StringUtils.isEmpty(this.getEncode())) { - this.setEncode( - env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}")); - } - if (StringUtils.isEmpty(this.getNamespace())) { - this.setNamespace( - env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}")); + if (null != configService) { + return configService; } - if (StringUtils.isEmpty(this.getAccessKey())) { - this.setAccessKey( - env.resolvePlaceholders("${spring.cloud.nacos.config.access-key:}")); - } - if (StringUtils.isEmpty(this.getSecretKey())) { - this.setSecretKey( - env.resolvePlaceholders("${spring.cloud.nacos.config.secret-key:}")); - } - if (StringUtils.isEmpty(this.getContextPath())) { - this.setContextPath(env - .resolvePlaceholders("${spring.cloud.nacos.config.context-path:}")); - } - if (StringUtils.isEmpty(this.getClusterName())) { - this.setClusterName(env - .resolvePlaceholders("${spring.cloud.nacos.config.cluster-name:}")); - } - if (StringUtils.isEmpty(this.getEndpoint())) { - this.setEndpoint( - env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}")); + + Properties properties = new Properties(); + properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, "")); + properties.put(ENCODE, Objects.toString(this.encode, "")); + properties.put(NAMESPACE, Objects.toString(this.namespace, "")); + properties.put(ACCESS_KEY, Objects.toString(this.accessKey, "")); + properties.put(SECRET_KEY, Objects.toString(this.secretKey, "")); + properties.put(CONTEXT_PATH, Objects.toString(this.contextPath, "")); + properties.put(CLUSTER_NAME, Objects.toString(this.clusterName, "")); + properties.put(ENDPOINT, Objects.toString(this.endpoint, "")); + try { + configService = NacosFactory.createConfigService(properties); + return configService; } - if (StringUtils.isEmpty(this.getPrefix())) { - this.setPrefix( - env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}")); + catch (Exception e) { + LOGGER.error("create config service error!properties={},e=,", this, e); + return null; } } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java index fc0706465..16bf97d09 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java @@ -83,7 +83,6 @@ public class NacosPropertySourceBuilder { String data = null; try { data = configService.getConfig(dataId, group, timeout); - // todo add file extension yaml support if (!StringUtils.isEmpty(data)) { logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'", dataId, group)); diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index f507f1522..304fc5672 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -16,12 +16,9 @@ package org.springframework.cloud.alibaba.nacos.client; -import java.util.Properties; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.cloud.alibaba.nacos.NacosConfigProperties; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; @@ -30,11 +27,7 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.util.StringUtils; -import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.exception.NacosException; - -import static com.alibaba.nacos.api.PropertyKeyConst.*; /** * @author xiaojing @@ -48,47 +41,15 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { private static final String SEP1 = "-"; private static final String DOT = "."; - @Autowired - private ConfigurableListableBeanFactory beanFactory; - @Autowired private NacosConfigProperties nacosConfigProperties; - private ConfigService configService; - private NacosPropertySourceBuilder nacosPropertySourceBuilder; - private Properties getPropertiesFromEnv(Environment env) { - - nacosConfigProperties.overrideFromEnv(env); - - Properties properties = new Properties(); - properties.put(SERVER_ADDR, nacosConfigProperties.getServerAddr()); - properties.put(ENCODE, nacosConfigProperties.getEncode()); - properties.put(NAMESPACE, nacosConfigProperties.getNamespace()); - properties.put(ACCESS_KEY, nacosConfigProperties.getAccessKey()); - properties.put(SECRET_KEY, nacosConfigProperties.getSecretKey()); - properties.put(CONTEXT_PATH, nacosConfigProperties.getContextPath()); - properties.put(CLUSTER_NAME, nacosConfigProperties.getClusterName()); - properties.put(ENDPOINT, nacosConfigProperties.getEndpoint()); - return properties; - } - @Override public PropertySource locate(Environment env) { - Properties properties = getPropertiesFromEnv(env); - - try { - configService = NacosFactory.createConfigService(properties); - } - catch (NacosException e) { - logger.error("create config service error, nacosConfigProperties:{}, ", - properties, e); - return null; - } - - beanFactory.registerSingleton("configService", configService); + ConfigService configService = nacosConfigProperties.configServiceInstance(); if (null == configService) { logger.warn( @@ -99,13 +60,12 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); - String applicationName = env.getProperty("spring.application.name"); - logger.info("Initialize spring.application.name '" + applicationName + "'."); + String name = nacosConfigProperties.getName(); String nacosGroup = nacosConfigProperties.getGroup(); String dataIdPrefix = nacosConfigProperties.getPrefix(); if (StringUtils.isEmpty(dataIdPrefix)) { - dataIdPrefix = applicationName; + dataIdPrefix = name; } String fileExtension = nacosConfigProperties.getFileExtension(); @@ -113,23 +73,21 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); - loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix, - fileExtension); + loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); return composite; } private void loadApplicationConfiguration( - CompositePropertySource compositePropertySource, Environment environment, - String nacosGroup, String dataIdPrefix, String fileExtension) { + CompositePropertySource compositePropertySource, String nacosGroup, + String dataIdPrefix, String fileExtension) { loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension); - for (String profile : environment.getActiveProfiles()) { + for (String profile : nacosConfigProperties.getActiveProfiles()) { String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension); } - // todo multi profile active order and priority } private void loadNacosDataIfPresent(final CompositePropertySource composite, diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java index 5a020b949..f402acd26 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java @@ -44,15 +44,6 @@ public class NacosConfigEndpointAutoConfiguration { @Autowired private NacosPropertySourceRepository nacosPropertySourceRepository; - @Autowired - private ConfigService configService; - - @Bean - @ConditionalOnBean - public NacosConfigProperties nacosConfigProperties() { - return new NacosConfigProperties(); - } - @ConditionalOnMissingBean @Bean public NacosConfigEndpoint nacosConfigEndpoint() { @@ -64,6 +55,7 @@ public class NacosConfigEndpointAutoConfiguration { public NacosConfigHealthIndicator nacosConfigHealthIndicator( NacosPropertySourceRepository nacosPropertySourceRepository) { return new NacosConfigHealthIndicator(nacosConfigProperties, - nacosPropertySourceRepository, configService); + nacosPropertySourceRepository, + nacosConfigProperties.configServiceInstance()); } } diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java index 0b3d82630..b27f14b42 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java @@ -38,66 +38,55 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class NacosConfigAutoConfigurationTests { - private ConfigurableApplicationContext context; - - @Before - public void setUp() throws Exception { - this.context = new SpringApplicationBuilder( - NacosConfigBootstrapConfiguration.class, - NacosConfigAutoConfiguration.class, - TestConfiguration.class) - .web(false).run( - "--spring.cloud.config.enabled=true", - "--spring.cloud.nacos.config.server-addr=127.0.0.1:8080", - "--spring.cloud.nacos.config.prefix=myapp"); - } - - @After - public void tearDown() throws Exception { - if (this.context != null) { - this.context.close(); - } - } - - @Test - public void testNacosConfigProperties() { - - NacosPropertySourceLocator nacosPropertySourceLocator = this.context.getBean(NacosPropertySourceLocator.class); - Environment environment = this.context.getEnvironment(); - try{ - nacosPropertySourceLocator.locate(environment); - }catch (Exception e){ - - } - - NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class); - assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); - assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp"); - - } - - - @Test - public void testNacosRefreshProperties() { - - NacosRefreshProperties nacosRefreshProperties = this.context.getBean(NacosRefreshProperties.class); - assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true); - - } - - @Configuration - @AutoConfigureBefore(NacosConfigAutoConfiguration.class) - static class TestConfiguration{ - - - @Autowired - ConfigurableApplicationContext context; - - @Bean - ContextRefresher contextRefresher(){ - return new ContextRefresher(context, new RefreshScope()); - } - - } + private ConfigurableApplicationContext context; + + @Before + public void setUp() throws Exception { + this.context = new SpringApplicationBuilder( + NacosConfigBootstrapConfiguration.class, + NacosConfigAutoConfiguration.class, TestConfiguration.class).web(false) + .run("--spring.cloud.config.enabled=true", + "--spring.cloud.nacos.config.server-addr=127.0.0.1:8080", + "--spring.cloud.nacos.config.prefix=myapp"); + } + + @After + public void tearDown() throws Exception { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void testNacosConfigProperties() { + NacosConfigProperties nacosConfigProperties = this.context.getParent() + .getBean(NacosConfigProperties.class); + assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); + assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp"); + + } + + @Test + public void testNacosRefreshProperties() { + + NacosRefreshProperties nacosRefreshProperties = this.context + .getBean(NacosRefreshProperties.class); + assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true); + + } + + @Configuration + @AutoConfigureBefore(NacosConfigAutoConfiguration.class) + static class TestConfiguration { + + @Autowired + ConfigurableApplicationContext context; + + @Bean + ContextRefresher contextRefresher() { + return new ContextRefresher(context, new RefreshScope()); + } + + } } diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java index 81466271a..d6aa10245 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java @@ -67,15 +67,14 @@ public class NacosConfigBootstrapConfigurationTests { } - Field configServiceField = ReflectionUtils - .findField(NacosPropertySourceLocator.class, "configService"); - configServiceField.setAccessible(true); + Field nacosConfigPropertiesField = ReflectionUtils + .findField(NacosPropertySourceLocator.class, "nacosConfigProperties"); + nacosConfigPropertiesField.setAccessible(true); - ConfigService configService = (ConfigService) ReflectionUtils - .getField(configServiceField, locator); + NacosConfigProperties configService = (NacosConfigProperties) ReflectionUtils + .getField(nacosConfigPropertiesField, locator); assertThat(configService).isNotNull(); } - }