Skip to content

Commit

Permalink
1. 支持监听整个配置分组
Browse files Browse the repository at this point in the history
2. 支持对接 TSF 时监听默认分组
3. 支持单配置刷新
  • Loading branch information
shedfreez committed Nov 21, 2024
1 parent 84a98de commit 1fc1680
Show file tree
Hide file tree
Showing 23 changed files with 1,071 additions and 725 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ private void initDataSource(ConfigurationImpl configuration) {
throw new RuntimeException("Config server address is blank. Please check your config in bootstrap.yml"
+ " with spring.cloud.polaris.address or spring.cloud.polaris.config.address");
}

checkAddressAccessible(configAddresses);
if (polarisConfigProperties.isCheckAddress()) {
checkAddressAccessible(configAddresses);
}

configuration.getConfigFile().getServerConnector().setAddresses(configAddresses);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@

import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher;
import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor;
import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.listener.PolarisConfigChangeEventListener;
import com.tencent.cloud.polaris.config.listener.PolarisConfigRefreshOptimizationListener;
import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerApplicationListener;
import com.tencent.cloud.polaris.config.spring.annotation.SpringValueProcessor;
import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper;
import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry;
import com.tencent.polaris.configuration.api.core.ConfigFileService;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -78,46 +77,38 @@ public ConfigurationPropertiesRebinder affectedConfigurationPropertiesRebinder(
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(
PolarisConfigProperties polarisConfigProperties, ContextRefresher contextRefresher) {
return new PolarisRefreshEntireContextRefresher(polarisConfigProperties, contextRefresher);
PolarisConfigProperties polarisConfigProperties, SpringValueRegistry springValueRegistry,
ConfigFileService configFileService, ContextRefresher contextRefresher) {
return new PolarisRefreshEntireContextRefresher(polarisConfigProperties,
springValueRegistry, configFileService, contextRefresher);
}

@Bean
public SpringValueRegistry springValueRegistry() {
return new SpringValueRegistry();
}

@Bean
public PlaceholderHelper placeholderHelper() {
return new PlaceholderHelper();
}

@Bean
public SpringValueProcessor springValueProcessor(PlaceholderHelper placeholderHelper,
SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties) {
return new SpringValueProcessor(placeholderHelper, springValueRegistry, polarisConfigProperties);
}

@Configuration(proxyBeanMethods = false)
@ConditionalOnReflectRefreshType
@AutoConfigureBefore(PolarisConfigAutoConfiguration.class)
public static class PolarisReflectRefresherAutoConfiguration {
@Bean
public SpringValueRegistry springValueRegistry() {
return new SpringValueRegistry();
}

@Bean
public PlaceholderHelper placeholderHelper() {
return new PlaceholderHelper();
}

@Bean
public SpringValueProcessor springValueProcessor(PlaceholderHelper placeholderHelper,
SpringValueRegistry springValueRegistry, PolarisConfigProperties polarisConfigProperties) {
return new SpringValueProcessor(placeholderHelper, springValueRegistry, polarisConfigProperties);
}

@Bean
public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(
PolarisConfigProperties polarisConfigProperties, SpringValueRegistry springValueRegistry,
PlaceholderHelper placeholderHelper) {
PlaceholderHelper placeholderHelper, ConfigFileService configFileService, ContextRefresher contextRefresher) {
return new PolarisRefreshAffectedContextRefresher(polarisConfigProperties,
springValueRegistry, placeholderHelper);
}

@Bean
public PolarisConfigRefreshScopeAnnotationDetector polarisConfigRefreshScopeAnnotationDetector() {
return new PolarisConfigRefreshScopeAnnotationDetector();
}

@Bean
public PolarisConfigRefreshOptimizationListener polarisConfigRefreshOptimizationListener() {
return new PolarisConfigRefreshOptimizationListener();
springValueRegistry, placeholderHelper, configFileService, contextRefresher);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ public interface PolarisConfigCustomExtensionLayer {

void executeAfterLocateConfigReturning(CompositePropertySource compositePropertySource);

boolean executeRegisterPublishChangeListener(PolarisPropertySource polarisPropertySource);
boolean executeRegisterPublishChangeListener(PolarisPropertySource polarisPropertySource, PolarisPropertySource effectPolarisPropertySource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.client.internal.CompositeConfigFile;
import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -68,6 +69,8 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
// this class provides customized logic for some customers to configure special business group files
private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer();

private volatile static CompositePropertySource compositePropertySourceCache = null;

public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, Environment environment) {
this.polarisConfigProperties = polarisConfigProperties;
Expand All @@ -76,21 +79,34 @@ public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties,
this.environment = environment;
}

/**
* order: spring boot default config files > custom config files > tsf default config group.
* @param environment The current Environment.
* @return The PropertySource to be added to the Environment.
*/
@Override
public PropertySource<?> locate(Environment environment) {
if (polarisConfigProperties.isEnabled()) {
// use cache when refreshing context
if (compositePropertySourceCache != null) {
return compositePropertySourceCache;
}
CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME);
compositePropertySourceCache = compositePropertySource;
try {
// load custom config extension files
initCustomPolarisConfigExtensionFiles(compositePropertySource);
// load spring boot default config files
initInternalConfigFiles(compositePropertySource);
// load custom config files
List<ConfigFileGroup> configFileGroups = polarisConfigProperties.getGroups();
if (CollectionUtils.isEmpty(configFileGroups)) {
return compositePropertySource;

if (!CollectionUtils.isEmpty(configFileGroups)) {
initCustomPolarisConfigFiles(compositePropertySource, configFileGroups);
}
initCustomPolarisConfigFiles(compositePropertySource, configFileGroups);
// load tsf default config group
initTsfConfigGroups(compositePropertySource);

return compositePropertySource;
}
finally {
Expand Down Expand Up @@ -123,7 +139,10 @@ private void initInternalConfigFiles(CompositePropertySource compositePropertySo
List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles();

for (ConfigFileMetadata configFile : internalConfigFiles) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName());
if (StringUtils.isEmpty(configFile.getFileGroup())) {
continue;
}
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService, configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName());

compositePropertySource.addPropertySource(polarisPropertySource);

Expand Down Expand Up @@ -190,6 +209,29 @@ private void buildInternalBootstrapConfigFiles(List<ConfigFileMetadata> internal
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yaml"));
}

private 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(tsfId) || StringUtils.isEmpty(tsfNamespaceName) || StringUtils.isEmpty(tsfGroupName)) {
return;
}
String namespace = polarisContextProperties.getNamespace();
List<String> tsfConfigGroups = Arrays.asList(
tsfId + "." + tsfGroupName + ".application_config_group",
tsfId + "." + tsfNamespaceName + ".global_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();

Expand All @@ -201,27 +243,35 @@ private void initCustomPolarisConfigFiles(CompositePropertySource compositePrope

String group = configFileGroup.getName();
if (!StringUtils.hasText(group)) {
throw new IllegalArgumentException("polaris config group name cannot be empty.");
continue;
}

List<String> files = configFileGroup.getFiles();

if (CollectionUtils.isEmpty(files)) {
return;
PolarisPropertySource polarisPropertySource = loadGroupPolarisPropertySource(configFileService, namespace, group);
if (polarisPropertySource == null) {
continue;
}
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);

for (String fileName : files) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(groupNamespace, group, fileName);
compositePropertySource.addPropertySource(polarisPropertySource);

compositePropertySource.addPropertySource(polarisPropertySource);

PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);

LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", groupNamespace, group, fileName);
LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", groupNamespace, group, fileName);
}
}
}
}

private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName) {
public static PolarisPropertySource loadPolarisPropertySource(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile;
// unknown extension is resolved as yaml file
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
Expand All @@ -243,4 +293,47 @@ else if (ConfigFileFormat.isPropertyFile(fileName)) {

return new PolarisPropertySource(namespace, group, fileName, configKVFile, map);
}

public static PolarisPropertySource loadGroupPolarisPropertySource(ConfigFileService configFileService, String namespace, String group) {
List<ConfigKVFile> configKVFiles = new ArrayList<>();

com.tencent.polaris.configuration.api.core.ConfigFileGroup remoteGroup = configFileService.getConfigFileGroup(namespace, group);
if (remoteGroup == null) {
return null;
}

for (ConfigFileMetadata configFile : remoteGroup.getConfigFileMetadataList()) {
String fileName = configFile.getFileName();
ConfigKVFile configKVFile;
// unknown extension is resolved as properties file
if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isYamlFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
}
else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);

throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context");
}
configKVFiles.add(configKVFile);
}

CompositeConfigFile compositeConfigFile = new CompositeConfigFile(configKVFiles);

Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : compositeConfigFile.getPropertyNames()) {
String value = compositeConfigFile.getProperty(key, null);
map.put(key, value);
}

if (LOGGER.isDebugEnabled()) {
LOGGER.debug("namespace='" + namespace + '\''
+ ", group='" + group + '\'' + ", fileName='" + compositeConfigFile + '\''
+ ", map='" + map + '\'');
}

return new PolarisPropertySource(namespace, group, "", compositeConfigFile, map);
}
}
Loading

0 comments on commit 1fc1680

Please sign in to comment.