enhance ACM module base spring-cloud-common 2.2.1

pull/1177/head
yuhuangbin
parent 5f8d8cc043
commit acf7001d9f

@ -65,9 +65,8 @@ public final class NacosPropertySourceRepository {
public static void collectNacosPropertySource(
NacosPropertySource nacosPropertySource) {
NACOS_PROPERTY_SOURCE_REPOSITORY
.putIfAbsent(getMapKey(nacosPropertySource.getDataId(),
nacosPropertySource.getGroup()), nacosPropertySource);
NACOS_PROPERTY_SOURCE_REPOSITORY.putIfAbsent(getMapKey(nacosPropertySource.getDataId(),
nacosPropertySource.getGroup()), nacosPropertySource);
}
public static NacosPropertySource getNacosPropertySource(String dataId,

@ -21,12 +21,9 @@ import com.alibaba.alicloud.acm.refresh.AcmRefreshHistory;
import com.alibaba.alicloud.context.acm.AcmIntegrationProperties;
import com.taobao.diamond.client.Diamond;
import org.springframework.beans.BeansException;
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.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -38,14 +35,7 @@ import org.springframework.context.annotation.Configuration;
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ Diamond.class })
@ConditionalOnProperty(name = "spring.cloud.alicloud.acm.enabled", matchIfMissing = true)
public class AcmAutoConfiguration implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Bean
public AcmPropertySourceRepository acmPropertySourceRepository() {
return new AcmPropertySourceRepository(applicationContext);
}
public class AcmAutoConfiguration {
@Bean
public AcmRefreshHistory acmRefreshHistory() {
@ -56,15 +46,9 @@ public class AcmAutoConfiguration implements ApplicationContextAware {
public AcmContextRefresher acmContextRefresher(
AcmIntegrationProperties acmIntegrationProperties,
ContextRefresher contextRefresher, AcmRefreshHistory refreshHistory,
AcmPropertySourceRepository propertySourceRepository) {
AcmPropertySourceRepository acmPropertySourceRepository) {
return new AcmContextRefresher(contextRefresher, acmIntegrationProperties,
refreshHistory, propertySourceRepository);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
refreshHistory, acmPropertySourceRepository);
}
}

@ -17,54 +17,46 @@
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.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.PropertySource;
/**
* @author juven.xuxb, 5/17/16.
* @author yuhuangbin
*/
public class AcmPropertySourceRepository {
private final ApplicationContext applicationContext;
public AcmPropertySourceRepository(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
private Map<String, AcmPropertySource> acmPropertySourceMap = new ConcurrentHashMap<>();
/**
* get all acm properties from application context.
* get all acm properties from AcmPropertySourceRepository.
* @return list of acm propertysource
*/
public List<AcmPropertySource> getAll() {
public List<AcmPropertySource> allAcmPropertySource() {
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);
}
}
result.addAll(this.acmPropertySourceMap.values());
return result;
}
private void collectAcmPropertySources(CompositePropertySource composite,
List<AcmPropertySource> result) {
for (PropertySource p : composite.getPropertySources()) {
if (p instanceof AcmPropertySource) {
result.add((AcmPropertySource) p);
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);
}
else if (p instanceof CompositePropertySource) {
collectAcmPropertySources((CompositePropertySource) p, result);
}
}
});
}
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);
}
}

@ -29,14 +29,17 @@ public class AcmPropertySource extends MapPropertySource {
private final String dataId;
private final String group;
private final Date timestamp;
private final boolean groupLevel;
AcmPropertySource(String dataId, Map<String, Object> source, Date timestamp,
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;
}
@ -49,6 +52,10 @@ public class AcmPropertySource extends MapPropertySource {
return timestamp;
}
public String getGroup() {
return group;
}
public boolean isGroupLevel() {
return groupLevel;
}

@ -52,7 +52,8 @@ class AcmPropertySourceBuilder {
if (properties == null) {
return null;
}
return new AcmPropertySource(dataId, toMap(properties), new Date(), groupLevel);
return new AcmPropertySource(dataId, diamondGroup, toMap(properties), new Date(),
groupLevel);
}
private Properties loadDiamondData(String dataId, String diamondGroup) {

@ -16,10 +16,9 @@
package com.alibaba.alicloud.acm.bootstrap;
import com.alibaba.alicloud.acm.AcmPropertySourceRepository;
import com.alibaba.alicloud.context.acm.AcmIntegrationProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
@ -28,17 +27,24 @@ import org.springframework.core.env.PropertySource;
/**
* @author juven.xuxb
* @author xiaolongzuo
* @author yuhuangbin
*/
@ConditionalOnProperty(name = "spring.cloud.alicloud.acm.enabled", matchIfMissing = true)
public class AcmPropertySourceLocator implements PropertySourceLocator {
private static final String DIAMOND_PROPERTY_SOURCE_NAME = "diamond";
private AcmPropertySourceBuilder acmPropertySourceBuilder = new AcmPropertySourceBuilder();
@Autowired
private AcmIntegrationProperties acmIntegrationProperties;
private AcmPropertySourceRepository acmPropertySourceRepository;
public AcmPropertySourceLocator(AcmIntegrationProperties acmIntegrationProperties,
AcmPropertySourceRepository acmPropertySourceRepository) {
this.acmIntegrationProperties = acmIntegrationProperties;
this.acmPropertySourceRepository = acmPropertySourceRepository;
}
@Override
public PropertySource<?> locate(Environment environment) {
@ -57,7 +63,8 @@ public class AcmPropertySourceLocator implements PropertySourceLocator {
loadDiamondDataIfPresent(compositePropertySource, dataId,
acmIntegrationProperties.getAcmProperties().getGroup(), false);
}
acmPropertySourceRepository
.collectAcmPropertySource(compositePropertySource.getPropertySources());
return compositePropertySource;
}

@ -43,7 +43,7 @@ public class AcmEndpoint {
private final AcmRefreshHistory refreshHistory;
private final AcmPropertySourceRepository propertySourceRepository;
private final AcmPropertySourceRepository acmPropertySourceRepository;
private ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
@Override
@ -53,10 +53,10 @@ public class AcmEndpoint {
};
public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory,
AcmPropertySourceRepository propertySourceRepository) {
AcmPropertySourceRepository acmPropertySourceRepository) {
this.properties = properties;
this.refreshHistory = refreshHistory;
this.propertySourceRepository = propertySourceRepository;
this.acmPropertySourceRepository = acmPropertySourceRepository;
}
@ReadOperation
@ -65,7 +65,7 @@ public class AcmEndpoint {
result.put("config", properties);
Map<String, Object> runtime = new HashMap<>();
List<AcmPropertySource> all = propertySourceRepository.getAll();
List<AcmPropertySource> all = acmPropertySourceRepository.allAcmPropertySource();
List<Map<String, Object>> sources = new ArrayList<>();
for (AcmPropertySource ps : all) {

@ -43,13 +43,11 @@ public class AcmEndpointAutoConfiguration {
@Autowired
private AcmRefreshHistory acmRefreshHistory;
@Autowired
private AcmPropertySourceRepository acmPropertySourceRepository;
@ConditionalOnMissingBean
@ConditionalOnEnabledEndpoint
@Bean
public AcmEndpoint acmEndpoint() {
public AcmEndpoint acmEndpoint(
AcmPropertySourceRepository acmPropertySourceRepository) {
return new AcmEndpoint(acmProperties, acmRefreshHistory,
acmPropertySourceRepository);
}

@ -36,10 +36,10 @@ public class AcmHealthIndicator extends AbstractHealthIndicator {
private final AcmProperties acmProperties;
private final AcmPropertySourceRepository acmPropertySourceRepository;
private final List<String> dataIds;
private final AcmPropertySourceRepository acmPropertySourceRepository;
public AcmHealthIndicator(AcmProperties acmProperties,
AcmPropertySourceRepository acmPropertySourceRepository) {
this.acmProperties = acmProperties;
@ -47,7 +47,7 @@ public class AcmHealthIndicator extends AbstractHealthIndicator {
this.dataIds = new ArrayList<>();
for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository
.getAll()) {
.allAcmPropertySource()) {
this.dataIds.add(acmPropertySource.getDataId());
}
}

@ -57,9 +57,9 @@ public class AcmContextRefresher
private final AcmRefreshHistory refreshHistory;
private ApplicationContext applicationContext;
private AcmPropertySourceRepository acmPropertySourceRepository;
private final AcmPropertySourceRepository acmPropertySourceRepository;
private ApplicationContext applicationContext;
private Map<String, ConfigChangeListener> listenerMap = new ConcurrentHashMap<>(16);
@ -82,14 +82,15 @@ public class AcmContextRefresher
if (acmIntegrationProperties.getAcmProperties().isRefreshEnabled()) {
for (String dataId : acmIntegrationProperties
.getApplicationConfigurationDataIds()) {
registerDiamondListener(dataId);
registerDiamondListener(dataId,
acmIntegrationProperties.getAcmProperties().getGroup());
}
}
}
private void registerDiamondListener(final String dataId) {
ConfigChangeListener listener = listenerMap.computeIfAbsent(dataId,
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) {
@ -111,8 +112,7 @@ public class AcmContextRefresher
"ACM Refresh, dataId=" + dataId));
}
});
ConfigService.addListener(dataId,
acmIntegrationProperties.getAcmProperties().getGroup(), listener);
ConfigService.addListener(dataId, group, listener);
}
@Override

@ -1,5 +1,5 @@
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.alibaba.alicloud.acm.bootstrap.AcmPropertySourceLocator
com.alibaba.alicloud.acm.bootstrap.AcmConfigBootStrapConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.alicloud.acm.AcmAutoConfiguration,\

@ -95,9 +95,6 @@ public class AcmEndpointTests {
@Autowired
private AcmRefreshHistory refreshHistory;
@Autowired
private AcmPropertySourceRepository propertySourceRepository;
@Autowired
private AcmPropertySourceRepository acmPropertySourceRepository;
@ -133,7 +130,7 @@ public class AcmEndpointTests {
private void checkoutEndpoint() throws Exception {
AcmEndpoint acmEndpoint = new AcmEndpoint(properties, refreshHistory,
propertySourceRepository);
acmPropertySourceRepository);
Map<String, Object> map = acmEndpoint.invoke();
assertThat(properties).isEqualTo(map.get("config"));
assertThat(refreshHistory.getRecords())

Loading…
Cancel
Save