mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
update nacos config properties , content-type to file-extension
This commit is contained in:
@@ -49,9 +49,9 @@ public class NacosConfigProperties {
|
||||
*/
|
||||
private String prefix;
|
||||
/**
|
||||
* the content type of nacos config content.
|
||||
* the suffix of nacos config dataId, also the file extension of config content.
|
||||
*/
|
||||
private String contentType = "properties";
|
||||
private String fileExtension = "properties";
|
||||
|
||||
/**
|
||||
* timeout for get config from nacos.
|
||||
@@ -59,7 +59,8 @@ public class NacosConfigProperties {
|
||||
private int timeout = 3000;
|
||||
|
||||
/**
|
||||
* endpoint for Nacos, the domain name of a service, through which the server address can be dynamically obtained.
|
||||
* endpoint for Nacos, the domain name of a service, through which the server address
|
||||
* can be dynamically obtained.
|
||||
*/
|
||||
private String endpoint;
|
||||
|
||||
@@ -88,7 +89,7 @@ public class NacosConfigProperties {
|
||||
*/
|
||||
private String clusterName;
|
||||
|
||||
//todo sts support
|
||||
// todo sts support
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
@@ -106,12 +107,12 @@ public class NacosConfigProperties {
|
||||
this.prefix = prefix;
|
||||
}
|
||||
|
||||
public String getContentType() {
|
||||
return contentType;
|
||||
public String getFileExtension() {
|
||||
return fileExtension;
|
||||
}
|
||||
|
||||
public void setContentType(String contentType) {
|
||||
this.contentType = contentType;
|
||||
public void setFileExtension(String fileExtension) {
|
||||
this.fileExtension = fileExtension;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
@@ -188,50 +189,52 @@ public class NacosConfigProperties {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NacosConfigProperties{" +
|
||||
"serverAddr='" + serverAddr + '\'' +
|
||||
", encode='" + encode + '\'' +
|
||||
", group='" + group + '\'' +
|
||||
", prefix='" + prefix + '\'' +
|
||||
", contentType='" + contentType + '\'' +
|
||||
", timeout=" + timeout +
|
||||
", endpoint='" + endpoint + '\'' +
|
||||
", namespace='" + namespace + '\'' +
|
||||
", accessKey='" + accessKey + '\'' +
|
||||
", secretKey='" + secretKey + '\'' +
|
||||
", contextPath='" + contextPath + '\'' +
|
||||
", clusterName='" + clusterName + '\'' +
|
||||
'}';
|
||||
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
||||
+ ", encode='" + encode + '\'' + ", group='" + group + '\'' + ", prefix='"
|
||||
+ prefix + '\'' + ", fileExtension='" + fileExtension + '\''
|
||||
+ ", timeout=" + timeout + ", endpoint='" + endpoint + '\''
|
||||
+ ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\''
|
||||
+ ", secretKey='" + secretKey + '\'' + ", contextPath='" + contextPath
|
||||
+ '\'' + ", clusterName='" + clusterName + '\'' + '}';
|
||||
}
|
||||
|
||||
public void overrideFromEnv(Environment env){
|
||||
public void overrideFromEnv(Environment env) {
|
||||
|
||||
if(StringUtils.isEmpty(this.getServerAddr())) {
|
||||
this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}"));
|
||||
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.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.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.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.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.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.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.getEndpoint())) {
|
||||
this.setEndpoint(
|
||||
env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}"));
|
||||
}
|
||||
if(StringUtils.isEmpty(this.getPrefix())) {
|
||||
this.setPrefix(env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}"));
|
||||
if (StringUtils.isEmpty(this.getPrefix())) {
|
||||
this.setPrefix(
|
||||
env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -69,15 +69,15 @@ public class NacosPropertySourceBuilder {
|
||||
* @param dataId Nacos dataId
|
||||
* @param group Nacos group
|
||||
*/
|
||||
NacosPropertySource build(String dataId, String group, String contentType) {
|
||||
Properties p = loadNacosData(dataId, group, contentType);
|
||||
NacosPropertySource build(String dataId, String group, String fileExtension) {
|
||||
Properties p = loadNacosData(dataId, group, fileExtension);
|
||||
if (p == null) {
|
||||
return null;
|
||||
}
|
||||
return new NacosPropertySource(dataId, propertiesToMap(p), new Date());
|
||||
}
|
||||
|
||||
private Properties loadNacosData(String dataId, String group, String contentType) {
|
||||
private Properties loadNacosData(String dataId, String group, String fileExtension) {
|
||||
String data = null;
|
||||
try {
|
||||
data = configService.getConfig(dataId, group, timeout);
|
||||
|
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.nacos.client;
|
||||
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
@@ -81,13 +80,14 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
Properties properties = getPropertiesFromEnv(env);
|
||||
|
||||
try {
|
||||
configService = NacosFactory.createConfigService(properties);
|
||||
configService = NacosFactory.createConfigService(properties);
|
||||
}
|
||||
catch (NacosException e) {
|
||||
logger.error("create config service error, nacosConfigProperties:{}, ", properties, e);
|
||||
logger.error("create config service error, nacosConfigProperties:{}, ",
|
||||
properties, e);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
beanFactory.registerSingleton("configService", configService);
|
||||
|
||||
if (null == configService) {
|
||||
@@ -96,7 +96,8 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
return null;
|
||||
}
|
||||
long timeout = nacosConfigProperties.getTimeout();
|
||||
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout);
|
||||
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
|
||||
timeout);
|
||||
|
||||
String applicationName = env.getProperty("spring.application.name");
|
||||
logger.info("Initialize spring.application.name '" + applicationName + "'.");
|
||||
@@ -107,32 +108,34 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
dataIdPrefix = applicationName;
|
||||
}
|
||||
|
||||
String contentType = nacosConfigProperties.getContentType();
|
||||
String fileExtension = nacosConfigProperties.getFileExtension();
|
||||
|
||||
CompositePropertySource composite = new CompositePropertySource(
|
||||
NACOS_PROPERTY_SOURCE_NAME);
|
||||
|
||||
loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix, contentType);
|
||||
loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix,
|
||||
fileExtension);
|
||||
|
||||
return composite;
|
||||
}
|
||||
|
||||
private void loadApplicationConfiguration(
|
||||
CompositePropertySource compositePropertySource, Environment environment,
|
||||
String nacosGroup, String dataIdPrefix, String contentType) {
|
||||
loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + contentType,
|
||||
nacosGroup, contentType);
|
||||
String nacosGroup, String dataIdPrefix, String fileExtension) {
|
||||
loadNacosDataIfPresent(compositePropertySource,
|
||||
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension);
|
||||
for (String profile : environment.getActiveProfiles()) {
|
||||
String dataId = dataIdPrefix + SEP1 + profile + DOT + contentType;
|
||||
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
|
||||
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
|
||||
contentType);
|
||||
fileExtension);
|
||||
}
|
||||
// todo multi profile active order and priority
|
||||
}
|
||||
|
||||
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
||||
final String dataId, final String group,String contentType) {
|
||||
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group, contentType);
|
||||
final String dataId, final String group, String fileExtension) {
|
||||
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
|
||||
fileExtension);
|
||||
if (ps != null) {
|
||||
composite.addFirstPropertySource(ps);
|
||||
}
|
||||
|
Reference in New Issue
Block a user