1
0
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:
flystar32 2018-12-17 16:32:18 +08:00
parent 8dff53a1f5
commit 97e500609e
15 changed files with 150 additions and 117 deletions

View File

@ -25,9 +25,9 @@ Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。
**[RocketMQ](https://rocketmq.apache.org/)**:一款开源的分布式消息系统,基于高可用分布式集群技术,提供低延时的、高可靠的消息发布与订阅服务。
**[AliCloud ACM](https://www.aliyun.com/product/acm)**:一款在分布式架构环境中对应用配置进行集中管理和推送的应用配置中心产品。
**[Alibaba Cloud ACM](https://www.aliyun.com/product/acm)**:一款在分布式架构环境中对应用配置进行集中管理和推送的应用配置中心产品。
**[AliCloud OSS](https://www.aliyun.com/product/oss)**: 阿里云对象存储服务Object Storage Service简称 OSS是阿里云提供的海量、安全、低成本、高可靠的云存储服务。您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。
**[Alibaba Cloud OSS](https://www.aliyun.com/product/oss)**: 阿里云对象存储服务Object Storage Service简称 OSS是阿里云提供的海量、安全、低成本、高可靠的云存储服务。您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。
更多组件请参考 [Roadmap](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/Roadmap-zh.md)。
@ -90,11 +90,11 @@ Example 列表:
[RocketMQ Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/rocketmq-example/readme-zh.md)
[AliCloud OSS Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/oss-example/readme-zh.md)
[Alibaba Cloud OSS Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/oss-example/readme-zh.md)
[AliCloud ANS Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/ans-example/ans-provider-example/readme-zh.md)
[Alibaba Cloud ANS Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/ans-example/ans-provider-example/readme-zh.md)
[AliCloud ACM Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/acm-example/acm-local-example/readme-zh.md)
[Alibaba Cloud ACM Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/acm-example/acm-local-example/readme-zh.md)
## 版本管理规范
项目的版本号格式为 x.x.x 的形式,其中 x 的数值类型为数字从0开始取值且不限于 0~9 这个范围。项目处于孵化器阶段时第一位版本号固定使用0即版本号为 0.x.x 的格式。
@ -116,4 +116,4 @@ spring-cloud-alibaba@googlegroups.com欢迎通过此邮件列表讨论与 spr
### 钉钉群
![DingQR](https://cdn.nlark.com/lark/0/2018/png/64647/1535108150178-409a1689-437f-495b-8dcb-b667ccb32f85.png)
![DingQR](https://cdn.nlark.com/lark/0/2018/png/54319/1544667717958-b3022f21-3357-4270-836d-4064e7ac728c.png)

View File

@ -15,7 +15,7 @@ With Spring Cloud Alibaba, you only need to add some annotations and a small amo
* **Service registration and discovery**Service can be registered and clients can discover the instances using Spring-managed beans, auto integration Ribbon.
* **Distributed configuration**support for externalized configuration in a distributed system, auto refresh when configuration changes.
* **Event-driven**support for building highly scalable event-driven microservices connected with shared messaging systems.
* **AliCloud Object Storage**massive, secure, low-cost, and highly reliable cloud storage services. Support for storing and accessing any type of data in any application, anytime, anywhere.
* **Alibaba Cloud Object Storage**massive, secure, low-cost, and highly reliable cloud storage services. Support for storing and accessing any type of data in any application, anytime, anywhere.
For more features, please refer to [Roadmap](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/Roadmap.md).
## Components
@ -26,9 +26,9 @@ For more features, please refer to [Roadmap](https://github.com/spring-cloud-inc
**[RocketMQ](https://rocketmq.apache.org/)**a distributed messaging and streaming platform with low latency, high performance and reliability, trillion-level capacity and flexible scalability.
**[AliCloud ACM](https://www.aliyun.com/product/acm)**an application configuration center that enables you to centralize the management of application configurations, and accomplish real-time configuration push in a distributed environment.
**[Alibaba Cloud ACM](https://www.aliyun.com/product/acm)**an application configuration center that enables you to centralize the management of application configurations, and accomplish real-time configuration push in a distributed environment.
**[AliCloud OSS](https://www.aliyun.com/product/oss)**: An encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the world.
**[Alibaba Cloud OSS](https://www.aliyun.com/product/oss)**: An encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the world.
For more features please refer to [Roadmap](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/Roadmap.md).
@ -102,7 +102,7 @@ Examples
[RocketMQ Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/rocketmq-example/readme.md)
[AliCloud OSS Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/oss-example/readme.md)
[Alibaba Cloud OSS Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/oss-example/readme.md)
## Version control guidelines
The version number of the project is in the form of x.x.x, where x is a number, starting from 0, and is not limited to the range 0~9. When the project is in the incubator phase, the first version number is fixed to 0, that is, the version number is 0.x.x.

View File

@ -15,7 +15,7 @@ Spring Cloud Alibaba 致力于提供分布式应用服务开发的一站式解
**Nacos**
阿里巴巴开源产品,一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
**AliCloud OSS**
**Alibaba Cloud OSS**
阿里云对象存储服务Object Storage Service简称 OSS是阿里云提供的海量、安全、低成本、高可靠的云存储服务。您可以在任何应用、任何时间、任何地点存储和访问任意类型的数据。
## 即将加入的组件
@ -25,8 +25,8 @@ Apache Dubbo™ (incubating) 是一款高性能Java RPC框架。
**RocketMQ**
Apache RocketMQ™ 基于Java的高性能、高吞吐量的分布式消息和流计算平台。
**Schedulerx**
**Alibaba Cloud Schedulerx**
阿里中间件团队开发的一款分布式任务调度产品,支持周期性的任务与固定时间点触发任务。
**AliCloud SLS**
**Alibaba Cloud SLS**
针对日志类数据的一站式服务,在阿里巴巴集团经历大量大数据场景锤炼而成。您无需开发就能快捷完成日志数据采集、消费、投递以及查询分析等功能,提升运维、运营效率,建立 DT 时代海量日志处理能力。

View File

@ -20,9 +20,9 @@ An open-source project of Alibaba, Sentinel takes "flow" as breakthrough point,
An opensource project of Alibaba, an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
**AliCloud OSS**
**Alibaba Cloud OSS**
AliCloud Object Storage Service, An encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the world.
Alibaba Cloud Object Storage Service, An encrypted and secure cloud storage service which stores, processes and accesses massive amounts of data from anywhere in the world.
More components will be supported by Spring Cloud Alibaba in the future, which may include but are not limited to the following:
@ -32,8 +32,8 @@ Apache Dubbo™ (incubating) is a high-performance, Java based open source RPC f
**RocketMQ**
Apache RocketMQ™ is an open source distributed messaging and streaming data platform.
**Schedulerx**
**Alibaba Cloud Schedulerx**
A distributed task scheduling product developed by Alibaba Middleware team. It supports both periodical tasks and tasks to be triggered at specified time points.
**AliCloud SLS**
**Alibaba Cloud SLS**
Aliyun Log Service is an all-in-one service for log-type data. It helps increase Operations & Management and operational efficiency, as well as build the processing capability to deal with massive logs.

View File

@ -55,6 +55,13 @@
<name>hengyunabc</name>
<email>hengyunabc@gmail.com</email>
</developer>
<developer>
<id>mercyblitz</id>
<name>Mercy Ma</name>
<email>mercyblitz@gmail.com</email>
<organization>Alibaba</organization>
<url>https://github.com/mercyblitz</url>
</developer>
</developers>
<properties>

View File

@ -18,7 +18,7 @@
<properties>
<sentinel.version>1.4.0</sentinel.version>
<oss.version>3.1.0</oss.version>
<nacos.version>0.5.0</nacos.version>
<nacos.version>0.6.0</nacos.version>
<acm.version>1.0.8</acm.version>
<ans.version>1.0.1</ans.version>
<aliyun.sdk.version>4.0.1</aliyun.sdk.version>

View File

@ -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());
}
}

View File

@ -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

View File

@ -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() {

View File

@ -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);

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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

View File

@ -18,11 +18,9 @@ package org.springframework.cloud.alicloud.acm;
import org.springframework.beans.BeansException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.alicloud.acm.endpoint.AcmHealthIndicator;
import org.springframework.cloud.alicloud.acm.refresh.AcmContextRefresher;
import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory;
import org.springframework.cloud.alicloud.context.acm.AcmIntegrationProperties;
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@ -47,12 +45,6 @@ public class AcmAutoConfiguration implements ApplicationContextAware {
return new AcmPropertySourceRepository(applicationContext);
}
@Bean
public AcmHealthIndicator acmHealthIndicator(AcmProperties acmProperties,
AcmPropertySourceRepository acmPropertySourceRepository) {
return new AcmHealthIndicator(acmProperties, acmPropertySourceRepository);
}
@Bean
public AcmRefreshHistory acmRefreshHistory() {
return new AcmRefreshHistory();

View File

@ -47,4 +47,10 @@ public class AcmEndpointAutoConfiguration {
return new AcmEndpoint(acmProperties, acmRefreshHistory,
acmPropertySourceRepository);
}
@Bean
public AcmHealthIndicator acmHealthIndicator(AcmProperties acmProperties,
AcmPropertySourceRepository acmPropertySourceRepository) {
return new AcmHealthIndicator(acmProperties, acmPropertySourceRepository);
}
}