1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

Merge pull request #202 from pbting/master

Prevent non-refreshing
This commit is contained in:
pbting 2018-12-19 21:17:42 +08:00 committed by GitHub
commit c300ffbbc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 80 additions and 86 deletions

View File

@ -56,9 +56,8 @@ public class NacosConfigAutoConfiguration {
public NacosContextRefresher nacosContextRefresher(
NacosConfigProperties nacosConfigProperties,
NacosRefreshProperties nacosRefreshProperties,
NacosRefreshHistory refreshHistory,
NacosPropertySourceRepository propertySourceRepository) {
NacosRefreshHistory refreshHistory) {
return new NacosContextRefresher(nacosRefreshProperties, refreshHistory,
propertySourceRepository, nacosConfigProperties.configServiceInstance());
nacosConfigProperties.configServiceInstance());
}
}

View File

@ -38,9 +38,4 @@ public class NacosConfigBootstrapConfiguration {
return new NacosConfigProperties();
}
@Bean
public NacosPropertySourceRepository nacosPropertySourceRepository() {
return new NacosPropertySourceRepository();
}
}

View File

@ -16,24 +16,21 @@
package org.springframework.cloud.alibaba.nacos;
import static com.alibaba.nacos.api.PropertyKeyConst.*;
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;
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 javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import static com.alibaba.nacos.api.PropertyKeyConst.*;
/**
* nacos properties
@ -110,7 +107,6 @@ public class NacosConfigProperties {
*/
private String clusterName;
@Value("${spring.application.name}")
private String name;
private String[] activeProfiles;
@ -271,6 +267,10 @@ public class NacosConfigProperties {
this.extConfig = extConfig;
}
public void setActiveProfiles(String[] activeProfiles) {
this.activeProfiles = activeProfiles;
}
public static class Config {
/**
* the data id of extended configuration
@ -348,4 +348,5 @@ public class NacosConfigProperties {
return null;
}
}
}

View File

@ -27,19 +27,24 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class NacosPropertySourceRepository {
private ConcurrentHashMap<String, NacosPropertySource> nacosPropertySourceRepository = new ConcurrentHashMap<>();
private final static ConcurrentHashMap<String, NacosPropertySource> nacosPropertySourceRepository = new ConcurrentHashMap<>();
/**
* @return all nacos properties from application context
*/
public List<NacosPropertySource> getAll() {
public static List<NacosPropertySource> getAll() {
List<NacosPropertySource> result = new ArrayList<>();
result.addAll(nacosPropertySourceRepository.values());
return result;
}
public void collectNacosPropertySources(NacosPropertySource nacosPropertySource) {
public static void collectNacosPropertySources(
NacosPropertySource nacosPropertySource) {
nacosPropertySourceRepository.putIfAbsent(nacosPropertySource.getDataId(),
nacosPropertySource);
}
public static NacosPropertySource getNacosPropertySource(String dataId) {
return nacosPropertySourceRepository.get(dataId);
}
}

View File

@ -37,7 +37,6 @@ public class NacosPropertySourceBuilder {
.getLogger(NacosPropertySourceBuilder.class);
private static final Properties EMPTY_PROPERTIES = new Properties();
private NacosPropertySourceRepository nacosPropertySourceRepository;
private ConfigService configService;
private long timeout;
@ -74,7 +73,7 @@ public class NacosPropertySourceBuilder {
}
NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
propertiesToMap(p), new Date(), isRefreshable);
nacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource);
NacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource);
return nacosPropertySource;
}
@ -128,12 +127,4 @@ public class NacosPropertySourceBuilder {
return result;
}
public NacosPropertySourceRepository getNacosPropertySourceRepository() {
return nacosPropertySourceRepository;
}
public void setNacosPropertySourceRepository(
NacosPropertySourceRepository nacosPropertySourceRepository) {
this.nacosPropertySourceRepository = nacosPropertySourceRepository;
}
}

View File

@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.CompositePropertySource;
@ -51,9 +52,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
@Autowired
private NacosConfigProperties nacosConfigProperties;
@Autowired
private NacosPropertySourceRepository nacosPropertySourceRepository;
public NacosPropertySourceLocator() {
}
@ -72,8 +70,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
long timeout = nacosConfigProperties.getTimeout();
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
timeout);
nacosPropertySourceBuilder
.setNacosPropertySourceRepository(nacosPropertySourceRepository);
String name = nacosConfigProperties.getName();
String nacosGroup = nacosConfigProperties.getGroup();
@ -82,6 +78,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
dataIdPrefix = name;
}
if (StringUtils.isEmpty(dataIdPrefix)) {
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(
@ -164,9 +167,20 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
private void loadNacosDataIfPresent(final CompositePropertySource composite,
final String dataId, final String group, String fileExtension,
boolean isRefreshable) {
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
fileExtension, isRefreshable);
if (ps != null) {
if (NacosContextRefresher.loadCount.get() != 0) {
NacosPropertySource ps;
if (!isRefreshable) {
ps = NacosPropertySourceRepository.getNacosPropertySource(dataId);
}
else {
ps = nacosPropertySourceBuilder.build(dataId, group, fileExtension, true);
}
composite.addFirstPropertySource(ps);
}
else {
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
fileExtension, isRefreshable);
composite.addFirstPropertySource(ps);
}
}

View File

@ -16,13 +16,6 @@
package org.springframework.cloud.alibaba.nacos.endpoint;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
@ -30,6 +23,13 @@ import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Endpoint for Nacos, contains config data and refresh history
* @author xiaojing
@ -41,15 +41,12 @@ public class NacosConfigEndpoint {
private final NacosRefreshHistory refreshHistory;
private final NacosPropertySourceRepository propertySourceRepository;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public NacosConfigEndpoint(NacosConfigProperties properties, NacosRefreshHistory refreshHistory,
NacosPropertySourceRepository propertySourceRepository) {
public NacosConfigEndpoint(NacosConfigProperties properties,
NacosRefreshHistory refreshHistory) {
this.properties = properties;
this.refreshHistory = refreshHistory;
this.propertySourceRepository = propertySourceRepository;
}
@ReadOperation
@ -57,7 +54,7 @@ public class NacosConfigEndpoint {
Map<String, Object> result = new HashMap<>(16);
result.put("NacosConfigProperties", properties);
List<NacosPropertySource> all = propertySourceRepository.getAll();
List<NacosPropertySource> all = NacosPropertySourceRepository.getAll();
List<Map<String, Object>> sources = new ArrayList<>();
for (NacosPropertySource ps : all) {

View File

@ -19,12 +19,10 @@ package org.springframework.cloud.alibaba.nacos.endpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
import org.springframework.context.annotation.Bean;
@ -41,22 +39,16 @@ public class NacosConfigEndpointAutoConfiguration {
@Autowired
private NacosRefreshHistory nacosRefreshHistory;
@Autowired
private NacosPropertySourceRepository nacosPropertySourceRepository;
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
@Bean
public NacosConfigEndpoint nacosConfigEndpoint() {
return new NacosConfigEndpoint(nacosConfigProperties, nacosRefreshHistory,
nacosPropertySourceRepository);
return new NacosConfigEndpoint(nacosConfigProperties, nacosRefreshHistory);
}
@Bean
public NacosConfigHealthIndicator nacosConfigHealthIndicator(
NacosPropertySourceRepository nacosPropertySourceRepository) {
public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
return new NacosConfigHealthIndicator(nacosConfigProperties,
nacosPropertySourceRepository,
nacosConfigProperties.configServiceInstance());
}
}

View File

@ -16,11 +16,7 @@
package org.springframework.cloud.alibaba.nacos.endpoint;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
@ -28,6 +24,9 @@ import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
import org.springframework.cloud.alibaba.nacos.client.NacosPropertySource;
import org.springframework.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @author xiaojing
*/
@ -35,21 +34,17 @@ public class NacosConfigHealthIndicator extends AbstractHealthIndicator {
private final NacosConfigProperties nacosConfigProperties;
private final NacosPropertySourceRepository nacosPropertySourceRepository;
private final List<String> dataIds;
private final ConfigService configService;
public NacosConfigHealthIndicator(NacosConfigProperties nacosConfigProperties,
NacosPropertySourceRepository nacosPropertySourceRepository,
ConfigService configService) {
this.nacosConfigProperties = nacosConfigProperties;
this.nacosPropertySourceRepository = nacosPropertySourceRepository;
this.configService = configService;
this.dataIds = new ArrayList<>();
for (NacosPropertySource nacosPropertySource : this.nacosPropertySourceRepository
for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository
.getAll()) {
this.dataIds.add(nacosPropertySource.getDataId());
}

View File

@ -38,6 +38,7 @@ import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
/**
* On application start up, NacosContextRefresher add nacos listeners to all application
@ -53,34 +54,31 @@ public class NacosContextRefresher
private final static Logger LOGGER = LoggerFactory
.getLogger(NacosContextRefresher.class);
public static final AtomicLong loadCount = new AtomicLong(0);
private final NacosRefreshProperties refreshProperties;
private final NacosRefreshHistory refreshHistory;
private final NacosPropertySourceRepository nacosPropertySourceRepository;
private final ConfigService configService;
private ApplicationContext applicationContext;
private AtomicBoolean ready = new AtomicBoolean(true);
private AtomicBoolean ready = new AtomicBoolean(false);
private Map<String, Listener> listenerMap = new ConcurrentHashMap<>(16);
public NacosContextRefresher(NacosRefreshProperties refreshProperties,
NacosRefreshHistory refreshHistory,
NacosPropertySourceRepository nacosPropertySourceRepository,
ConfigService configService) {
NacosRefreshHistory refreshHistory, ConfigService configService) {
this.refreshProperties = refreshProperties;
this.refreshHistory = refreshHistory;
this.nacosPropertySourceRepository = nacosPropertySourceRepository;
this.configService = configService;
}
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// many Spring context
if (this.ready.compareAndSet(true, false)) {
if (this.ready.compareAndSet(false, true)) {
this.registerNacosListenersForApplications();
}
}
@ -92,7 +90,7 @@ public class NacosContextRefresher
private void registerNacosListenersForApplications() {
if (refreshProperties.isEnabled()) {
for (NacosPropertySource nacosPropertySource : nacosPropertySourceRepository
for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository
.getAll()) {
if (!nacosPropertySource.isRefreshable()) {
@ -110,6 +108,7 @@ public class NacosContextRefresher
Listener listener = listenerMap.computeIfAbsent(dataId, i -> new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
loadCount.incrementAndGet();
String md5 = "";
if (!StringUtils.isEmpty(configInfo)) {
try {

View File

@ -3,27 +3,33 @@ package org.springframework.cloud.alicloud.context.nacos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.cloud.alicloud.context.listener.AbstractOnceApplicationListener;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfiguration;
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
public class NacosParameterInitListener
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
private static final Logger log = LoggerFactory
.getLogger(NacosParameterInitListener.class);
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
protected String conditionalOnClass() {
return "org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration";
}
@Override
protected void handleEvent(ApplicationEnvironmentPreparedEvent event) {
preparedNacosConfiguration();
}
private void preparedNacosConfiguration() {
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
.getEdasChangeOrderConfiguration();
log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",
edasChangeOrderConfiguration.isEdasManaged());
if (!edasChangeOrderConfiguration.isEdasManaged()) {
return;
}