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:
parent
b4ec84c078
commit
e42225fbcc
@ -112,7 +112,7 @@ Nacos Client 从 Nacos Server 端获取数据时,调用的是此接口 `Config
|
||||
|
||||
在 Nacos Config Starter 中,dataId 的拼接格式如下
|
||||
|
||||
${prefix} - ${spring.active.profile} . ${content-type}
|
||||
${prefix} - ${spring.active.profile} . ${file-extension}
|
||||
|
||||
* `prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。
|
||||
|
||||
@ -120,7 +120,7 @@ Nacos Client 从 Nacos Server 端获取数据时,调用的是此接口 `Config
|
||||
|
||||
**注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${context.type}`**
|
||||
|
||||
* `content-type` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.content-type`来配置。
|
||||
* `file-extension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.content-type`来配置。
|
||||
目前只支持 `properties` 类型。
|
||||
|
||||
#### group
|
||||
@ -168,7 +168,7 @@ Spring Boot 2.x 可以通过访问 http://127.0.0.1:18084/actuator/nacos-config
|
||||
服务端地址|spring.cloud.nacos.config.server-addr||
|
||||
DataId前缀|spring.cloud.nacos.config.prefix||spring.application.name
|
||||
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
||||
dataID后缀及数据格式|spring.cloud.nacos.config.content-type|properties|目前只支持 properties
|
||||
dataID后缀及数据格式|spring.cloud.nacos.config.file-extension|properties|目前只支持 properties
|
||||
配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码
|
||||
获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms
|
||||
配置的命名空间|spring.cloud.nacos.config.namespace||常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。
|
||||
|
@ -113,7 +113,7 @@ Nacos Client gets data from Nacos Server through this method. `ConfigService.get
|
||||
|
||||
In Nacos Config Starter, the splicing format of dataId is as follows
|
||||
|
||||
${prefix} - ${spring.active.profile} . ${content-type}
|
||||
${prefix} - ${spring.active.profile} . ${file-extension}
|
||||
|
||||
* `prefix` default value is `spring.application.name` value, which can also be configured via the configuration item `spring.cloud.nacos.config.prefix`.
|
||||
|
||||
@ -121,7 +121,7 @@ In Nacos Config Starter, the splicing format of dataId is as follows
|
||||
|
||||
**Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${context.type}`**
|
||||
|
||||
* `content-type` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.content-type`.
|
||||
* `file-extension` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.content-type`.
|
||||
Currently only the `properties` type is supported.
|
||||
|
||||
#### group
|
||||
@ -172,7 +172,7 @@ Configuration item|key|default value|Description
|
||||
server address|spring.cloud.nacos.config.server-addr||
|
||||
DataId prefix|spring.cloud.nacos.config.prefix||spring.application.name
|
||||
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
||||
dataID content type|spring.cloud.nacos.config.content-type|properties|currently only support properties
|
||||
dataID suffix|spring.cloud.nacos.config.file-extension|properties|the suffix of nacos config dataId, also the file extension of config content.
|
||||
encoding |spring.cloud.nacos.config.encode|UTF-8|Content encoding
|
||||
timeout|spring.cloud.nacos.config.timeout|3000|Get the configuration timeout period,unit is ms
|
||||
namespace|spring.cloud.nacos.config.namespace||One of the common scenarios is the separation of the configuration of different environments, such as the development of the test environment and the resource isolation of the production environment.
|
||||
|
@ -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,10 +80,11 @@ 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;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
@ -9,12 +9,12 @@
|
||||
"name": "spring.cloud.nacos.config.prefix",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "${spring.application.name}",
|
||||
"description": "the prefix of dataId, nacos config data meta info. dataId = prefix + '-' + ${spring.active.profile} + `-` + ${spring.cloud.nacos.config.content-type}."
|
||||
"description": "the prefix of dataId, nacos config data meta info. dataId = prefix + '-' + ${spring.active.profile} + `.` + ${spring.cloud.nacos.config.file-extension}."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.nacos.config.content-type",
|
||||
"name": "spring.cloud.nacos.config.file-extension",
|
||||
"type": "java.lang.String",
|
||||
"defaultValue": "properties",
|
||||
"description": "the content type of nacos config content, only support properties now."
|
||||
"description": "the suffix of nacos config dataId, also the file extension of config content, only support properties now."
|
||||
}
|
||||
]}
|
@ -4,4 +4,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration,\
|
||||
org.springframework.cloud.alibaba.nacos.endpoint.NacosConfigEndpointAutoConfiguration
|
||||
org.springframework.boot.diagnostics.FailureAnalyzer=\
|
||||
org.springframework.cloud.alibaba.nacos..analyzer.NacosConnectionFailureAnalyzerdiagnostics
|
||||
org.springframework.cloud.alibaba.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer
|
@ -72,7 +72,7 @@ public class NacosConfigAutoConfigurationTests {
|
||||
}
|
||||
|
||||
NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class);
|
||||
assertThat(nacosConfigProperties.getContentType()).isEqualTo("properties");
|
||||
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
|
||||
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp");
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user