feat:support consul config data. (#1401)

Co-authored-by: Haotian Zhang <skyebefreeman@qq.com>
pull/1402/head
Fishtail 3 months ago committed by GitHub
parent ec6854bd1a
commit 805087701a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -22,3 +22,4 @@
- [feat:support consul service update task.](https://github.com/Tencent/spring-cloud-tencent/pull/1397) - [feat:support consul service update task.](https://github.com/Tencent/spring-cloud-tencent/pull/1397)
- [fix:fix app starting failed when user using custom OpenAPI bean.](https://github.com/Tencent/spring-cloud-tencent/pull/1398) - [fix:fix app starting failed when user using custom OpenAPI bean.](https://github.com/Tencent/spring-cloud-tencent/pull/1398)
- [fix: memory cost too many when using wildcard feign calls](https://github.com/Tencent/spring-cloud-tencent/pull/1400) - [fix: memory cost too many when using wildcard feign calls](https://github.com/Tencent/spring-cloud-tencent/pull/1400)
- [feat:support consul config data.](https://github.com/Tencent/spring-cloud-tencent/pull/1401)

@ -36,6 +36,7 @@ import org.springframework.cloud.context.properties.ConfigurationPropertiesRebin
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
/** /**
@ -84,6 +85,7 @@ public class PolarisConfigBootstrapAutoConfiguration {
} }
@Bean @Bean
@Primary
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
@ConditionalOnReflectRefreshType @ConditionalOnReflectRefreshType
public ConfigurationPropertiesRebinder affectedConfigurationPropertiesRebinder( public ConfigurationPropertiesRebinder affectedConfigurationPropertiesRebinder(

@ -117,6 +117,9 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
} }
private void initInternalConfigFiles(CompositePropertySource compositePropertySource) { private void initInternalConfigFiles(CompositePropertySource compositePropertySource) {
if (!polarisConfigProperties.isInternalEnabled()) {
return;
}
List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles(); List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles();
for (ConfigFileMetadata configFile : internalConfigFiles) { for (ConfigFileMetadata configFile : internalConfigFiles) {

@ -18,14 +18,17 @@
package com.tencent.cloud.polaris.config.adapter; package com.tencent.cloud.polaris.config.adapter;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerContext; import com.tencent.cloud.polaris.config.logger.PolarisConfigLoggerContext;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener; import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeListener;
import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo;
import com.tencent.polaris.configuration.client.internal.CompositeConfigFile;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -79,10 +82,20 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
// register polaris config publish event // register polaris config publish event
for (PolarisPropertySource polarisPropertySource : polarisPropertySources) { for (PolarisPropertySource polarisPropertySource : polarisPropertySources) {
if (polarisPropertySource.getConfigKVFile() instanceof CompositeConfigFile) {
CompositeConfigFile configKVFile = (CompositeConfigFile) polarisPropertySource.getConfigKVFile();
for (ConfigKVFile cf : configKVFile.getConfigKVFiles()) {
PolarisPropertySource p = new PolarisPropertySource(cf.getNamespace(), cf.getFileGroup(), cf.getFileName(), cf, new HashMap<>());
registerPolarisConfigPublishChangeListener(p);
customRegisterPolarisConfigPublishChangeListener(p);
}
}
else {
registerPolarisConfigPublishChangeListener(polarisPropertySource); registerPolarisConfigPublishChangeListener(polarisPropertySource);
customRegisterPolarisConfigPublishChangeListener(polarisPropertySource); customRegisterPolarisConfigPublishChangeListener(polarisPropertySource);
} }
} }
}
private void customInitRegisterPolarisConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher) { private void customInitRegisterPolarisConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher) {
if (polarisConfigCustomExtensionLayer == null) { if (polarisConfigCustomExtensionLayer == null) {
@ -93,6 +106,7 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
} }
public void registerPolarisConfigPublishChangeListener(PolarisPropertySource polarisPropertySource) { public void registerPolarisConfigPublishChangeListener(PolarisPropertySource polarisPropertySource) {
LOGGER.info("{} will register polaris config publish listener", polarisPropertySource.getPropertySourceName());
polarisPropertySource.getConfigKVFile() polarisPropertySource.getConfigKVFile()
.addChangeListener((ConfigKVFileChangeListener) configKVFileChangeEvent -> { .addChangeListener((ConfigKVFileChangeListener) configKVFileChangeEvent -> {

@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.config.adapter;
import java.util.Map; import java.util.Map;
import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils;
import com.tencent.polaris.configuration.api.core.ConfigKVFile; import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
@ -40,7 +41,7 @@ public class PolarisPropertySource extends MapPropertySource {
private final ConfigKVFile configKVFile; private final ConfigKVFile configKVFile;
public PolarisPropertySource(String namespace, String group, String fileName, ConfigKVFile configKVFile, Map<String, Object> source) { public PolarisPropertySource(String namespace, String group, String fileName, ConfigKVFile configKVFile, Map<String, Object> source) {
super(namespace + "-" + group + "-" + fileName, source); super(PolarisPropertySourceUtils.generateName(namespace, group, fileName), source);
this.namespace = namespace; this.namespace = namespace;
this.group = group; this.group = group;
@ -64,7 +65,7 @@ public class PolarisPropertySource extends MapPropertySource {
return namespace + "-" + group + "-" + fileName; return namespace + "-" + group + "-" + fileName;
} }
ConfigKVFile getConfigKVFile() { public ConfigKVFile getConfigKVFile() {
return configKVFile; return configKVFile;
} }

@ -91,6 +91,11 @@ public class PolarisConfigProperties {
*/ */
private String localFileRootPath = "./polaris/backup/config"; private String localFileRootPath = "./polaris/backup/config";
/**
* If internal config file enabled.
*/
private boolean internalEnabled = true;
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
@ -179,6 +184,14 @@ public class PolarisConfigProperties {
this.localFileRootPath = localFileRootPath; this.localFileRootPath = localFileRootPath;
} }
public boolean isInternalEnabled() {
return internalEnabled;
}
public void setInternalEnabled(boolean internalEnabled) {
this.internalEnabled = internalEnabled;
}
@Override @Override
public String toString() { public String toString() {
return "PolarisConfigProperties{" + return "PolarisConfigProperties{" +
@ -193,6 +206,7 @@ public class PolarisConfigProperties {
", groups=" + groups + ", groups=" + groups +
", dataSource='" + dataSource + '\'' + ", dataSource='" + dataSource + '\'' +
", localFileRootPath='" + localFileRootPath + '\'' + ", localFileRootPath='" + localFileRootPath + '\'' +
", internalEnabled=" + internalEnabled +
'}'; '}';
} }
} }

@ -27,7 +27,6 @@ import com.tencent.tsf.consul.config.watch.TsfConsulConfigRefreshEventListener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -46,8 +45,7 @@ public class PolarisAdaptorTsfConfigAutoConfiguration {
{ {
System.setProperty("spring.cloud.polaris.config.refresh-type", "refresh_context"); System.setProperty("spring.cloud.polaris.config.refresh-type", "refresh_context");
LOGGER.info( LOGGER.info("PolarisAdaptorTsfConfigAutoConfiguration init set spring.cloud.polaris.config.refresh-type to refresh_context");
"[SCTT Config] PolarisAdaptorTsfConfigAutoConfiguration init set spring.cloud.polaris.config.refresh-type to refresh_context");
} }
@Bean @Bean
@ -65,7 +63,7 @@ public class PolarisAdaptorTsfConfigAutoConfiguration {
*/ */
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
@ConditionalOnExpression("${spring.cloud.consul.config.enabled:true} == false and ${tsf.config.instance.released-config.lookup.enabled:true} == true") @ConditionalOnProperty(name = "tsf.config.instance.released-config.lookup.enabled", matchIfMissing = true)
public PolarisAdaptorTsfConfigController polarisAdaptorTsfConfigController() { public PolarisAdaptorTsfConfigController polarisAdaptorTsfConfigController() {
return new PolarisAdaptorTsfConfigController(); return new PolarisAdaptorTsfConfigController();
} }

@ -22,7 +22,6 @@ import com.tencent.cloud.polaris.config.ConditionalOnPolarisConfigEnabled;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/** /**
* @author juanyinyang * @author juanyinyang
@ -32,7 +31,6 @@ import org.springframework.context.annotation.Import;
@ConditionalOnProperty("spring.cloud.polaris.enabled") @ConditionalOnProperty("spring.cloud.polaris.enabled")
@ConditionalOnTsfEnabled @ConditionalOnTsfEnabled
@ConditionalOnPolarisConfigEnabled @ConditionalOnPolarisConfigEnabled
@Import(PolarisAdaptorTsfConfigAutoConfiguration.class)
public class PolarisAdaptorTsfConfigBootstrapConfiguration { public class PolarisAdaptorTsfConfigBootstrapConfiguration {

@ -17,8 +17,8 @@
package com.tencent.cloud.polaris.config.tsf.adaptor; package com.tencent.cloud.polaris.config.tsf.adaptor;
import java.util.Arrays; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -30,14 +30,13 @@ import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyAutoRefresh
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import com.tencent.cloud.polaris.config.enums.ConfigFileFormat; import com.tencent.cloud.polaris.config.enums.ConfigFileFormat;
import com.tencent.cloud.polaris.config.tsf.cache.PolarisPropertyCache;
import com.tencent.cloud.polaris.config.tsf.encrypt.EncryptConfig; import com.tencent.cloud.polaris.config.tsf.encrypt.EncryptConfig;
import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils;
import com.tencent.polaris.configuration.api.core.ConfigFileGroup; import com.tencent.polaris.configuration.api.core.ConfigFileGroup;
import com.tencent.polaris.configuration.api.core.ConfigFileGroupChangeListener;
import com.tencent.polaris.configuration.api.core.ConfigFileGroupChangedEvent;
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata; import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile; import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.client.internal.CompositeConfigFile;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -100,16 +99,41 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
@Override @Override
public void initConfigFiles(Environment environment, CompositePropertySource compositePropertySource, public void initConfigFiles(Environment environment, CompositePropertySource compositePropertySource,
ConfigFileService configFileService) { ConfigFileService configFileService) {
String tsfApplicationId = environment.getProperty(TSF_APPLICATION_ID); String tsfApplicationId = "";
String tsfGroupId = environment.getProperty(TSF_GROUP_ID); String tsfGroupId = "";
String tsfNamespaceId = environment.getProperty(TSF_NAMESPACE_ID); String tsfNamespaceId = "";
String polarisAdaptorTsfConfigFormat = environment.getProperty(POLARIS_ADAPTOR_TSF_CONFIG_FORMAT); String polarisAdaptorTsfConfigFormat = "";
if (environment != null) {
tsfApplicationId = environment.getProperty(TSF_APPLICATION_ID);
tsfGroupId = environment.getProperty(TSF_GROUP_ID);
tsfNamespaceId = environment.getProperty(TSF_NAMESPACE_ID);
polarisAdaptorTsfConfigFormat = environment.getProperty(POLARIS_ADAPTOR_TSF_CONFIG_FORMAT);
}
else {
tsfApplicationId = System.getProperty(TSF_APPLICATION_ID);
if (StringUtils.isEmpty(tsfApplicationId)) {
tsfApplicationId = System.getenv(TSF_APPLICATION_ID);
}
tsfGroupId = System.getProperty(TSF_GROUP_ID);
if (StringUtils.isEmpty(tsfGroupId)) {
tsfGroupId = System.getenv(TSF_GROUP_ID);
}
tsfNamespaceId = System.getProperty(TSF_NAMESPACE_ID);
if (StringUtils.isEmpty(tsfNamespaceId)) {
tsfNamespaceId = System.getenv(TSF_NAMESPACE_ID);
}
polarisAdaptorTsfConfigFormat = System.getProperty(POLARIS_ADAPTOR_TSF_CONFIG_FORMAT);
if (StringUtils.isEmpty(polarisAdaptorTsfConfigFormat)) {
polarisAdaptorTsfConfigFormat = System.getenv(POLARIS_ADAPTOR_TSF_CONFIG_FORMAT);
}
}
LOGGER.info( LOGGER.info(
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer initConfigFiles start, tsfNamespaceId:{}, tsfApplicationId:{}, tsfGroupId:{}", "PolarisAdaptorTsfConfigExtensionLayer initConfigFiles start, tsfNamespaceId:{}, tsfApplicationId:{}, tsfGroupId:{}",
tsfNamespaceId, tsfApplicationId, tsfGroupId); tsfNamespaceId, tsfApplicationId, tsfGroupId);
loadAllPolarisConfigFile(compositePropertySource, configFileService, loadAllPolarisConfigFile(compositePropertySource, configFileService,
tsfNamespaceId, tsfApplicationId, tsfGroupId, polarisAdaptorTsfConfigFormat); tsfNamespaceId, tsfApplicationId, tsfGroupId, polarisAdaptorTsfConfigFormat);
LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer initConfigFiles end"); LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer initConfigFiles end");
} }
private void loadAllPolarisConfigFile(CompositePropertySource compositePropertySource, private void loadAllPolarisConfigFile(CompositePropertySource compositePropertySource,
@ -128,13 +152,21 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
compositePropertySource, configFileService, pubConfigGroup); compositePropertySource, configFileService, pubConfigGroup);
} }
private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName, private PolarisPropertySource loadPolarisPropertySource(String configFileGroupNamespace, String configFileGroupName,
String polarisAdaptorTsfConfigFormat, ConfigFileService configFileService) { List<ConfigFileMetadata> configFileMetadataList, String polarisAdaptorTsfConfigFormat,
ConfigFileService configFileService) {
List<ConfigKVFile> configKVFiles = new ArrayList<>();
for (ConfigFileMetadata configFileMetadata : configFileMetadataList) {
ConfigKVFile configKVFile; ConfigKVFile configKVFile;
String namespace = configFileMetadata.getNamespace();
String group = configFileMetadata.getFileGroup();
String fileName = configFileMetadata.getFileName();
if (StringUtils.isNotBlank(polarisAdaptorTsfConfigFormat)) { if (StringUtils.isNotBlank(polarisAdaptorTsfConfigFormat)) {
switch (polarisAdaptorTsfConfigFormat) { switch (polarisAdaptorTsfConfigFormat) {
case "properties": case "properties":
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName); configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
break;
case "yaml": case "yaml":
default: default:
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
@ -148,106 +180,114 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName); configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
} }
else { else {
LOGGER.warn("[SCTT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, LOGGER.warn("Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace,
group, fileName); group, fileName);
throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml"
+ " can be injected into the spring context"); + " can be injected into the spring context");
} }
configKVFiles.add(configKVFile);
}
CompositeConfigFile compositeConfigFile = new CompositeConfigFile(configKVFiles);
Map<String, Object> map = new ConcurrentHashMap<>(); Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : configKVFile.getPropertyNames()) { for (String key : compositeConfigFile.getPropertyNames()) {
String value = configKVFile.getProperty(key, null); String value = compositeConfigFile.getProperty(key, null);
if (EncryptConfig.needDecrypt(value)) { if (EncryptConfig.needDecrypt(value)) {
LOGGER.debug("[SCTT Config] Need Decrypt {}: {}", key, value); LOGGER.debug("Need Decrypt {}: {}", key, value);
value = EncryptConfig.getProvider() value = EncryptConfig.getProvider()
.decrypt(EncryptConfig.realContent(value), EncryptConfig.getPassword()); .decrypt(EncryptConfig.realContent(value), EncryptConfig.getPassword());
} }
map.put(key, value); map.put(key, value);
} }
return new PolarisPropertySource(namespace, group, fileName, configKVFile, map); if (LOGGER.isDebugEnabled()) {
LOGGER.debug("namespace='" + configFileGroupNamespace + '\''
+ ", group='" + configFileGroupName + '\'' + ", fileName='" + compositeConfigFile + '\''
+ ", map='" + map + '\'');
}
return new PolarisPropertySource(configFileGroupNamespace, configFileGroupName, "", compositeConfigFile, map);
} }
private void loadPolarisConfigFile(String namespace, String tsfApplicationId, String tsfGroupId, private void loadPolarisConfigFile(String namespace, String tsfApplicationId, String tsfGroupId,
String polarisAdaptorTsfConfigFormat, CompositePropertySource compositePropertySource, String polarisAdaptorTsfConfigFormat, CompositePropertySource compositePropertySource,
ConfigFileService configFileService, String configGroup) { ConfigFileService configFileService, String configGroup) {
LOGGER.debug( LOGGER.debug(
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile start, namespace:{}, group:{}", "PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile start, namespace:{}, group:{}",
namespace, configGroup); namespace, configGroup);
ConfigFileGroup configFileGroup = configFileService.getConfigFileGroup(namespace, configGroup); ConfigFileGroup configFileGroup = configFileService.getConfigFileGroup(namespace, configGroup);
if (configFileGroup == null) { if (configFileGroup == null) {
throw new IllegalStateException( throw new IllegalStateException(
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer configFileGroup is null"); "PolarisAdaptorTsfConfigExtensionLayer configFileGroup is null");
} }
List<ConfigFileMetadata> configFileMetadataList = configFileGroup.getConfigFileMetadataList(); List<ConfigFileMetadata> configFileMetadataList = configFileGroup.getConfigFileMetadataList();
if (!CollectionUtils.isEmpty(configFileMetadataList)) { if (!CollectionUtils.isEmpty(configFileMetadataList)) {
LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList:{}", LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList:{}",
configFileMetadataList); configFileMetadataList);
for (ConfigFileMetadata configFile : configFileMetadataList) { }
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFile.getNamespace(), else {
configFile.getFileGroup(), configFile.getFileName(), polarisAdaptorTsfConfigFormat, LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList empty. file = {}:{}",
configFileService); configFileGroup.getNamespace(), configFileGroup.getNamespace());
}
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(namespace, configGroup,
configFileMetadataList, polarisAdaptorTsfConfigFormat, configFileService);
compositePropertySource.addPropertySource(polarisPropertySource); compositePropertySource.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info( LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from config group:{}. file = {}",
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from config group:{}. file = {}", configGroup, polarisPropertySource.getConfigKVFile());
configGroup, configFile);
}
}
String namespaceConfigGroup = namespace + "-" + configGroup; String namespaceConfigGroup = namespace + "-" + configGroup;
// 用ConcurrentHashSet保证不重复添加ConfigFileGroupChangeListener // 用ConcurrentHashSet保证不重复添加ConfigFileGroupChangeListener
if (registedConfigFileGroupListenerSets.add(namespaceConfigGroup)) { if (registedConfigFileGroupListenerSets.add(namespaceConfigGroup)) {
LOGGER.info( LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer configFileGroup addChangeListener namespaceConfigGroup:{}",
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer configFileGroup addChangeListener namespaceConfigGroup:{}",
namespaceConfigGroup); namespaceConfigGroup);
configFileGroup.addChangeListener(new ConfigFileGroupChangeListener() { configFileGroup.addChangeListener(event -> {
@Override
public void onChange(ConfigFileGroupChangedEvent event) {
try { try {
LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive onChange event:{}", LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer receive onChange event:{}",
event); event);
List<ConfigFileMetadata> configFileMetadataList = event.getConfigFileMetadataList(); List<ConfigFileMetadata> oldConfigFileMetadataList = event.getOldConfigFileMetadataList();
if (CollectionUtils.isEmpty(configFileMetadataList)) { List<ConfigFileMetadata> newConfigFileMetadataList = event.getNewConfigFileMetadataList();
LOGGER.info(
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive configFileMetadataList is empty"); List<String> toUnregister = calculateUnregister(oldConfigFileMetadataList, newConfigFileMetadataList);
return; toUnregister.forEach(registedPolarisPropertySets::remove);
}
boolean needRefreshAll = false; if (CollectionUtils.isEmpty(newConfigFileMetadataList)) {
for (ConfigFileMetadata configFile : configFileMetadataList) { LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer receive configFileMetadataList is empty");
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( }
configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName(), else {
polarisAdaptorTsfConfigFormat, configFileService); PolarisPropertySource polarisPropertySource1 = loadPolarisPropertySource(
LOGGER.info( event.getConfigFileGroupMetadata().getNamespace(),
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from onChange event config group:{}. file = {}", event.getConfigFileGroupMetadata().getFileGroupName(),
configGroup, configFile); newConfigFileMetadataList, polarisAdaptorTsfConfigFormat, configFileService);
// 用ConcurrentHashSet保证不重复注册PolarisConfigPublishChangeListener // 用ConcurrentHashSet保证不重复注册PolarisConfigPublishChangeListener
if (executeRegisterPublishChangeListener(polarisPropertySource)) { CompositeConfigFile configKVFile = (CompositeConfigFile) polarisPropertySource1.getConfigKVFile();
polarisConfigPropertyAutoRefresher.registerPolarisConfigPublishChangeListener( for (ConfigKVFile cf : configKVFile.getConfigKVFiles()) {
polarisPropertySource); LOGGER.info(
needRefreshAll = true; "PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from onChange event config group:{}. file = {}",
configGroup, cf);
PolarisPropertySource p = new PolarisPropertySource(cf.getNamespace(), cf.getFileGroup(), cf.getFileName(), cf, new HashMap<>());
if (executeRegisterPublishChangeListener(p)) {
polarisConfigPropertyAutoRefresher.registerPolarisConfigPublishChangeListener(p);
} }
} }
if (needRefreshAll) {
LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer start refresh All Config");
polarisConfigPropertyAutoRefresher.refreshConfigurationProperties(null);
} }
LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer start refresh All Config");
polarisConfigPropertyAutoRefresher.refreshConfigurationProperties(null);
} }
catch (Exception e) { catch (Exception e) {
LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive onChange exception:", LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer receive onChange exception:",
e); e);
} }
}
}); });
} }
LOGGER.info( LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile end, namespace:{}, group:{}",
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile end, namespace:{}, group:{}",
namespace, configGroup); namespace, configGroup);
} }
@ -256,10 +296,7 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
*/ */
@Override @Override
public void executeAfterLocateConfigReturning(CompositePropertySource compositePropertySource) { public void executeAfterLocateConfigReturning(CompositePropertySource compositePropertySource) {
PolarisPropertyCache.getInstance().clear();
PolarisPropertyCache.getInstance().getCache()
.addAll(new HashSet<>(Arrays.asList(compositePropertySource.getPropertyNames())));
LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer executeAfterLocateConfigReturning finished");
} }
/** /**
@ -268,7 +305,7 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
@Override @Override
public void initRegisterConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher) { public void initRegisterConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher) {
LOGGER.info( LOGGER.info(
"[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer initRegisterConfig polarisConfigPropertyAutoRefresher:{}", "PolarisAdaptorTsfConfigExtensionLayer initRegisterConfig polarisConfigPropertyAutoRefresher:{}",
polarisConfigPropertyAutoRefresher.getClass()); polarisConfigPropertyAutoRefresher.getClass());
this.polarisConfigPropertyAutoRefresher = polarisConfigPropertyAutoRefresher; this.polarisConfigPropertyAutoRefresher = polarisConfigPropertyAutoRefresher;
} }
@ -281,9 +318,27 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto
boolean isRegisterSuccess = registedPolarisPropertySets.add(polarisPropertySource.getPropertySourceName()); boolean isRegisterSuccess = registedPolarisPropertySets.add(polarisPropertySource.getPropertySourceName());
if (isRegisterSuccess) { if (isRegisterSuccess) {
// 已防止重复注册,仅打印注册成功的即可 // 已防止重复注册,仅打印注册成功的即可
LOGGER.info("[SCTT Config] start to register configFile polarisConfigPublishChangeListener:{}", LOGGER.info("start to register configFile polarisConfigPublishChangeListener:{}",
polarisPropertySource); polarisPropertySource);
} }
return isRegisterSuccess; return isRegisterSuccess;
} }
private List<String> calculateUnregister(List<ConfigFileMetadata> oldConfigFileMetadataList, List<ConfigFileMetadata> newConfigFileMetadataList) {
List<String> newNameList = new ArrayList<>();
for (ConfigFileMetadata configFileMetadata : newConfigFileMetadataList) {
newNameList.add(PolarisPropertySourceUtils.generateName(configFileMetadata.getNamespace(),
configFileMetadata.getFileGroup(), configFileMetadata.getFileName()));
}
List<String> toUnregister = new ArrayList<>();
for (ConfigFileMetadata configFileMetadata : oldConfigFileMetadataList) {
String oldName = (PolarisPropertySourceUtils.generateName(configFileMetadata.getNamespace(),
configFileMetadata.getFileGroup(), configFileMetadata.getFileName()));
if (!newNameList.contains(oldName)) {
toUnregister.add(oldName);
}
}
return toUnregister;
}
} }

@ -18,11 +18,15 @@
package com.tencent.cloud.polaris.config.tsf.controller; package com.tencent.cloud.polaris.config.tsf.controller;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import com.tencent.cloud.polaris.config.tsf.cache.PolarisPropertyCache; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -44,7 +48,7 @@ public class PolarisAdaptorTsfConfigController {
Environment environment; Environment environment;
public PolarisAdaptorTsfConfigController() { public PolarisAdaptorTsfConfigController() {
LOG.info("[SCTT Config] init PolarisAdaptorTsfConfigController"); LOG.info("init PolarisAdaptorTsfConfigController");
} }
/** /**
@ -52,7 +56,13 @@ public class PolarisAdaptorTsfConfigController {
*/ */
@RequestMapping("/tsf/innerApi/config/findAllConfig") @RequestMapping("/tsf/innerApi/config/findAllConfig")
public Map<String, Object> findAllConfig() { public Map<String, Object> findAllConfig() {
Set<String> keys = PolarisPropertyCache.getInstance().getCache(); List<PolarisPropertySource> propertySourceList = PolarisPropertySourceManager.getAllPropertySources();
Set<String> keys = new HashSet<>();
for (PolarisPropertySource propertySource : propertySourceList) {
keys.addAll(Arrays.asList(propertySource.getPropertyNames()));
}
return keys.stream() return keys.stream()
.collect(HashMap::new, (map, key) -> map.put(key, environment.getProperty(key)), HashMap::putAll); .collect(HashMap::new, (map, key) -> map.put(key, environment.getProperty(key)), HashMap::putAll);
} }

@ -30,7 +30,7 @@ public class ConfigEncryptAESProvider extends ConfigEncryptProvider {
return EncryptAlgorithm.AES256.encrypt(content, password); return EncryptAlgorithm.AES256.encrypt(content, password);
} }
catch (Exception e) { catch (Exception e) {
log.error("[SCTT Config] Error on encrypting.", e); log.error("Error on encrypting.", e);
throw e; throw e;
} }
} }
@ -41,7 +41,7 @@ public class ConfigEncryptAESProvider extends ConfigEncryptProvider {
return EncryptAlgorithm.AES256.decrypt(encryptedContent, password); return EncryptAlgorithm.AES256.decrypt(encryptedContent, password);
} }
catch (Exception e) { catch (Exception e) {
log.error("[SCTT Config] Error on decrypting.", e); log.error("Error on decrypting.", e);
throw e; throw e;
} }
} }

@ -71,7 +71,7 @@ public class EncryptAlgorithm {
return new String(enryptedContent); return new String(enryptedContent);
} }
catch (Exception e) { catch (Exception e) {
throw new RuntimeException("[SCTT Encrypt] Failed encrypt.", e); throw new RuntimeException("Failed encrypt.", e);
} }
} }
@ -110,7 +110,7 @@ public class EncryptAlgorithm {
return new String(result); return new String(result);
} }
catch (Exception e) { catch (Exception e) {
throw new RuntimeException("[SCTT Encrypt] Failed decrypt.", e); throw new RuntimeException("Failed decrypt.", e);
} }
} }
} }
@ -139,7 +139,7 @@ public class EncryptAlgorithm {
private static final long serialVersionUID = -2843758461182470411L; private static final long serialVersionUID = -2843758461182470411L;
public PasswordNotFoundException() { public PasswordNotFoundException() {
super("[SCTT Encrypt] Password not found."); super("Password not found.");
} }
} }

@ -14,35 +14,21 @@
* CONDITIONS OF ANY KIND, either express or implied. See the License for the * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
*/ */
package com.tencent.cloud.polaris.config.tsf.cache;
import java.util.HashSet;
import java.util.Set;
package com.tencent.cloud.polaris.config.utils;
/** /**
* @author juanyinyang * Utils for PolarisPropertySource.
* @Date 202388 4:56:18 *
* @author Haotian Zhang
*/ */
public final class PolarisPropertyCache { public final class PolarisPropertySourceUtils {
private static final PolarisPropertyCache instance = new PolarisPropertyCache();
private final Set<String> cache = new HashSet<>();
private PolarisPropertyCache() { private PolarisPropertySourceUtils() {
}
public static PolarisPropertyCache getInstance() {
return instance;
}
public Set<String> getCache() {
return cache;
} }
public void clear() { public static String generateName(String namespace, String group, String fileName) {
cache.clear(); return namespace + "-" + group + "-" + fileName;
} }
} }

@ -92,6 +92,7 @@ public class PolarisConfigFileLocatorTest {
when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.isEnabled()).thenReturn(true);
when(polarisConfigProperties.getGroups()).thenReturn(null); when(polarisConfigProperties.getGroups()).thenReturn(null);
when(polarisConfigProperties.isInternalEnabled()).thenReturn(true);
when(environment.getActiveProfiles()).thenReturn(new String[] {}); when(environment.getActiveProfiles()).thenReturn(new String[] {});
PropertySource<?> propertySource = locator.locate(environment); PropertySource<?> propertySource = locator.locate(environment);
@ -140,6 +141,7 @@ public class PolarisConfigFileLocatorTest {
when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.isEnabled()).thenReturn(true);
when(polarisConfigProperties.getGroups()).thenReturn(null); when(polarisConfigProperties.getGroups()).thenReturn(null);
when(polarisConfigProperties.isInternalEnabled()).thenReturn(true);
when(environment.getActiveProfiles()).thenReturn(new String[] {"dev"}); when(environment.getActiveProfiles()).thenReturn(new String[] {"dev"});
PropertySource<?> propertySource = locator.locate(environment); PropertySource<?> propertySource = locator.locate(environment);
@ -178,6 +180,7 @@ public class PolarisConfigFileLocatorTest {
when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.isEnabled()).thenReturn(true);
when(polarisConfigProperties.getGroups()).thenReturn(customFiles); when(polarisConfigProperties.getGroups()).thenReturn(customFiles);
when(polarisConfigProperties.isInternalEnabled()).thenReturn(true);
when(environment.getActiveProfiles()).thenReturn(new String[] {}); when(environment.getActiveProfiles()).thenReturn(new String[] {});
// file1.properties // file1.properties

@ -56,7 +56,7 @@ public class TsfContractProperties implements ExtendedContractProperties {
@Override @Override
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return false;
} }
@Override @Override

@ -31,6 +31,14 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

@ -22,3 +22,9 @@ logging:
name: /tsf-demo-logs/${spring.application.name}/root.log name: /tsf-demo-logs/${spring.application.name}/root.log
level: level:
root: INFO root: INFO
management:
endpoints:
web:
exposure:
include:
- polaris-config

@ -23,6 +23,7 @@ import com.tencent.polaris.logging.PolarisLogging;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.context.logging.LoggingApplicationListener; import org.springframework.boot.context.logging.LoggingApplicationListener;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.GenericApplicationListener; import org.springframework.context.event.GenericApplicationListener;
import org.springframework.core.ResolvableType; import org.springframework.core.ResolvableType;
@ -45,7 +46,8 @@ public class PolarisLoggingApplicationListener implements GenericApplicationList
return false; return false;
} }
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(type) return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(type)
|| ApplicationFailedEvent.class.isAssignableFrom(type); || ApplicationFailedEvent.class.isAssignableFrom(type)
|| EnvironmentChangeEvent.class.isAssignableFrom(type);
} }
@Override @Override

Loading…
Cancel
Save