update nacos config properties , content-type to file-extension

pull/89/head
flystar32 7 years ago
parent 21dfd637b3
commit bfa8f24cc8

@ -112,15 +112,15 @@ 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`来配置。
* `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` 类型。
#### 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|dataId的后缀同时也是配置内容的文件格式目前只支持 properties
配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码
获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms
配置的命名空间|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
${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`.
* `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.
#### 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.

@ -197,7 +197,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key||
SecretKey|spring.cloud.nacos.discovery.secret-key||
Metadata|spring.cloud.nacos.discovery.metadata||使用Map格式配置
日志文件名|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|

@ -205,7 +205,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key||
SecretKey|spring.cloud.nacos.discovery.secret-key||
Metadata|spring.cloud.nacos.discovery.metadata||Extended data, Configure using Map format
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|

@ -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 + '\'' +
'}';
}
public void overrideFromEnv(Environment env){
if(StringUtils.isEmpty(this.getServerAddr())) {
this.setServerAddr(env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}"));
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) {
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,19 +69,19 @@ 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);
// todo add content type yaml support
// todo add file extension yaml support
if (!StringUtils.isEmpty(data)) {
Properties properties = new Properties();
logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",

@ -108,34 +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);
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) {
final String dataId, final String group, String fileExtension) {
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
contentType);
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."
}
]}

@ -71,7 +71,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…
Cancel
Save