From 792d008863af3bc86eae0c3cfa980af514f73a71 Mon Sep 17 00:00:00 2001 From: yuhuangbin Date: Wed, 11 Dec 2019 14:17:08 +0800 Subject: [PATCH] Support more config for Nacos --- spring-cloud-alibaba-dependencies/pom.xml | 13 +++++++ spring-cloud-alibaba-nacos-config/pom.xml | 5 +++ .../cloud/nacos/NacosConfigProperties.java | 38 ++++++++++++++++++- .../client/NacosPropertySourceLocator.java | 2 +- spring-cloud-alibaba-nacos-discovery/pom.xml | 5 +++ .../cloud/nacos/NacosDiscoveryProperties.java | 34 ++++++++++++++++- .../config/NacosDataSourceProperties.java | 3 +- 7 files changed, 94 insertions(+), 6 deletions(-) diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 13dc4f598..326931d42 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -33,6 +33,7 @@ 1.1.0 1.1.8.6 1.1.1 + 1.0.5 @@ -385,6 +386,11 @@ ${project.version} + + com.alibaba.spring + spring-context-support + ${spring.context.support.version} + @@ -422,6 +428,13 @@ false + + sonatype-nexus + https://oss.sonatype.org/content/repositories/releases + + true + + diff --git a/spring-cloud-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-nacos-config/pom.xml index cf06182e1..b51f6d9d7 100644 --- a/spring-cloud-alibaba-nacos-config/pom.xml +++ b/spring-cloud-alibaba-nacos-config/pom.xml @@ -19,6 +19,11 @@ nacos-client + + com.alibaba.spring + spring-context-support + + org.springframework.cloud diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java index 853d8ee07..0a5005d3a 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/NacosConfigProperties.java @@ -17,8 +17,11 @@ package com.alibaba.cloud.nacos; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.annotation.PostConstruct; @@ -26,11 +29,13 @@ import com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureExcept import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.spring.util.PropertySourcesUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; @@ -64,6 +69,8 @@ public class NacosConfigProperties { */ public static final String PREFIX = "spring.cloud.nacos.config"; + private static final Pattern PATTERN = Pattern.compile("-(\\w)"); + private static final Logger log = LoggerFactory .getLogger(NacosConfigProperties.class); @@ -80,8 +87,8 @@ public class NacosConfigProperties { String serverAddr = environment .resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}"); if (StringUtils.isEmpty(serverAddr)) { - serverAddr = environment - .resolvePlaceholders("${spring.cloud.nacos.server-addr:localhost:8848}"); + serverAddr = environment.resolvePlaceholders( + "${spring.cloud.nacos.server-addr:localhost:8848}"); } this.setServerAddr(serverAddr); } @@ -357,6 +364,14 @@ public class NacosConfigProperties { this.name = name; } + public Environment getEnvironment() { + return environment; + } + + public void setEnvironment(Environment environment) { + this.environment = environment; + } + /** * @return ConfigService */ @@ -399,9 +414,28 @@ public class NacosConfigProperties { else { properties.put(ENDPOINT, endpoint); } + + enrichNacosConfigProperties(properties); return properties; } + private void enrichNacosConfigProperties(Properties nacosConfigProperties) { + Map properties = PropertySourcesUtils + .getSubProperties((ConfigurableEnvironment) environment, PREFIX); + properties.forEach((k, v) -> nacosConfigProperties.putIfAbsent(resolveKey(k), + String.valueOf(v))); + } + + private String resolveKey(String key) { + Matcher matcher = PATTERN.matcher(key); + StringBuffer sb = new StringBuffer(); + while (matcher.find()) { + matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); + } + matcher.appendTail(sb); + return sb.toString(); + } + @Override public String toString() { return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\'' diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java index 560545a28..af51b3aab 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/com/alibaba/cloud/nacos/client/NacosPropertySourceLocator.java @@ -62,7 +62,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { @Override public PropertySource locate(Environment env) { - + nacosConfigProperties.setEnvironment(env); ConfigService configService = nacosConfigProperties.configServiceInstance(); if (null == configService) { diff --git a/spring-cloud-alibaba-nacos-discovery/pom.xml b/spring-cloud-alibaba-nacos-discovery/pom.xml index 968267718..c87b1793e 100644 --- a/spring-cloud-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-alibaba-nacos-discovery/pom.xml @@ -19,6 +19,11 @@ nacos-client + + com.alibaba.spring + spring-context-support + + org.springframework.cloud spring-cloud-commons diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java index 23e476876..3fb9dfec1 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/com/alibaba/cloud/nacos/NacosDiscoveryProperties.java @@ -25,6 +25,8 @@ import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import javax.annotation.PostConstruct; @@ -34,6 +36,7 @@ import com.alibaba.nacos.api.naming.NamingMaintainService; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.PreservedMetadataKeys; import com.alibaba.nacos.client.naming.utils.UtilAndComs; +import com.alibaba.spring.util.PropertySourcesUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,6 +44,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.commons.util.InetUtils; +import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; @@ -66,6 +70,13 @@ public class NacosDiscoveryProperties { private static final Logger log = LoggerFactory .getLogger(NacosDiscoveryProperties.class); + /** + * Prefix of {@link NacosDiscoveryProperties}. + */ + public static final String PREFIX = "spring.cloud.nacos.discovery"; + + private static final Pattern PATTERN = Pattern.compile("-(\\w)"); + /** * nacos discovery server address. */ @@ -430,8 +441,8 @@ public class NacosDiscoveryProperties { String serverAddr = env .resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}"); if (StringUtils.isEmpty(serverAddr)) { - serverAddr = env - .resolvePlaceholders("${spring.cloud.nacos.server-addr:localhost:8848}"); + serverAddr = env.resolvePlaceholders( + "${spring.cloud.nacos.server-addr:localhost:8848}"); } this.setServerAddr(serverAddr); } @@ -518,7 +529,26 @@ public class NacosDiscoveryProperties { properties.put(SECRET_KEY, secretKey); properties.put(CLUSTER_NAME, clusterName); properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart); + + enrichNacosDiscoveryProperties(properties); return properties; } + private void enrichNacosDiscoveryProperties(Properties nacosDiscoveryProperties) { + Map properties = PropertySourcesUtils + .getSubProperties((ConfigurableEnvironment) environment, PREFIX); + properties.forEach((k, v) -> nacosDiscoveryProperties.putIfAbsent(resolveKey(k), + String.valueOf(v))); + } + + private String resolveKey(String key) { + Matcher matcher = PATTERN.matcher(key); + StringBuffer sb = new StringBuffer(); + while (matcher.find()) { + matcher.appendReplacement(sb, matcher.group(1).toUpperCase()); + } + matcher.appendTail(sb); + return sb.toString(); + } + } diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/NacosDataSourceProperties.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/NacosDataSourceProperties.java index 3fc74945c..e0aa9e741 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/NacosDataSourceProperties.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/com/alibaba/cloud/sentinel/datasource/config/NacosDataSourceProperties.java @@ -54,7 +54,8 @@ public class NacosDataSourceProperties extends AbstractDataSourceProperties { public void preCheck(String dataSourceName) { if (StringUtils.isEmpty(serverAddr)) { serverAddr = this.getEnv().getProperty( - "spring.cloud.sentinel.datasource.nacos.server-addr", "localhost:8848"); + "spring.cloud.sentinel.datasource.nacos.server-addr", + "localhost:8848"); } }