mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
update 1.x branch code from master
This commit is contained in:
@@ -16,14 +16,11 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.nacos;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshProperties;
|
||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -31,19 +28,23 @@ import org.springframework.context.annotation.Configuration;
|
||||
* @author juven.xuxb
|
||||
*/
|
||||
@Configuration
|
||||
public class NacosConfigAutoConfiguration implements ApplicationContextAware {
|
||||
public class NacosConfigAutoConfiguration {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
@Autowired
|
||||
private NacosRefreshProperties nacosRefreshProperties;
|
||||
@Bean
|
||||
public NacosConfigProperties nacosConfigProperties(ApplicationContext context) {
|
||||
if (context.getParent() != null
|
||||
&& BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
||||
context.getParent(), NacosConfigProperties.class).length > 0) {
|
||||
return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(),
|
||||
NacosConfigProperties.class);
|
||||
}
|
||||
NacosConfigProperties nacosConfigProperties = new NacosConfigProperties();
|
||||
return nacosConfigProperties;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosPropertySourceRepository nacosPropertySourceRepository() {
|
||||
return new NacosPropertySourceRepository(applicationContext);
|
||||
return new NacosPropertySourceRepository();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -57,17 +58,12 @@ public class NacosConfigAutoConfiguration implements ApplicationContextAware {
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosContextRefresher nacosContextRefresher(ContextRefresher contextRefresher,
|
||||
public NacosContextRefresher nacosContextRefresher(
|
||||
NacosConfigProperties nacosConfigProperties,
|
||||
NacosRefreshProperties nacosRefreshProperties,
|
||||
NacosRefreshHistory refreshHistory,
|
||||
NacosPropertySourceRepository propertySourceRepository) {
|
||||
return new NacosContextRefresher(contextRefresher, nacosConfigProperties,
|
||||
nacosRefreshProperties, refreshHistory, propertySourceRepository,
|
||||
nacosConfigProperties.configServiceInstance());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
return new NacosContextRefresher(nacosRefreshProperties, refreshHistory,
|
||||
propertySourceRepository, nacosConfigProperties.configServiceInstance());
|
||||
}
|
||||
}
|
||||
|
@@ -28,8 +28,9 @@ import org.springframework.context.annotation.Configuration;
|
||||
public class NacosConfigBootstrapConfiguration {
|
||||
|
||||
@Bean
|
||||
public NacosPropertySourceLocator nacosPropertySourceLocator() {
|
||||
return new NacosPropertySourceLocator();
|
||||
public NacosPropertySourceLocator nacosPropertySourceLocator(
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
return new NacosPropertySourceLocator(nacosConfigProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@@ -16,8 +16,15 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.nacos;
|
||||
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -25,13 +32,8 @@ import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
|
||||
import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
||||
import com.alibaba.nacos.api.NacosFactory;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
/**
|
||||
* nacos properties
|
||||
@@ -40,9 +42,11 @@ import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
||||
* @author xiaojing
|
||||
* @author pbting
|
||||
*/
|
||||
@ConfigurationProperties("spring.cloud.nacos.config")
|
||||
@ConfigurationProperties(NacosConfigProperties.PREFIX)
|
||||
public class NacosConfigProperties {
|
||||
|
||||
public static final String PREFIX = "spring.cloud.nacos.config";
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory
|
||||
.getLogger(NacosConfigProperties.class);
|
||||
|
||||
@@ -116,10 +120,12 @@ public class NacosConfigProperties {
|
||||
* commas .
|
||||
*/
|
||||
private String sharedDataids;
|
||||
|
||||
/**
|
||||
* refreshable dataids , multiple separated by commas .
|
||||
*/
|
||||
private String refreshableDataids;
|
||||
|
||||
/**
|
||||
* a set of extended configurations .
|
||||
*/
|
||||
@@ -307,17 +313,15 @@ public class NacosConfigProperties {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\''
|
||||
+ ", encode='" + encode + '\'' + ", group='" + group + '\'' + ", prefix='"
|
||||
+ ", encode='" + encode + '\'' + ", group='" + group + '\''
|
||||
+ ", sharedDataids='" + this.sharedDataids + '\''
|
||||
+ ", refreshableDataids='" + this.refreshableDataids + '\'' + ", 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
|
||||
+ ", configService=" + configService + ", environment=" + environment
|
||||
+ '}';
|
||||
+ ", activeProfiles=" + Arrays.toString(activeProfiles) + '}';
|
||||
}
|
||||
|
||||
public ConfigService configServiceInstance() {
|
||||
|
@@ -21,6 +21,7 @@ import java.util.List;
|
||||
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
@@ -28,11 +29,12 @@ import org.springframework.core.env.PropertySource;
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
public class NacosPropertySourceRepository {
|
||||
public class NacosPropertySourceRepository implements ApplicationContextAware {
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
public NacosPropertySourceRepository(ApplicationContext applicationContext) {
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@@ -54,7 +56,7 @@ public class NacosPropertySourceRepository {
|
||||
}
|
||||
|
||||
private void collectNacosPropertySources(CompositePropertySource composite,
|
||||
List<NacosPropertySource> result) {
|
||||
List<NacosPropertySource> result) {
|
||||
for (PropertySource p : composite.getPropertySources()) {
|
||||
if (p instanceof NacosPropertySource) {
|
||||
result.add((NacosPropertySource) p);
|
||||
|
@@ -16,32 +16,28 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.nacos.client;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import java.io.StringReader;
|
||||
import java.util.*;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.*;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
* @author pbting
|
||||
*/
|
||||
public class NacosPropertySourceBuilder {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
private static final Logger LOGGER = LoggerFactory
|
||||
.getLogger(NacosPropertySourceBuilder.class);
|
||||
|
||||
private ConfigService configService;
|
||||
private long timeout;
|
||||
|
||||
public NacosPropertySourceBuilder() {
|
||||
}
|
||||
|
||||
public NacosPropertySourceBuilder(ConfigService configService, long timeout) {
|
||||
this.configService = configService;
|
||||
this.timeout = timeout;
|
||||
@@ -82,7 +78,7 @@ public class NacosPropertySourceBuilder {
|
||||
try {
|
||||
data = configService.getConfig(dataId, group, timeout);
|
||||
if (!StringUtils.isEmpty(data)) {
|
||||
logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
|
||||
LOGGER.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
|
||||
dataId, group));
|
||||
|
||||
if (fileExtension.equalsIgnoreCase("properties")) {
|
||||
@@ -101,10 +97,10 @@ public class NacosPropertySourceBuilder {
|
||||
}
|
||||
}
|
||||
catch (NacosException e) {
|
||||
logger.error("get data from Nacos error,dataId:{}, ", dataId, e);
|
||||
LOGGER.error("get data from Nacos error,dataId:{}, ", dataId, e);
|
||||
}
|
||||
catch (Exception e) {
|
||||
logger.error("parse data from Nacos error,dataId:{},data:{},", dataId, data,
|
||||
LOGGER.error("parse data from Nacos error,dataId:{},data:{},", dataId, data,
|
||||
e);
|
||||
}
|
||||
return null;
|
||||
|
@@ -16,10 +16,11 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.nacos.client;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
||||
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
|
||||
import org.springframework.core.annotation.Order;
|
||||
@@ -28,8 +29,7 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
@@ -38,7 +38,7 @@ import java.util.List;
|
||||
@Order(0)
|
||||
public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private static final Logger logger = LoggerFactory
|
||||
private static final Logger LOGGER = LoggerFactory
|
||||
.getLogger(NacosPropertySourceLocator.class);
|
||||
private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";
|
||||
private static final String SEP1 = "-";
|
||||
@@ -47,9 +47,12 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
|
||||
"yaml", "yml");
|
||||
|
||||
@Autowired
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
public NacosPropertySourceLocator(NacosConfigProperties nacosConfigProperties) {
|
||||
this.nacosConfigProperties = nacosConfigProperties;
|
||||
}
|
||||
|
||||
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
|
||||
|
||||
@Override
|
||||
@@ -58,7 +61,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
||||
|
||||
if (null == configService) {
|
||||
logger.warn(
|
||||
LOGGER.warn(
|
||||
"no instance of config service found, can't load config from nacos");
|
||||
return null;
|
||||
}
|
||||
@@ -90,16 +93,20 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
CompositePropertySource compositePropertySource) {
|
||||
String sharedDataIds = nacosConfigProperties.getSharedDataids();
|
||||
String refreshDataIds = nacosConfigProperties.getRefreshableDataids();
|
||||
|
||||
if (sharedDataIds == null || sharedDataIds.trim().length() == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
String[] sharedDataIdArry = sharedDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR);
|
||||
checkDataIdFileExtension(sharedDataIdArry);
|
||||
|
||||
for (int i = 0; i < sharedDataIdArry.length; i++) {
|
||||
String dataId = sharedDataIdArry[i];
|
||||
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
|
||||
boolean isRefreshable = checkDataIdIsRefreshbable(refreshDataIds,
|
||||
sharedDataIdArry[i]);
|
||||
|
||||
loadNacosDataIfPresent(compositePropertySource, dataId, "DEFAULT_GROUP",
|
||||
fileExtension, isRefreshable);
|
||||
}
|
||||
@@ -110,9 +117,11 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
|| nacosConfigProperties.getExtConfig().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<NacosConfigProperties.Config> extConfigs = nacosConfigProperties
|
||||
.getExtConfig();
|
||||
checkExtConfiguration(extConfigs);
|
||||
|
||||
for (NacosConfigProperties.Config config : extConfigs) {
|
||||
String dataId = config.getDataId();
|
||||
String fileExtension = dataId.substring(dataId.lastIndexOf(".") + 1);
|
||||
@@ -167,6 +176,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
}
|
||||
stringBuilder.append(sharedDataIdArry[i] + ",");
|
||||
}
|
||||
|
||||
if (stringBuilder.length() > 0) {
|
||||
String result = stringBuilder.substring(0, stringBuilder.length() - 1);
|
||||
throw new IllegalStateException(String.format(
|
||||
@@ -180,12 +190,15 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
if (refreshDataIds == null || "".equals(refreshDataIds)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String[] refreshDataIdArry = refreshDataIds.split(SHARED_CONFIG_SEPRATOR_CHAR);
|
||||
for (String refreshDataId : refreshDataIdArry) {
|
||||
if (refreshDataId.equals(sharedDataId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -16,19 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.alibaba.nacos.refresh;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
@@ -36,6 +23,22 @@ import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||
import org.springframework.cloud.endpoint.event.RefreshEvent;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.alibaba.nacos.api.config.ConfigService;
|
||||
import com.alibaba.nacos.api.config.listener.Listener;
|
||||
import com.alibaba.nacos.api.exception.NacosException;
|
||||
|
||||
/**
|
||||
* On application start up, NacosContextRefresher add nacos listeners to all application
|
||||
@@ -45,13 +48,11 @@ import java.util.concurrent.Executor;
|
||||
* @author juven.xuxb
|
||||
* @author pbting
|
||||
*/
|
||||
public class NacosContextRefresher implements ApplicationListener<ApplicationReadyEvent> {
|
||||
public class NacosContextRefresher
|
||||
implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware {
|
||||
|
||||
private Logger logger = LoggerFactory.getLogger(NacosContextRefresher.class);
|
||||
|
||||
private final ContextRefresher contextRefresher;
|
||||
|
||||
private final NacosConfigProperties properties;
|
||||
private final static Logger LOGGER = LoggerFactory
|
||||
.getLogger(NacosContextRefresher.class);
|
||||
|
||||
private final NacosRefreshProperties refreshProperties;
|
||||
|
||||
@@ -61,15 +62,16 @@ public class NacosContextRefresher implements ApplicationListener<ApplicationRea
|
||||
|
||||
private final ConfigService configService;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private AtomicBoolean ready = new AtomicBoolean(true);
|
||||
|
||||
private Map<String, Listener> listenerMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
public NacosContextRefresher(ContextRefresher contextRefresher,
|
||||
NacosConfigProperties properties, NacosRefreshProperties refreshProperties,
|
||||
public NacosContextRefresher(NacosRefreshProperties refreshProperties,
|
||||
NacosRefreshHistory refreshHistory,
|
||||
NacosPropertySourceRepository nacosPropertySourceRepository,
|
||||
ConfigService configService) {
|
||||
this.contextRefresher = contextRefresher;
|
||||
this.properties = properties;
|
||||
this.refreshProperties = refreshProperties;
|
||||
this.refreshHistory = refreshHistory;
|
||||
this.nacosPropertySourceRepository = nacosPropertySourceRepository;
|
||||
@@ -78,7 +80,16 @@ public class NacosContextRefresher implements ApplicationListener<ApplicationRea
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||
this.registerNacosListenersForApplications();
|
||||
// many Spring context
|
||||
if (this.ready.get()) {
|
||||
this.registerNacosListenersForApplications();
|
||||
this.ready.compareAndSet(true, false);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
private void registerNacosListenersForApplications() {
|
||||
@@ -112,12 +123,17 @@ public class NacosContextRefresher implements ApplicationListener<ApplicationRea
|
||||
}
|
||||
catch (NoSuchAlgorithmException
|
||||
| UnsupportedEncodingException e) {
|
||||
logger.warn("[Nacos] unable to get md5 for dataId: " + dataId,
|
||||
LOGGER.warn("[Nacos] unable to get md5 for dataId: " + dataId,
|
||||
e);
|
||||
}
|
||||
}
|
||||
refreshHistory.add(dataId, md5);
|
||||
contextRefresher.refresh();
|
||||
applicationContext.publishEvent(
|
||||
new RefreshEvent(this, null, "Refresh Nacos config"));
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("Refresh Nacos config group{},dataId{}", group,
|
||||
dataId);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user