1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

enhance ACM module base spring-cloud-common 2.2.1

This commit is contained in:
yuhuangbin 2020-01-16 11:43:58 +08:00
parent 5f8d8cc043
commit acf7001d9f
13 changed files with 116 additions and 82 deletions

View File

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

View File

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

View File

@ -17,54 +17,46 @@
package com.alibaba.alicloud.acm; package com.alibaba.alicloud.acm;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.alicloud.acm.bootstrap.AcmPropertySource; 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; import org.springframework.core.env.PropertySource;
/** /**
* @author juven.xuxb, 5/17/16. * @author juven.xuxb, 5/17/16.
* @author yuhuangbin
*/ */
public class AcmPropertySourceRepository { public class AcmPropertySourceRepository {
private final ApplicationContext applicationContext; private Map<String, AcmPropertySource> acmPropertySourceMap = new ConcurrentHashMap<>();
public AcmPropertySourceRepository(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
/** /**
* get all acm properties from application context. * get all acm properties from AcmPropertySourceRepository.
* @return list of acm propertysource * @return list of acm propertysource
*/ */
public List<AcmPropertySource> getAll() { public List<AcmPropertySource> allAcmPropertySource() {
List<AcmPropertySource> result = new ArrayList<>(); List<AcmPropertySource> result = new ArrayList<>();
ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext; result.addAll(this.acmPropertySourceMap.values());
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; return result;
} }
private void collectAcmPropertySources(CompositePropertySource composite, public void collectAcmPropertySource(
List<AcmPropertySource> result) { Collection<PropertySource<?>> acmPropertySources) {
for (PropertySource p : composite.getPropertySources()) { acmPropertySources.forEach(propertySource -> {
if (p instanceof AcmPropertySource) { if (propertySource.getClass().isAssignableFrom(AcmPropertySource.class)) {
result.add((AcmPropertySource) p); AcmPropertySource acmPropertySource = (AcmPropertySource) propertySource;
} this.acmPropertySourceMap.put(getMapKey(acmPropertySource.getDataId(),
else if (p instanceof CompositePropertySource) { acmPropertySource.getGroup()), acmPropertySource);
collectAcmPropertySources((CompositePropertySource) p, result);
} }
});
} }
public String getMapKey(String dataId, String group) {
return String.join(",", dataId, group);
} }
} }

View File

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

View File

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

View File

@ -52,7 +52,8 @@ class AcmPropertySourceBuilder {
if (properties == null) { if (properties == null) {
return 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) { private Properties loadDiamondData(String dataId, String diamondGroup) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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