mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
update nacos config support enabled
This commit is contained in:
parent
6f0d76e4aa
commit
ca5d5282a5
@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.alibaba.nacos;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshProperties;
|
||||
@ -28,6 +29,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author juven.xuxb
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
|
||||
public class NacosConfigAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
|
@ -26,6 +26,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author xiaojing
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
|
||||
public class NacosConfigBootstrapConfiguration {
|
||||
|
||||
@Bean
|
||||
@ -35,7 +36,6 @@ public class NacosConfigBootstrapConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
|
||||
public NacosPropertySourceLocator nacosPropertySourceLocator(
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
return new NacosPropertySourceLocator(nacosConfigProperties);
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.cloud.alibaba.nacos;
|
||||
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -30,7 +31,14 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.CONTEXT_PATH;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.ENCODE;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
|
||||
|
||||
/**
|
||||
* nacos properties
|
||||
@ -47,11 +55,6 @@ public class NacosConfigProperties {
|
||||
private static final Logger log = LoggerFactory
|
||||
.getLogger(NacosConfigProperties.class);
|
||||
|
||||
/**
|
||||
* whether to enable nacos config.
|
||||
*/
|
||||
private boolean enabled = true;
|
||||
|
||||
/**
|
||||
* nacos config server address
|
||||
*/
|
||||
@ -114,8 +117,6 @@ public class NacosConfigProperties {
|
||||
|
||||
private String name;
|
||||
|
||||
private String[] activeProfiles;
|
||||
|
||||
/**
|
||||
* the dataids for configurable multiple shared configurations , multiple separated by
|
||||
* commas .
|
||||
@ -134,24 +135,8 @@ public class NacosConfigProperties {
|
||||
|
||||
private ConfigService configService;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
this.activeProfiles = environment.getActiveProfiles();
|
||||
}
|
||||
|
||||
// todo sts support
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getServerAddr() {
|
||||
return serverAddr;
|
||||
}
|
||||
@ -252,10 +237,6 @@ public class NacosConfigProperties {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String[] getActiveProfiles() {
|
||||
return activeProfiles;
|
||||
}
|
||||
|
||||
public String getSharedDataids() {
|
||||
return sharedDataids;
|
||||
}
|
||||
@ -284,10 +265,6 @@ public class NacosConfigProperties {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setActiveProfiles(String[] activeProfiles) {
|
||||
this.activeProfiles = activeProfiles;
|
||||
}
|
||||
|
||||
public static class Config {
|
||||
/**
|
||||
* the data id of extended configuration
|
||||
@ -329,17 +306,15 @@ public class NacosConfigProperties {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NacosConfigProperties{" + "enabled=" + enabled + ", serverAddr='"
|
||||
+ serverAddr + '\'' + ", encode='" + encode + '\'' + ", group='" + group
|
||||
+ '\'' + ", prefix='" + prefix + '\'' + ", fileExtension='"
|
||||
+ fileExtension + '\'' + ", timeout=" + timeout + ", endpoint='"
|
||||
+ endpoint + '\'' + ", namespace='" + namespace + '\'' + ", accessKey='"
|
||||
+ accessKey + '\'' + ", secretKey='" + secretKey + '\''
|
||||
+ ", contextPath='" + contextPath + '\'' + ", clusterName='" + clusterName
|
||||
+ '\'' + ", name='" + name + '\'' + ", activeProfiles="
|
||||
+ Arrays.toString(activeProfiles) + ", sharedDataids='" + sharedDataids
|
||||
+ '\'' + ", refreshableDataids='" + refreshableDataids + '\''
|
||||
+ ", extConfig=" + extConfig + '}';
|
||||
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 + '\'' + ", name='" + name + '\''
|
||||
+ ", sharedDataids='" + sharedDataids + '\'' + ", refreshableDataids='"
|
||||
+ refreshableDataids + '\'' + ", extConfig=" + extConfig + '}';
|
||||
}
|
||||
|
||||
public ConfigService configServiceInstance() {
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.cloud.alibaba.nacos.client;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
@ -26,7 +27,11 @@ import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.*;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
|
@ -17,6 +17,7 @@
|
||||
package org.springframework.cloud.alibaba.nacos.client;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
||||
@ -70,7 +71,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
timeout);
|
||||
String name = nacosConfigProperties.getName();
|
||||
|
||||
String nacosGroup = nacosConfigProperties.getGroup();
|
||||
String dataIdPrefix = nacosConfigProperties.getPrefix();
|
||||
if (StringUtils.isEmpty(dataIdPrefix)) {
|
||||
dataIdPrefix = name;
|
||||
@ -80,17 +80,12 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
dataIdPrefix = env.getProperty("spring.application.name");
|
||||
}
|
||||
|
||||
List<String> profiles = Arrays.asList(env.getActiveProfiles());
|
||||
nacosConfigProperties.setActiveProfiles(profiles.toArray(new String[0]));
|
||||
|
||||
String fileExtension = nacosConfigProperties.getFileExtension();
|
||||
|
||||
CompositePropertySource composite = new CompositePropertySource(
|
||||
NACOS_PROPERTY_SOURCE_NAME);
|
||||
|
||||
loadSharedConfiguration(composite);
|
||||
loadExtConfiguration(composite);
|
||||
loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension);
|
||||
loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);
|
||||
|
||||
return composite;
|
||||
}
|
||||
@ -151,11 +146,15 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
}
|
||||
|
||||
private void loadApplicationConfiguration(
|
||||
CompositePropertySource compositePropertySource, String nacosGroup,
|
||||
String dataIdPrefix, String fileExtension) {
|
||||
CompositePropertySource compositePropertySource, String dataIdPrefix,
|
||||
NacosConfigProperties properties, Environment environment) {
|
||||
|
||||
String fileExtension = properties.getFileExtension();
|
||||
String nacosGroup = properties.getGroup();
|
||||
|
||||
loadNacosDataIfPresent(compositePropertySource,
|
||||
dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true);
|
||||
for (String profile : nacosConfigProperties.getActiveProfiles()) {
|
||||
for (String profile : environment.getActiveProfiles()) {
|
||||
String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
|
||||
loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
|
||||
fileExtension, true);
|
||||
|
@ -19,6 +19,7 @@ package org.springframework.cloud.alibaba.nacos.endpoint;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
||||
@ -29,6 +30,7 @@ import org.springframework.context.annotation.Bean;
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnClass(name = "org.springframework.boot.actuate.endpoint.AbstractEndpoint")
|
||||
@ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true)
|
||||
public class NacosConfigEndpointAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
|
@ -163,7 +163,6 @@ public class NacosConfigurationTests {
|
||||
checkoutNacosConfigFileExtension();
|
||||
checkoutNacosConfigTimeout();
|
||||
checkoutNacosConfigEncode();
|
||||
checkoutNacosConfigProfiles();
|
||||
|
||||
checkoutEndpoint();
|
||||
checkoutDataLoad();
|
||||
@ -239,11 +238,6 @@ public class NacosConfigurationTests {
|
||||
Assert.assertEquals(environment.getProperty("user.age"), "12");
|
||||
}
|
||||
|
||||
private void checkoutNacosConfigProfiles() {
|
||||
assertEquals("NacosConfigProperties' profiles is wrong",
|
||||
new String[] { "dev", "test" }, properties.getActiveProfiles());
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
NacosConfigEndpoint nacosConfigEndpoint = new NacosConfigEndpoint(properties,
|
||||
refreshHistory);
|
||||
|
Loading…
x
Reference in New Issue
Block a user