From db1885dbe9454bcc0c19852af1a08f6ea13b3f62 Mon Sep 17 00:00:00 2001 From: Haotian Zhang Date: Thu, 6 Nov 2025 10:20:56 +0800 Subject: [PATCH] refactor:optimize config locate. (#1741) Signed-off-by: Haotian Zhang <928016560@qq.com> --- CHANGELOG.md | 1 + .../adapter/PolarisConfigFileLocator.java | 252 ++---------------- .../adapter/PolarisConfigFilePuller.java | 129 +++++++-- .../configdata/PolarisConfigDataLoader.java | 97 +++++-- .../adapter/PolarisConfigFileLocatorTest.java | 52 ---- .../adapter/PolarisConfigFilePullerTest.java | 46 ++++ .../PolarisConfigDataLoaderTest.java | 9 - 7 files changed, 248 insertions(+), 338 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b1759581..7c1cb0860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,3 +38,4 @@ - [fix: get source service name from upstream application meta in getServerPreSpanAttributes & set custom tag in current server span in tsf.](https://github.com/Tencent/spring-cloud-tencent/pull/1767) - [feat:support body store switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1771) - [refactor:optimize auto configuration.](https://github.com/Tencent/spring-cloud-tencent/pull/1739) +- [refactor:optimize config locate.](https://github.com/Tencent/spring-cloud-tencent/pull/1741) 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 9db25194b..00ab88629 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 @@ -17,25 +17,14 @@ package com.tencent.cloud.polaris.config.adapter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedList; import java.util.List; -import java.util.Map; -import com.tencent.cloud.common.util.EnvironmentUtils; -import com.tencent.cloud.plugin.tsf.tls.utils.SyncUtils; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.context.config.PolarisContextProperties; -import com.tencent.cloud.polaris.context.config.extend.tsf.TsfTlsProperties; -import com.tencent.polaris.api.utils.ClassUtils; import com.tencent.polaris.api.utils.CollectionUtils; import com.tencent.polaris.api.utils.StringUtils; -import com.tencent.polaris.configuration.api.core.ConfigFileMetadata; import com.tencent.polaris.configuration.api.core.ConfigFileService; -import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,12 +32,8 @@ import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.Environment; -import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; -import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadGroupPolarisPropertySource; -import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadPolarisPropertySource; - /** * Spring cloud reserved core configuration loading SPI. *

@@ -66,6 +51,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { private final PolarisConfigProperties polarisConfigProperties; private final PolarisContextProperties polarisContextProperties; private final ConfigFileService configFileService; + private final PolarisConfigFilePuller puller; private final Environment environment; // this class provides customized logic for some customers to configure special business group files private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer(); @@ -75,6 +61,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { this.polarisConfigProperties = polarisConfigProperties; this.polarisContextProperties = polarisContextProperties; this.configFileService = configFileService; + this.puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService); this.environment = environment; } @@ -100,10 +87,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { // load spring boot default config files initInternalConfigFiles(compositePropertySource); // load custom config files - List configFileGroups = polarisConfigProperties.getGroups(); - if (CollectionUtils.isNotEmpty(configFileGroups)) { - initCustomPolarisConfigFiles(compositePropertySource, configFileGroups); - } + initCustomPolarisConfigFiles(compositePropertySource); // load tsf default config group initTsfConfigGroups(compositePropertySource); // load tsf tls properties if need. @@ -137,226 +121,46 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { if (!polarisConfigProperties.isInternalEnabled()) { return; } - List internalConfigFiles = getInternalConfigFiles(); - - for (ConfigFileMetadata configFile : internalConfigFiles) { - if (StringUtils.isEmpty(configFile.getFileGroup())) { - continue; - } - PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService, configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); - - compositePropertySource.addPropertySource(polarisPropertySource); - - PolarisPropertySourceManager.addPropertySource(polarisPropertySource); - - LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile); - } - } - - private List getInternalConfigFiles() { - String namespace = polarisContextProperties.getNamespace(); - String serviceName = polarisContextProperties.getService(); - if (StringUtils.isBlank(serviceName)) { - serviceName = environment.getProperty("spring.application.name"); - } - - List internalConfigFiles = new LinkedList<>(); - // priority: application-${profile} > application > boostrap-${profile} > boostrap String[] activeProfiles = environment.getActiveProfiles(); String[] defaultProfiles = environment.getDefaultProfiles(); - List profileList = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(activeProfiles)) { - profileList.addAll(Arrays.asList(activeProfiles)); - } - else if (CollectionUtils.isNotEmpty(defaultProfiles)) { - profileList.addAll(Arrays.asList(defaultProfiles)); + String serviceName = polarisContextProperties.getService(); + if (StringUtils.isBlank(serviceName)) { + serviceName = environment.getProperty("spring.application.name"); } - // build application config files - buildInternalApplicationConfigFiles(internalConfigFiles, namespace, serviceName, profileList); - // build bootstrap config files - buildInternalBootstrapConfigFiles(internalConfigFiles, namespace, serviceName, profileList); - - return internalConfigFiles; + this.puller.initInternalConfigFiles(compositePropertySource, activeProfiles, defaultProfiles, serviceName); } - private void buildInternalApplicationConfigFiles(List internalConfigFiles, String namespace, String serviceName, List profileList) { - for (String profile : profileList) { - if (StringUtils.isBlank(profile)) { - continue; - } - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".yml")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".yaml")); + private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource) { + List configFileGroups = polarisConfigProperties.getGroups(); + if (CollectionUtils.isNotEmpty(configFileGroups)) { + this.puller.initCustomPolarisConfigFiles(compositePropertySource, configFileGroups); } - // build default config properties files. - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yaml")); } - private void buildInternalBootstrapConfigFiles(List internalConfigFiles, String namespace, String serviceName, List profileList) { - for (String profile : profileList) { - if (StringUtils.isBlank(profile)) { - continue; - } - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".yml")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".yaml")); - } - // build default config properties files. - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yaml")); + private void initTsfConfigGroups(CompositePropertySource compositePropertySource) { + this.puller.initTsfConfigGroups(compositePropertySource); } - void initTsfConfigGroups(CompositePropertySource compositePropertySource) { - String tsfId = environment.getProperty("tsf_id"); - String tsfNamespaceName = environment.getProperty("tsf_namespace_name"); - String tsfGroupName = environment.getProperty("tsf_group_name"); - - if (StringUtils.isEmpty(tsfNamespaceName) || StringUtils.isEmpty(tsfGroupName)) { - return; + private void initTsfTlsPropertySource(CompositePropertySource compositePropertySource) { + // get service name + Object serviceName = compositePropertySource.getProperty("spring.cloud.polaris.service"); + if (serviceName == null) { + serviceName = compositePropertySource.getProperty("spring.cloud.polaris.discovery.service"); } - String namespace = polarisContextProperties.getNamespace(); - List tsfConfigGroups = new ArrayList<>(); - tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group"); - tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfNamespaceName + ".global_config_group"); - - if (EnvironmentUtils.isGateway()) { - tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".gateway_config_group"); + if (serviceName == null) { + serviceName = compositePropertySource.getProperty("spring.application.name"); } - for (String tsfConfigGroup : tsfConfigGroups) { - PolarisPropertySource polarisPropertySource = loadGroupPolarisPropertySource(configFileService, namespace, tsfConfigGroup); - if (polarisPropertySource == null) { - // not register to polaris - continue; - } - compositePropertySource.addPropertySource(polarisPropertySource); - PolarisPropertySourceManager.addPropertySource(polarisPropertySource); + if (serviceName == null) { + serviceName = environment.getProperty("spring.cloud.polaris.service"); } - } - - void initTsfTlsPropertySource(CompositePropertySource compositePropertySource) { - String address = System.getProperty("MESH_CITADEL_ADDR", System.getenv("MESH_CITADEL_ADDR")); - if (StringUtils.isNotBlank(address) - && (StringUtils.equals("tsf", environment.getProperty("server.ssl.bundle")) - || "tsf".equals(compositePropertySource.getProperty(("server.ssl.bundle")))) - && ClassUtils.isClassPresent("com.tencent.cloud.plugin.tsf.tls.utils.SyncUtils") - && !SyncUtils.isInitialized()) { - // get common name - Object commonName = compositePropertySource.getProperty("spring.cloud.polaris.service"); - if (commonName == null) { - commonName = compositePropertySource.getProperty("spring.cloud.polaris.discovery.service"); - } - if (commonName == null) { - commonName = compositePropertySource.getProperty("spring.application.name"); - } - if (commonName == null) { - commonName = environment.getProperty("spring.cloud.polaris.service"); - } - if (commonName == null) { - commonName = environment.getProperty("spring.cloud.polaris.discovery.service"); - } - if (commonName == null) { - commonName = environment.getProperty("spring.application.name"); - } - // get certPath - String certPath = System.getProperty("MESH_CITADEL_CERT", System.getenv("MESH_CITADEL_CERT")); - // get token - String token = System.getProperty("tsf_token", System.getenv("tsf_token")); - // get validityDuration - Object validityDuration = compositePropertySource.getProperty("spring.cloud.polaris.tls.validityDuration"); - if (validityDuration == null) { - validityDuration = environment.getProperty("spring.cloud.polaris.tls.validityDuration", Long.class, TsfTlsProperties.DEFAULT_VALIDITY_DURATION); - } - if (validityDuration instanceof String) { - validityDuration = Long.valueOf((String) validityDuration); - } - // get refreshBefore - Object refreshBefore = compositePropertySource.getProperty("spring.cloud.polaris.tls.refreshBefore"); - if (refreshBefore == null) { - refreshBefore = environment.getProperty("spring.cloud.polaris.tls.refreshBefore", Long.class, TsfTlsProperties.DEFAULT_REFRESH_BEFORE); - } - if (refreshBefore instanceof String) { - refreshBefore = Long.valueOf((String) refreshBefore); - } - // get watchInterval - Object watchInterval = compositePropertySource.getProperty("spring.cloud.polaris.tls.watchInterval"); - if (watchInterval == null) { - watchInterval = environment.getProperty("spring.cloud.polaris.tls.watchInterval", Long.class, TsfTlsProperties.DEFAULT_WATCH_INTERVAL); - } - if (watchInterval instanceof String) { - watchInterval = Long.valueOf((String) watchInterval); - } - SyncUtils.init((String) commonName, address, certPath, token, (Long) validityDuration, (Long) refreshBefore, (Long) watchInterval); - if (SyncUtils.isVerified()) { - Map tlsEnvProperties = new HashMap<>(); - // set ssl - Object clientAuth = compositePropertySource.getProperty("server.ssl.client-auth"); - if (clientAuth == null) { - clientAuth = environment.getProperty("server.ssl.client-auth", "want"); - } - tlsEnvProperties.put("server.ssl.client-auth", clientAuth); - Object protocol = compositePropertySource.getProperty("spring.cloud.polaris.discovery.protocol"); - if (protocol == null) { - protocol = environment.getProperty("spring.cloud.polaris.discovery.protocol", "https"); - } - tlsEnvProperties.put("spring.cloud.polaris.discovery.protocol", protocol); - tlsEnvProperties.put("tsf.discovery.scheme", protocol); - // set tsf spring ssl bundle - tlsEnvProperties.put("spring.ssl.bundle.pem.tsf.reload-on-update", "true"); - if (StringUtils.isNotBlank(SyncUtils.getPemKeyStoreCertPath()) && StringUtils.isNotBlank(SyncUtils.getPemKeyStoreKeyPath())) { - tlsEnvProperties.put("spring.ssl.bundle.pem.tsf.keystore.certificate", SyncUtils.getPemKeyStoreCertPath()); - tlsEnvProperties.put("spring.ssl.bundle.pem.tsf.keystore.private-key", SyncUtils.getPemKeyStoreKeyPath()); - } - if (StringUtils.isNotBlank(SyncUtils.getPemTrustStoreCertPath())) { - tlsEnvProperties.put("spring.ssl.bundle.pem.tsf.truststore.certificate", SyncUtils.getPemTrustStoreCertPath()); - } - - // process environment - MapPropertySource propertySource = new MapPropertySource("tsf-tls-config", tlsEnvProperties); - compositePropertySource.addPropertySource(propertySource); - } + if (serviceName == null) { + serviceName = environment.getProperty("spring.cloud.polaris.discovery.service"); } - } - - private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, List configFileGroups) { - String namespace = polarisContextProperties.getNamespace(); - - for (ConfigFileGroup configFileGroup : configFileGroups) { - String groupNamespace = configFileGroup.getNamespace(); - if (StringUtils.isBlank(groupNamespace)) { - groupNamespace = namespace; - } - - String group = configFileGroup.getName(); - if (StringUtils.isBlank(group)) { - continue; - } - - List files = configFileGroup.getFiles(); - - if (CollectionUtils.isEmpty(files)) { - 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); - - compositePropertySource.addPropertySource(polarisPropertySource); - - PolarisPropertySourceManager.addPropertySource(polarisPropertySource); - - LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", groupNamespace, group, fileName); - } - } + if (serviceName == null) { + serviceName = environment.getProperty("spring.application.name"); } + String commonName = (String) serviceName; + this.puller.initTsfTlsPropertySource(compositePropertySource, null, environment, commonName); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java index 7c6f482a1..b8232f565 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java @@ -36,10 +36,13 @@ import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.configuration.api.core.ConfigFileMetadata; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata; +import org.apache.commons.logging.Log; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadGroupPolarisPropertySource; @@ -52,7 +55,9 @@ import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils. */ public final class PolarisConfigFilePuller { - private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigFileLocator.class); + private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigFilePuller.class); + + private static Log log; private PolarisContextProperties polarisContextProperties; @@ -61,6 +66,10 @@ public final class PolarisConfigFilePuller { private PolarisConfigFilePuller() { } + public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService) { + return get(polarisContextProperties, configFileService, null); + } + /** * Factory method to create PolarisConfigFilePuller for * {@link PolarisConfigDataLoader},{@link PolarisConfigFileLocator}. @@ -69,13 +78,26 @@ public final class PolarisConfigFilePuller { * @param configFileService configFileService * @return PolarisConfigFilePuller instance */ - public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService) { + public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, + ConfigFileService configFileService, DeferredLogFactory logFactory) { PolarisConfigFilePuller puller = new PolarisConfigFilePuller(); puller.polarisContextProperties = polarisContextProperties; puller.configFileService = configFileService; + if (logFactory != null) { + log = logFactory.getLog(PolarisConfigFilePuller.class); + } return puller; } + private static void logInfo(String msg) { + if (log != null) { + log.info(msg); + } + else { + LOGGER.info(msg); + } + } + /** * InitInternalConfigFiles for {@link PolarisConfigDataLoader}. * @@ -86,13 +108,14 @@ public final class PolarisConfigFilePuller { */ public void initInternalConfigFiles(CompositePropertySource compositePropertySource, String[] activeProfiles, String[] defaultProfiles, String serviceName) { + logInfo("[SCT Config] Loading polaris internal config files"); List internalConfigFiles = getInternalConfigFiles(activeProfiles, defaultProfiles, serviceName); for (ConfigFileMetadata configFile : internalConfigFiles) { PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService, configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); compositePropertySource.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource); - LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile); + logInfo(String.format("[SCT Config] Load and inject polaris internal config file success. file = %s", configFile)); } } @@ -104,6 +127,7 @@ public final class PolarisConfigFilePuller { */ public void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, List configFileGroups) { + logInfo("[SCT Config] Loading polaris custom config files"); configFileGroups.forEach( configFileGroup -> initCustomPolarisConfigFile(compositePropertySource, configFileGroup) ); @@ -127,15 +151,21 @@ public final class PolarisConfigFilePuller { } List files = configFileGroup.getFiles(); if (CollectionUtils.isEmpty(files)) { - return; - } - for (String fileName : files) { - PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService, groupNamespace, group, fileName); + PolarisPropertySource polarisPropertySource = loadGroupPolarisPropertySource(configFileService, groupNamespace, group); + if (polarisPropertySource == null) { + return; + } compositePropertySource.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource); - LOGGER.info( - "[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", - groupNamespace, group, fileName); + logInfo(String.format("[SCT Config] Load and inject polaris custom group config file success. namespace = %s, group = %s", groupNamespace, group)); + } + else { + for (String fileName : files) { + PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService, groupNamespace, group, fileName); + compositePropertySource.addPropertySource(polarisPropertySource); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); + logInfo(String.format("[SCT Config] Load and inject polaris custom config file success. namespace = %s, group = %s, fileName = %s", groupNamespace, group, fileName)); + } } } @@ -151,6 +181,7 @@ public final class PolarisConfigFilePuller { if (StringUtils.isEmpty(tsfNamespaceName) || StringUtils.isEmpty(tsfGroupName)) { return; } + logInfo("[SCT Config] Loading tsf config files"); String namespace = polarisContextProperties.getNamespace(); List tsfConfigGroups = new ArrayList<>(); tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group"); @@ -167,29 +198,43 @@ public final class PolarisConfigFilePuller { } compositePropertySource.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource); + logInfo(String.format("[SCT Config] Load and inject tsf config file success. namespace = %s, group = %s", namespace, tsfConfigGroup)); } } /** * Init TSF TLS property source. * @param compositePropertySource compositePropertySource + * @param tsfTlsProperties tsfTlsProperties + * @param environment environment + * @param serviceName serviceName */ public void initTsfTlsPropertySource(CompositePropertySource compositePropertySource, - TsfTlsProperties tsfTlsProperties, String serviceName) { - String address = System.getProperty("MESH_CITADEL_ADDR", System.getenv("MESH_CITADEL_ADDR")); + TsfTlsProperties tsfTlsProperties, Environment environment, String serviceName) { + String address = getSystemPropertyOrEnv("MESH_CITADEL_ADDR"); if (StringUtils.isNotBlank(address) && (StringUtils.equals("tsf", System.getProperty("server.ssl.bundle")) + || (environment != null && StringUtils.equals("tsf", environment.getProperty("server.ssl.bundle"))) || "tsf".equals(compositePropertySource.getProperty(("server.ssl.bundle")))) && ClassUtils.isClassPresent("com.tencent.cloud.plugin.tsf.tls.utils.SyncUtils") && !SyncUtils.isInitialized()) { // get certPath - String certPath = System.getProperty("MESH_CITADEL_CERT", System.getenv("MESH_CITADEL_CERT")); + String certPath = getSystemPropertyOrEnv("MESH_CITADEL_CERT"); // get token - String token = System.getProperty("tsf_token", System.getenv("tsf_token")); + String token = getSystemPropertyOrEnv("tsf_token"); // get validityDuration Object validityDuration = compositePropertySource.getProperty("spring.cloud.polaris.tls.validityDuration"); if (validityDuration == null) { - validityDuration = tsfTlsProperties.getValidityDuration(); + if (environment != null) { + validityDuration = environment.getProperty("spring.cloud.polaris.tls.validityDuration", + Long.class, TsfTlsProperties.DEFAULT_VALIDITY_DURATION); + } + else if (tsfTlsProperties != null) { + validityDuration = tsfTlsProperties.getValidityDuration(); + } + else { + validityDuration = TsfTlsProperties.DEFAULT_VALIDITY_DURATION; + } } if (validityDuration instanceof String) { validityDuration = Long.valueOf((String) validityDuration); @@ -197,7 +242,16 @@ public final class PolarisConfigFilePuller { // get refreshBefore Object refreshBefore = compositePropertySource.getProperty("spring.cloud.polaris.tls.refreshBefore"); if (refreshBefore == null) { - refreshBefore = tsfTlsProperties.getRefreshBefore(); + if (environment != null) { + refreshBefore = environment.getProperty("spring.cloud.polaris.tls.refreshBefore", + Long.class, TsfTlsProperties.DEFAULT_REFRESH_BEFORE); + } + else if (tsfTlsProperties != null) { + refreshBefore = tsfTlsProperties.getRefreshBefore(); + } + else { + refreshBefore = TsfTlsProperties.DEFAULT_REFRESH_BEFORE; + } } if (refreshBefore instanceof String) { refreshBefore = Long.valueOf((String) refreshBefore); @@ -205,7 +259,16 @@ public final class PolarisConfigFilePuller { // get watchInterval Object watchInterval = compositePropertySource.getProperty("spring.cloud.polaris.tls.watchInterval"); if (watchInterval == null) { - watchInterval = tsfTlsProperties.getWatchInterval(); + if (environment != null) { + watchInterval = environment.getProperty("spring.cloud.polaris.tls.watchInterval", + Long.class, TsfTlsProperties.DEFAULT_WATCH_INTERVAL); + } + else if (tsfTlsProperties != null) { + watchInterval = tsfTlsProperties.getWatchInterval(); + } + else { + watchInterval = TsfTlsProperties.DEFAULT_WATCH_INTERVAL; + } } if (watchInterval instanceof String) { watchInterval = Long.valueOf((String) watchInterval); @@ -214,15 +277,11 @@ public final class PolarisConfigFilePuller { if (SyncUtils.isVerified()) { Map tlsEnvProperties = new HashMap<>(); // set ssl - Object clientAuth = compositePropertySource.getProperty("server.ssl.client-auth"); - if (clientAuth == null) { - clientAuth = System.getProperty("server.ssl.client-auth", "want"); - } + Object clientAuth = getPropertyWithDefault(compositePropertySource, environment, + "server.ssl.client-auth", "want"); tlsEnvProperties.put("server.ssl.client-auth", clientAuth); - Object protocol = compositePropertySource.getProperty("spring.cloud.polaris.discovery.protocol"); - if (protocol == null) { - protocol = System.getProperty("spring.cloud.polaris.discovery.protocol", "https"); - } + Object protocol = getPropertyWithDefault(compositePropertySource, environment, + "spring.cloud.polaris.discovery.protocol", "https"); tlsEnvProperties.put("spring.cloud.polaris.discovery.protocol", protocol); tlsEnvProperties.put("tsf.discovery.scheme", protocol); @@ -237,12 +296,30 @@ public final class PolarisConfigFilePuller { } // process environment - MapPropertySource propertySource = new MapPropertySource("tsf-tls-config-data", tlsEnvProperties); + MapPropertySource propertySource = new MapPropertySource("tsf-tls-config", tlsEnvProperties); compositePropertySource.addPropertySource(propertySource); } } } + private String getSystemPropertyOrEnv(String key) { + return System.getProperty(key, System.getenv(key)); + } + + private Object getPropertyWithDefault(CompositePropertySource compositePropertySource, + Environment environment, String propertyKey, String defaultValue) { + Object value = compositePropertySource.getProperty(propertyKey); + if (value == null) { + if (environment != null) { + value = environment.getProperty(propertyKey, defaultValue); + } + else { + value = System.getProperty(propertyKey, defaultValue); + } + } + return value; + } + private List getInternalConfigFiles( String[] activeProfiles, String[] defaultProfiles, String serviceName) { String namespace = polarisContextProperties.getNamespace(); 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 e2bc2edcf..e8cc3ee61 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 @@ -28,12 +28,13 @@ 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.polaris.api.utils.CollectionUtils; +import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; import org.apache.commons.logging.Log; -import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.context.config.ConfigData; import org.springframework.boot.context.config.ConfigDataLoader; import org.springframework.boot.context.config.ConfigDataLoaderContext; @@ -42,8 +43,6 @@ 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; import static org.springframework.boot.context.config.ConfigData.Option.IGNORE_IMPORTS; import static org.springframework.boot.context.config.ConfigData.Option.IGNORE_PROFILES; @@ -61,13 +60,17 @@ public class PolarisConfigDataLoader implements ConfigDataLoader> propertySourceList = new ArrayList<>(compositePropertySource.getPropertySources()); Collections.reverse(propertySourceList); return new ConfigData(propertySourceList, getOptions(resource)); } - private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapContext, - PolarisConfigDataResource resource) { + private CompositePropertySource locate(PolarisConfigDataResource resource) { CompositePropertySource compositePropertySource = new CompositePropertySource( POLARIS_CONFIG_PROPERTY_SOURCE_NAME); SDKContext sdkContext = PolarisConfigSDKContextManager.innerGetConfigSDKContext(); @@ -102,43 +104,84 @@ public class PolarisConfigDataLoader implements ConfigDataLoader mockedStatic = mockStatic(PolarisPropertySourceUtils.class)) { - mockedStatic.when(() -> PolarisPropertySourceUtils.loadGroupPolarisPropertySource( - eq(configFileService), - eq(polarisNamespace), - any() - )).thenReturn(mockPropertySource); - - PolarisConfigFileLocator locator = new PolarisConfigFileLocator( - polarisConfigProperties, - polarisContextProperties, - configFileService, - environment - ); - // Act - locator.initTsfConfigGroups(compositePropertySource); - - // Verify - List polarisPropertySources = PolarisPropertySourceManager.getAllPropertySources(); - assertThat(polarisPropertySources.stream().map(PolarisPropertySource::getPropertySourceName). - filter(name -> name.equals(expectedAppConfigGroup)).count() == 1); - } - } - @Test void testPolarisConfigProperties() { PolarisConfigProperties testProperties = new PolarisConfigProperties(); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java index f7a12eb8c..fad664196 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Map; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; +import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils; import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigKVFile; @@ -30,11 +31,16 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.core.env.CompositePropertySource; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; /** @@ -175,4 +181,44 @@ public class PolarisConfigFilePullerTest { assertThat(compositePropertySource.getProperty("k2")).isEqualTo("v2"); assertThat(compositePropertySource.getProperty("k3")).isEqualTo("v3"); } + + @Test + void testInitTsfConfigGroupsSuccessfulLoad() { + PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService); + + // Arrange + String tsfId = "test-id"; + String tsfNamespace = "test-namespace"; + String tsfGroup = "test-group"; + String polarisNamespace = "polaris-namespace"; + + System.setProperty("tsf_id", tsfId); + System.setProperty("tsf_namespace_name", tsfNamespace); + System.setProperty("tsf_group_name", tsfGroup); + + when(polarisContextProperties.getNamespace()).thenReturn(polarisNamespace); + + String expectedAppConfigGroup = tsfId + "." + tsfGroup + ".application_config_group"; + + // mock polaris config properties + PolarisPropertySource mockPropertySource = mock(PolarisPropertySource.class); + when(mockPropertySource.getPropertySourceName()).thenReturn(expectedAppConfigGroup); + + CompositePropertySource compositePropertySource = mock(CompositePropertySource.class); + try (MockedStatic mockedStatic = mockStatic(PolarisPropertySourceUtils.class)) { + mockedStatic.when(() -> PolarisPropertySourceUtils.loadGroupPolarisPropertySource( + eq(configFileService), + eq(polarisNamespace), + any() + )).thenReturn(mockPropertySource); + + // Act + puller.initTsfConfigGroups(compositePropertySource); + + // Verify + List polarisPropertySources = PolarisPropertySourceManager.getAllPropertySources(); + assertThat(polarisPropertySources.stream().map(PolarisPropertySource::getPropertySourceName). + filter(name -> name.equals(expectedAppConfigGroup)).count() == 1); + } + } } 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 f7fdb2adf..9bdec9385 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 @@ -37,7 +37,6 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; -import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.context.config.ConfigData; import org.springframework.boot.context.config.ConfigDataLoaderContext; import org.springframework.boot.context.config.Profiles; @@ -82,7 +81,6 @@ public class PolarisConfigDataLoaderTest { try (MockedStatic mockedStatic = mockStatic(ConfigFileServiceFactory.class)) { ConfigDataLoaderContext context = mock(ConfigDataLoaderContext.class); PolarisConfigDataResource polarisConfigDataResource = mock(PolarisConfigDataResource.class); - ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class); PolarisConfigProperties polarisConfigProperties = mock(PolarisConfigProperties.class); PolarisContextProperties polarisContextProperties = mock(PolarisContextProperties.class); ConfigFileService configFileService = mock(ConfigFileService.class); @@ -101,7 +99,6 @@ public class PolarisConfigDataLoaderTest { ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties); when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")) .thenReturn(propertiesFile); - when(context.getBootstrapContext()).thenReturn(bootstrapContext); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -141,7 +138,6 @@ public class PolarisConfigDataLoaderTest { try (MockedStatic mockedStatic = mockStatic(ConfigFileServiceFactory.class)) { ConfigDataLoaderContext context = mock(ConfigDataLoaderContext.class); PolarisConfigDataResource polarisConfigDataResource = mock(PolarisConfigDataResource.class); - ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class); PolarisConfigProperties polarisConfigProperties = mock(PolarisConfigProperties.class); PolarisContextProperties polarisContextProperties = mock(PolarisContextProperties.class); ConfigFileService configFileService = mock(ConfigFileService.class); @@ -180,8 +176,6 @@ public class PolarisConfigDataLoaderTest { active.add("dev"); when(profiles.getActive()).thenReturn(active); - when(context.getBootstrapContext()).thenReturn(bootstrapContext); - when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -221,7 +215,6 @@ public class PolarisConfigDataLoaderTest { try (MockedStatic mockedStatic = mockStatic(ConfigFileServiceFactory.class)) { ConfigDataLoaderContext context = mock(ConfigDataLoaderContext.class); PolarisConfigDataResource polarisConfigDataResource = mock(PolarisConfigDataResource.class); - ConfigurableBootstrapContext bootstrapContext = mock(ConfigurableBootstrapContext.class); PolarisConfigProperties polarisConfigProperties = mock(PolarisConfigProperties.class); PolarisContextProperties polarisContextProperties = mock(PolarisContextProperties.class); ConfigFileService configFileService = mock(ConfigFileService.class); @@ -253,8 +246,6 @@ public class PolarisConfigDataLoaderTest { ConfigKVFile file1 = new MockedConfigKVFile(file1Map); when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1); - when(context.getBootstrapContext()).thenReturn(bootstrapContext); - when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName);