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
21dfd637b3
commit
bfa8f24cc8
@ -112,15 +112,15 @@ Nacos Client 从 Nacos Server 端获取数据时,调用的是此接口 `Config
|
|||||||
|
|
||||||
在 Nacos Config Starter 中,dataId 的拼接格式如下
|
在 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`来配置。
|
* `prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。
|
||||||
|
|
||||||
* `spring.active.profile` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
* `spring.active.profile` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
||||||
|
|
||||||
**注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${context.type}`**
|
**注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${file-extension}`**
|
||||||
|
|
||||||
* `content-type` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.content-type`来配置。
|
* `file-extension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.file-extension`来配置。
|
||||||
目前只支持 `properties` 类型。
|
目前只支持 `properties` 类型。
|
||||||
|
|
||||||
#### group
|
#### group
|
||||||
@ -168,7 +168,7 @@ Spring Boot 2.x 可以通过访问 http://127.0.0.1:18084/actuator/nacos-config
|
|||||||
服务端地址|spring.cloud.nacos.config.server-addr||
|
服务端地址|spring.cloud.nacos.config.server-addr||
|
||||||
DataId前缀|spring.cloud.nacos.config.prefix||spring.application.name
|
DataId前缀|spring.cloud.nacos.config.prefix||spring.application.name
|
||||||
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
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|dataId的后缀,同时也是配置内容的文件格式,目前只支持 properties
|
||||||
配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码
|
配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码
|
||||||
获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms
|
获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms
|
||||||
配置的命名空间|spring.cloud.nacos.config.namespace||常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。
|
配置的命名空间|spring.cloud.nacos.config.namespace||常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。
|
||||||
|
@ -113,15 +113,15 @@ Nacos Client gets data from Nacos Server through this method. `ConfigService.get
|
|||||||
|
|
||||||
In Nacos Config Starter, the splicing format of dataId is as follows
|
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`.
|
* `prefix` default value is `spring.application.name` value, which can also be configured via the configuration item `spring.cloud.nacos.config.prefix`.
|
||||||
|
|
||||||
* `spring.active.profile` is the profile corresponding to the current environment. For details, please refer to [Spring Boot Doc](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
* `spring.active.profile` is the profile corresponding to the current environment. For details, please refer to [Spring Boot Doc](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
||||||
|
|
||||||
**Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${context.type}`**
|
**Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${file-extension}`**
|
||||||
|
|
||||||
* `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.file-extension`.
|
||||||
Currently only the `properties` type is supported.
|
Currently only the `properties` type is supported.
|
||||||
|
|
||||||
#### group
|
#### group
|
||||||
@ -172,7 +172,7 @@ Configuration item|key|default value|Description
|
|||||||
server address|spring.cloud.nacos.config.server-addr||
|
server address|spring.cloud.nacos.config.server-addr||
|
||||||
DataId prefix|spring.cloud.nacos.config.prefix||spring.application.name
|
DataId prefix|spring.cloud.nacos.config.prefix||spring.application.name
|
||||||
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
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
|
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
|
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.
|
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.
|
||||||
|
@ -197,7 +197,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key||
|
|||||||
SecretKey|spring.cloud.nacos.discovery.secret-key||
|
SecretKey|spring.cloud.nacos.discovery.secret-key||
|
||||||
Metadata|spring.cloud.nacos.discovery.metadata||使用Map格式配置
|
Metadata|spring.cloud.nacos.discovery.metadata||使用Map格式配置
|
||||||
日志文件名|spring.cloud.nacos.discovery.log-name||
|
日志文件名|spring.cloud.nacos.discovery.log-name||
|
||||||
接入点|spring.cloud.nacos.discovery.enpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
|
接入点|spring.cloud.nacos.discovery.endpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
|
||||||
是否集成Ribbon|ribbon.nacos.enabled|true|
|
是否集成Ribbon|ribbon.nacos.enabled|true|
|
||||||
|
|
||||||
|
|
||||||
|
@ -205,7 +205,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key||
|
|||||||
SecretKey|spring.cloud.nacos.discovery.secret-key||
|
SecretKey|spring.cloud.nacos.discovery.secret-key||
|
||||||
Metadata|spring.cloud.nacos.discovery.metadata||Extended data, Configure using Map format
|
Metadata|spring.cloud.nacos.discovery.metadata||Extended data, Configure using Map format
|
||||||
log name|spring.cloud.nacos.discovery.log-name||
|
log name|spring.cloud.nacos.discovery.log-name||
|
||||||
endpoint|spring.cloud.nacos.discovery.enpoint||The domain name of a service, through which the server address can be dynamically obtained.
|
endpoint|spring.cloud.nacos.discovery.endpoint||The domain name of a service, through which the server address can be dynamically obtained.
|
||||||
Integration Ribbon|ribbon.nacos.enabled|true|
|
Integration Ribbon|ribbon.nacos.enabled|true|
|
||||||
|
|
||||||
|
|
||||||
|
@ -49,9 +49,9 @@ public class NacosConfigProperties {
|
|||||||
*/
|
*/
|
||||||
private String prefix;
|
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.
|
* timeout for get config from nacos.
|
||||||
@ -59,7 +59,8 @@ public class NacosConfigProperties {
|
|||||||
private int timeout = 3000;
|
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;
|
private String endpoint;
|
||||||
|
|
||||||
@ -88,7 +89,7 @@ public class NacosConfigProperties {
|
|||||||
*/
|
*/
|
||||||
private String clusterName;
|
private String clusterName;
|
||||||
|
|
||||||
//todo sts support
|
// todo sts support
|
||||||
|
|
||||||
public String getServerAddr() {
|
public String getServerAddr() {
|
||||||
return serverAddr;
|
return serverAddr;
|
||||||
@ -106,12 +107,12 @@ public class NacosConfigProperties {
|
|||||||
this.prefix = prefix;
|
this.prefix = prefix;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContentType() {
|
public String getFileExtension() {
|
||||||
return contentType;
|
return fileExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setContentType(String contentType) {
|
public void setFileExtension(String fileExtension) {
|
||||||
this.contentType = contentType;
|
this.fileExtension = fileExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getGroup() {
|
public String getGroup() {
|
||||||
@ -188,50 +189,52 @@ public class NacosConfigProperties {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NacosConfigProperties{" +
|
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
||||||
"serverAddr='" + serverAddr + '\'' +
|
+ ", encode='" + encode + '\'' + ", group='" + group + '\'' + ", prefix='"
|
||||||
", encode='" + encode + '\'' +
|
+ prefix + '\'' + ", fileExtension='" + fileExtension + '\''
|
||||||
", group='" + group + '\'' +
|
+ ", timeout=" + timeout + ", endpoint='" + endpoint + '\''
|
||||||
", prefix='" + prefix + '\'' +
|
+ ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\''
|
||||||
", contentType='" + contentType + '\'' +
|
+ ", secretKey='" + secretKey + '\'' + ", contextPath='" + contextPath
|
||||||
", timeout=" + timeout +
|
+ '\'' + ", clusterName='" + clusterName + '\'' + '}';
|
||||||
", 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())) {
|
if (StringUtils.isEmpty(this.getServerAddr())) {
|
||||||
this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}"));
|
this.setServerAddr(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getEncode())) {
|
if (StringUtils.isEmpty(this.getEncode())) {
|
||||||
this.setEncode(env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}"));
|
this.setEncode(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getNamespace())) {
|
if (StringUtils.isEmpty(this.getNamespace())) {
|
||||||
this.setNamespace(env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}"));
|
this.setNamespace(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getAccessKey())) {
|
if (StringUtils.isEmpty(this.getAccessKey())) {
|
||||||
this.setAccessKey(env.resolvePlaceholders("${spring.cloud.nacos.config.access-key:}"));
|
this.setAccessKey(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.access-key:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getSecretKey())) {
|
if (StringUtils.isEmpty(this.getSecretKey())) {
|
||||||
this.setSecretKey(env.resolvePlaceholders("${spring.cloud.nacos.config.secret-key:}"));
|
this.setSecretKey(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.secret-key:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getContextPath())) {
|
if (StringUtils.isEmpty(this.getContextPath())) {
|
||||||
this.setContextPath(env.resolvePlaceholders("${spring.cloud.nacos.config.context-path:}"));
|
this.setContextPath(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.config.context-path:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getClusterName())) {
|
if (StringUtils.isEmpty(this.getClusterName())) {
|
||||||
this.setClusterName(env.resolvePlaceholders("${spring.cloud.nacos.config.cluster-name:}"));
|
this.setClusterName(env
|
||||||
|
.resolvePlaceholders("${spring.cloud.nacos.config.cluster-name:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getEndpoint())) {
|
if (StringUtils.isEmpty(this.getEndpoint())) {
|
||||||
this.setEndpoint(env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}"));
|
this.setEndpoint(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}"));
|
||||||
}
|
}
|
||||||
if(StringUtils.isEmpty(this.getPrefix())) {
|
if (StringUtils.isEmpty(this.getPrefix())) {
|
||||||
this.setPrefix(env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}"));
|
this.setPrefix(
|
||||||
|
env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,19 +69,19 @@ public class NacosPropertySourceBuilder {
|
|||||||
* @param dataId Nacos dataId
|
* @param dataId Nacos dataId
|
||||||
* @param group Nacos group
|
* @param group Nacos group
|
||||||
*/
|
*/
|
||||||
NacosPropertySource build(String dataId, String group, String contentType) {
|
NacosPropertySource build(String dataId, String group, String fileExtension) {
|
||||||
Properties p = loadNacosData(dataId, group, contentType);
|
Properties p = loadNacosData(dataId, group, fileExtension);
|
||||||
if (p == null) {
|
if (p == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return new NacosPropertySource(dataId, propertiesToMap(p), new Date());
|
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;
|
String data = null;
|
||||||
try {
|
try {
|
||||||
data = configService.getConfig(dataId, group, timeout);
|
data = configService.getConfig(dataId, group, timeout);
|
||||||
// todo add content type yaml support
|
// todo add file extension yaml support
|
||||||
if (!StringUtils.isEmpty(data)) {
|
if (!StringUtils.isEmpty(data)) {
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
|
logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
|
||||||
|
@ -108,34 +108,34 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
dataIdPrefix = applicationName;
|
dataIdPrefix = applicationName;
|
||||||
}
|
}
|
||||||
|
|
||||||
String contentType = nacosConfigProperties.getContentType();
|
String fileExtension = nacosConfigProperties.getFileExtension();
|
||||||
|
|
||||||
CompositePropertySource composite = new CompositePropertySource(
|
CompositePropertySource composite = new CompositePropertySource(
|
||||||
NACOS_PROPERTY_SOURCE_NAME);
|
NACOS_PROPERTY_SOURCE_NAME);
|
||||||
|
|
||||||
loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix,
|
loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix,
|
||||||
contentType);
|
fileExtension);
|
||||||
|
|
||||||
return composite;
|
return composite;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadApplicationConfiguration(
|
private void loadApplicationConfiguration(
|
||||||
CompositePropertySource compositePropertySource, Environment environment,
|
CompositePropertySource compositePropertySource, Environment environment,
|
||||||
String nacosGroup, String dataIdPrefix, String contentType) {
|
String nacosGroup, String dataIdPrefix, String fileExtension) {
|
||||||
loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + contentType,
|
loadNacosDataIfPresent(compositePropertySource,
|
||||||
nacosGroup, contentType);
|
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension);
|
||||||
for (String profile : environment.getActiveProfiles()) {
|
for (String profile : environment.getActiveProfiles()) {
|
||||||
String dataId = dataIdPrefix + SEP1 + profile + DOT + contentType;
|
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
|
||||||
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
|
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
|
||||||
contentType);
|
fileExtension);
|
||||||
}
|
}
|
||||||
// todo multi profile active order and priority
|
// todo multi profile active order and priority
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
||||||
final String dataId, final String group, String contentType) {
|
final String dataId, final String group, String fileExtension) {
|
||||||
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
|
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
|
||||||
contentType);
|
fileExtension);
|
||||||
if (ps != null) {
|
if (ps != null) {
|
||||||
composite.addFirstPropertySource(ps);
|
composite.addFirstPropertySource(ps);
|
||||||
}
|
}
|
||||||
|
@ -9,12 +9,12 @@
|
|||||||
"name": "spring.cloud.nacos.config.prefix",
|
"name": "spring.cloud.nacos.config.prefix",
|
||||||
"type": "java.lang.String",
|
"type": "java.lang.String",
|
||||||
"defaultValue": "${spring.application.name}",
|
"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",
|
"type": "java.lang.String",
|
||||||
"defaultValue": "properties",
|
"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."
|
||||||
}
|
}
|
||||||
]}
|
]}
|
@ -71,7 +71,7 @@ public class NacosConfigAutoConfigurationTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class);
|
NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class);
|
||||||
assertThat(nacosConfigProperties.getContentType()).isEqualTo("properties");
|
assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties");
|
||||||
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp");
|
assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user