put all nacos config metadata to bootstrap

pull/898/head
flystar32 6 years ago
parent bd0f7fb497
commit 45632ed5a2

@ -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,10 +16,27 @@
package org.springframework.cloud.alibaba.nacos;
import java.util.Properties;
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.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,15 +46,18 @@ 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
*/
private String serverAddr;
private String serverAddr = "";
/**
* encode for nacos config content.
*/
private String encode;
private String encode = "";
/**
* nacos config group, group is config data meta info.
@ -47,7 +67,7 @@ public class NacosConfigProperties {
/**
* nacos config dataId prefix
*/
private String prefix;
private String prefix = "";
/**
* the suffix of nacos config dataId, also the file extension of config content.
*/
@ -62,32 +82,34 @@ public class NacosConfigProperties {
* endpoint for Nacos, the domain name of a service, through which the server address
* can be dynamically obtained.
*/
private String endpoint;
private String endpoint = "";
/**
* namespace, separation configuration of different environments.
*/
private String namespace;
private String namespace = "";
/**
* access key for namespace.
*/
private String accessKey;
private String accessKey = "";
/**
* secret key for namespace.
*/
private String secretKey;
private String secretKey = "";
/**
* context path for nacos config server.
*/
private String contextPath;
private String contextPath = "";
/**
* nacos config cluster name
*/
private String clusterName;
private String clusterName = "";
private ConfigService configService;
// todo sts support
@ -237,4 +259,29 @@ public class NacosConfigProperties {
env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}"));
}
}
public ConfigService configServiceInstance() {
if (null != configService) {
return configService;
}
Properties properties = new Properties();
properties.put(SERVER_ADDR, this.serverAddr);
properties.put(ENCODE, this.encode);
properties.put(NAMESPACE, this.namespace);
properties.put(ACCESS_KEY, this.accessKey);
properties.put(SECRET_KEY, this.secretKey);
properties.put(CONTEXT_PATH, this.contextPath);
properties.put(CLUSTER_NAME, this.clusterName);
properties.put(ENDPOINT, this.endpoint);
try {
configService = NacosFactory.createConfigService(properties);
return configService;
}
catch (Exception e) {
LOGGER.error("create config service error!properties={},e=,", this, e);
return null;
}
}
}

@ -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(

@ -16,8 +16,6 @@
package org.springframework.cloud.alibaba.nacos.endpoint;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
@ -46,15 +44,6 @@ public class NacosConfigEndpointAutoConfiguration {
@Autowired
private NacosPropertySourceRepository nacosPropertySourceRepository;
@Autowired
private ConfigService configService;
@Bean
@ConditionalOnBean
public NacosConfigProperties nacosConfigProperties() {
return new NacosConfigProperties();
}
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
@Bean
@ -67,6 +56,7 @@ public class NacosConfigEndpointAutoConfiguration {
public NacosConfigHealthIndicator nacosConfigHealthIndicator(
NacosPropertySourceRepository nacosPropertySourceRepository) {
return new NacosConfigHealthIndicator(nacosConfigProperties,
nacosPropertySourceRepository, configService);
nacosPropertySourceRepository,
nacosConfigProperties.configServiceInstance());
}
}

@ -63,15 +63,7 @@ public class NacosConfigAutoConfigurationTests {
@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);
NacosConfigProperties nacosConfigProperties = this.context.getParent().getBean(NacosConfigProperties.class);
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp");

@ -68,15 +68,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…
Cancel
Save