|
|
|
|
@ -17,20 +17,14 @@
|
|
|
|
|
|
|
|
|
|
package com.tencent.cloud.polaris.config.adapter;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
import com.tencent.cloud.common.util.EnvironmentUtils;
|
|
|
|
|
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
|
|
|
|
|
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
|
|
|
|
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
|
|
|
|
|
import com.tencent.polaris.api.utils.CollectionUtils;
|
|
|
|
|
import com.tencent.polaris.api.utils.StringUtils;
|
|
|
|
|
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
|
|
|
|
|
import com.tencent.polaris.configuration.api.core.ConfigFileService;
|
|
|
|
|
import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
@ -40,9 +34,6 @@ import org.springframework.core.env.CompositePropertySource;
|
|
|
|
|
import org.springframework.core.env.Environment;
|
|
|
|
|
import org.springframework.core.env.PropertySource;
|
|
|
|
|
|
|
|
|
|
import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadGroupPolarisPropertySource;
|
|
|
|
|
import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadPolarisPropertySource;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Spring cloud reserved core configuration loading SPI.
|
|
|
|
|
* <p>
|
|
|
|
|
@ -60,6 +51,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
|
|
|
|
|
private final PolarisConfigProperties polarisConfigProperties;
|
|
|
|
|
private final PolarisContextProperties polarisContextProperties;
|
|
|
|
|
private final ConfigFileService configFileService;
|
|
|
|
|
private final PolarisConfigFilePuller puller;
|
|
|
|
|
private final Environment environment;
|
|
|
|
|
// this class provides customized logic for some customers to configure special business group files
|
|
|
|
|
private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer();
|
|
|
|
|
@ -69,6 +61,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
|
|
|
|
|
this.polarisConfigProperties = polarisConfigProperties;
|
|
|
|
|
this.polarisContextProperties = polarisContextProperties;
|
|
|
|
|
this.configFileService = configFileService;
|
|
|
|
|
this.puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService);
|
|
|
|
|
this.environment = environment;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -94,10 +87,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
|
|
|
|
|
// load spring boot default config files
|
|
|
|
|
initInternalConfigFiles(compositePropertySource);
|
|
|
|
|
// load custom config files
|
|
|
|
|
List<ConfigFileGroup> configFileGroups = polarisConfigProperties.getGroups();
|
|
|
|
|
if (CollectionUtils.isNotEmpty(configFileGroups)) {
|
|
|
|
|
initCustomPolarisConfigFiles(compositePropertySource, configFileGroups);
|
|
|
|
|
}
|
|
|
|
|
initCustomPolarisConfigFiles(compositePropertySource);
|
|
|
|
|
// load tsf default config group
|
|
|
|
|
initTsfConfigGroups(compositePropertySource);
|
|
|
|
|
return compositePropertySource;
|
|
|
|
|
@ -129,142 +119,24 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
|
|
|
|
|
if (!polarisConfigProperties.isInternalEnabled()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles();
|
|
|
|
|
|
|
|
|
|
for (ConfigFileMetadata configFile : internalConfigFiles) {
|
|
|
|
|
if (StringUtils.isEmpty(configFile.getFileGroup())) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService, configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName());
|
|
|
|
|
|
|
|
|
|
compositePropertySource.addPropertySource(polarisPropertySource);
|
|
|
|
|
|
|
|
|
|
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
|
|
|
|
|
|
|
|
|
|
LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<ConfigFileMetadata> getInternalConfigFiles() {
|
|
|
|
|
String namespace = polarisContextProperties.getNamespace();
|
|
|
|
|
String serviceName = polarisContextProperties.getService();
|
|
|
|
|
if (StringUtils.isBlank(serviceName)) {
|
|
|
|
|
serviceName = environment.getProperty("spring.application.name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<ConfigFileMetadata> internalConfigFiles = new LinkedList<>();
|
|
|
|
|
|
|
|
|
|
// priority: application-${profile} > application > boostrap-${profile} > boostrap
|
|
|
|
|
String[] activeProfiles = environment.getActiveProfiles();
|
|
|
|
|
String[] defaultProfiles = environment.getDefaultProfiles();
|
|
|
|
|
List<String> profileList = new ArrayList<>();
|
|
|
|
|
if (CollectionUtils.isNotEmpty(activeProfiles)) {
|
|
|
|
|
profileList.addAll(Arrays.asList(activeProfiles));
|
|
|
|
|
}
|
|
|
|
|
else if (CollectionUtils.isNotEmpty(defaultProfiles)) {
|
|
|
|
|
profileList.addAll(Arrays.asList(defaultProfiles));
|
|
|
|
|
}
|
|
|
|
|
// build application config files
|
|
|
|
|
buildInternalApplicationConfigFiles(internalConfigFiles, namespace, serviceName, profileList);
|
|
|
|
|
// build bootstrap config files
|
|
|
|
|
buildInternalBootstrapConfigFiles(internalConfigFiles, namespace, serviceName, profileList);
|
|
|
|
|
|
|
|
|
|
return internalConfigFiles;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buildInternalApplicationConfigFiles(List<ConfigFileMetadata> internalConfigFiles, String namespace, String serviceName, List<String> profileList) {
|
|
|
|
|
for (String profile : profileList) {
|
|
|
|
|
if (StringUtils.isBlank(profile)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".properties"));
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".yml"));
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".yaml"));
|
|
|
|
|
}
|
|
|
|
|
// build default config properties files.
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties"));
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml"));
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yaml"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buildInternalBootstrapConfigFiles(List<ConfigFileMetadata> internalConfigFiles, String namespace, String serviceName, List<String> profileList) {
|
|
|
|
|
for (String profile : profileList) {
|
|
|
|
|
if (StringUtils.isBlank(profile)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".properties"));
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".yml"));
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".yaml"));
|
|
|
|
|
}
|
|
|
|
|
// build default config properties files.
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties"));
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml"));
|
|
|
|
|
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yaml"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void initTsfConfigGroups(CompositePropertySource compositePropertySource) {
|
|
|
|
|
String tsfId = environment.getProperty("tsf_id");
|
|
|
|
|
String tsfNamespaceName = environment.getProperty("tsf_namespace_name");
|
|
|
|
|
String tsfGroupName = environment.getProperty("tsf_group_name");
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isEmpty(tsfNamespaceName) || StringUtils.isEmpty(tsfGroupName)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String namespace = polarisContextProperties.getNamespace();
|
|
|
|
|
List<String> tsfConfigGroups = new ArrayList<>();
|
|
|
|
|
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group");
|
|
|
|
|
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfNamespaceName + ".global_config_group");
|
|
|
|
|
|
|
|
|
|
if (EnvironmentUtils.isGateway()) {
|
|
|
|
|
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".gateway_config_group");
|
|
|
|
|
}
|
|
|
|
|
for (String tsfConfigGroup : tsfConfigGroups) {
|
|
|
|
|
PolarisPropertySource polarisPropertySource = loadGroupPolarisPropertySource(configFileService, namespace, tsfConfigGroup);
|
|
|
|
|
if (polarisPropertySource == null) {
|
|
|
|
|
// not register to polaris
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
compositePropertySource.addPropertySource(polarisPropertySource);
|
|
|
|
|
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, List<ConfigFileGroup> configFileGroups) {
|
|
|
|
|
String namespace = polarisContextProperties.getNamespace();
|
|
|
|
|
|
|
|
|
|
for (ConfigFileGroup configFileGroup : configFileGroups) {
|
|
|
|
|
String groupNamespace = configFileGroup.getNamespace();
|
|
|
|
|
if (StringUtils.isBlank(groupNamespace)) {
|
|
|
|
|
groupNamespace = namespace;
|
|
|
|
|
String serviceName = polarisContextProperties.getService();
|
|
|
|
|
if (StringUtils.isBlank(serviceName)) {
|
|
|
|
|
serviceName = environment.getProperty("spring.application.name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String group = configFileGroup.getName();
|
|
|
|
|
if (StringUtils.isBlank(group)) {
|
|
|
|
|
continue;
|
|
|
|
|
this.puller.initInternalConfigFiles(compositePropertySource, activeProfiles, defaultProfiles, serviceName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<String> files = configFileGroup.getFiles();
|
|
|
|
|
|
|
|
|
|
if (CollectionUtils.isEmpty(files)) {
|
|
|
|
|
PolarisPropertySource polarisPropertySource = loadGroupPolarisPropertySource(configFileService, namespace, group);
|
|
|
|
|
if (polarisPropertySource == null) {
|
|
|
|
|
continue;
|
|
|
|
|
private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource) {
|
|
|
|
|
List<ConfigFileGroup> configFileGroups = polarisConfigProperties.getGroups();
|
|
|
|
|
if (CollectionUtils.isNotEmpty(configFileGroups)) {
|
|
|
|
|
this.puller.initCustomPolarisConfigFiles(compositePropertySource, configFileGroups);
|
|
|
|
|
}
|
|
|
|
|
compositePropertySource.addPropertySource(polarisPropertySource);
|
|
|
|
|
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
|
|
|
|
|
LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}", namespace, group);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (String fileName : files) {
|
|
|
|
|
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService, groupNamespace, group, fileName);
|
|
|
|
|
|
|
|
|
|
compositePropertySource.addPropertySource(polarisPropertySource);
|
|
|
|
|
|
|
|
|
|
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
|
|
|
|
|
|
|
|
|
|
LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", groupNamespace, group, fileName);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private void initTsfConfigGroups(CompositePropertySource compositePropertySource) {
|
|
|
|
|
this.puller.initTsfConfigGroups(compositePropertySource);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|