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 22:16:15 +08:00
parent f4a2ddaf4b
commit 3acfc67d6a
9 changed files with 71 additions and 90 deletions

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,10 +16,27 @@
package org.springframework.cloud.alibaba.nacos; 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.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.StringUtils; 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,15 +46,18 @@ 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
*/ */
private String serverAddr; private String serverAddr = "";
/** /**
* encode for nacos config content. * encode for nacos config content.
*/ */
private String encode; private String encode = "";
/** /**
* nacos config group, group is config data meta info. * nacos config group, group is config data meta info.
@ -47,7 +67,7 @@ public class NacosConfigProperties {
/** /**
* nacos config dataId prefix * nacos config dataId prefix
*/ */
private String prefix; private String prefix = "";
/** /**
* the suffix of nacos config dataId, also the file extension of config content. * 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 * endpoint for Nacos, the domain name of a service, through which the server address
* can be dynamically obtained. * can be dynamically obtained.
*/ */
private String endpoint; private String endpoint = "";
/** /**
* namespace, separation configuration of different environments. * namespace, separation configuration of different environments.
*/ */
private String namespace; private String namespace = "";
/** /**
* access key for namespace. * access key for namespace.
*/ */
private String accessKey; private String accessKey = "";
/** /**
* secret key for namespace. * secret key for namespace.
*/ */
private String secretKey; private String secretKey = "";
/** /**
* context path for nacos config server. * context path for nacos config server.
*/ */
private String contextPath; private String contextPath = "";
/** /**
* nacos config cluster name * nacos config cluster name
*/ */
private String clusterName; private String clusterName = "";
private ConfigService configService;
// todo sts support // todo sts support
@ -237,4 +259,29 @@ public class NacosConfigProperties {
env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}")); 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;
}
}
} }

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(

View File

@ -16,8 +16,6 @@
package org.springframework.cloud.alibaba.nacos.endpoint; package org.springframework.cloud.alibaba.nacos.endpoint;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
@ -46,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
@ConditionalOnEnabledEndpoint @ConditionalOnEnabledEndpoint
@Bean @Bean
@ -67,6 +56,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

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

View File

@ -68,15 +68,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();
} }
} }