mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
Polish alibaba/spring-cloud-alibaba/#1222 : Combining the code-based and starter modules
This commit is contained in:
@@ -11,13 +11,74 @@
|
||||
<name>Spring Cloud Starter Alibaba Cloud ACM</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alicloud-acm</artifactId>
|
||||
<artifactId>spring-cloud-alicloud-context</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-edas</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.edas.acm</groupId>
|
||||
<artifactId>acm-sdk</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
<!-- Spring Cloud -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito2</artifactId>
|
||||
<version>2.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm;
|
||||
|
||||
import com.alibaba.alicloud.acm.refresh.AcmContextRefresher;
|
||||
import com.alibaba.alicloud.acm.refresh.AcmRefreshHistory;
|
||||
import com.alibaba.alicloud.context.acm.AcmIntegrationProperties;
|
||||
import com.taobao.diamond.client.Diamond;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Created on 01/10/2017.
|
||||
*
|
||||
* @author juven.xuxb
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass({ Diamond.class })
|
||||
@ConditionalOnProperty(name = "spring.cloud.alicloud.acm.enabled", matchIfMissing = true)
|
||||
public class AcmAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public AcmRefreshHistory acmRefreshHistory() {
|
||||
return new AcmRefreshHistory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AcmContextRefresher acmContextRefresher(
|
||||
AcmIntegrationProperties acmIntegrationProperties,
|
||||
ContextRefresher contextRefresher, AcmRefreshHistory refreshHistory,
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
return new AcmContextRefresher(contextRefresher, acmIntegrationProperties,
|
||||
refreshHistory, acmPropertySourceRepository);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.alibaba.alicloud.acm.bootstrap.AcmPropertySource;
|
||||
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb, 5/17/16.
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
public class AcmPropertySourceRepository {
|
||||
|
||||
private Map<String, AcmPropertySource> acmPropertySourceMap = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* get all acm properties from AcmPropertySourceRepository.
|
||||
* @return list of acm propertysource
|
||||
*/
|
||||
public List<AcmPropertySource> allAcmPropertySource() {
|
||||
List<AcmPropertySource> result = new ArrayList<>();
|
||||
result.addAll(this.acmPropertySourceMap.values());
|
||||
return result;
|
||||
}
|
||||
|
||||
public void collectAcmPropertySource(
|
||||
Collection<PropertySource<?>> acmPropertySources) {
|
||||
acmPropertySources.forEach(propertySource -> {
|
||||
if (propertySource.getClass().isAssignableFrom(AcmPropertySource.class)) {
|
||||
AcmPropertySource acmPropertySource = (AcmPropertySource) propertySource;
|
||||
this.acmPropertySourceMap.put(getMapKey(acmPropertySource.getDataId(),
|
||||
acmPropertySource.getGroup()), acmPropertySource);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String getMapKey(String dataId, String group) {
|
||||
return String.join(",", dataId, group);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.bootstrap;
|
||||
|
||||
import com.alibaba.alicloud.acm.AcmPropertySourceRepository;
|
||||
import com.alibaba.alicloud.context.acm.AcmIntegrationProperties;
|
||||
import com.taobao.diamond.client.Diamond;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnClass({ Diamond.class })
|
||||
@ConditionalOnProperty(name = "spring.cloud.alicloud.acm.enabled", matchIfMissing = true)
|
||||
public class AcmConfigBootStrapConfiguration {
|
||||
|
||||
@Bean
|
||||
public AcmPropertySourceRepository acmPropertySourceRepository() {
|
||||
return new AcmPropertySourceRepository();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AcmPropertySourceLocator acmPropertySourceLocator(
|
||||
AcmPropertySourceRepository acmPropertySourceRepository,
|
||||
AcmIntegrationProperties acmIntegrationProperties) {
|
||||
return new AcmPropertySourceLocator(acmIntegrationProperties,
|
||||
acmPropertySourceRepository);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.bootstrap;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb
|
||||
* @author xiaolongzuo
|
||||
*/
|
||||
public class AcmPropertySource extends MapPropertySource {
|
||||
|
||||
private final String dataId;
|
||||
|
||||
private final String group;
|
||||
|
||||
private final Date timestamp;
|
||||
|
||||
private final boolean groupLevel;
|
||||
|
||||
AcmPropertySource(String dataId, String group, Map<String, Object> source,
|
||||
Date timestamp, boolean groupLevel) {
|
||||
super(dataId, source);
|
||||
this.dataId = dataId;
|
||||
this.group = group;
|
||||
this.timestamp = timestamp;
|
||||
this.groupLevel = groupLevel;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public Date getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public boolean isGroupLevel() {
|
||||
return groupLevel;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.bootstrap;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
import com.alibaba.edas.acm.exception.ConfigException;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb
|
||||
* @author xiaolongzuo
|
||||
*/
|
||||
class AcmPropertySourceBuilder {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(AcmPropertySourceBuilder.class);
|
||||
|
||||
/**
|
||||
* 传入 ACM 的 DataId 和 groupID,获取到解析后的 AcmProperty 对象.
|
||||
* @param dataId dataid of diamond
|
||||
* @param diamondGroup group of diamond
|
||||
* @param groupLevel group level of diamond
|
||||
* @return acm property source
|
||||
*/
|
||||
AcmPropertySource build(String dataId, String diamondGroup, boolean groupLevel) {
|
||||
Properties properties = loadDiamondData(dataId, diamondGroup);
|
||||
if (properties == null) {
|
||||
return null;
|
||||
}
|
||||
return new AcmPropertySource(dataId, diamondGroup, toMap(properties), new Date(),
|
||||
groupLevel);
|
||||
}
|
||||
|
||||
private Properties loadDiamondData(String dataId, String diamondGroup) {
|
||||
try {
|
||||
String data = ConfigService.getConfig(dataId, diamondGroup, 3000L);
|
||||
if (StringUtils.isEmpty(data)) {
|
||||
return null;
|
||||
}
|
||||
if (dataId.endsWith(".properties")) {
|
||||
Properties properties = new Properties();
|
||||
log.info(String.format("Loading acm data, dataId: '%s', group: '%s'",
|
||||
dataId, diamondGroup));
|
||||
properties.load(new StringReader(data));
|
||||
return properties;
|
||||
}
|
||||
else if (dataId.endsWith(".yaml") || dataId.endsWith(".yml")) {
|
||||
YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
|
||||
yamlFactory.setResources(new ByteArrayResource(data.getBytes()));
|
||||
return yamlFactory.getObject();
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
if (e instanceof ConfigException) {
|
||||
log.error("DIAMOND-100500:" + dataId + ", " + e.toString(), e);
|
||||
}
|
||||
else {
|
||||
log.error("DIAMOND-100500:" + dataId, e);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> toMap(Properties properties) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
Enumeration<String> keys = (Enumeration<String>) properties.propertyNames();
|
||||
while (keys.hasMoreElements()) {
|
||||
String key = keys.nextElement();
|
||||
Object value = properties.getProperty(key);
|
||||
if (value != null) {
|
||||
result.put(key, ((String) value).trim());
|
||||
}
|
||||
else {
|
||||
result.put(key, null);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.bootstrap;
|
||||
|
||||
import com.alibaba.alicloud.acm.AcmPropertySourceRepository;
|
||||
import com.alibaba.alicloud.context.acm.AcmIntegrationProperties;
|
||||
|
||||
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb
|
||||
* @author xiaolongzuo
|
||||
* @author yuhuangbin
|
||||
*/
|
||||
public class AcmPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private static final String DIAMOND_PROPERTY_SOURCE_NAME = "diamond";
|
||||
|
||||
private AcmPropertySourceBuilder acmPropertySourceBuilder = new AcmPropertySourceBuilder();
|
||||
|
||||
private AcmIntegrationProperties acmIntegrationProperties;
|
||||
|
||||
private AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
public AcmPropertySourceLocator(AcmIntegrationProperties acmIntegrationProperties,
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
this.acmIntegrationProperties = acmIntegrationProperties;
|
||||
this.acmPropertySourceRepository = acmPropertySourceRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertySource<?> locate(Environment environment) {
|
||||
|
||||
CompositePropertySource compositePropertySource = new CompositePropertySource(
|
||||
DIAMOND_PROPERTY_SOURCE_NAME);
|
||||
|
||||
acmIntegrationProperties.setActiveProfiles(environment.getActiveProfiles());
|
||||
|
||||
for (String dataId : acmIntegrationProperties.getGroupConfigurationDataIds()) {
|
||||
loadDiamondDataIfPresent(compositePropertySource, dataId,
|
||||
acmIntegrationProperties.getAcmProperties().getGroup(), true);
|
||||
}
|
||||
|
||||
for (String dataId : acmIntegrationProperties
|
||||
.getApplicationConfigurationDataIds()) {
|
||||
loadDiamondDataIfPresent(compositePropertySource, dataId,
|
||||
acmIntegrationProperties.getAcmProperties().getGroup(), false);
|
||||
}
|
||||
acmPropertySourceRepository
|
||||
.collectAcmPropertySource(compositePropertySource.getPropertySources());
|
||||
return compositePropertySource;
|
||||
}
|
||||
|
||||
private void loadDiamondDataIfPresent(final CompositePropertySource composite,
|
||||
final String dataId, final String diamondGroup, final boolean groupLevel) {
|
||||
AcmPropertySource ps = acmPropertySourceBuilder.build(dataId, diamondGroup,
|
||||
groupLevel);
|
||||
if (ps != null) {
|
||||
composite.addFirstPropertySource(ps);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.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 com.alibaba.alicloud.acm.AcmPropertySourceRepository;
|
||||
import com.alibaba.alicloud.acm.bootstrap.AcmPropertySource;
|
||||
import com.alibaba.alicloud.acm.refresh.AcmRefreshHistory;
|
||||
import com.alibaba.alicloud.context.acm.AcmProperties;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
|
||||
/**
|
||||
* Created on 01/10/2017.
|
||||
*
|
||||
* @author juven.xuxb
|
||||
*/
|
||||
@Endpoint(id = "acm")
|
||||
public class AcmEndpoint {
|
||||
|
||||
private final AcmProperties properties;
|
||||
|
||||
private final AcmRefreshHistory refreshHistory;
|
||||
|
||||
private final AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
|
||||
@Override
|
||||
protected DateFormat initialValue() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
};
|
||||
|
||||
public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory,
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
this.properties = properties;
|
||||
this.refreshHistory = refreshHistory;
|
||||
this.acmPropertySourceRepository = acmPropertySourceRepository;
|
||||
}
|
||||
|
||||
@ReadOperation
|
||||
public Map<String, Object> invoke() {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("config", properties);
|
||||
|
||||
Map<String, Object> runtime = new HashMap<>();
|
||||
List<AcmPropertySource> all = acmPropertySourceRepository.allAcmPropertySource();
|
||||
|
||||
List<Map<String, Object>> sources = new ArrayList<>();
|
||||
for (AcmPropertySource ps : all) {
|
||||
Map<String, Object> source = new HashMap<>();
|
||||
source.put("dataId", ps.getDataId());
|
||||
source.put("lastSynced", dateFormat.get().format(ps.getTimestamp()));
|
||||
sources.add(source);
|
||||
}
|
||||
runtime.put("sources", sources);
|
||||
runtime.put("refreshHistory", refreshHistory.getRecords());
|
||||
|
||||
result.put("runtime", runtime);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.endpoint;
|
||||
|
||||
import com.alibaba.alicloud.acm.AcmPropertySourceRepository;
|
||||
import com.alibaba.alicloud.acm.refresh.AcmRefreshHistory;
|
||||
import com.alibaba.alicloud.context.acm.AcmProperties;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnClass(
|
||||
name = "org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration")
|
||||
@ConditionalOnProperty(name = "spring.cloud.alicloud.acm.enabled", matchIfMissing = true)
|
||||
public class AcmEndpointAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private AcmProperties acmProperties;
|
||||
|
||||
@Autowired
|
||||
private AcmRefreshHistory acmRefreshHistory;
|
||||
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledEndpoint
|
||||
@Bean
|
||||
public AcmEndpoint acmEndpoint(
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
return new AcmEndpoint(acmProperties, acmRefreshHistory,
|
||||
acmPropertySourceRepository);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public AcmHealthIndicator acmHealthIndicator(AcmProperties acmProperties,
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
return new AcmHealthIndicator(acmProperties, acmPropertySourceRepository);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.endpoint;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.alicloud.acm.AcmPropertySourceRepository;
|
||||
import com.alibaba.alicloud.acm.bootstrap.AcmPropertySource;
|
||||
import com.alibaba.alicloud.context.acm.AcmProperties;
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author leijuan
|
||||
* @author juven
|
||||
*/
|
||||
public class AcmHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private final AcmProperties acmProperties;
|
||||
|
||||
private final List<String> dataIds;
|
||||
|
||||
private final AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
public AcmHealthIndicator(AcmProperties acmProperties,
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
this.acmProperties = acmProperties;
|
||||
this.acmPropertySourceRepository = acmPropertySourceRepository;
|
||||
|
||||
this.dataIds = new ArrayList<>();
|
||||
for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository
|
||||
.allAcmPropertySource()) {
|
||||
this.dataIds.add(acmPropertySource.getDataId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doHealthCheck(Health.Builder builder) throws Exception {
|
||||
for (String dataId : dataIds) {
|
||||
try {
|
||||
String config = ConfigService.getConfig(dataId, acmProperties.getGroup(),
|
||||
acmProperties.getTimeout());
|
||||
if (StringUtils.isEmpty(config)) {
|
||||
builder.down().withDetail(String.format("dataId: '%s', group: '%s'",
|
||||
dataId, acmProperties.getGroup()), "config is empty");
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
builder.down().withDetail(String.format("dataId: '%s', group: '%s'",
|
||||
dataId, acmProperties.getGroup()), e.getMessage());
|
||||
}
|
||||
}
|
||||
builder.up().withDetail("dataIds", dataIds);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.refresh;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigInteger;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.alibaba.alicloud.acm.AcmPropertySourceRepository;
|
||||
import com.alibaba.alicloud.context.acm.AcmIntegrationProperties;
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
import com.alibaba.edas.acm.listener.ConfigChangeListener;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||
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;
|
||||
|
||||
/**
|
||||
* On application start up, AcmContextRefresher add diamond listeners to all application
|
||||
* level dataIds, when there is a change in the data, listeners will refresh
|
||||
* configurations.
|
||||
*
|
||||
* @author juven.xuxb, 5/13/16.
|
||||
*/
|
||||
public class AcmContextRefresher
|
||||
implements ApplicationListener<ApplicationReadyEvent>, ApplicationContextAware {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(AcmContextRefresher.class);
|
||||
|
||||
private final ContextRefresher contextRefresher;
|
||||
|
||||
private final AcmIntegrationProperties acmIntegrationProperties;
|
||||
|
||||
private final AcmRefreshHistory refreshHistory;
|
||||
|
||||
private AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private Map<String, ConfigChangeListener> listenerMap = new ConcurrentHashMap<>(16);
|
||||
|
||||
public AcmContextRefresher(ContextRefresher contextRefresher,
|
||||
AcmIntegrationProperties acmIntegrationProperties,
|
||||
AcmRefreshHistory refreshHistory,
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
this.contextRefresher = contextRefresher;
|
||||
this.acmIntegrationProperties = acmIntegrationProperties;
|
||||
this.refreshHistory = refreshHistory;
|
||||
this.acmPropertySourceRepository = acmPropertySourceRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationReadyEvent event) {
|
||||
this.registerDiamondListenersForApplications();
|
||||
}
|
||||
|
||||
private void registerDiamondListenersForApplications() {
|
||||
if (acmIntegrationProperties.getAcmProperties().isRefreshEnabled()) {
|
||||
for (String dataId : acmIntegrationProperties
|
||||
.getApplicationConfigurationDataIds()) {
|
||||
registerDiamondListener(dataId,
|
||||
acmIntegrationProperties.getAcmProperties().getGroup());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerDiamondListener(final String dataId, final String group) {
|
||||
String key = acmPropertySourceRepository.getMapKey(dataId, group);
|
||||
ConfigChangeListener listener = listenerMap.computeIfAbsent(key,
|
||||
i -> new ConfigChangeListener() {
|
||||
@Override
|
||||
public void receiveConfigInfo(String configInfo) {
|
||||
String md5 = "";
|
||||
if (!StringUtils.isEmpty(configInfo)) {
|
||||
try {
|
||||
MessageDigest md = MessageDigest.getInstance("MD5");
|
||||
md5 = new BigInteger(1,
|
||||
md.digest(configInfo.getBytes("UTF-8")))
|
||||
.toString(16);
|
||||
}
|
||||
catch (NoSuchAlgorithmException
|
||||
| UnsupportedEncodingException e) {
|
||||
log.warn("unable to get md5 for dataId: " + dataId, e);
|
||||
}
|
||||
}
|
||||
refreshHistory.add(dataId, md5);
|
||||
applicationContext.publishEvent(new RefreshEvent(this, md5,
|
||||
"ACM Refresh, dataId=" + dataId));
|
||||
}
|
||||
});
|
||||
ConfigService.addListener(dataId, group, listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.refresh;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb, 5/16/16.
|
||||
*/
|
||||
public class AcmRefreshHistory {
|
||||
|
||||
private static final int MAX_SIZE = 20;
|
||||
|
||||
private LinkedList<Record> records = new LinkedList<>();
|
||||
|
||||
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
|
||||
@Override
|
||||
protected DateFormat initialValue() {
|
||||
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
};
|
||||
|
||||
public void add(String dataId, String md5) {
|
||||
records.addFirst(new Record(dateFormat.get().format(new Date()), dataId, md5));
|
||||
if (records.size() > MAX_SIZE) {
|
||||
records.removeLast();
|
||||
}
|
||||
}
|
||||
|
||||
public LinkedList<Record> getRecords() {
|
||||
return records;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Record {
|
||||
|
||||
private final String timestamp;
|
||||
|
||||
private final String dataId;
|
||||
|
||||
private final String md5;
|
||||
|
||||
Record(String timestamp, String dataId, String md5) {
|
||||
this.timestamp = timestamp;
|
||||
this.dataId = dataId;
|
||||
this.md5 = md5;
|
||||
}
|
||||
|
||||
public String getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public String getMd5() {
|
||||
return md5;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"properties": [
|
||||
{
|
||||
"name": "spring.application.group",
|
||||
"type": "java.lang.String",
|
||||
"description": "spring application group."
|
||||
},
|
||||
{
|
||||
"name": "spring.cloud.alicloud.acm.enabled",
|
||||
"type": "java.lang.Boolean",
|
||||
"defaultValue": true,
|
||||
"description": "enable acm or not."
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
com.alibaba.alicloud.acm.bootstrap.AcmConfigBootStrapConfiguration
|
||||
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.alibaba.alicloud.acm.AcmAutoConfiguration,\
|
||||
com.alibaba.alicloud.acm.endpoint.AcmEndpointAutoConfiguration
|
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.alibaba.alicloud.acm.bootstrap.AcmPropertySourceLocator;
|
||||
import com.alibaba.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import com.alibaba.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import com.alibaba.alicloud.context.acm.AcmIntegrationProperties;
|
||||
import com.alibaba.alicloud.context.acm.AcmProperties;
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ ConfigService.class })
|
||||
@SpringBootTest(classes = AcmConfigurationTests.TestConfig.class,
|
||||
properties = { "spring.application.name=test-name",
|
||||
"spring.profiles.active=dev,test",
|
||||
"spring.cloud.alicloud.acm.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.acm.server-port=8848",
|
||||
"spring.cloud.alicloud.acm.endpoint=test-endpoint",
|
||||
"spring.cloud.alicloud.acm.namespace=test-namespace",
|
||||
"spring.cloud.alicloud.acm.timeout=1000",
|
||||
"spring.cloud.alicloud.acm.group=test-group",
|
||||
"spring.cloud.alicloud.acm.refresh-enabled=false",
|
||||
"spring.cloud.alicloud.acm.file-extension=properties" },
|
||||
webEnvironment = NONE)
|
||||
public class AcmConfigurationTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(ConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
|
||||
if ("test-name-dev.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "user.name=dev";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Autowired
|
||||
private AcmPropertySourceLocator locator;
|
||||
|
||||
@Autowired
|
||||
private AcmIntegrationProperties integrationProperties;
|
||||
|
||||
@Autowired
|
||||
private AcmProperties properties;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
assertThat(locator).isNotNull();
|
||||
assertThat(properties).isNotNull();
|
||||
assertThat(integrationProperties).isNotNull();
|
||||
|
||||
checkoutAcmServerAddr();
|
||||
checkoutAcmServerPort();
|
||||
checkoutAcmEndpoint();
|
||||
checkoutAcmNamespace();
|
||||
checkoutAcmGroup();
|
||||
checkoutAcmFileExtension();
|
||||
checkoutAcmTimeout();
|
||||
checkoutAcmProfiles();
|
||||
checkoutAcmRefreshEnabled();
|
||||
checkoutDataLoad();
|
||||
checkoutProfileDataLoad();
|
||||
}
|
||||
|
||||
private void checkoutAcmServerAddr() {
|
||||
assertThat(properties.getServerList()).isEqualTo("127.0.0.1");
|
||||
}
|
||||
|
||||
private void checkoutAcmServerPort() {
|
||||
assertThat(properties.getServerPort()).isEqualTo("8848");
|
||||
}
|
||||
|
||||
private void checkoutAcmEndpoint() {
|
||||
assertThat(properties.getEndpoint()).isEqualTo("test-endpoint");
|
||||
}
|
||||
|
||||
private void checkoutAcmNamespace() {
|
||||
assertThat(properties.getNamespace()).isEqualTo("test-namespace");
|
||||
}
|
||||
|
||||
private void checkoutAcmGroup() {
|
||||
assertThat(properties.getGroup()).isEqualTo("test-group");
|
||||
}
|
||||
|
||||
private void checkoutAcmFileExtension() {
|
||||
assertThat(properties.getFileExtension()).isEqualTo("properties");
|
||||
}
|
||||
|
||||
private void checkoutAcmTimeout() {
|
||||
assertThat(properties.getTimeout()).isEqualTo(1000);
|
||||
}
|
||||
|
||||
private void checkoutAcmRefreshEnabled() {
|
||||
assertThat(properties.isRefreshEnabled()).isEqualTo(false);
|
||||
}
|
||||
|
||||
private void checkoutAcmProfiles() {
|
||||
assertThat(integrationProperties.getActiveProfiles())
|
||||
.isEqualTo(new String[] { "dev", "test" });
|
||||
}
|
||||
|
||||
private void checkoutDataLoad() {
|
||||
assertThat(environment.getProperty("user.age")).isEqualTo("12");
|
||||
}
|
||||
|
||||
private void checkoutProfileDataLoad() {
|
||||
assertThat(environment.getProperty("user.name")).isEqualTo("dev");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.alibaba.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import com.alibaba.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ ConfigService.class })
|
||||
@SpringBootTest(classes = AcmFileExtensionTest.TestConfig.class,
|
||||
properties = { "spring.application.name=test-name",
|
||||
"spring.cloud.alicloud.acm.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.acm.server-port=8080",
|
||||
"spring.cloud.alicloud.acm.file-extension=yaml" },
|
||||
webEnvironment = NONE)
|
||||
public class AcmFileExtensionTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
Method method = PowerMockito.method(ConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
if ("test-name.yaml".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user:\n name: hello\n age: 12";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
Assert.assertEquals(environment.getProperty("user.name"), "hello");
|
||||
Assert.assertEquals(environment.getProperty("user.age"), "12");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import com.alibaba.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import com.alibaba.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ ConfigService.class })
|
||||
@SpringBootTest(classes = AcmGroupConfigurationTest.TestConfig.class,
|
||||
properties = { "spring.application.name=test-name",
|
||||
"spring.application.group=com.test.hello",
|
||||
"spring.cloud.alicloud.acm.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.acm.server-port=8080",
|
||||
"spring.cloud.alicloud.acm.timeout=1000",
|
||||
"spring.cloud.alicloud.acm.group=test-group" },
|
||||
webEnvironment = NONE)
|
||||
public class AcmGroupConfigurationTest {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
Method method = PowerMockito.method(ConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
if ("com.test:application.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "com.test.value=com.test\ntest.priority=1";
|
||||
}
|
||||
if ("com.test.hello:application.properties".equals(args[0])
|
||||
&& "test-group".equals(args[1])) {
|
||||
return "com.test.hello.value=com.test.hello\ntest.priority=2";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
Assert.assertEquals(environment.getProperty("com.test.value"), "com.test");
|
||||
Assert.assertEquals(environment.getProperty("test.priority"), "2");
|
||||
Assert.assertEquals(environment.getProperty("com.test.hello.value"),
|
||||
"com.test.hello");
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.alicloud.acm.endpoint;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.alicloud.acm.AcmAutoConfiguration;
|
||||
import com.alibaba.alicloud.acm.AcmPropertySourceRepository;
|
||||
import com.alibaba.alicloud.acm.refresh.AcmRefreshHistory;
|
||||
import com.alibaba.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import com.alibaba.alicloud.context.acm.AcmProperties;
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.api.support.MethodProxy;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.modules.junit4.PowerMockRunnerDelegate;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.actuate.health.Health.Builder;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PowerMockRunnerDelegate(SpringRunner.class)
|
||||
@PrepareForTest({ ConfigService.class })
|
||||
@SpringBootTest(classes = AcmEndpointTests.TestConfig.class,
|
||||
properties = { "spring.application.name=test-name",
|
||||
"spring.cloud.alicloud.acm.server-list=127.0.0.1",
|
||||
"spring.cloud.alicloud.acm.server-port=8848",
|
||||
"spring.cloud.alicloud.acm.file-extension=properties" },
|
||||
webEnvironment = NONE)
|
||||
public class AcmEndpointTests {
|
||||
|
||||
static {
|
||||
|
||||
try {
|
||||
|
||||
Method method = PowerMockito.method(ConfigService.class, "getConfig",
|
||||
String.class, String.class, long.class);
|
||||
MethodProxy.proxy(method, new InvocationHandler() {
|
||||
@Override
|
||||
public Object invoke(Object proxy, Method method, Object[] args)
|
||||
throws Throwable {
|
||||
|
||||
if ("test-name.properties".equals(args[0])
|
||||
&& "DEFAULT_GROUP".equals(args[1])) {
|
||||
return "user.name=hello\nuser.age=12";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (Exception ignore) {
|
||||
ignore.printStackTrace();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private AcmProperties properties;
|
||||
|
||||
@Autowired
|
||||
private AcmRefreshHistory refreshHistory;
|
||||
|
||||
@Autowired
|
||||
private AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
@Test
|
||||
public void contextLoads() throws Exception {
|
||||
|
||||
checkoutEndpoint();
|
||||
checkoutAcmHealthIndicator();
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmHealthIndicator() {
|
||||
try {
|
||||
Builder builder = new Builder();
|
||||
|
||||
AcmHealthIndicator healthIndicator = new AcmHealthIndicator(properties,
|
||||
acmPropertySourceRepository);
|
||||
healthIndicator.doHealthCheck(builder);
|
||||
|
||||
Builder builder1 = new Builder();
|
||||
List<String> dataIds = new ArrayList<>();
|
||||
dataIds.add("test-name.properties");
|
||||
builder1.up().withDetail("dataIds", dataIds);
|
||||
|
||||
Assert.assertTrue(builder.build().equals(builder1.build()));
|
||||
|
||||
}
|
||||
catch (Exception ignoreE) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void checkoutEndpoint() throws Exception {
|
||||
AcmEndpoint acmEndpoint = new AcmEndpoint(properties, refreshHistory,
|
||||
acmPropertySourceRepository);
|
||||
Map<String, Object> map = acmEndpoint.invoke();
|
||||
assertThat(properties).isEqualTo(map.get("config"));
|
||||
assertThat(refreshHistory.getRecords())
|
||||
.isEqualTo(((Map) map.get("runtime")).get("refreshHistory"));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user