1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

put all nacos config metadata to bootstrap

This commit is contained in:
flystar32 2018-10-29 23:01:30 +08:00
parent 43a4f97907
commit a2b158d67d
13 changed files with 158 additions and 194 deletions

View File

@ -19,9 +19,10 @@
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency> </dependency>
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 打开动态刷新功能 3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能
@ -70,8 +71,8 @@
1. 增加配置,在应用的 /src/main/resources/application.properties 中添加基本配置信息 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 直接启动和编译打包后启动。 2. 启动应用,支持 IDE 直接启动和编译打包后启动。

View File

@ -19,9 +19,10 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency> </dependency>
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 . 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 @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 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. 2. Start the application in IDE or by building a fatjar.

View File

@ -16,39 +16,39 @@ import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication @SpringBootApplication
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
} }
@Component @Component
class SampleRunner implements ApplicationRunner { class SampleRunner implements ApplicationRunner {
@Value("${user.name}") @Value("${user.name}")
String userName; String userName;
@Value("${user.age}") @Value("${user.age}")
int userAge; int userAge;
@Override @Override
public void run(ApplicationArguments args) throws Exception { public void run(ApplicationArguments args) throws Exception {
System.out.println(userName); System.out.println(userName);
System.out.println(userAge); System.out.println(userAge);
} }
} }
@RestController @RestController
@RefreshScope @RefreshScope
class SampleController { class SampleController {
@Value("${user.name}") @Value("${user.name}")
String userName; String userName;
@Value("${user.age}") @Value("${user.age}")
int age; int age;
@RequestMapping("/user") @RequestMapping("/user")
public String simple() { public String simple() {
return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!"; return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!";
} }
} }

View File

@ -1,3 +1,2 @@
spring.application.name=nacos-config-example
server.port=18084 server.port=18084
management.endpoints.web.exposure.include=* management.endpoints.web.exposure.include=*

View File

@ -1 +1,2 @@
spring.application.name=nacos-config-example
spring.cloud.nacos.config.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.server-addr=127.0.0.1:8848

View File

@ -16,8 +16,6 @@
package org.springframework.cloud.alibaba.nacos; package org.springframework.cloud.alibaba.nacos;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher; import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher;
@ -43,14 +41,6 @@ public class NacosConfigAutoConfiguration implements ApplicationContextAware {
@Autowired @Autowired
private NacosRefreshProperties nacosRefreshProperties; private NacosRefreshProperties nacosRefreshProperties;
@Autowired
private ConfigService configService;
@Bean
public NacosConfigProperties nacosConfigProperties() {
return new NacosConfigProperties();
}
@Bean @Bean
public NacosPropertySourceRepository nacosPropertySourceRepository() { public NacosPropertySourceRepository nacosPropertySourceRepository() {
return new NacosPropertySourceRepository(applicationContext); return new NacosPropertySourceRepository(applicationContext);
@ -69,10 +59,10 @@ public class NacosConfigAutoConfiguration implements ApplicationContextAware {
@Bean @Bean
public NacosContextRefresher nacosContextRefresher(ContextRefresher contextRefresher, public NacosContextRefresher nacosContextRefresher(ContextRefresher contextRefresher,
NacosRefreshHistory refreshHistory, NacosRefreshHistory refreshHistory,
NacosPropertySourceRepository propertySourceRepository, NacosPropertySourceRepository propertySourceRepository) {
ConfigService configService) {
return new NacosContextRefresher(contextRefresher, nacosConfigProperties, return new NacosContextRefresher(contextRefresher, nacosConfigProperties,
nacosRefreshProperties, refreshHistory, propertySourceRepository,configService); nacosRefreshProperties, refreshHistory, propertySourceRepository,
nacosConfigProperties.configServiceInstance());
} }
@Override @Override

View File

@ -16,6 +16,7 @@
package org.springframework.cloud.alibaba.nacos; package org.springframework.cloud.alibaba.nacos;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator; import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -32,6 +33,7 @@ public class NacosConfigBootstrapConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean
public NacosConfigProperties nacosConfigProperties() { public NacosConfigProperties nacosConfigProperties() {
return new NacosConfigProperties(); return new NacosConfigProperties();
} }

View File

@ -16,9 +16,30 @@
package org.springframework.cloud.alibaba.nacos; 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.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment; 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 * nacos properties
@ -29,6 +50,9 @@ import org.springframework.util.StringUtils;
@ConfigurationProperties("spring.cloud.nacos.config") @ConfigurationProperties("spring.cloud.nacos.config")
public class NacosConfigProperties { public class NacosConfigProperties {
private static final Logger LOGGER = LoggerFactory
.getLogger(NacosConfigProperties.class);
/** /**
* nacos config server address * nacos config server address
*/ */
@ -89,6 +113,21 @@ public class NacosConfigProperties {
*/ */
private String clusterName; 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 // todo sts support
public String getServerAddr() { public String getServerAddr() {
@ -187,6 +226,14 @@ public class NacosConfigProperties {
this.clusterName = clusterName; this.clusterName = clusterName;
} }
public String getName() {
return name;
}
public String[] getActiveProfiles() {
return activeProfiles;
}
@Override @Override
public String toString() { public String toString() {
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\'' return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
@ -195,46 +242,32 @@ public class NacosConfigProperties {
+ ", timeout=" + timeout + ", endpoint='" + endpoint + '\'' + ", timeout=" + timeout + ", endpoint='" + endpoint + '\''
+ ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\'' + ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\''
+ ", secretKey='" + secretKey + '\'' + ", contextPath='" + contextPath + ", 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())) { if (null != configService) {
this.setServerAddr( return configService;
env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}"));
} }
if (StringUtils.isEmpty(this.getEncode())) {
this.setEncode( Properties properties = new Properties();
env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}")); 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.getNamespace())) { catch (Exception e) {
this.setNamespace( LOGGER.error("create config service error!properties={},e=,", this, e);
env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}")); return null;
}
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:}"));
}
if (StringUtils.isEmpty(this.getPrefix())) {
this.setPrefix(
env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}"));
} }
} }
} }

View File

@ -83,7 +83,6 @@ public class NacosPropertySourceBuilder {
String data = null; String data = null;
try { try {
data = configService.getConfig(dataId, group, timeout); data = configService.getConfig(dataId, group, timeout);
// todo add file extension yaml support
if (!StringUtils.isEmpty(data)) { if (!StringUtils.isEmpty(data)) {
logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'", logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
dataId, group)); dataId, group));

View File

@ -16,12 +16,9 @@
package org.springframework.cloud.alibaba.nacos.client; package org.springframework.cloud.alibaba.nacos.client;
import java.util.Properties;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; 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.alibaba.nacos.NacosConfigProperties;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.annotation.Order; 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.core.env.PropertySource;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import static com.alibaba.nacos.api.PropertyKeyConst.*;
/** /**
* @author xiaojing * @author xiaojing
@ -48,47 +41,15 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
private static final String SEP1 = "-"; private static final String SEP1 = "-";
private static final String DOT = "."; private static final String DOT = ".";
@Autowired
private ConfigurableListableBeanFactory beanFactory;
@Autowired @Autowired
private NacosConfigProperties nacosConfigProperties; private NacosConfigProperties nacosConfigProperties;
private ConfigService configService;
private NacosPropertySourceBuilder nacosPropertySourceBuilder; 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 @Override
public PropertySource<?> locate(Environment env) { public PropertySource<?> locate(Environment env) {
Properties properties = getPropertiesFromEnv(env); ConfigService configService = nacosConfigProperties.configServiceInstance();
try {
configService = NacosFactory.createConfigService(properties);
}
catch (NacosException e) {
logger.error("create config service error, nacosConfigProperties:{}, ",
properties, e);
return null;
}
beanFactory.registerSingleton("configService", configService);
if (null == configService) { if (null == configService) {
logger.warn( logger.warn(
@ -99,13 +60,12 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
timeout); timeout);
String applicationName = env.getProperty("spring.application.name"); String name = nacosConfigProperties.getName();
logger.info("Initialize spring.application.name '" + applicationName + "'.");
String nacosGroup = nacosConfigProperties.getGroup(); String nacosGroup = nacosConfigProperties.getGroup();
String dataIdPrefix = nacosConfigProperties.getPrefix(); String dataIdPrefix = nacosConfigProperties.getPrefix();
if (StringUtils.isEmpty(dataIdPrefix)) { if (StringUtils.isEmpty(dataIdPrefix)) {
dataIdPrefix = applicationName; dataIdPrefix = name;
} }
String fileExtension = nacosConfigProperties.getFileExtension(); String fileExtension = nacosConfigProperties.getFileExtension();
@ -113,23 +73,21 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
CompositePropertySource composite = new CompositePropertySource( CompositePropertySource composite = new CompositePropertySource(
NACOS_PROPERTY_SOURCE_NAME); NACOS_PROPERTY_SOURCE_NAME);
loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix, loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension);
fileExtension);
return composite; return composite;
} }
private void loadApplicationConfiguration( private void loadApplicationConfiguration(
CompositePropertySource compositePropertySource, Environment environment, CompositePropertySource compositePropertySource, String nacosGroup,
String nacosGroup, String dataIdPrefix, String fileExtension) { String dataIdPrefix, String fileExtension) {
loadNacosDataIfPresent(compositePropertySource, loadNacosDataIfPresent(compositePropertySource,
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension); dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension);
for (String profile : environment.getActiveProfiles()) { for (String profile : nacosConfigProperties.getActiveProfiles()) {
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
fileExtension); fileExtension);
} }
// todo multi profile active order and priority
} }
private void loadNacosDataIfPresent(final CompositePropertySource composite, private void loadNacosDataIfPresent(final CompositePropertySource composite,

View File

@ -44,15 +44,6 @@ public class NacosConfigEndpointAutoConfiguration {
@Autowired @Autowired
private NacosPropertySourceRepository nacosPropertySourceRepository; private NacosPropertySourceRepository nacosPropertySourceRepository;
@Autowired
private ConfigService configService;
@Bean
@ConditionalOnBean
public NacosConfigProperties nacosConfigProperties() {
return new NacosConfigProperties();
}
@ConditionalOnMissingBean @ConditionalOnMissingBean
@Bean @Bean
public NacosConfigEndpoint nacosConfigEndpoint() { public NacosConfigEndpoint nacosConfigEndpoint() {
@ -64,6 +55,7 @@ public class NacosConfigEndpointAutoConfiguration {
public NacosConfigHealthIndicator nacosConfigHealthIndicator( public NacosConfigHealthIndicator nacosConfigHealthIndicator(
NacosPropertySourceRepository nacosPropertySourceRepository) { NacosPropertySourceRepository nacosPropertySourceRepository) {
return new NacosConfigHealthIndicator(nacosConfigProperties, return new NacosConfigHealthIndicator(nacosConfigProperties,
nacosPropertySourceRepository, configService); nacosPropertySourceRepository,
nacosConfigProperties.configServiceInstance());
} }
} }

View File

@ -38,66 +38,55 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
public class NacosConfigAutoConfigurationTests { public class NacosConfigAutoConfigurationTests {
private ConfigurableApplicationContext context; private ConfigurableApplicationContext context;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
this.context = new SpringApplicationBuilder( this.context = new SpringApplicationBuilder(
NacosConfigBootstrapConfiguration.class, NacosConfigBootstrapConfiguration.class,
NacosConfigAutoConfiguration.class, NacosConfigAutoConfiguration.class, TestConfiguration.class).web(false)
TestConfiguration.class) .run("--spring.cloud.config.enabled=true",
.web(false).run( "--spring.cloud.nacos.config.server-addr=127.0.0.1:8080",
"--spring.cloud.config.enabled=true", "--spring.cloud.nacos.config.prefix=myapp");
"--spring.cloud.nacos.config.server-addr=127.0.0.1:8080", }
"--spring.cloud.nacos.config.prefix=myapp");
}
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
if (this.context != null) { if (this.context != null) {
this.context.close(); this.context.close();
} }
} }
@Test @Test
public void testNacosConfigProperties() { public void testNacosConfigProperties() {
NacosConfigProperties nacosConfigProperties = this.context.getParent()
.getBean(NacosConfigProperties.class);
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp");
NacosPropertySourceLocator nacosPropertySourceLocator = this.context.getBean(NacosPropertySourceLocator.class); }
Environment environment = this.context.getEnvironment();
try{
nacosPropertySourceLocator.locate(environment);
}catch (Exception e){
} @Test
public void testNacosRefreshProperties() {
NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class); NacosRefreshProperties nacosRefreshProperties = this.context
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); .getBean(NacosRefreshProperties.class);
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp"); assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true);
} }
@Configuration
@AutoConfigureBefore(NacosConfigAutoConfiguration.class)
static class TestConfiguration {
@Test @Autowired
public void testNacosRefreshProperties() { ConfigurableApplicationContext context;
NacosRefreshProperties nacosRefreshProperties = this.context.getBean(NacosRefreshProperties.class); @Bean
assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true); ContextRefresher contextRefresher() {
return new ContextRefresher(context, new RefreshScope());
}
} }
@Configuration
@AutoConfigureBefore(NacosConfigAutoConfiguration.class)
static class TestConfiguration{
@Autowired
ConfigurableApplicationContext context;
@Bean
ContextRefresher contextRefresher(){
return new ContextRefresher(context, new RefreshScope());
}
}
} }

View File

@ -67,15 +67,14 @@ public class NacosConfigBootstrapConfigurationTests {
} }
Field configServiceField = ReflectionUtils Field nacosConfigPropertiesField = ReflectionUtils
.findField(NacosPropertySourceLocator.class, "configService"); .findField(NacosPropertySourceLocator.class, "nacosConfigProperties");
configServiceField.setAccessible(true); nacosConfigPropertiesField.setAccessible(true);
ConfigService configService = (ConfigService) ReflectionUtils NacosConfigProperties configService = (NacosConfigProperties) ReflectionUtils
.getField(configServiceField, locator); .getField(nacosConfigPropertiesField, locator);
assertThat(configService).isNotNull(); assertThat(configService).isNotNull();
} }
} }