1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00
This commit is contained in:
chenzhu.zxl
2018-10-29 18:09:23 +08:00
parent bf7aec2fbc
commit e74d54604d
4 changed files with 191 additions and 91 deletions

View File

@@ -16,20 +16,12 @@
package org.springframework.cloud.alicloud.acm.bootstrap;
import com.taobao.diamond.maintenance.DiamondHealth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.alicloud.acm.diagnostics.analyzer.DiamondConnectionFailureException;
import org.springframework.cloud.alicloud.context.acm.AcmProperties;
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;
import org.springframework.util.StringUtils;
import static com.taobao.diamond.client.impl.ServerHttpAgent.addressPort;
import static com.taobao.diamond.client.impl.ServerHttpAgent.domainName;
/**
* @author juven.xuxb
@@ -37,8 +29,6 @@ import static com.taobao.diamond.client.impl.ServerHttpAgent.domainName;
*/
public class AcmPropertySourceLocator implements PropertySourceLocator {
private final Logger logger = LoggerFactory.getLogger(AcmPropertySourceLocator.class);
private static final String DIAMOND_PROPERTY_SOURCE_NAME = "diamond";
private static String defaultDiamondGroup = "DEFAULT_GROUP";
@@ -46,87 +36,26 @@ public class AcmPropertySourceLocator implements PropertySourceLocator {
private AcmPropertySourceBuilder acmPropertySourceBuilder = new AcmPropertySourceBuilder();
@Autowired
private AcmProperties acmProperties;
private AcmIntegrationProperties acmIntegrationProperties;
@Override
public PropertySource<?> locate(Environment environment) {
String applicationName = environment.getProperty("spring.application.name");
logger.info("Initialize spring.application.name '" + applicationName + "'.");
String applicationGroup = environment.getProperty("spring.application.group");
if (StringUtils.isEmpty(applicationName)) {
throw new IllegalStateException(
"'spring.application.name' must be configured.");
}
CompositePropertySource compositePropertySource = new CompositePropertySource(
DIAMOND_PROPERTY_SOURCE_NAME);
loadGroupConfigurationRecursively(compositePropertySource, applicationGroup);
loadApplicationConfiguration(compositePropertySource, environment,
applicationGroup, applicationName);
return compositePropertySource;
}
private void checkDiamondHealth() {
logger.info("Checking ACM health");
try {
if (!"UP".equals(DiamondHealth.getHealth())) {
throw new DiamondConnectionFailureException(domainName, addressPort,
DiamondHealth.getHealth());
}
}
catch (Throwable t) {
throw new DiamondConnectionFailureException(domainName, addressPort,
"ACM Health error", t);
}
}
private void loadGroupConfigurationRecursively(
CompositePropertySource compositePropertySource, String applicationGroup) {
if (StringUtils.isEmpty(applicationGroup)) {
return;
}
String[] parts = applicationGroup.split("\\.");
for (int i = 1; i < parts.length; i++) {
String subGroup = parts[0];
for (int j = 1; j <= i; j++) {
subGroup = subGroup + "." + parts[j];
}
String dataId = subGroup + ":application." + acmProperties.getFileExtension();
for (String dataId : acmIntegrationProperties.getGroupConfigurationDataIds()) {
loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup,
true);
}
}
private void loadApplicationConfiguration(
CompositePropertySource compositePropertySource, Environment environment,
String applicationGroup, String applicationName) {
if (!StringUtils.isEmpty(applicationGroup)) {
String dataId = applicationGroup + ":" + applicationName + "."
+ acmProperties.getFileExtension();
loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup,
false);
for (String profile : environment.getActiveProfiles()) {
dataId = applicationGroup + ":" + applicationName + "-" + profile + "."
+ acmProperties.getFileExtension();
loadDiamondDataIfPresent(compositePropertySource, dataId,
defaultDiamondGroup, false);
}
}
String dataId = applicationName + "." + acmProperties.getFileExtension();
loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup,
false);
for (String profile : environment.getActiveProfiles()) {
dataId = applicationName + "-" + profile + "."
+ acmProperties.getFileExtension();
for (String dataId : acmIntegrationProperties
.getApplicationConfigurationDataIds()) {
loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup,
false);
}
return compositePropertySource;
}
private void loadDiamondDataIfPresent(final CompositePropertySource composite,