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:
parent
43a4f97907
commit
a2b158d67d
@ -19,9 +19,10 @@
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</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 打开动态刷新功能
|
||||
|
||||
@ -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 直接启动和编译打包后启动。
|
||||
|
@ -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>
|
||||
</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 .
|
||||
@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.
|
||||
|
@ -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 + "!";
|
||||
}
|
||||
}
|
@ -1,3 +1,2 @@
|
||||
spring.application.name=nacos-config-example
|
||||
server.port=18084
|
||||
management.endpoints.web.exposure.include=*
|
@ -1 +1,2 @@
|
||||
spring.application.name=nacos-config-example
|
||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
|
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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 (null != configService) {
|
||||
return configService;
|
||||
}
|
||||
if (StringUtils.isEmpty(this.getEncode())) {
|
||||
this.setEncode(
|
||||
env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}"));
|
||||
|
||||
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.getNamespace())) {
|
||||
this.setNamespace(
|
||||
env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}"));
|
||||
}
|
||||
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:}"));
|
||||
catch (Exception e) {
|
||||
LOGGER.error("create config service error!properties={},e=,", this, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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,
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -38,66 +38,55 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*/
|
||||
public class NacosConfigAutoConfigurationTests {
|
||||
|
||||
private ConfigurableApplicationContext context;
|
||||
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");
|
||||
}
|
||||
@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();
|
||||
}
|
||||
}
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
if (this.context != null) {
|
||||
this.context.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNacosConfigProperties() {
|
||||
@Test
|
||||
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);
|
||||
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
|
||||
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp");
|
||||
NacosRefreshProperties nacosRefreshProperties = this.context
|
||||
.getBean(NacosRefreshProperties.class);
|
||||
assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@AutoConfigureBefore(NacosConfigAutoConfiguration.class)
|
||||
static class TestConfiguration {
|
||||
|
||||
@Test
|
||||
public void testNacosRefreshProperties() {
|
||||
@Autowired
|
||||
ConfigurableApplicationContext context;
|
||||
|
||||
NacosRefreshProperties nacosRefreshProperties = this.context.getBean(NacosRefreshProperties.class);
|
||||
assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true);
|
||||
@Bean
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user