From b2d127d5f0e6561d2d3f6ed296196208f7c87f0c Mon Sep 17 00:00:00 2001 From: Fishtail <49390359+fuyuwei01@users.noreply.github.com> Date: Wed, 28 Aug 2024 11:04:13 +0800 Subject: [PATCH] feat:support consul config data. (#1417) Co-authored-by: Haotian Zhang --- CHANGELOG.md | 1 + ...larisConfigBootstrapAutoConfiguration.java | 2 + .../adapter/PolarisConfigFileLocator.java | 3 + .../PolarisConfigPropertyAutoRefresher.java | 18 +- .../config/adapter/PolarisPropertySource.java | 5 +- .../config/PolarisConfigProperties.java | 14 + .../configdata/PolarisConfigDataLoader.java | 24 +- ...arisAdaptorTsfConfigAutoConfiguration.java | 6 +- ...daptorTsfConfigBootstrapConfiguration.java | 2 - ...PolarisAdaptorTsfConfigExtensionLayer.java | 243 +++++++++++------- .../PolarisAdaptorTsfConfigController.java | 16 +- .../tsf/encrypt/ConfigEncryptAESProvider.java | 4 +- .../config/tsf/encrypt/EncryptAlgorithm.java | 6 +- .../PolarisPropertySourceUtils.java} | 30 +-- .../adapter/PolarisConfigFileLocatorTest.java | 3 + .../PolarisConfigDataLoaderTest.java | 5 +- .../contract/tsf/TsfContractProperties.java | 2 +- .../tsf-example/provider-demo/pom.xml | 8 + .../{application.yml => bootstrap.yml} | 8 +- .../PolarisLoggingApplicationListener.java | 4 +- .../env/TsfCoreEnvironmentPostProcessor.java | 5 +- 21 files changed, 264 insertions(+), 145 deletions(-) rename spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/{tsf/cache/PolarisPropertyCache.java => utils/PolarisPropertySourceUtils.java} (58%) rename spring-cloud-tencent-examples/tsf-example/provider-demo/src/main/resources/{application.yml => bootstrap.yml} (82%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 929623458..dfd87b7a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,3 +26,4 @@ - [fix:fix app starting failed when user using custom OpenAPI bean.](https://github.com/Tencent/spring-cloud-tencent/pull/1414) - [fix: move ConditionalOnTsfEnabled to spring-cloud-tencent-commons and fix PolarisInetUtilsAutoConfiguration.](https://github.com/Tencent/spring-cloud-tencent/pull/1415) - [fix: memory cost too many when using wildcard feign calls](https://github.com/Tencent/spring-cloud-tencent/pull/1416) +- [fix: memory cost too many when using wildcard feign calls](https://github.com/Tencent/spring-cloud-tencent/pull/1417) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java index 9fdc8fb22..b7be26460 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigBootstrapAutoConfiguration.java @@ -36,6 +36,7 @@ import org.springframework.cloud.context.properties.ConfigurationPropertiesRebin import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.context.annotation.Primary; import org.springframework.core.env.Environment; /** @@ -84,6 +85,7 @@ public class PolarisConfigBootstrapAutoConfiguration { } @Bean + @Primary @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) @ConditionalOnReflectRefreshType public ConfigurationPropertiesRebinder affectedConfigurationPropertiesRebinder( diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java index e50eb6bae..56a3d08ad 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocator.java @@ -117,6 +117,9 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { } private void initInternalConfigFiles(CompositePropertySource compositePropertySource) { + if (!polarisConfigProperties.isInternalEnabled()) { + return; + } List internalConfigFiles = getInternalConfigFiles(); for (ConfigFileMetadata configFile : internalConfigFiles) { diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java index 6bc5206d9..dc6989066 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigPropertyAutoRefresher.java @@ -18,14 +18,17 @@ package com.tencent.cloud.polaris.config.adapter; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; 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.ConfigPropertyChangeInfo; +import com.tencent.polaris.configuration.client.internal.CompositeConfigFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -79,8 +82,18 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL // register polaris config publish event for (PolarisPropertySource polarisPropertySource : polarisPropertySources) { - registerPolarisConfigPublishChangeListener(polarisPropertySource); - customRegisterPolarisConfigPublishChangeListener(polarisPropertySource); + 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); + customRegisterPolarisConfigPublishChangeListener(polarisPropertySource); + } } } @@ -93,6 +106,7 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL } public void registerPolarisConfigPublishChangeListener(PolarisPropertySource polarisPropertySource) { + LOGGER.info("{} will register polaris config publish listener", polarisPropertySource.getPropertySourceName()); polarisPropertySource.getConfigKVFile() .addChangeListener((ConfigKVFileChangeListener) configKVFileChangeEvent -> { diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySource.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySource.java index c1dcefa51..34da59376 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySource.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySource.java @@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.config.adapter; import java.util.Map; +import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils; import com.tencent.polaris.configuration.api.core.ConfigKVFile; import org.springframework.core.env.MapPropertySource; @@ -40,7 +41,7 @@ public class PolarisPropertySource extends MapPropertySource { private final ConfigKVFile configKVFile; public PolarisPropertySource(String namespace, String group, String fileName, ConfigKVFile configKVFile, Map source) { - super(namespace + "-" + group + "-" + fileName, source); + super(PolarisPropertySourceUtils.generateName(namespace, group, fileName), source); this.namespace = namespace; this.group = group; @@ -64,7 +65,7 @@ public class PolarisPropertySource extends MapPropertySource { return namespace + "-" + group + "-" + fileName; } - ConfigKVFile getConfigKVFile() { + public ConfigKVFile getConfigKVFile() { return configKVFile; } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java index 38687ddd8..4586134ee 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java @@ -91,6 +91,11 @@ public class PolarisConfigProperties { */ private String localFileRootPath = "./polaris/backup/config"; + /** + * If internal config file enabled. + */ + private boolean internalEnabled = true; + public boolean isEnabled() { return enabled; } @@ -179,6 +184,14 @@ public class PolarisConfigProperties { this.localFileRootPath = localFileRootPath; } + public boolean isInternalEnabled() { + return internalEnabled; + } + + public void setInternalEnabled(boolean internalEnabled) { + this.internalEnabled = internalEnabled; + } + @Override public String toString() { return "PolarisConfigProperties{" + @@ -193,6 +206,7 @@ public class PolarisConfigProperties { ", groups=" + groups + ", dataSource='" + dataSource + '\'' + ", localFileRootPath='" + localFileRootPath + '\'' + + ", internalEnabled=" + internalEnabled + '}'; } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java index eeefd2d04..58adcb0a8 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoader.java @@ -18,10 +18,13 @@ package com.tencent.cloud.polaris.config.configdata; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import com.tencent.cloud.polaris.config.adapter.PolarisConfigCustomExtensionLayer; import com.tencent.cloud.polaris.config.adapter.PolarisConfigFilePuller; +import com.tencent.cloud.polaris.config.adapter.PolarisServiceLoaderUtil; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; @@ -38,6 +41,7 @@ import org.springframework.boot.context.config.ConfigDataResourceNotFoundExcepti import org.springframework.boot.context.config.Profiles; import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.PropertySource; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; @@ -59,6 +63,7 @@ public class PolarisConfigDataLoader implements ConfigDataLoader> propertySourceList = new ArrayList<>(compositePropertySource.getPropertySources()); + Collections.reverse(propertySourceList); + return new ConfigData(propertySourceList, getOptions(resource)); } private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapContext, @@ -97,16 +104,21 @@ public class PolarisConfigDataLoader implements ConfigDataLoader configFileMetadataList, String polarisAdaptorTsfConfigFormat, + ConfigFileService configFileService) { + List configKVFiles = new ArrayList<>(); + + for (ConfigFileMetadata configFileMetadata : configFileMetadataList) { + ConfigKVFile configKVFile; + String namespace = configFileMetadata.getNamespace(); + String group = configFileMetadata.getFileGroup(); + String fileName = configFileMetadata.getFileName(); + if (StringUtils.isNotBlank(polarisAdaptorTsfConfigFormat)) { + switch (polarisAdaptorTsfConfigFormat) { + case "properties": + configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName); + break; + case "yaml": + default: + configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); + } + } + // unknown extension is resolved as yaml file + else if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) { configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); } - } - // unknown extension is resolved as yaml file - else if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) { - configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); - } - else if (ConfigFileFormat.isPropertyFile(fileName)) { - configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName); - } - else { - LOGGER.warn("[SCTT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, - group, fileName); + else if (ConfigFileFormat.isPropertyFile(fileName)) { + configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName); + } + else { + LOGGER.warn("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"); + 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 map = new ConcurrentHashMap<>(); - for (String key : configKVFile.getPropertyNames()) { - String value = configKVFile.getProperty(key, null); + for (String key : compositeConfigFile.getPropertyNames()) { + String value = compositeConfigFile.getProperty(key, null); if (EncryptConfig.needDecrypt(value)) { - LOGGER.debug("[SCTT Config] Need Decrypt {}: {}", key, value); + LOGGER.debug("Need Decrypt {}: {}", key, value); value = EncryptConfig.getProvider() .decrypt(EncryptConfig.realContent(value), EncryptConfig.getPassword()); } 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, String polarisAdaptorTsfConfigFormat, CompositePropertySource compositePropertySource, ConfigFileService configFileService, String configGroup) { LOGGER.debug( - "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile start, namespace:{}, group:{}", + "PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile start, namespace:{}, group:{}", namespace, configGroup); ConfigFileGroup configFileGroup = configFileService.getConfigFileGroup(namespace, configGroup); if (configFileGroup == null) { throw new IllegalStateException( - "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer configFileGroup is null"); + "PolarisAdaptorTsfConfigExtensionLayer configFileGroup is null"); } List configFileMetadataList = configFileGroup.getConfigFileMetadataList(); if (!CollectionUtils.isEmpty(configFileMetadataList)) { - LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList:{}", + LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList:{}", configFileMetadataList); - for (ConfigFileMetadata configFile : configFileMetadataList) { - PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFile.getNamespace(), - configFile.getFileGroup(), configFile.getFileName(), polarisAdaptorTsfConfigFormat, - configFileService); + } + else { + LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer getConfigFileMetadataList empty. file = {}:{}", + 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("PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from config group:{}. file = {}", + configGroup, polarisPropertySource.getConfigKVFile()); - LOGGER.info( - "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from config group:{}. file = {}", - configGroup, configFile); - } - } String namespaceConfigGroup = namespace + "-" + configGroup; // 用ConcurrentHashSet保证不重复添加ConfigFileGroupChangeListener if (registedConfigFileGroupListenerSets.add(namespaceConfigGroup)) { - LOGGER.info( - "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer configFileGroup addChangeListener namespaceConfigGroup:{}", + LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer configFileGroup addChangeListener namespaceConfigGroup:{}", namespaceConfigGroup); - configFileGroup.addChangeListener(new ConfigFileGroupChangeListener() { - - @Override - public void onChange(ConfigFileGroupChangedEvent event) { - try { - LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive onChange event:{}", - event); - List configFileMetadataList = event.getConfigFileMetadataList(); - if (CollectionUtils.isEmpty(configFileMetadataList)) { - LOGGER.info( - "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive configFileMetadataList is empty"); - return; - } - boolean needRefreshAll = false; - for (ConfigFileMetadata configFile : configFileMetadataList) { - PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( - configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName(), - polarisAdaptorTsfConfigFormat, configFileService); + configFileGroup.addChangeListener(event -> { + try { + LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer receive onChange event:{}", + event); + List oldConfigFileMetadataList = event.getOldConfigFileMetadataList(); + List newConfigFileMetadataList = event.getNewConfigFileMetadataList(); + + List toUnregister = calculateUnregister(oldConfigFileMetadataList, newConfigFileMetadataList); + toUnregister.forEach(registedPolarisPropertySets::remove); + + if (CollectionUtils.isEmpty(newConfigFileMetadataList)) { + LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer receive configFileMetadataList is empty"); + } + else { + PolarisPropertySource polarisPropertySource1 = loadPolarisPropertySource( + event.getConfigFileGroupMetadata().getNamespace(), + event.getConfigFileGroupMetadata().getFileGroupName(), + newConfigFileMetadataList, polarisAdaptorTsfConfigFormat, configFileService); + // 用ConcurrentHashSet保证不重复注册PolarisConfigPublishChangeListener + CompositeConfigFile configKVFile = (CompositeConfigFile) polarisPropertySource1.getConfigKVFile(); + for (ConfigKVFile cf : configKVFile.getConfigKVFiles()) { LOGGER.info( - "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer Load and inject polaris config file from onChange event config group:{}. file = {}", - configGroup, configFile); - // 用ConcurrentHashSet保证不重复注册PolarisConfigPublishChangeListener - if (executeRegisterPublishChangeListener(polarisPropertySource)) { - polarisConfigPropertyAutoRefresher.registerPolarisConfigPublishChangeListener( - polarisPropertySource); - 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); - } - } - catch (Exception e) { - LOGGER.info("[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer receive onChange exception:", - e); } + LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer start refresh All Config"); + polarisConfigPropertyAutoRefresher.refreshConfigurationProperties(null); + } + catch (Exception e) { + LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer receive onChange exception:", + e); } }); } - LOGGER.info( - "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile end, namespace:{}, group:{}", + LOGGER.info("PolarisAdaptorTsfConfigExtensionLayer loadPolarisConfigFile end, namespace:{}, group:{}", namespace, configGroup); } @@ -258,10 +298,7 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto */ @Override 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"); + } /** @@ -270,7 +307,7 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto @Override public void initRegisterConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher) { LOGGER.info( - "[SCTT Config] PolarisAdaptorTsfConfigExtensionLayer initRegisterConfig polarisConfigPropertyAutoRefresher:{}", + "PolarisAdaptorTsfConfigExtensionLayer initRegisterConfig polarisConfigPropertyAutoRefresher:{}", polarisConfigPropertyAutoRefresher.getClass()); this.polarisConfigPropertyAutoRefresher = polarisConfigPropertyAutoRefresher; } @@ -283,9 +320,27 @@ public class PolarisAdaptorTsfConfigExtensionLayer implements PolarisConfigCusto boolean isRegisterSuccess = registedPolarisPropertySets.add(polarisPropertySource.getPropertySourceName()); if (isRegisterSuccess) { // 已防止重复注册,仅打印注册成功的即可 - LOGGER.info("[SCTT Config] start to register configFile polarisConfigPublishChangeListener:{}", + LOGGER.info("start to register configFile polarisConfigPublishChangeListener:{}", polarisPropertySource); } return isRegisterSuccess; } + + private List calculateUnregister(List oldConfigFileMetadataList, List newConfigFileMetadataList) { + List newNameList = new ArrayList<>(); + for (ConfigFileMetadata configFileMetadata : newConfigFileMetadataList) { + newNameList.add(PolarisPropertySourceUtils.generateName(configFileMetadata.getNamespace(), + configFileMetadata.getFileGroup(), configFileMetadata.getFileName())); + } + + List 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; + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/controller/PolarisAdaptorTsfConfigController.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/controller/PolarisAdaptorTsfConfigController.java index b8719b0fb..bf9a044a2 100755 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/controller/PolarisAdaptorTsfConfigController.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/controller/PolarisAdaptorTsfConfigController.java @@ -18,11 +18,15 @@ package com.tencent.cloud.polaris.config.tsf.controller; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; 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.LoggerFactory; @@ -44,7 +48,7 @@ public class PolarisAdaptorTsfConfigController { Environment environment; public PolarisAdaptorTsfConfigController() { - LOG.info("[SCTT Config] init PolarisAdaptorTsfConfigController"); + LOG.info("init PolarisAdaptorTsfConfigController"); } /** @@ -52,7 +56,13 @@ public class PolarisAdaptorTsfConfigController { */ @RequestMapping("/tsf/innerApi/config/findAllConfig") public Map findAllConfig() { - Set keys = PolarisPropertyCache.getInstance().getCache(); + List propertySourceList = PolarisPropertySourceManager.getAllPropertySources(); + + Set keys = new HashSet<>(); + for (PolarisPropertySource propertySource : propertySourceList) { + keys.addAll(Arrays.asList(propertySource.getPropertyNames())); + } + return keys.stream() .collect(HashMap::new, (map, key) -> map.put(key, environment.getProperty(key)), HashMap::putAll); } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/encrypt/ConfigEncryptAESProvider.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/encrypt/ConfigEncryptAESProvider.java index 51f99e8bf..d6084889e 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/encrypt/ConfigEncryptAESProvider.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/encrypt/ConfigEncryptAESProvider.java @@ -30,7 +30,7 @@ public class ConfigEncryptAESProvider extends ConfigEncryptProvider { return EncryptAlgorithm.AES256.encrypt(content, password); } catch (Exception e) { - log.error("[SCTT Config] Error on encrypting.", e); + log.error("Error on encrypting.", e); throw e; } } @@ -41,7 +41,7 @@ public class ConfigEncryptAESProvider extends ConfigEncryptProvider { return EncryptAlgorithm.AES256.decrypt(encryptedContent, password); } catch (Exception e) { - log.error("[SCTT Config] Error on decrypting.", e); + log.error("Error on decrypting.", e); throw e; } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/encrypt/EncryptAlgorithm.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/encrypt/EncryptAlgorithm.java index 9a7959930..1ce56b75a 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/encrypt/EncryptAlgorithm.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/encrypt/EncryptAlgorithm.java @@ -71,7 +71,7 @@ public class EncryptAlgorithm { return new String(enryptedContent); } 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); } 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; public PasswordNotFoundException() { - super("[SCTT Encrypt] Password not found."); + super("Password not found."); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/cache/PolarisPropertyCache.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/utils/PolarisPropertySourceUtils.java similarity index 58% rename from spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/cache/PolarisPropertyCache.java rename to spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/utils/PolarisPropertySourceUtils.java index 4e80d0386..6eea01f4b 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/cache/PolarisPropertyCache.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/utils/PolarisPropertySourceUtils.java @@ -14,35 +14,21 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the * 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 - * @Date 2023年8月8日 下午4:56:18 + * Utils for PolarisPropertySource. + * + * @author Haotian Zhang */ -public final class PolarisPropertyCache { - - private static final PolarisPropertyCache instance = new PolarisPropertyCache(); - - private final Set cache = new HashSet<>(); +public final class PolarisPropertySourceUtils { - private PolarisPropertyCache() { - - } - - public static PolarisPropertyCache getInstance() { - return instance; - } + private PolarisPropertySourceUtils() { - public Set getCache() { - return cache; } - public void clear() { - cache.clear(); + public static String generateName(String namespace, String group, String fileName) { + return namespace + "-" + group + "-" + fileName; } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java index 9a397ac49..42dbd028f 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java @@ -91,6 +91,7 @@ public class PolarisConfigFileLocatorTest { when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.getGroups()).thenReturn(null); + when(polarisConfigProperties.isInternalEnabled()).thenReturn(true); when(environment.getActiveProfiles()).thenReturn(new String[] {}); PropertySource propertySource = locator.locate(environment); @@ -139,6 +140,7 @@ public class PolarisConfigFileLocatorTest { when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.getGroups()).thenReturn(null); + when(polarisConfigProperties.isInternalEnabled()).thenReturn(true); when(environment.getActiveProfiles()).thenReturn(new String[] {"dev"}); PropertySource propertySource = locator.locate(environment); @@ -177,6 +179,7 @@ public class PolarisConfigFileLocatorTest { when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.getGroups()).thenReturn(customFiles); + when(polarisConfigProperties.isInternalEnabled()).thenReturn(true); when(environment.getActiveProfiles()).thenReturn(new String[] {}); // file1.properties diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoaderTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoaderTest.java index 86cacfd2c..c283f3ca7 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoaderTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLoaderTest.java @@ -109,6 +109,7 @@ public class PolarisConfigDataLoaderTest { when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisConfigProperties.getGroups()).thenReturn(null); + when(polarisConfigProperties.isInternalEnabled()).thenReturn(true); when(profiles.getActive()).thenReturn(Lists.newArrayList()); PolarisConfigDataLoader polarisConfigDataLoader = new PolarisConfigDataLoader(new DeferredLogs()); @@ -187,6 +188,7 @@ public class PolarisConfigDataLoaderTest { when(polarisContextProperties.getService()).thenReturn(testServiceName); when(polarisConfigProperties.getGroups()).thenReturn(null); + when(polarisConfigProperties.isInternalEnabled()).thenReturn(true); PolarisConfigDataLoader polarisConfigDataLoader = new PolarisConfigDataLoader(new DeferredLogs()); if (INTERNAL_CONFIG_FILES_LOADED.get()) { @@ -208,7 +210,7 @@ public class PolarisConfigDataLoaderTest { List> propertySources = configData.getPropertySources(); CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); - propertySources.forEach(compositePropertySource::addPropertySource); + propertySources.forEach(compositePropertySource::addFirstPropertySource); assertThat(compositePropertySource.getProperty("k1")).isEqualTo("v11"); assertThat(compositePropertySource.getProperty("k2")).isEqualTo("v2"); @@ -242,6 +244,7 @@ public class PolarisConfigDataLoaderTest { when(polarisConfigDataResource.getGroupName()).thenReturn(customGroup); when(polarisConfigProperties.getGroups()).thenReturn(null); + when(polarisConfigProperties.isInternalEnabled()).thenReturn(true); when(profiles.getActive()).thenReturn(Lists.newArrayList()); // file1.properties diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/tsf/TsfContractProperties.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/tsf/TsfContractProperties.java index 64cd71051..997bfbc1d 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/tsf/TsfContractProperties.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/tsf/TsfContractProperties.java @@ -56,7 +56,7 @@ public class TsfContractProperties implements ExtendedContractProperties { @Override public boolean isEnabled() { - return enabled; + return false; } @Override diff --git a/spring-cloud-tencent-examples/tsf-example/provider-demo/pom.xml b/spring-cloud-tencent-examples/tsf-example/provider-demo/pom.xml index f5ed608fc..348438516 100644 --- a/spring-cloud-tencent-examples/tsf-example/provider-demo/pom.xml +++ b/spring-cloud-tencent-examples/tsf-example/provider-demo/pom.xml @@ -31,6 +31,14 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.cloud + spring-cloud-starter-bootstrap + diff --git a/spring-cloud-tencent-examples/tsf-example/provider-demo/src/main/resources/application.yml b/spring-cloud-tencent-examples/tsf-example/provider-demo/src/main/resources/bootstrap.yml similarity index 82% rename from spring-cloud-tencent-examples/tsf-example/provider-demo/src/main/resources/application.yml rename to spring-cloud-tencent-examples/tsf-example/provider-demo/src/main/resources/bootstrap.yml index d4ae0231b..5e43105a2 100644 --- a/spring-cloud-tencent-examples/tsf-example/provider-demo/src/main/resources/application.yml +++ b/spring-cloud-tencent-examples/tsf-example/provider-demo/src/main/resources/bootstrap.yml @@ -3,8 +3,6 @@ server: spring: application: name: provider-demo - config: - import: optional:polaris cloud: polaris: namespace: default @@ -24,3 +22,9 @@ logging: name: /tsf-demo-logs/${spring.application.name}/root.log level: root: INFO +management: + endpoints: + web: + exposure: + include: + - polaris-config diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/logging/PolarisLoggingApplicationListener.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/logging/PolarisLoggingApplicationListener.java index d01ef8a7f..67ad30a8b 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/logging/PolarisLoggingApplicationListener.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/logging/PolarisLoggingApplicationListener.java @@ -23,6 +23,7 @@ import com.tencent.polaris.logging.PolarisLogging; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.logging.LoggingApplicationListener; +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.context.ApplicationEvent; import org.springframework.context.event.GenericApplicationListener; import org.springframework.core.ResolvableType; @@ -45,7 +46,8 @@ public class PolarisLoggingApplicationListener implements GenericApplicationList return false; } return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(type) - || ApplicationFailedEvent.class.isAssignableFrom(type); + || ApplicationFailedEvent.class.isAssignableFrom(type) + || EnvironmentChangeEvent.class.isAssignableFrom(type); } @Override diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java index 1ebd2d2bc..1c2c258b7 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java @@ -106,14 +106,15 @@ public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostPro } else { defaultProperties.put("spring.cloud.polaris.config.enabled", "true"); + defaultProperties.put("spring.cloud.polaris.config.internal-enabled", "false"); defaultProperties.put("spring.cloud.polaris.config.data-source", "consul"); defaultProperties.put("spring.cloud.polaris.config.address", "http://" + tsfConsulIp + ":" + tsfConsulPort); defaultProperties.put("spring.cloud.polaris.config.port", tsfConsulPort); defaultProperties.put("spring.cloud.polaris.config.token", tsfConsulToken); defaultProperties.put("spring.cloud.polaris.config.groups[0].namespace", "config"); defaultProperties.put("spring.cloud.polaris.config.groups[0].name", "application"); - defaultProperties.put("spring.cloud.polaris.config.groups[0].files[0]", tsfNamespaceId + "/"); - defaultProperties.put("spring.cloud.polaris.config.groups[0].files[1]", tsfApplicationId + "/" + tsfGroupId + "/"); + defaultProperties.put("spring.cloud.polaris.config.groups[0].files[0]", tsfApplicationId + "/" + tsfGroupId + "/"); + defaultProperties.put("spring.cloud.polaris.config.groups[0].files[1]", tsfNamespaceId + "/"); } // tse_polaris_ip