mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Merge remote-tracking branch 'upstream/master'
# Conflicts: # pom.xml # spring-cloud-alibaba-dependencies/pom.xml # spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml
This commit is contained in:
@@ -27,15 +27,16 @@ import org.springframework.context.annotation.Configuration;
|
||||
@Configuration
|
||||
public class NacosConfigBootstrapConfiguration {
|
||||
|
||||
@Bean
|
||||
public NacosPropertySourceLocator nacosPropertySourceLocator() {
|
||||
return new NacosPropertySourceLocator();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public NacosConfigProperties nacosConfigProperties() {
|
||||
return new NacosConfigProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public NacosPropertySourceLocator nacosPropertySourceLocator(
|
||||
NacosConfigProperties nacosConfigProperties) {
|
||||
return new NacosPropertySourceLocator(nacosConfigProperties);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -18,8 +18,8 @@ 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.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -30,7 +30,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
|
||||
@@ -42,10 +49,9 @@ import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
||||
@ConfigurationProperties(NacosConfigProperties.PREFIX)
|
||||
public class NacosConfigProperties {
|
||||
|
||||
public static final String PREFIX = "spring.cloud.nacos.config";
|
||||
static final String PREFIX = "spring.cloud.nacos.config";
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory
|
||||
.getLogger(NacosConfigProperties.class);
|
||||
private static final Log log = LogFactory.getLog(NacosConfigProperties.class);
|
||||
|
||||
/**
|
||||
* nacos config server address
|
||||
@@ -348,7 +354,9 @@ public class NacosConfigProperties {
|
||||
return configService;
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOGGER.error("create config service error!properties={},e=,", this, e);
|
||||
log.error(
|
||||
"create config service error!properties=" + this.toString() + ",e=,",
|
||||
e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@@ -18,23 +18,26 @@ 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.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
|
||||
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
|
||||
* @author pbting
|
||||
*/
|
||||
public class NacosPropertySourceBuilder {
|
||||
private static final Logger LOGGER = LoggerFactory
|
||||
.getLogger(NacosPropertySourceBuilder.class);
|
||||
private static final Log log = LogFactory.getLog(NacosPropertySourceBuilder.class);
|
||||
private static final Properties EMPTY_PROPERTIES = new Properties();
|
||||
|
||||
private ConfigService configService;
|
||||
@@ -68,9 +71,6 @@ public class NacosPropertySourceBuilder {
|
||||
NacosPropertySource build(String dataId, String group, String fileExtension,
|
||||
boolean isRefreshable) {
|
||||
Properties p = loadNacosData(dataId, group, fileExtension);
|
||||
if (p == null) {
|
||||
p = EMPTY_PROPERTIES;
|
||||
}
|
||||
NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
|
||||
propertiesToMap(p), new Date(), isRefreshable);
|
||||
NacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource);
|
||||
@@ -82,7 +82,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'",
|
||||
log.info(String.format("Loading nacos data, dataId: '%s', group: '%s'",
|
||||
dataId, group));
|
||||
|
||||
if (fileExtension.equalsIgnoreCase("properties")) {
|
||||
@@ -101,13 +101,13 @@ public class NacosPropertySourceBuilder {
|
||||
}
|
||||
}
|
||||
catch (NacosException e) {
|
||||
LOGGER.error("get data from Nacos error,dataId:{}, ", dataId, e);
|
||||
log.error("get data from Nacos error,dataId:" + dataId + ", ", e);
|
||||
}
|
||||
catch (Exception e) {
|
||||
LOGGER.error("parse data from Nacos error,dataId:{},data:{},", dataId, data,
|
||||
e);
|
||||
log.error("parse data from Nacos error,dataId:" + dataId + ",data:" + data
|
||||
+ ",", e);
|
||||
}
|
||||
return null;
|
||||
return EMPTY_PROPERTIES;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@@ -17,9 +17,8 @@
|
||||
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.beans.factory.annotation.Autowired;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
|
||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher;
|
||||
@@ -40,8 +39,7 @@ import java.util.List;
|
||||
@Order(0)
|
||||
public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory
|
||||
.getLogger(NacosPropertySourceLocator.class);
|
||||
private static final Log log = LogFactory.getLog(NacosPropertySourceLocator.class);
|
||||
private static final String NACOS_PROPERTY_SOURCE_NAME = "NACOS";
|
||||
private static final String SEP1 = "-";
|
||||
private static final String DOT = ".";
|
||||
@@ -49,22 +47,21 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
private static final List<String> SUPPORT_FILE_EXTENSION = Arrays.asList("properties",
|
||||
"yaml", "yml");
|
||||
|
||||
@Autowired
|
||||
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
|
||||
|
||||
private NacosConfigProperties nacosConfigProperties;
|
||||
|
||||
public NacosPropertySourceLocator() {
|
||||
public NacosPropertySourceLocator(NacosConfigProperties nacosConfigProperties) {
|
||||
this.nacosConfigProperties = nacosConfigProperties;
|
||||
}
|
||||
|
||||
private NacosPropertySourceBuilder nacosPropertySourceBuilder;
|
||||
|
||||
@Override
|
||||
public PropertySource<?> locate(Environment env) {
|
||||
|
||||
ConfigService configService = nacosConfigProperties.configServiceInstance();
|
||||
|
||||
if (null == configService) {
|
||||
LOGGER.warn(
|
||||
"no instance of config service found, can't load config from nacos");
|
||||
log.warn("no instance of config service found, can't load config from nacos");
|
||||
return null;
|
||||
}
|
||||
long timeout = nacosConfigProperties.getTimeout();
|
||||
@@ -167,7 +164,7 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
||||
final String dataId, final String group, String fileExtension,
|
||||
boolean isRefreshable) {
|
||||
if (NacosContextRefresher.loadCount.get() != 0) {
|
||||
if (NacosContextRefresher.getRefreshCount() != 0) {
|
||||
NacosPropertySource ps;
|
||||
if (!isRefreshable) {
|
||||
ps = NacosPropertySourceRepository.getNacosPropertySource(dataId);
|
||||
@@ -187,13 +184,18 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private static void checkDataIdFileExtension(String[] sharedDataIdArry) {
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
outline: for (int i = 0; i < sharedDataIdArry.length; i++) {
|
||||
for (int i = 0; i < sharedDataIdArry.length; i++) {
|
||||
boolean isLegal = false;
|
||||
for (String fileExtension : SUPPORT_FILE_EXTENSION) {
|
||||
if (sharedDataIdArry[i].indexOf(fileExtension) > 0) {
|
||||
continue outline;
|
||||
isLegal = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
stringBuilder.append(sharedDataIdArry[i] + ",");
|
||||
// add tips
|
||||
if (!isLegal) {
|
||||
stringBuilder.append(sharedDataIdArry[i] + ",");
|
||||
}
|
||||
}
|
||||
|
||||
if (stringBuilder.length() > 0) {
|
||||
|
@@ -19,8 +19,8 @@ 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.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
|
||||
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||
@@ -51,10 +51,9 @@ import java.util.concurrent.atomic.AtomicLong;
|
||||
public class NacosContextRefresher
|
||||
implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware {
|
||||
|
||||
private final static Logger LOGGER = LoggerFactory
|
||||
.getLogger(NacosContextRefresher.class);
|
||||
private final static Log log = LogFactory.getLog(NacosContextRefresher.class);
|
||||
|
||||
public static final AtomicLong loadCount = new AtomicLong(0);
|
||||
private static final AtomicLong REFRESH_COUNT = new AtomicLong(0);
|
||||
|
||||
private final NacosRefreshProperties refreshProperties;
|
||||
|
||||
@@ -108,7 +107,7 @@ public class NacosContextRefresher
|
||||
Listener listener = listenerMap.computeIfAbsent(dataId, i -> new Listener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
loadCount.incrementAndGet();
|
||||
refreshCountIncrement();
|
||||
String md5 = "";
|
||||
if (!StringUtils.isEmpty(configInfo)) {
|
||||
try {
|
||||
@@ -117,14 +116,14 @@ public class NacosContextRefresher
|
||||
.toString(16);
|
||||
}
|
||||
catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
|
||||
LOGGER.warn("[Nacos] unable to get md5 for dataId: " + dataId, e);
|
||||
log.warn("[Nacos] unable to get md5 for dataId: " + dataId, e);
|
||||
}
|
||||
}
|
||||
refreshHistory.add(dataId, md5);
|
||||
applicationContext.publishEvent(
|
||||
new RefreshEvent(this, null, "Refresh Nacos config"));
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug("Refresh Nacos config group{},dataId{}", group, dataId);
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Refresh Nacos config group " + group + ",dataId" + dataId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,4 +141,11 @@ public class NacosContextRefresher
|
||||
}
|
||||
}
|
||||
|
||||
public static long getRefreshCount() {
|
||||
return REFRESH_COUNT.get();
|
||||
}
|
||||
|
||||
public static void refreshCountIncrement() {
|
||||
REFRESH_COUNT.incrementAndGet();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user