mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
commit
c300ffbbc6
@ -56,9 +56,8 @@ public class NacosConfigAutoConfiguration {
|
|||||||
public NacosContextRefresher nacosContextRefresher(
|
public NacosContextRefresher nacosContextRefresher(
|
||||||
NacosConfigProperties nacosConfigProperties,
|
NacosConfigProperties nacosConfigProperties,
|
||||||
NacosRefreshProperties nacosRefreshProperties,
|
NacosRefreshProperties nacosRefreshProperties,
|
||||||
NacosRefreshHistory refreshHistory,
|
NacosRefreshHistory refreshHistory) {
|
||||||
NacosPropertySourceRepository propertySourceRepository) {
|
|
||||||
return new NacosContextRefresher(nacosRefreshProperties, refreshHistory,
|
return new NacosContextRefresher(nacosRefreshProperties, refreshHistory,
|
||||||
propertySourceRepository, nacosConfigProperties.configServiceInstance());
|
nacosConfigProperties.configServiceInstance());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,9 +38,4 @@ public class NacosConfigBootstrapConfiguration {
|
|||||||
return new NacosConfigProperties();
|
return new NacosConfigProperties();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public NacosPropertySourceRepository nacosPropertySourceRepository() {
|
|
||||||
return new NacosPropertySourceRepository();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,24 +16,21 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos;
|
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.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import static com.alibaba.nacos.api.PropertyKeyConst.*;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nacos properties
|
* nacos properties
|
||||||
@ -110,7 +107,6 @@ public class NacosConfigProperties {
|
|||||||
*/
|
*/
|
||||||
private String clusterName;
|
private String clusterName;
|
||||||
|
|
||||||
@Value("${spring.application.name}")
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
private String[] activeProfiles;
|
private String[] activeProfiles;
|
||||||
@ -271,6 +267,10 @@ public class NacosConfigProperties {
|
|||||||
this.extConfig = extConfig;
|
this.extConfig = extConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
@ -348,4 +348,5 @@ public class NacosConfigProperties {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,19 +27,24 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
*/
|
*/
|
||||||
public class NacosPropertySourceRepository {
|
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
|
* @return all nacos properties from application context
|
||||||
*/
|
*/
|
||||||
public List<NacosPropertySource> getAll() {
|
public static List<NacosPropertySource> getAll() {
|
||||||
List<NacosPropertySource> result = new ArrayList<>();
|
List<NacosPropertySource> result = new ArrayList<>();
|
||||||
result.addAll(nacosPropertySourceRepository.values());
|
result.addAll(nacosPropertySourceRepository.values());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void collectNacosPropertySources(NacosPropertySource nacosPropertySource) {
|
public static void collectNacosPropertySources(
|
||||||
|
NacosPropertySource nacosPropertySource) {
|
||||||
nacosPropertySourceRepository.putIfAbsent(nacosPropertySource.getDataId(),
|
nacosPropertySourceRepository.putIfAbsent(nacosPropertySource.getDataId(),
|
||||||
nacosPropertySource);
|
nacosPropertySource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static NacosPropertySource getNacosPropertySource(String dataId) {
|
||||||
|
return nacosPropertySourceRepository.get(dataId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,6 @@ public class NacosPropertySourceBuilder {
|
|||||||
.getLogger(NacosPropertySourceBuilder.class);
|
.getLogger(NacosPropertySourceBuilder.class);
|
||||||
private static final Properties EMPTY_PROPERTIES = new Properties();
|
private static final Properties EMPTY_PROPERTIES = new Properties();
|
||||||
|
|
||||||
private NacosPropertySourceRepository nacosPropertySourceRepository;
|
|
||||||
private ConfigService configService;
|
private ConfigService configService;
|
||||||
private long timeout;
|
private long timeout;
|
||||||
|
|
||||||
@ -74,7 +73,7 @@ public class NacosPropertySourceBuilder {
|
|||||||
}
|
}
|
||||||
NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
|
NacosPropertySource nacosPropertySource = new NacosPropertySource(group, dataId,
|
||||||
propertiesToMap(p), new Date(), isRefreshable);
|
propertiesToMap(p), new Date(), isRefreshable);
|
||||||
nacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource);
|
NacosPropertySourceRepository.collectNacosPropertySources(nacosPropertySource);
|
||||||
return nacosPropertySource;
|
return nacosPropertySource;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,12 +127,4 @@ public class NacosPropertySourceBuilder {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public NacosPropertySourceRepository getNacosPropertySourceRepository() {
|
|
||||||
return nacosPropertySourceRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNacosPropertySourceRepository(
|
|
||||||
NacosPropertySourceRepository nacosPropertySourceRepository) {
|
|
||||||
this.nacosPropertySourceRepository = nacosPropertySourceRepository;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
||||||
import org.springframework.cloud.alibaba.nacos.NacosPropertySourceRepository;
|
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.cloud.bootstrap.config.PropertySourceLocator;
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
import org.springframework.core.env.CompositePropertySource;
|
import org.springframework.core.env.CompositePropertySource;
|
||||||
@ -51,9 +52,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private NacosConfigProperties nacosConfigProperties;
|
private NacosConfigProperties nacosConfigProperties;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private NacosPropertySourceRepository nacosPropertySourceRepository;
|
|
||||||
|
|
||||||
public NacosPropertySourceLocator() {
|
public NacosPropertySourceLocator() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,8 +70,6 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
long timeout = nacosConfigProperties.getTimeout();
|
long timeout = nacosConfigProperties.getTimeout();
|
||||||
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
|
nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService,
|
||||||
timeout);
|
timeout);
|
||||||
nacosPropertySourceBuilder
|
|
||||||
.setNacosPropertySourceRepository(nacosPropertySourceRepository);
|
|
||||||
String name = nacosConfigProperties.getName();
|
String name = nacosConfigProperties.getName();
|
||||||
|
|
||||||
String nacosGroup = nacosConfigProperties.getGroup();
|
String nacosGroup = nacosConfigProperties.getGroup();
|
||||||
@ -82,6 +78,13 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
dataIdPrefix = name;
|
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();
|
String fileExtension = nacosConfigProperties.getFileExtension();
|
||||||
|
|
||||||
CompositePropertySource composite = new CompositePropertySource(
|
CompositePropertySource composite = new CompositePropertySource(
|
||||||
@ -164,9 +167,20 @@ public class NacosPropertySourceLocator implements PropertySourceLocator {
|
|||||||
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
private void loadNacosDataIfPresent(final CompositePropertySource composite,
|
||||||
final String dataId, final String group, String fileExtension,
|
final String dataId, final String group, String fileExtension,
|
||||||
boolean isRefreshable) {
|
boolean isRefreshable) {
|
||||||
NacosPropertySource ps = nacosPropertySourceBuilder.build(dataId, group,
|
if (NacosContextRefresher.loadCount.get() != 0) {
|
||||||
fileExtension, isRefreshable);
|
NacosPropertySource ps;
|
||||||
if (ps != null) {
|
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);
|
composite.addFirstPropertySource(ps);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,13 +16,6 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos.endpoint;
|
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.Endpoint;
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
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.client.NacosPropertySource;
|
||||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
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
|
* Endpoint for Nacos, contains config data and refresh history
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
@ -41,15 +41,12 @@ public class NacosConfigEndpoint {
|
|||||||
|
|
||||||
private final NacosRefreshHistory refreshHistory;
|
private final NacosRefreshHistory refreshHistory;
|
||||||
|
|
||||||
private final NacosPropertySourceRepository propertySourceRepository;
|
|
||||||
|
|
||||||
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
|
||||||
public NacosConfigEndpoint(NacosConfigProperties properties, NacosRefreshHistory refreshHistory,
|
public NacosConfigEndpoint(NacosConfigProperties properties,
|
||||||
NacosPropertySourceRepository propertySourceRepository) {
|
NacosRefreshHistory refreshHistory) {
|
||||||
this.properties = properties;
|
this.properties = properties;
|
||||||
this.refreshHistory = refreshHistory;
|
this.refreshHistory = refreshHistory;
|
||||||
this.propertySourceRepository = propertySourceRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ReadOperation
|
@ReadOperation
|
||||||
@ -57,7 +54,7 @@ public class NacosConfigEndpoint {
|
|||||||
Map<String, Object> result = new HashMap<>(16);
|
Map<String, Object> result = new HashMap<>(16);
|
||||||
result.put("NacosConfigProperties", properties);
|
result.put("NacosConfigProperties", properties);
|
||||||
|
|
||||||
List<NacosPropertySource> all = propertySourceRepository.getAll();
|
List<NacosPropertySource> all = NacosPropertySourceRepository.getAll();
|
||||||
|
|
||||||
List<Map<String, Object>> sources = new ArrayList<>();
|
List<Map<String, Object>> sources = new ArrayList<>();
|
||||||
for (NacosPropertySource ps : all) {
|
for (NacosPropertySource ps : all) {
|
||||||
|
@ -19,12 +19,10 @@ 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.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
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.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
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.NacosPropertySourceRepository;
|
|
||||||
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
import org.springframework.cloud.alibaba.nacos.refresh.NacosRefreshHistory;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
@ -41,22 +39,16 @@ public class NacosConfigEndpointAutoConfiguration {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private NacosRefreshHistory nacosRefreshHistory;
|
private NacosRefreshHistory nacosRefreshHistory;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private NacosPropertySourceRepository nacosPropertySourceRepository;
|
|
||||||
|
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@ConditionalOnEnabledEndpoint
|
@ConditionalOnEnabledEndpoint
|
||||||
@Bean
|
@Bean
|
||||||
public NacosConfigEndpoint nacosConfigEndpoint() {
|
public NacosConfigEndpoint nacosConfigEndpoint() {
|
||||||
return new NacosConfigEndpoint(nacosConfigProperties, nacosRefreshHistory,
|
return new NacosConfigEndpoint(nacosConfigProperties, nacosRefreshHistory);
|
||||||
nacosPropertySourceRepository);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public NacosConfigHealthIndicator nacosConfigHealthIndicator(
|
public NacosConfigHealthIndicator nacosConfigHealthIndicator() {
|
||||||
NacosPropertySourceRepository nacosPropertySourceRepository) {
|
|
||||||
return new NacosConfigHealthIndicator(nacosConfigProperties,
|
return new NacosConfigHealthIndicator(nacosConfigProperties,
|
||||||
nacosPropertySourceRepository,
|
|
||||||
nacosConfigProperties.configServiceInstance());
|
nacosConfigProperties.configServiceInstance());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,11 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.cloud.alibaba.nacos.endpoint;
|
package org.springframework.cloud.alibaba.nacos.endpoint;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import com.alibaba.nacos.api.config.ConfigService;
|
import com.alibaba.nacos.api.config.ConfigService;
|
||||||
|
|
||||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||||
import org.springframework.boot.actuate.health.Health;
|
import org.springframework.boot.actuate.health.Health;
|
||||||
import org.springframework.cloud.alibaba.nacos.NacosConfigProperties;
|
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.cloud.alibaba.nacos.client.NacosPropertySource;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author xiaojing
|
* @author xiaojing
|
||||||
*/
|
*/
|
||||||
@ -35,21 +34,17 @@ public class NacosConfigHealthIndicator extends AbstractHealthIndicator {
|
|||||||
|
|
||||||
private final NacosConfigProperties nacosConfigProperties;
|
private final NacosConfigProperties nacosConfigProperties;
|
||||||
|
|
||||||
private final NacosPropertySourceRepository nacosPropertySourceRepository;
|
|
||||||
|
|
||||||
private final List<String> dataIds;
|
private final List<String> dataIds;
|
||||||
|
|
||||||
private final ConfigService configService;
|
private final ConfigService configService;
|
||||||
|
|
||||||
public NacosConfigHealthIndicator(NacosConfigProperties nacosConfigProperties,
|
public NacosConfigHealthIndicator(NacosConfigProperties nacosConfigProperties,
|
||||||
NacosPropertySourceRepository nacosPropertySourceRepository,
|
|
||||||
ConfigService configService) {
|
ConfigService configService) {
|
||||||
this.nacosConfigProperties = nacosConfigProperties;
|
this.nacosConfigProperties = nacosConfigProperties;
|
||||||
this.nacosPropertySourceRepository = nacosPropertySourceRepository;
|
|
||||||
this.configService = configService;
|
this.configService = configService;
|
||||||
|
|
||||||
this.dataIds = new ArrayList<>();
|
this.dataIds = new ArrayList<>();
|
||||||
for (NacosPropertySource nacosPropertySource : this.nacosPropertySourceRepository
|
for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository
|
||||||
.getAll()) {
|
.getAll()) {
|
||||||
this.dataIds.add(nacosPropertySource.getDataId());
|
this.dataIds.add(nacosPropertySource.getDataId());
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ import java.util.Map;
|
|||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* On application start up, NacosContextRefresher add nacos listeners to all application
|
* On application start up, NacosContextRefresher add nacos listeners to all application
|
||||||
@ -53,34 +54,31 @@ public class NacosContextRefresher
|
|||||||
private final static Logger LOGGER = LoggerFactory
|
private final static Logger LOGGER = LoggerFactory
|
||||||
.getLogger(NacosContextRefresher.class);
|
.getLogger(NacosContextRefresher.class);
|
||||||
|
|
||||||
|
public static final AtomicLong loadCount = new AtomicLong(0);
|
||||||
|
|
||||||
private final NacosRefreshProperties refreshProperties;
|
private final NacosRefreshProperties refreshProperties;
|
||||||
|
|
||||||
private final NacosRefreshHistory refreshHistory;
|
private final NacosRefreshHistory refreshHistory;
|
||||||
|
|
||||||
private final NacosPropertySourceRepository nacosPropertySourceRepository;
|
|
||||||
|
|
||||||
private final ConfigService configService;
|
private final ConfigService configService;
|
||||||
|
|
||||||
private ApplicationContext applicationContext;
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
private AtomicBoolean ready = new AtomicBoolean(true);
|
private AtomicBoolean ready = new AtomicBoolean(false);
|
||||||
|
|
||||||
private Map<String, Listener> listenerMap = new ConcurrentHashMap<>(16);
|
private Map<String, Listener> listenerMap = new ConcurrentHashMap<>(16);
|
||||||
|
|
||||||
public NacosContextRefresher(NacosRefreshProperties refreshProperties,
|
public NacosContextRefresher(NacosRefreshProperties refreshProperties,
|
||||||
NacosRefreshHistory refreshHistory,
|
NacosRefreshHistory refreshHistory, ConfigService configService) {
|
||||||
NacosPropertySourceRepository nacosPropertySourceRepository,
|
|
||||||
ConfigService configService) {
|
|
||||||
this.refreshProperties = refreshProperties;
|
this.refreshProperties = refreshProperties;
|
||||||
this.refreshHistory = refreshHistory;
|
this.refreshHistory = refreshHistory;
|
||||||
this.nacosPropertySourceRepository = nacosPropertySourceRepository;
|
|
||||||
this.configService = configService;
|
this.configService = configService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationReadyEvent event) {
|
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||||
// many Spring context
|
// many Spring context
|
||||||
if (this.ready.compareAndSet(true, false)) {
|
if (this.ready.compareAndSet(false, true)) {
|
||||||
this.registerNacosListenersForApplications();
|
this.registerNacosListenersForApplications();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,7 +90,7 @@ public class NacosContextRefresher
|
|||||||
|
|
||||||
private void registerNacosListenersForApplications() {
|
private void registerNacosListenersForApplications() {
|
||||||
if (refreshProperties.isEnabled()) {
|
if (refreshProperties.isEnabled()) {
|
||||||
for (NacosPropertySource nacosPropertySource : nacosPropertySourceRepository
|
for (NacosPropertySource nacosPropertySource : NacosPropertySourceRepository
|
||||||
.getAll()) {
|
.getAll()) {
|
||||||
|
|
||||||
if (!nacosPropertySource.isRefreshable()) {
|
if (!nacosPropertySource.isRefreshable()) {
|
||||||
@ -110,6 +108,7 @@ public class NacosContextRefresher
|
|||||||
Listener listener = listenerMap.computeIfAbsent(dataId, i -> new Listener() {
|
Listener listener = listenerMap.computeIfAbsent(dataId, i -> new Listener() {
|
||||||
@Override
|
@Override
|
||||||
public void receiveConfigInfo(String configInfo) {
|
public void receiveConfigInfo(String configInfo) {
|
||||||
|
loadCount.incrementAndGet();
|
||||||
String md5 = "";
|
String md5 = "";
|
||||||
if (!StringUtils.isEmpty(configInfo)) {
|
if (!StringUtils.isEmpty(configInfo)) {
|
||||||
try {
|
try {
|
||||||
|
@ -3,27 +3,33 @@ package org.springframework.cloud.alicloud.context.nacos;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
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.EdasChangeOrderConfiguration;
|
||||||
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
import com.alibaba.cloud.context.edas.EdasChangeOrderConfigurationFactory;
|
||||||
|
|
||||||
public class NacosParameterInitListener
|
public class NacosParameterInitListener
|
||||||
implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
extends AbstractOnceApplicationListener<ApplicationEnvironmentPreparedEvent> {
|
||||||
private static final Logger log = LoggerFactory
|
private static final Logger log = LoggerFactory
|
||||||
.getLogger(NacosParameterInitListener.class);
|
.getLogger(NacosParameterInitListener.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
|
protected String conditionalOnClass() {
|
||||||
|
return "org.springframework.cloud.alibaba.nacos.NacosConfigAutoConfiguration";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void handleEvent(ApplicationEnvironmentPreparedEvent event) {
|
||||||
preparedNacosConfiguration();
|
preparedNacosConfiguration();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void preparedNacosConfiguration() {
|
private void preparedNacosConfiguration() {
|
||||||
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
|
EdasChangeOrderConfiguration edasChangeOrderConfiguration = EdasChangeOrderConfigurationFactory
|
||||||
.getEdasChangeOrderConfiguration();
|
.getEdasChangeOrderConfiguration();
|
||||||
|
|
||||||
log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",
|
log.info("Initialize Nacos Parameter from edas change order,is edas managed {}.",
|
||||||
edasChangeOrderConfiguration.isEdasManaged());
|
edasChangeOrderConfiguration.isEdasManaged());
|
||||||
|
|
||||||
if (!edasChangeOrderConfiguration.isEdasManaged()) {
|
if (!edasChangeOrderConfiguration.isEdasManaged()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user