Support more config for Nacos

pull/1124/head
yuhuangbin 5 years ago
parent 829dc23550
commit 792d008863

@ -33,6 +33,7 @@
<aliyun.java.sdk.dysmsapi>1.1.0</aliyun.java.sdk.dysmsapi>
<aliyun.sdk.mns>1.1.8.6</aliyun.sdk.mns>
<aliyun.java.sdk.dyvmsapi>1.1.1</aliyun.java.sdk.dyvmsapi>
<spring.context.support.version>1.0.5</spring.context.support.version>
</properties>
<dependencyManagement>
@ -385,6 +386,11 @@
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.context.support.version}</version>
</dependency>
<!-- Testing Dependencies -->
@ -422,6 +428,13 @@
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>sonatype-nexus</id>
<url>https://oss.sonatype.org/content/repositories/releases</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>

@ -19,6 +19,11 @@
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<!--spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>

@ -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<String, Object> 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 + '\''

@ -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) {

@ -19,6 +19,11 @@
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>

@ -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<String, Object> 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();
}
}

@ -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");
}
}

Loading…
Cancel
Save