mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
#734 -> update maven coordinates
This commit is contained in:
111
alicloud-acm-spring-cloud/pom.xml
Normal file
111
alicloud-acm-spring-cloud/pom.xml
Normal file
@@ -0,0 +1,111 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<parent>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba</artifactId>
|
||||
<version>0.9.1.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>alicloud-acm-spring-cloud</artifactId>
|
||||
<name>Spring Cloud Alibaba Cloud ACM</name>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>alicloud-context-spring-cloud</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>
|
||||
<scope>provided</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
<scope>provided</scope>
|
||||
<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-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>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jacoco</groupId>
|
||||
<artifactId>jacoco-maven-plugin</artifactId>
|
||||
<version>${jacoco.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>jacoco-initialize</id>
|
||||
<goals>
|
||||
<goal>prepare-agent</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>jacoco-site</id>
|
||||
<phase>test</phase>
|
||||
<goals>
|
||||
<goal>report</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
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.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;
|
||||
|
||||
import com.taobao.diamond.client.Diamond;
|
||||
|
||||
/**
|
||||
* Created on 01/10/2017.
|
||||
*
|
||||
* @author juven.xuxb
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Diamond.class })
|
||||
public class AcmAutoConfiguration implements ApplicationContextAware {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public AcmPropertySourceRepository acmPropertySourceRepository() {
|
||||
return new AcmPropertySourceRepository(applicationContext);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AcmRefreshHistory acmRefreshHistory() {
|
||||
return new AcmRefreshHistory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AcmContextRefresher acmContextRefresher(
|
||||
AcmIntegrationProperties acmIntegrationProperties,
|
||||
ContextRefresher contextRefresher, AcmRefreshHistory refreshHistory,
|
||||
AcmPropertySourceRepository propertySourceRepository) {
|
||||
return new AcmContextRefresher(contextRefresher, acmIntegrationProperties,
|
||||
refreshHistory, propertySourceRepository);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm;
|
||||
|
||||
import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.env.CompositePropertySource;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb, 5/17/16.
|
||||
*/
|
||||
public class AcmPropertySourceRepository {
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public AcmPropertySourceRepository(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* get all acm properties from application context
|
||||
* @return
|
||||
*/
|
||||
public List<AcmPropertySource> getAll() {
|
||||
List<AcmPropertySource> result = new ArrayList<>();
|
||||
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext;
|
||||
for (PropertySource p : ctx.getEnvironment().getPropertySources()) {
|
||||
if (p instanceof AcmPropertySource) {
|
||||
result.add((AcmPropertySource) p);
|
||||
}
|
||||
else if (p instanceof CompositePropertySource) {
|
||||
collectAcmPropertySources((CompositePropertySource) p, result);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void collectAcmPropertySources(CompositePropertySource composite,
|
||||
List<AcmPropertySource> result) {
|
||||
for (PropertySource p : composite.getPropertySources()) {
|
||||
if (p instanceof AcmPropertySource) {
|
||||
result.add((AcmPropertySource) p);
|
||||
}
|
||||
else if (p instanceof CompositePropertySource) {
|
||||
collectAcmPropertySources((CompositePropertySource) p, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm.bootstrap;
|
||||
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb
|
||||
* @author xiaolongzuo
|
||||
*/
|
||||
public class AcmPropertySource extends MapPropertySource {
|
||||
|
||||
private final String dataId;
|
||||
|
||||
private final Date timestamp;
|
||||
|
||||
private final boolean groupLevel;
|
||||
|
||||
AcmPropertySource(String dataId, Map<String, Object> source, Date timestamp,
|
||||
boolean groupLevel) {
|
||||
super(dataId, source);
|
||||
this.dataId = dataId;
|
||||
this.timestamp = timestamp;
|
||||
this.groupLevel = groupLevel;
|
||||
}
|
||||
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
public Date getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public boolean isGroupLevel() {
|
||||
return groupLevel;
|
||||
}
|
||||
}
|
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm.bootstrap;
|
||||
|
||||
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;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.Date;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author juven.xuxb
|
||||
* @author xiaolongzuo
|
||||
*/
|
||||
class AcmPropertySourceBuilder {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(AcmPropertySourceBuilder.class);
|
||||
|
||||
/**
|
||||
* 传入 ACM 的 DataId 和 groupID,获取到解析后的 AcmProperty 对象
|
||||
*
|
||||
* @param dataId
|
||||
* @param diamondGroup
|
||||
* @param groupLevel
|
||||
* @return
|
||||
*/
|
||||
AcmPropertySource build(String dataId, String diamondGroup, boolean groupLevel) {
|
||||
Properties properties = loadDiamondData(dataId, diamondGroup);
|
||||
if (properties == null) {
|
||||
return null;
|
||||
}
|
||||
return new AcmPropertySource(dataId, 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,69 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm.bootstrap;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.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
|
||||
*/
|
||||
public class AcmPropertySourceLocator implements PropertySourceLocator {
|
||||
|
||||
private static final String DIAMOND_PROPERTY_SOURCE_NAME = "diamond";
|
||||
|
||||
private AcmPropertySourceBuilder acmPropertySourceBuilder = new AcmPropertySourceBuilder();
|
||||
|
||||
@Autowired
|
||||
private AcmIntegrationProperties acmIntegrationProperties;
|
||||
|
||||
@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);
|
||||
}
|
||||
|
||||
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,82 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm.endpoint;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository;
|
||||
import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource;
|
||||
import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 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 propertySourceRepository;
|
||||
|
||||
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 propertySourceRepository) {
|
||||
this.properties = properties;
|
||||
this.refreshHistory = refreshHistory;
|
||||
this.propertySourceRepository = propertySourceRepository;
|
||||
}
|
||||
|
||||
@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 = propertySourceRepository.getAll();
|
||||
|
||||
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,59 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm.endpoint;
|
||||
|
||||
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.ConditionalOnWebApplication;
|
||||
import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository;
|
||||
import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@ConditionalOnWebApplication
|
||||
@ConditionalOnClass(name = "org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration")
|
||||
public class AcmEndpointAutoConfiguration {
|
||||
|
||||
@Autowired
|
||||
private AcmProperties acmProperties;
|
||||
|
||||
@Autowired
|
||||
private AcmRefreshHistory acmRefreshHistory;
|
||||
|
||||
@Autowired
|
||||
private AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnEnabledEndpoint
|
||||
@Bean
|
||||
public AcmEndpoint acmEndpoint() {
|
||||
return new AcmEndpoint(acmProperties, acmRefreshHistory,
|
||||
acmPropertySourceRepository);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public AcmHealthIndicator acmHealthIndicator(AcmProperties acmProperties,
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
return new AcmHealthIndicator(acmProperties, acmPropertySourceRepository);
|
||||
}
|
||||
}
|
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm.endpoint;
|
||||
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository;
|
||||
import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author leijuan
|
||||
* @author juven
|
||||
*/
|
||||
public class AcmHealthIndicator extends AbstractHealthIndicator {
|
||||
|
||||
private final AcmProperties acmProperties;
|
||||
|
||||
private final AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
private final List<String> dataIds;
|
||||
|
||||
public AcmHealthIndicator(AcmProperties acmProperties,
|
||||
AcmPropertySourceRepository acmPropertySourceRepository) {
|
||||
this.acmProperties = acmProperties;
|
||||
this.acmPropertySourceRepository = acmPropertySourceRepository;
|
||||
|
||||
this.dataIds = new ArrayList<>();
|
||||
for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository
|
||||
.getAll()) {
|
||||
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,123 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm.refresh;
|
||||
|
||||
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.alicloud.acm.AcmPropertySourceRepository;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmIntegrationProperties;
|
||||
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;
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 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 ApplicationContext applicationContext;
|
||||
|
||||
private final AcmPropertySourceRepository acmPropertySourceRepository;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void registerDiamondListener(final String dataId) {
|
||||
|
||||
ConfigChangeListener listener = listenerMap.computeIfAbsent(dataId,
|
||||
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,
|
||||
acmIntegrationProperties.getAcmProperties().getGroup(), listener);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.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;
|
||||
|
||||
public 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,9 @@
|
||||
{
|
||||
"properties": [
|
||||
{
|
||||
"name": "spring.application.group",
|
||||
"type": "java.lang.String",
|
||||
"description": "spring application group."
|
||||
}
|
||||
]
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
|
||||
org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySourceLocator
|
||||
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
org.springframework.cloud.alicloud.acm.AcmAutoConfiguration,\
|
||||
org.springframework.cloud.alicloud.acm.endpoint.AcmEndpointAutoConfiguration
|
@@ -0,0 +1,195 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
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.cloud.alicloud.acm.bootstrap.AcmPropertySourceLocator;
|
||||
import org.springframework.cloud.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmIntegrationProperties;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @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 {
|
||||
|
||||
assertNotNull("AcmPropertySourceLocator was not created", locator);
|
||||
assertNotNull("AcmProperties was not created", properties);
|
||||
assertNotNull("AcmIntegrationProperties was not created", integrationProperties);
|
||||
|
||||
checkoutAcmServerAddr();
|
||||
checkoutAcmServerPort();
|
||||
checkoutAcmEndpoint();
|
||||
checkoutAcmNamespace();
|
||||
checkoutAcmGroup();
|
||||
checkoutAcmFileExtension();
|
||||
checkoutAcmTimeout();
|
||||
checkoutAcmProfiles();
|
||||
checkoutAcmRefreshEnabled();
|
||||
checkoutDataLoad();
|
||||
checkoutProfileDataLoad();
|
||||
}
|
||||
|
||||
private void checkoutAcmServerAddr() {
|
||||
assertEquals("AcmProperties server address is wrong", "127.0.0.1",
|
||||
properties.getServerList());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmServerPort() {
|
||||
assertEquals("AcmProperties server port is wrong", "8848",
|
||||
properties.getServerPort());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmEndpoint() {
|
||||
assertEquals("AcmProperties endpoint is wrong", "test-endpoint",
|
||||
properties.getEndpoint());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmNamespace() {
|
||||
assertEquals("AcmProperties namespace is wrong", "test-namespace",
|
||||
properties.getNamespace());
|
||||
|
||||
}
|
||||
|
||||
private void checkoutAcmGroup() {
|
||||
assertEquals("AcmProperties' group is wrong", "test-group",
|
||||
properties.getGroup());
|
||||
}
|
||||
|
||||
private void checkoutAcmFileExtension() {
|
||||
assertEquals("AcmProperties' file extension is wrong", "properties",
|
||||
properties.getFileExtension());
|
||||
}
|
||||
|
||||
private void checkoutAcmTimeout() {
|
||||
assertEquals("AcmProperties' timeout is wrong", 1000, properties.getTimeout());
|
||||
}
|
||||
|
||||
private void checkoutAcmRefreshEnabled() {
|
||||
assertEquals("AcmProperties' refresh enabled is wrong", false,
|
||||
properties.isRefreshEnabled());
|
||||
}
|
||||
|
||||
private void checkoutAcmProfiles() {
|
||||
assertArrayEquals("AcmProperties' profiles is wrong",
|
||||
new String[] { "dev", "test" },
|
||||
integrationProperties.getActiveProfiles());
|
||||
}
|
||||
|
||||
private void checkoutDataLoad() {
|
||||
Assert.assertEquals(environment.getProperty("user.age"), "12");
|
||||
}
|
||||
|
||||
private void checkoutProfileDataLoad() {
|
||||
Assert.assertEquals(environment.getProperty("user.name"), "dev");
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm;
|
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
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.cloud.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
|
||||
/**
|
||||
* @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,106 @@
|
||||
/*
|
||||
* Copyright (C) 2019 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
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.cloud.alicloud.acm.endpoint.AcmEndpointAutoConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
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 (C) 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
|
||||
*
|
||||
* http://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 org.springframework.cloud.alicloud.acm.endpoint;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.NONE;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
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.cloud.alicloud.acm.AcmAutoConfiguration;
|
||||
import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository;
|
||||
import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration;
|
||||
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.alibaba.edas.acm.ConfigService;
|
||||
|
||||
/**
|
||||
* @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 propertySourceRepository;
|
||||
|
||||
@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,
|
||||
propertySourceRepository);
|
||||
Map<String, Object> map = acmEndpoint.invoke();
|
||||
assertEquals(map.get("config"), properties);
|
||||
assertEquals(((Map) map.get("runtime")).get("refreshHistory"),
|
||||
refreshHistory.getRecords());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@ImportAutoConfiguration({ AcmEndpointAutoConfiguration.class,
|
||||
AcmAutoConfiguration.class, AcmContextBootstrapConfiguration.class })
|
||||
public static class TestConfig {
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user