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