mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge pull request #1124 from yuhuangbin/nacos-branch
support more config for Nacos
This commit is contained in:
commit
1d99e35977
@ -33,6 +33,7 @@
|
|||||||
<aliyun.java.sdk.dysmsapi>1.1.0</aliyun.java.sdk.dysmsapi>
|
<aliyun.java.sdk.dysmsapi>1.1.0</aliyun.java.sdk.dysmsapi>
|
||||||
<aliyun.sdk.mns>1.1.8.6</aliyun.sdk.mns>
|
<aliyun.sdk.mns>1.1.8.6</aliyun.sdk.mns>
|
||||||
<aliyun.java.sdk.dyvmsapi>1.1.1</aliyun.java.sdk.dyvmsapi>
|
<aliyun.java.sdk.dyvmsapi>1.1.1</aliyun.java.sdk.dyvmsapi>
|
||||||
|
<spring.context.support.version>1.0.5</spring.context.support.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@ -385,6 +386,11 @@
|
|||||||
<version>${project.version}</version>
|
<version>${project.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.spring</groupId>
|
||||||
|
<artifactId>spring-context-support</artifactId>
|
||||||
|
<version>${spring.context.support.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Testing Dependencies -->
|
<!-- Testing Dependencies -->
|
||||||
|
|
||||||
@ -422,6 +428,13 @@
|
|||||||
<enabled>false</enabled>
|
<enabled>false</enabled>
|
||||||
</snapshots>
|
</snapshots>
|
||||||
</repository>
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>sonatype-nexus</id>
|
||||||
|
<url>https://oss.sonatype.org/content/repositories/releases</url>
|
||||||
|
<releases>
|
||||||
|
<enabled>true</enabled>
|
||||||
|
</releases>
|
||||||
|
</repository>
|
||||||
</repositories>
|
</repositories>
|
||||||
<pluginRepositories>
|
<pluginRepositories>
|
||||||
<pluginRepository>
|
<pluginRepository>
|
||||||
|
@ -19,6 +19,11 @@
|
|||||||
<artifactId>nacos-client</artifactId>
|
<artifactId>nacos-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.spring</groupId>
|
||||||
|
<artifactId>spring-context-support</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!--spring cloud-->
|
<!--spring cloud-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
@ -17,8 +17,11 @@
|
|||||||
package com.alibaba.cloud.nacos;
|
package com.alibaba.cloud.nacos;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
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.NacosFactory;
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
import com.alibaba.nacos.api.exception.NacosException;
|
import com.alibaba.nacos.api.exception.NacosException;
|
||||||
|
import com.alibaba.spring.util.PropertySourcesUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@ -64,6 +69,8 @@ public class NacosConfigProperties {
|
|||||||
*/
|
*/
|
||||||
public static final String PREFIX = "spring.cloud.nacos.config";
|
public static final String PREFIX = "spring.cloud.nacos.config";
|
||||||
|
|
||||||
|
private static final Pattern PATTERN = Pattern.compile("-(\\w)");
|
||||||
|
|
||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(NacosConfigProperties.class);
|
.getLogger(NacosConfigProperties.class);
|
||||||
|
|
||||||
@ -80,8 +87,8 @@ public class NacosConfigProperties {
|
|||||||
String serverAddr = environment
|
String serverAddr = environment
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}");
|
.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}");
|
||||||
if (StringUtils.isEmpty(serverAddr)) {
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
serverAddr = environment
|
serverAddr = environment.resolvePlaceholders(
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.server-addr:localhost:8848}");
|
"${spring.cloud.nacos.server-addr:localhost:8848}");
|
||||||
}
|
}
|
||||||
this.setServerAddr(serverAddr);
|
this.setServerAddr(serverAddr);
|
||||||
}
|
}
|
||||||
@ -357,6 +364,14 @@ public class NacosConfigProperties {
|
|||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Environment getEnvironment() {
|
||||||
|
return environment;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnvironment(Environment environment) {
|
||||||
|
this.environment = environment;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ConfigService
|
* @return ConfigService
|
||||||
*/
|
*/
|
||||||
@ -399,9 +414,28 @@ public class NacosConfigProperties {
|
|||||||
else {
|
else {
|
||||||
properties.put(ENDPOINT, endpoint);
|
properties.put(ENDPOINT, endpoint);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enrichNacosConfigProperties(properties);
|
||||||
return 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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
||||||
|
@ -62,7 +62,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PropertySource<?> locate(Environment env) {
|
public PropertySource<?> locate(Environment env) {
|
||||||
|
nacosConfigProperties.setEnvironment(env);
|
||||||
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
||||||
|
|
||||||
if (null == configService) {
|
if (null == configService) {
|
||||||
|
@ -19,6 +19,11 @@
|
|||||||
<artifactId>nacos-client</artifactId>
|
<artifactId>nacos-client</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.spring</groupId>
|
||||||
|
<artifactId>spring-context-support</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-commons</artifactId>
|
<artifactId>spring-cloud-commons</artifactId>
|
||||||
|
@ -25,6 +25,8 @@ import java.util.HashMap;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
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.NamingService;
|
||||||
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
|
||||||
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
|
||||||
|
import com.alibaba.spring.util.PropertySourcesUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
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.beans.factory.annotation.Value;
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.cloud.commons.util.InetUtils;
|
import org.springframework.cloud.commons.util.InetUtils;
|
||||||
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
@ -66,6 +70,13 @@ public class NacosDiscoveryProperties {
|
|||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(NacosDiscoveryProperties.class);
|
.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.
|
* nacos discovery server address.
|
||||||
*/
|
*/
|
||||||
@ -430,8 +441,8 @@ public class NacosDiscoveryProperties {
|
|||||||
String serverAddr = env
|
String serverAddr = env
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}");
|
.resolvePlaceholders("${spring.cloud.nacos.discovery.server-addr:}");
|
||||||
if (StringUtils.isEmpty(serverAddr)) {
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
serverAddr = env
|
serverAddr = env.resolvePlaceholders(
|
||||||
.resolvePlaceholders("${spring.cloud.nacos.server-addr:localhost:8848}");
|
"${spring.cloud.nacos.server-addr:localhost:8848}");
|
||||||
}
|
}
|
||||||
this.setServerAddr(serverAddr);
|
this.setServerAddr(serverAddr);
|
||||||
}
|
}
|
||||||
@ -518,7 +529,26 @@ public class NacosDiscoveryProperties {
|
|||||||
properties.put(SECRET_KEY, secretKey);
|
properties.put(SECRET_KEY, secretKey);
|
||||||
properties.put(CLUSTER_NAME, clusterName);
|
properties.put(CLUSTER_NAME, clusterName);
|
||||||
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
|
properties.put(NAMING_LOAD_CACHE_AT_START, namingLoadCacheAtStart);
|
||||||
|
|
||||||
|
enrichNacosDiscoveryProperties(properties);
|
||||||
return 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) {
|
public void preCheck(String dataSourceName) {
|
||||||
if (StringUtils.isEmpty(serverAddr)) {
|
if (StringUtils.isEmpty(serverAddr)) {
|
||||||
serverAddr = this.getEnv().getProperty(
|
serverAddr = this.getEnv().getProperty(
|
||||||
"spring.cloud.sentinel.datasource.nacos.server-addr", "localhost:8848");
|
"spring.cloud.sentinel.datasource.nacos.server-addr",
|
||||||
|
"localhost:8848");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user