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:59:46 +08:00
parent a60402beaf
commit 45e4ec3a84
2 changed files with 51 additions and 68 deletions

View File

@ -16,17 +16,21 @@
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 java.util.Properties;
import javax.annotation.PostConstruct;
import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
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.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.ACCESS_KEY;
import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME; import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME;
@ -52,12 +56,12 @@ public class NacosConfigProperties {
/** /**
* 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.
@ -67,7 +71,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.
*/ */
@ -82,35 +86,48 @@ 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;
@Value("${spring.application.name}")
private String name;
private String[] activeProfiles;
private ConfigService configService; 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() {
@ -209,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 + '\''
@ -217,47 +242,8 @@ 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) {
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 (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:}"));
}
} }
public ConfigService configServiceInstance() { public ConfigService configServiceInstance() {
@ -267,14 +253,14 @@ public class NacosConfigProperties {
} }
Properties properties = new Properties(); Properties properties = new Properties();
properties.put(SERVER_ADDR, this.serverAddr); properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, ""));
properties.put(ENCODE, this.encode); properties.put(ENCODE, Objects.toString(this.encode, ""));
properties.put(NAMESPACE, this.namespace); properties.put(NAMESPACE, Objects.toString(this.namespace, ""));
properties.put(ACCESS_KEY, this.accessKey); properties.put(ACCESS_KEY, Objects.toString(this.accessKey, ""));
properties.put(SECRET_KEY, this.secretKey); properties.put(SECRET_KEY, Objects.toString(this.secretKey, ""));
properties.put(CONTEXT_PATH, this.contextPath); properties.put(CONTEXT_PATH, Objects.toString(this.contextPath, ""));
properties.put(CLUSTER_NAME, this.clusterName); properties.put(CLUSTER_NAME, Objects.toString(this.clusterName, ""));
properties.put(ENDPOINT, this.endpoint); properties.put(ENDPOINT, Objects.toString(this.endpoint, ""));
try { try {
configService = NacosFactory.createConfigService(properties); configService = NacosFactory.createConfigService(properties);
return configService; return configService;

View File

@ -60,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();
@ -74,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,