From b9632fc24ee6b1a6b2468aae637aaa69314d5ccc Mon Sep 17 00:00:00 2001 From: Fishtail <49390359+fuyuwei01@users.noreply.github.com> Date: Wed, 17 Jul 2024 13:06:51 +0800 Subject: [PATCH] refactor:let the configuration SDK context stand alone. (#1344) --- .github/PULL_REQUEST_TEMPLATE.md | 8 +- CHANGELOG.md | 3 +- .../polaris/config/ConfigurationModifier.java | 5 +- .../PolarisConfigAutoConfiguration.java | 13 +-- ...larisConfigBootstrapAutoConfiguration.java | 13 +-- .../PolarisConfigCustomExtensionLayer.java | 3 +- .../adapter/PolarisConfigFileLocator.java | 12 +- .../adapter/PolarisConfigFilePuller.java | 11 +- .../PolarisConfigPropertyAutoRefresher.java | 14 ++- .../adapter/PolarisPropertySourceManager.java | 20 +++- ...olarisRefreshAffectedContextRefresher.java | 5 +- .../PolarisRefreshEntireContextRefresher.java | 3 +- .../configdata/PolarisConfigDataLoader.java | 7 +- .../PolarisConfigDataLocationResolver.java | 28 +---- .../endpoint/PolarisConfigEndpoint.java | 7 +- ...olarisConfigEndpointAutoConfiguration.java | 6 +- ...arisConfigRefreshOptimizationListener.java | 7 +- .../adapter/PolarisConfigFileLocatorTest.java | 14 ++- .../adapter/PolarisConfigFilePullerTest.java | 17 +-- ...arisPropertiesSourceAutoRefresherTest.java | 14 ++- .../ConditionalOnReflectRefreshTypeTest.java | 2 - .../PolarisConfigDataLoaderTest.java | 28 ++--- .../endpoint/PolarisConfigEndpointTest.java | 13 ++- ...hOptimizationListenerNotTriggeredTest.java | 17 ++- ...reshOptimizationListenerTriggeredTest.java | 17 ++- .../pom.xml | 7 -- .../src/main/resources/application.yml | 2 + .../quickstart-example/pom.xml | 7 -- .../{bootstrap.yml => application.yml} | 2 + .../{bootstrap.yml => application.yml} | 2 + .../{bootstrap.yml => application.yml} | 2 + .../{bootstrap.yml => application.yml} | 2 + .../PolarisConfigurationConfigModifier.java | 26 +++++ .../context/PolarisSDKContextManager.java | 104 +++++++++++++++--- .../config/PolarisContextProperties.java | 1 + 35 files changed, 255 insertions(+), 187 deletions(-) rename spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/{bootstrap.yml => application.yml} (96%) rename spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/{bootstrap.yml => application.yml} (96%) rename spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/{bootstrap.yml => application.yml} (96%) rename spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/{bootstrap.yml => application.yml} (97%) create mode 100644 spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisConfigurationConfigModifier.java diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index ddd1ce6e1..d2d4a33e2 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -26,8 +26,8 @@ closes # ## Checklist (Optional) -- [ ] Will pull request to branch of 2023. -- [ ] Will pull request to branch of 2022. -- [ ] Will pull request to branch of 2021. -- [ ] Will pull request to branch of 2020. +- [ ] Will pull request to branch of 2023.0. +- [ ] Will pull request to branch of 2022.0. +- [ ] Will pull request to branch of 2021.0. +- [ ] Will pull request to branch of 2020.0. - [ ] Will pull request to branch of hoxton. diff --git a/CHANGELOG.md b/CHANGELOG.md index b392889fc..596543acb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,4 +10,5 @@ - [feat: add lane router examples](https://github.com/Tencent/spring-cloud-tencent/pull/1340) - [fix: fix the lossless provider override in multiple registries scenario](https://github.com/Tencent/spring-cloud-tencent/pull/1341) - [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1342) -- [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1343) \ No newline at end of file +- [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1343) +- [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1344) \ No newline at end of file diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java index 4b53d8305..1df53b537 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java @@ -13,7 +13,6 @@ * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * 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; @@ -26,7 +25,7 @@ import com.tencent.cloud.common.constant.OrderConstant; import com.tencent.cloud.common.util.AddressUtils; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties; -import com.tencent.cloud.polaris.context.PolarisConfigModifier; +import com.tencent.cloud.polaris.context.PolarisConfigurationConfigModifier; import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.factory.config.ConfigurationImpl; import com.tencent.polaris.factory.config.configuration.ConfigFilterConfigImpl; @@ -42,7 +41,7 @@ import org.springframework.util.CollectionUtils; * * @author lepdou 2022-03-10 */ -public class ConfigurationModifier implements PolarisConfigModifier { +public class ConfigurationModifier implements PolarisConfigurationConfigModifier { private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class); private static final String DATA_SOURCE_POLARIS = "polaris"; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java index b0c212a14..7b39f4bd1 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/PolarisConfigAutoConfiguration.java @@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config; import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder; import com.tencent.cloud.polaris.config.adapter.PolarisConfigPropertyRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher; import com.tencent.cloud.polaris.config.annotation.PolarisConfigAnnotationProcessor; @@ -78,9 +77,9 @@ public class PolarisConfigAutoConfiguration { @Bean @ConditionalOnMissingBean(search = SearchStrategy.CURRENT) - public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) { - return new PolarisRefreshEntireContextRefresher(polarisConfigProperties, polarisPropertySourceManager, contextRefresher); + public PolarisConfigPropertyRefresher polarisRefreshContextPropertySourceAutoRefresher( + PolarisConfigProperties polarisConfigProperties, ContextRefresher contextRefresher) { + return new PolarisRefreshEntireContextRefresher(polarisConfigProperties, contextRefresher); } @Configuration(proxyBeanMethods = false) @@ -104,10 +103,10 @@ public class PolarisConfigAutoConfiguration { } @Bean - public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, + public PolarisConfigPropertyRefresher polarisReflectPropertySourceAutoRefresher( + PolarisConfigProperties polarisConfigProperties, SpringValueRegistry springValueRegistry, PlaceholderHelper placeholderHelper) { - return new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, polarisPropertySourceManager, + return new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, springValueRegistry, placeholderHelper); } 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 2383cbf6b..9fdc8fb22 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 @@ -20,7 +20,6 @@ package com.tencent.cloud.polaris.config; import com.tencent.cloud.polaris.config.adapter.AffectedConfigurationPropertiesRebinder; import com.tencent.cloud.polaris.config.adapter.PolarisConfigFileLocator; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.condition.ConditionalOnReflectRefreshType; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties; @@ -59,16 +58,10 @@ public class PolarisConfigBootstrapAutoConfiguration { return new PolarisCryptoConfigProperties(); } - @Bean - @ConditionalOnMissingBean - public PolarisPropertySourceManager polarisPropertySourceManager() { - return new PolarisPropertySourceManager(); - } - @Bean @ConditionalOnConnectRemoteServerEnabled public ConfigFileService configFileService(PolarisSDKContextManager polarisSDKContextManager) { - return ConfigFileServiceFactory.createConfigFileService(polarisSDKContextManager.getSDKContext()); + return ConfigFileServiceFactory.createConfigFileService(polarisSDKContextManager.getConfigSDKContext()); } @Bean @@ -77,11 +70,9 @@ public class PolarisConfigBootstrapAutoConfiguration { PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, - PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) { return new PolarisConfigFileLocator(polarisConfigProperties, - polarisContextProperties, configFileService, - polarisPropertySourceManager, environment); + polarisContextProperties, configFileService, environment); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigCustomExtensionLayer.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigCustomExtensionLayer.java index 2549f0773..19d873857 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigCustomExtensionLayer.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigCustomExtensionLayer.java @@ -31,8 +31,9 @@ import org.springframework.core.env.Environment; public interface PolarisConfigCustomExtensionLayer { void initRegisterConfig(PolarisConfigPropertyAutoRefresher polarisConfigPropertyAutoRefresher); - void initConfigFiles(Environment environment, CompositePropertySource compositePropertySource, PolarisPropertySourceManager polarisPropertySourceManager, ConfigFileService configFileService); + void initConfigFiles(Environment environment, CompositePropertySource compositePropertySource, ConfigFileService configFileService); void executeAfterLocateConfigReturning(CompositePropertySource compositePropertySource); + boolean executeRegisterPublishChangeListener(PolarisPropertySource polarisPropertySource); } 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 47e149873..974aff31b 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 @@ -64,17 +64,15 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { private final ConfigFileService configFileService; - private final PolarisPropertySourceManager polarisPropertySourceManager; - private final Environment environment; // this class provides customized logic for some customers to configure special business group files private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer(); - public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) { + public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, + PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, Environment environment) { this.polarisConfigProperties = polarisConfigProperties; this.polarisContextProperties = polarisContextProperties; this.configFileService = configFileService; - this.polarisPropertySourceManager = polarisPropertySourceManager; this.environment = environment; } @@ -104,7 +102,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { LOGGER.debug("[SCT Config] PolarisConfigCustomExtensionLayer is not init, ignore the following execution steps"); return; } - polarisConfigCustomExtensionLayer.initConfigFiles(environment, compositePropertySource, polarisPropertySourceManager, configFileService); + polarisConfigCustomExtensionLayer.initConfigFiles(environment, compositePropertySource, configFileService); } private void afterLocatePolarisConfigExtension(CompositePropertySource compositePropertySource) { @@ -123,7 +121,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { compositePropertySource.addPropertySource(polarisPropertySource); - polarisPropertySourceManager.addPropertySource(polarisPropertySource); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile); } @@ -206,7 +204,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { compositePropertySource.addPropertySource(polarisPropertySource); - polarisPropertySourceManager.addPropertySource(polarisPropertySource); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); } 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 f9df0c063..2e31716f3 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 @@ -53,8 +53,6 @@ public final class PolarisConfigFilePuller { private ConfigFileService configFileService; - private PolarisPropertySourceManager polarisPropertySourceManager; - private PolarisConfigFilePuller() { } @@ -64,15 +62,12 @@ public final class PolarisConfigFilePuller { * * @param polarisContextProperties polarisContextProperties * @param configFileService configFileService - * @param polarisPropertySourceManager polarisPropertySourceManager * @return PolarisConfigFilePuller instance */ - public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, - PolarisPropertySourceManager polarisPropertySourceManager) { + public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService) { PolarisConfigFilePuller puller = new PolarisConfigFilePuller(); puller.polarisContextProperties = polarisContextProperties; puller.configFileService = configFileService; - puller.polarisPropertySourceManager = polarisPropertySourceManager; return puller; } @@ -91,7 +86,7 @@ public final class PolarisConfigFilePuller { PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); compositePropertySource.addPropertySource(polarisPropertySource); - polarisPropertySourceManager.addPropertySource(polarisPropertySource); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile); } } @@ -129,7 +124,7 @@ public final class PolarisConfigFilePuller { for (String fileName : files) { PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(namespace, group, fileName); compositePropertySource.addPropertySource(polarisPropertySource); - polarisPropertySourceManager.addPropertySource(polarisPropertySource); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); LOGGER.info( "[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); 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 c698cb6db..6bc5206d9 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 @@ -46,16 +46,13 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL private final PolarisConfigProperties polarisConfigProperties; - private final PolarisPropertySourceManager polarisPropertySourceManager; - private final AtomicBoolean registered = new AtomicBoolean(false); // this class provides customized logic for some customers to configure special business group files private final PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer = PolarisServiceLoaderUtil.getPolarisConfigCustomExtensionLayer(); - public PolarisConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties, PolarisPropertySourceManager polarisPropertySourceManager) { + public PolarisConfigPropertyAutoRefresher(PolarisConfigProperties polarisConfigProperties) { this.polarisConfigProperties = polarisConfigProperties; - this.polarisPropertySourceManager = polarisPropertySourceManager; } @Override @@ -68,7 +65,7 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL return; } - List polarisPropertySources = polarisPropertySourceManager.getAllPropertySources(); + List polarisPropertySources = PolarisPropertySourceManager.getAllPropertySources(); if (CollectionUtils.isEmpty(polarisPropertySources)) { return; } @@ -144,4 +141,11 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL } polarisConfigCustomExtensionLayer.executeRegisterPublishChangeListener(polarisPropertySource); } + + /** + * Just for junit test. + */ + public void setRegistered(boolean registered) { + this.registered.set(registered); + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceManager.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceManager.java index f8736e20e..096ab7d96 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceManager.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertySourceManager.java @@ -28,15 +28,25 @@ import java.util.concurrent.ConcurrentHashMap; * * @author lepdou 2022-03-28 */ -public class PolarisPropertySourceManager { +public final class PolarisPropertySourceManager { - private final Map polarisPropertySources = new ConcurrentHashMap<>(); + private static final Map polarisPropertySources = new ConcurrentHashMap<>(); - public void addPropertySource(PolarisPropertySource polarisPropertySource) { - polarisPropertySources.putIfAbsent(polarisPropertySource.getPropertySourceName(), polarisPropertySource); + private PolarisPropertySourceManager() { } - public List getAllPropertySources() { + public static void addPropertySource(PolarisPropertySource polarisPropertySource) { + polarisPropertySources.put(polarisPropertySource.getPropertySourceName(), polarisPropertySource); + } + + public static List getAllPropertySources() { return new ArrayList<>(polarisPropertySources.values()); } + + /** + * Just for test. + */ + public static void clearPropertySources() { + polarisPropertySources.clear(); + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshAffectedContextRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshAffectedContextRefresher.java index dffb23adf..bbe3e71aa 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshAffectedContextRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshAffectedContextRefresher.java @@ -59,9 +59,8 @@ public class PolarisRefreshAffectedContextRefresher extends PolarisConfigPropert private TypeConverter typeConverter; public PolarisRefreshAffectedContextRefresher(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, SpringValueRegistry springValueRegistry, - PlaceholderHelper placeholderHelper) { - super(polarisConfigProperties, polarisPropertySourceManager); + SpringValueRegistry springValueRegistry, PlaceholderHelper placeholderHelper) { + super(polarisConfigProperties); this.springValueRegistry = springValueRegistry; this.placeholderHelper = placeholderHelper; } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshEntireContextRefresher.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshEntireContextRefresher.java index 2f02ab946..3c2ca5ac7 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshEntireContextRefresher.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisRefreshEntireContextRefresher.java @@ -35,9 +35,8 @@ public class PolarisRefreshEntireContextRefresher extends PolarisConfigPropertyA private final ContextRefresher contextRefresher; public PolarisRefreshEntireContextRefresher(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager, ContextRefresher contextRefresher) { - super(polarisConfigProperties, polarisPropertySourceManager); + super(polarisConfigProperties); this.contextRefresher = contextRefresher; } 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 613f03603..eeefd2d04 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 @@ -22,9 +22,9 @@ import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import com.tencent.cloud.polaris.config.adapter.PolarisConfigFilePuller; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; @@ -90,13 +90,12 @@ public class PolarisConfigDataLoader implements ConfigDataLoader { - // destroy earlier Polaris sdkContext - event.getBootstrapContext().get(SDKContext.class).destroy(); - // register PolarisPropertySourceManager to context - PolarisPropertySourceManager polarisPropertySourceManager = event.getBootstrapContext() - .get(PolarisPropertySourceManager.class); - event.getApplicationContext().getBeanFactory().registerSingleton( - "polarisPropertySourceManager", polarisPropertySourceManager); - } - ); - return loadConfigDataResources(resolverContext, location, profiles, polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties); } @@ -273,7 +258,7 @@ public class PolarisConfigDataLocationResolver implements ((ConfigurationImpl) sdkContext.getConfig()).getGlobal().getStatReporter().setEnable(false); } sdkContext.init(); - bootstrapContext.register(SDKContext.class, BootstrapRegistry.InstanceSupplier.of(sdkContext)); + PolarisSDKContextManager.setConfigSDKContext(sdkContext); } } @@ -282,11 +267,10 @@ public class PolarisConfigDataLocationResolver implements PolarisConfigProperties polarisConfigProperties, PolarisCryptoConfigProperties polarisCryptoConfigProperties, PolarisContextProperties polarisContextProperties) { List modifierList = modifierList(polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties); - return SDKContext.initContextByConfig(polarisContextProperties.configuration(modifierList, () -> { - return loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address"); - }, () -> { - return loadPolarisConfigProperties(resolverContext, Integer.class, "spring.cloud.polaris.local-port"); - })); + return SDKContext.initContextByConfig(polarisContextProperties.configuration( + modifierList, + () -> loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address"), + () -> loadPolarisConfigProperties(resolverContext, Integer.class, "spring.cloud.polaris.local-port"))); } private List modifierList(PolarisConfigProperties polarisConfigProperties, diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpoint.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpoint.java index e7d71f966..2740d0fe2 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpoint.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpoint.java @@ -37,12 +37,9 @@ import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; public class PolarisConfigEndpoint { private final PolarisConfigProperties polarisConfigProperties; - private final PolarisPropertySourceManager polarisPropertySourceManager; - public PolarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager) { + public PolarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties) { this.polarisConfigProperties = polarisConfigProperties; - this.polarisPropertySourceManager = polarisPropertySourceManager; } @ReadOperation @@ -50,7 +47,7 @@ public class PolarisConfigEndpoint { Map configInfo = new HashMap<>(); configInfo.put("PolarisConfigProperties", polarisConfigProperties); - List propertySourceList = polarisPropertySourceManager.getAllPropertySources(); + List propertySourceList = PolarisPropertySourceManager.getAllPropertySources(); configInfo.put("PolarisPropertySource", propertySourceList); return configInfo; diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointAutoConfiguration.java index 815d5798e..3f530c3ab 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointAutoConfiguration.java @@ -18,7 +18,6 @@ package com.tencent.cloud.polaris.config.endpoint; import com.tencent.cloud.polaris.config.ConditionalOnPolarisConfigEnabled; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint; @@ -41,8 +40,7 @@ public class PolarisConfigEndpointAutoConfiguration { @Bean @ConditionalOnAvailableEndpoint @ConditionalOnMissingBean - public PolarisConfigEndpoint polarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties, - PolarisPropertySourceManager polarisPropertySourceManager) { - return new PolarisConfigEndpoint(polarisConfigProperties, polarisPropertySourceManager); + public PolarisConfigEndpoint polarisConfigEndpoint(PolarisConfigProperties polarisConfigProperties) { + return new PolarisConfigEndpoint(polarisConfigProperties); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListener.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListener.java index 54f0c6246..357b6bbb2 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListener.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListener.java @@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.listener; import java.util.Collections; import com.tencent.cloud.polaris.config.adapter.PolarisConfigRefreshScopeAnnotationDetector; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.enums.RefreshType; @@ -112,7 +111,7 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList beanFactory.removeBeanDefinition(REFLECT_REBINDER_BEAN_NAME); } catch (BeansException e) { - // If there is a removeBean exception in this code, do not affect the main process startup. Some user usage may cause the polarisReflectPropertySourceAutoRefresher to not load, and the removeBeanDefinition will report an error + // If there is a removeBean exception in this code, do not affect the main process startup. Some user usage may cause the polarisReflectPropertySourceAutoRefresher to not load, and the removeBeanDefinition will report an error LOGGER.debug("removeRelatedBeansOfReflect occur error:", e); } } @@ -122,12 +121,10 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition().getBeanDefinition(); beanDefinition.setBeanClass(PolarisRefreshEntireContextRefresher.class); PolarisConfigProperties polarisConfigProperties = beanFactory.getBean(PolarisConfigProperties.class); - PolarisPropertySourceManager polarisPropertySourceManager = beanFactory.getBean(PolarisPropertySourceManager.class); ContextRefresher contextRefresher = beanFactory.getBean(ContextRefresher.class); ConstructorArgumentValues constructorArgumentValues = beanDefinition.getConstructorArgumentValues(); constructorArgumentValues.addIndexedArgumentValue(0, polarisConfigProperties); - constructorArgumentValues.addIndexedArgumentValue(1, polarisPropertySourceManager); - constructorArgumentValues.addIndexedArgumentValue(2, contextRefresher); + constructorArgumentValues.addIndexedArgumentValue(1, contextRefresher); beanFactory.registerBeanDefinition(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, beanDefinition); } 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 e7bbc6f86..b2f98817c 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 @@ -29,6 +29,7 @@ import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigKVFile; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -56,14 +57,17 @@ public class PolarisConfigFileLocatorTest { @Mock private ConfigFileService configFileService; @Mock - private PolarisPropertySourceManager polarisPropertySourceManager; - @Mock private Environment environment; + @BeforeEach + public void setUp() { + PolarisPropertySourceManager.clearPropertySources(); + } + @Test public void testLoadApplicationPropertiesFile() { PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, - configFileService, polarisPropertySourceManager, environment); + configFileService, environment); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -98,7 +102,7 @@ public class PolarisConfigFileLocatorTest { @Test public void testActiveProfileFilesPriorityBiggerThanDefault() { PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, - configFileService, polarisPropertySourceManager, environment); + configFileService, environment); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -145,7 +149,7 @@ public class PolarisConfigFileLocatorTest { @Test public void testGetCustomFiles() { PolarisConfigFileLocator locator = new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, - configFileService, polarisPropertySourceManager, environment); + configFileService, environment); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); 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 6d316f33f..adf7286c0 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 @@ -27,6 +27,7 @@ import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigKVFile; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -52,13 +53,15 @@ public class PolarisConfigFilePullerTest { private PolarisContextProperties polarisContextProperties; @Mock private ConfigFileService configFileService; - @Mock - private PolarisPropertySourceManager polarisPropertySourceManager; + + @BeforeEach + public void setUp() { + PolarisPropertySourceManager.clearPropertySources(); + } @Test public void testPullInternalConfigFiles() { - PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, - polarisPropertySourceManager); + PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -90,8 +93,7 @@ public class PolarisConfigFilePullerTest { @Test public void testPullInternalConfigFilesWithProfile() { - PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, - polarisPropertySourceManager); + PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -137,8 +139,7 @@ public class PolarisConfigFilePullerTest { @Test public void testPullCustomConfigFilesWithProfile() { - PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, - polarisPropertySourceManager); + PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java index ba38d135e..0d10255fa 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisPropertiesSourceAutoRefresherTest.java @@ -24,7 +24,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; -import com.google.common.collect.Lists; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.spring.property.PlaceholderHelper; import com.tencent.cloud.polaris.config.spring.property.SpringValue; @@ -32,6 +31,7 @@ import com.tencent.cloud.polaris.config.spring.property.SpringValueRegistry; import com.tencent.polaris.configuration.api.core.ChangeType; import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; @@ -59,8 +59,6 @@ public class PolarisPropertiesSourceAutoRefresherTest { private final String testFileName = "application.properties"; @Mock private PolarisConfigProperties polarisConfigProperties; - @Mock - private PolarisPropertySourceManager polarisPropertySourceManager; @Mock private SpringValueRegistry springValueRegistry; @@ -68,10 +66,14 @@ public class PolarisPropertiesSourceAutoRefresherTest { @Mock private PlaceholderHelper placeholderHelper; + @BeforeEach + public void setUp() { + PolarisPropertySourceManager.clearPropertySources(); + } + @Test public void testConfigFileChanged() throws Exception { - PolarisRefreshAffectedContextRefresher refresher = new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, - polarisPropertySourceManager, springValueRegistry, placeholderHelper); + PolarisRefreshAffectedContextRefresher refresher = new PolarisRefreshAffectedContextRefresher(polarisConfigProperties, springValueRegistry, placeholderHelper); ConfigurableApplicationContext applicationContext = mock(ConfigurableApplicationContext.class); ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class); TypeConverter typeConverter = mock(TypeConverter.class); @@ -99,7 +101,7 @@ public class PolarisPropertiesSourceAutoRefresherTest { PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName, file, content); - when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource)); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED); ConfigPropertyChangeInfo changeInfo2 = new ConfigPropertyChangeInfo("k4", null, "v4", ChangeType.ADDED); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java index ebde3e470..058d4d1ba 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/condition/ConditionalOnReflectRefreshTypeTest.java @@ -24,7 +24,6 @@ import java.util.Objects; import com.tencent.cloud.polaris.config.PolarisConfigAutoConfiguration; import com.tencent.cloud.polaris.config.PolarisConfigBootstrapAutoConfiguration; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshAffectedContextRefresher; import com.tencent.cloud.polaris.config.adapter.PolarisRefreshEntireContextRefresher; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; @@ -107,7 +106,6 @@ public class ConditionalOnReflectRefreshTypeTest { .withPropertyValues("spring.cloud.polaris.config.enabled=true"); contextRunner.run(context -> { assertThat(context).hasSingleBean(PolarisConfigProperties.class); - assertThat(context).hasSingleBean(PolarisPropertySourceManager.class); assertThat(context).hasSingleBean(ContextRefresher.class); assertThat(context).hasSingleBean(PolarisRefreshEntireContextRefresher.class); }); 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 51b7744fc..86cacfd2c 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 @@ -25,14 +25,15 @@ import java.util.Map; import com.google.common.collect.Lists; import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; -import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.config.PolarisContextProperties; import com.tencent.polaris.client.api.SDKContext; import com.tencent.polaris.configuration.api.core.ConfigFileService; import com.tencent.polaris.configuration.api.core.ConfigKVFile; import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.MockedStatic; @@ -49,7 +50,7 @@ import org.springframework.core.env.PropertySource; import static com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader.CUSTOM_POLARIS_CONFIG_FILE_LOADED; import static com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader.INTERNAL_CONFIG_FILES_LOADED; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mockStatic; import static org.mockito.Mockito.when; @@ -68,11 +69,14 @@ public class PolarisConfigDataLoaderTest { private final String testServiceName = "testServiceName"; private final String polarisConfigPropertySourceName = "polaris-config"; + @BeforeAll + static void beforeAll() { + PolarisSDKContextManager.setConfigSDKContext(sdkContext); + } + @AfterAll static void afterAll() { - if (sdkContext != null) { - sdkContext.destroy(); - } + PolarisSDKContextManager.innerGetConfigSDKContext(); } @Test @@ -100,9 +104,6 @@ public class PolarisConfigDataLoaderTest { when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")) .thenReturn(propertiesFile); when(context.getBootstrapContext()).thenReturn(bootstrapContext); - when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext); - - when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager()); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -123,7 +124,7 @@ public class PolarisConfigDataLoaderTest { when(polarisConfigDataResource.getProfiles()).thenReturn(profiles); mockedStatic.when(() -> { - ConfigFileServiceFactory.createConfigFileService(sdkContext); + ConfigFileServiceFactory.createConfigFileService(any(SDKContext.class)); }).thenReturn(configFileService); ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource); @@ -181,8 +182,6 @@ public class PolarisConfigDataLoaderTest { when(profiles.getActive()).thenReturn(active); when(context.getBootstrapContext()).thenReturn(bootstrapContext); - when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext); - when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager()); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -202,7 +201,7 @@ public class PolarisConfigDataLoaderTest { when(polarisConfigDataResource.getProfiles()).thenReturn(profiles); mockedStatic.when(() -> { - ConfigFileServiceFactory.createConfigFileService(sdkContext); + ConfigFileServiceFactory.createConfigFileService(any(SDKContext.class)); }).thenReturn(configFileService); ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource); @@ -254,9 +253,6 @@ public class PolarisConfigDataLoaderTest { when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1); when(context.getBootstrapContext()).thenReturn(bootstrapContext); - when(bootstrapContext.get(eq(SDKContext.class))).thenReturn(sdkContext); - - when(bootstrapContext.get(eq(PolarisPropertySourceManager.class))).thenReturn(new PolarisPropertySourceManager()); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); when(polarisContextProperties.getService()).thenReturn(testServiceName); @@ -278,7 +274,7 @@ public class PolarisConfigDataLoaderTest { when(polarisConfigDataResource.getProfiles()).thenReturn(profiles); mockedStatic.when(() -> { - ConfigFileServiceFactory.createConfigFileService(sdkContext); + ConfigFileServiceFactory.createConfigFileService(any(SDKContext.class)); }).thenReturn(configFileService); ConfigData configData = polarisConfigDataLoader.load(context, polarisConfigDataResource); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointTest.java index 92933fcc7..0389f1607 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/endpoint/PolarisConfigEndpointTest.java @@ -26,13 +26,13 @@ import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; +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.junit.jupiter.MockitoExtension; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.when; /** * Test for polaris config endpoint. @@ -48,8 +48,11 @@ public class PolarisConfigEndpointTest { @Mock private PolarisConfigProperties polarisConfigProperties; - @Mock - private PolarisPropertySourceManager polarisPropertySourceManager; + + @BeforeEach + public void setUp() { + PolarisPropertySourceManager.clearPropertySources(); + } @Test public void testPolarisConfigEndpoint() { @@ -60,9 +63,9 @@ public class PolarisConfigEndpointTest { MockedConfigKVFile file = new MockedConfigKVFile(content); PolarisPropertySource polarisPropertySource = new PolarisPropertySource(testNamespace, testServiceName, testFileName, file, content); - when(polarisPropertySourceManager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource)); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); - PolarisConfigEndpoint endpoint = new PolarisConfigEndpoint(polarisConfigProperties, polarisPropertySourceManager); + PolarisConfigEndpoint endpoint = new PolarisConfigEndpoint(polarisConfigProperties); Map info = endpoint.polarisConfig(); assertThat(polarisConfigProperties).isEqualTo(info.get("PolarisConfigProperties")); assertThat(Lists.newArrayList(polarisPropertySource)).isEqualTo(info.get("PolarisPropertySource")); diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerNotTriggeredTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerNotTriggeredTest.java index eef16a89f..191af7110 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerNotTriggeredTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerNotTriggeredTest.java @@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.listener; import java.util.HashMap; import java.util.Map; -import com.google.common.collect.Lists; import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; @@ -31,6 +30,7 @@ import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.polaris.configuration.api.core.ChangeType; import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; @@ -77,6 +77,11 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest { @Autowired private ConfigurableApplicationContext context; + @BeforeAll + static void beforeAll() { + PolarisPropertySourceManager.clearPropertySources(); + } + @Test public void testNotSwitchConfigRefreshType() { RefreshType actualRefreshType = context.getEnvironment() @@ -100,12 +105,12 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest { PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME, file, content); - PolarisPropertySourceManager manager = context.getBean(PolarisPropertySourceManager.class); - when(manager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource)); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); PolarisRefreshAffectedContextRefresher refresher = context.getBean(PolarisRefreshAffectedContextRefresher.class); PolarisRefreshAffectedContextRefresher spyRefresher = Mockito.spy(refresher); + refresher.setRegistered(false); spyRefresher.onApplicationEvent(null); ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED); @@ -134,12 +139,6 @@ public class PolarisConfigRefreshOptimizationListenerNotTriggeredTest { @SpringBootApplication protected static class TestApplication { - @Primary - @Bean - public PolarisPropertySourceManager polarisPropertySourceManager() { - return mock(PolarisPropertySourceManager.class); - } - @Primary @Bean public ContextRefresher contextRefresher() { diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerTriggeredTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerTriggeredTest.java index 24aa7ae08..ef273483e 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerTriggeredTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/listener/PolarisConfigRefreshOptimizationListenerTriggeredTest.java @@ -21,7 +21,6 @@ package com.tencent.cloud.polaris.config.listener; import java.util.HashMap; import java.util.Map; -import com.google.common.collect.Lists; import com.tencent.cloud.polaris.config.adapter.MockedConfigKVFile; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; @@ -31,6 +30,7 @@ import com.tencent.cloud.polaris.config.enums.RefreshType; import com.tencent.polaris.configuration.api.core.ChangeType; import com.tencent.polaris.configuration.api.core.ConfigKVFileChangeEvent; import com.tencent.polaris.configuration.api.core.ConfigPropertyChangeInfo; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mockito; @@ -78,6 +78,11 @@ public class PolarisConfigRefreshOptimizationListenerTriggeredTest { @Autowired private ConfigurableApplicationContext context; + @BeforeEach + public void setUp() { + PolarisPropertySourceManager.clearPropertySources(); + } + @Test public void testSwitchConfigRefreshType() { RefreshType actualRefreshType = context.getEnvironment() @@ -101,12 +106,12 @@ public class PolarisConfigRefreshOptimizationListenerTriggeredTest { PolarisPropertySource polarisPropertySource = new PolarisPropertySource(TEST_NAMESPACE, TEST_SERVICE_NAME, TEST_FILE_NAME, file, content); - PolarisPropertySourceManager manager = context.getBean(PolarisPropertySourceManager.class); - when(manager.getAllPropertySources()).thenReturn(Lists.newArrayList(polarisPropertySource)); + PolarisPropertySourceManager.addPropertySource(polarisPropertySource); PolarisRefreshEntireContextRefresher refresher = context.getBean(PolarisRefreshEntireContextRefresher.class); PolarisRefreshEntireContextRefresher spyRefresher = Mockito.spy(refresher); + refresher.setRegistered(false); spyRefresher.onApplicationEvent(null); ConfigPropertyChangeInfo changeInfo = new ConfigPropertyChangeInfo("k1", "v1", "v11", ChangeType.MODIFIED); @@ -135,12 +140,6 @@ public class PolarisConfigRefreshOptimizationListenerTriggeredTest { @SpringBootApplication protected static class TestApplication { - @Primary - @Bean - public PolarisPropertySourceManager polarisPropertySourceManager() { - return mock(PolarisPropertySourceManager.class); - } - @Primary @Bean public ContextRefresher contextRefresher() { diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/pom.xml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/pom.xml index 5b5eb0a57..6f602207d 100644 --- a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/pom.xml +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/pom.xml @@ -21,11 +21,4 @@ pom Spring Cloud Tencent Polaris Router Lane Example Example of Spring Cloud Tencent Polaris Router Lane - - - - org.springframework.cloud - spring-cloud-starter-bootstrap - - \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml index 5c39a3f50..1c02136d0 100644 --- a/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml +++ b/spring-cloud-tencent-examples/polaris-router-grayrelease-lane-example/router-grayrelease-lane-gateway/src/main/resources/application.yml @@ -3,6 +3,8 @@ server: spring: application: name: LaneRouterGatewayService + config: + import: optional:polaris cloud: polaris: address: grpc://119.91.66.223:8091 diff --git a/spring-cloud-tencent-examples/quickstart-example/pom.xml b/spring-cloud-tencent-examples/quickstart-example/pom.xml index d14e7eed4..07bd50ffc 100644 --- a/spring-cloud-tencent-examples/quickstart-example/pom.xml +++ b/spring-cloud-tencent-examples/quickstart-example/pom.xml @@ -20,11 +20,4 @@ quickstart-callee-service-a quickstart-callee-service-b - - - - org.springframework.cloud - spring-cloud-starter-bootstrap - - \ No newline at end of file diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml similarity index 96% rename from spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml index e4c6ada51..787baac99 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml @@ -3,6 +3,8 @@ server: spring: application: name: QuickstartCalleeService + config: + import: optional:polaris cloud: polaris: address: grpc://119.91.66.223:8091 diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/application.yml similarity index 96% rename from spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/application.yml index be96bfdcc..b74bef5a1 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-b/src/main/resources/application.yml @@ -3,6 +3,8 @@ server: spring: application: name: QuickstartCalleeService + config: + import: optional:polaris cloud: polaris: address: grpc://119.91.66.223:8091 diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/application.yml similarity index 96% rename from spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/application.yml index 7c538b8fa..6383b4505 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/application.yml @@ -3,6 +3,8 @@ server: spring: application: name: QuickstartCallerService + config: + import: optional:polaris cloud: polaris: address: grpc://119.91.66.223:8091 diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/application.yml similarity index 97% rename from spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/bootstrap.yml rename to spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/application.yml index 52efd5e2d..fd9dd9f05 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-gateway-service/src/main/resources/application.yml @@ -3,6 +3,8 @@ server: spring: application: name: QuickStartGatewayService + config: + import: optional:polaris cloud: tencent: plugin: diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisConfigurationConfigModifier.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisConfigurationConfigModifier.java new file mode 100644 index 000000000..f627d46ed --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisConfigurationConfigModifier.java @@ -0,0 +1,26 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * 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.context; + +/** + * Config modifier interface for Polaris configuration. + * + * @author Haotian Zhang + */ +public interface PolarisConfigurationConfigModifier extends PolarisConfigModifier { +} diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java index 143b6fbb7..d84a000b0 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java @@ -17,6 +17,7 @@ package com.tencent.cloud.polaris.context; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -25,6 +26,7 @@ import com.tencent.polaris.api.control.Destroyable; import com.tencent.polaris.api.core.ConsumerAPI; import com.tencent.polaris.api.core.LosslessAPI; import com.tencent.polaris.api.core.ProviderAPI; +import com.tencent.polaris.api.utils.CollectionUtils; import com.tencent.polaris.assembly.api.AssemblyAPI; import com.tencent.polaris.assembly.factory.AssemblyAPIFactory; import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI; @@ -53,7 +55,8 @@ public class PolarisSDKContextManager { * Constant for checking before destroy SDK context. */ public volatile static boolean isRegistered = false; - private volatile static SDKContext sdkContext; + private volatile static SDKContext configSDKContext; + private volatile static SDKContext serviceSdkContext; private volatile static ProviderAPI providerAPI; private volatile static ConsumerAPI consumerAPI; private volatile static LosslessAPI losslessAPI; @@ -75,7 +78,7 @@ public class PolarisSDKContextManager { * Don't call this method directly. */ public static void innerDestroy() { - if (Objects.nonNull(sdkContext)) { + if (Objects.nonNull(serviceSdkContext)) { try { // destroy ProviderAPI if (Objects.nonNull(providerAPI)) { @@ -119,9 +122,9 @@ public class PolarisSDKContextManager { assemblyAPI = null; } - if (Objects.nonNull(sdkContext)) { - sdkContext.destroy(); - sdkContext = null; + if (Objects.nonNull(serviceSdkContext)) { + serviceSdkContext.destroy(); + serviceSdkContext = null; } LOG.info("Polaris SDK context is destroyed."); } @@ -131,35 +134,58 @@ public class PolarisSDKContextManager { } } + /** + * Used for config data. + */ + public static SDKContext innerGetConfigSDKContext() { + if (configSDKContext == null) { + throw new IllegalArgumentException("configSDKContext is not initialized."); + } + return configSDKContext; + } + + public static void innerConfigDestroy() { + try { + if (Objects.nonNull(configSDKContext)) { + configSDKContext.destroy(); + configSDKContext = null; + } + LOG.info("Polaris SDK config context is destroyed."); + } + catch (Throwable throwable) { + LOG.info("Polaris SDK config context is destroyed failed.", throwable); + } + } + public void init() { - if (null == sdkContext) { + if (null == serviceSdkContext) { try { // init SDKContext - sdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList, + serviceSdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList, () -> environment.getProperty("spring.cloud.client.ip-address"), () -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0))); - sdkContext.init(); + serviceSdkContext.init(); // init ProviderAPI - providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(sdkContext); + providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(serviceSdkContext); // init losslessAPI - losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(sdkContext); + losslessAPI = DiscoveryAPIFactory.createLosslessAPIByContext(serviceSdkContext); // init ConsumerAPI - consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext); + consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(serviceSdkContext); // init RouterAPI - routerAPI = RouterAPIFactory.createRouterAPIByContext(sdkContext); + routerAPI = RouterAPIFactory.createRouterAPIByContext(serviceSdkContext); // init CircuitBreakAPI - circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(sdkContext); + circuitBreakAPI = CircuitBreakAPIFactory.createCircuitBreakAPIByContext(serviceSdkContext); // init LimitAPI - limitAPI = LimitAPIFactory.createLimitAPIByContext(sdkContext); + limitAPI = LimitAPIFactory.createLimitAPIByContext(serviceSdkContext); // init AssemblyAPI - assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(sdkContext); + assemblyAPI = AssemblyAPIFactory.createAssemblyAPIByContext(serviceSdkContext); // add shutdown hook Runtime.getRuntime().addShutdownHook(new Thread(() -> { @@ -182,11 +208,25 @@ public class PolarisSDKContextManager { throw throwable; } } + + initConfig(); + } + + /** + * Used for config data. + */ + public static void setConfigSDKContext(SDKContext context) { + if (configSDKContext == null) { + configSDKContext = context; + // add shutdown hook + Runtime.getRuntime().addShutdownHook(new Thread(PolarisSDKContextManager::innerConfigDestroy)); + LOG.info("create Polaris config SDK context successfully."); + } } public SDKContext getSDKContext() { init(); - return sdkContext; + return serviceSdkContext; } public ProviderAPI getProviderAPI() { @@ -222,4 +262,36 @@ public class PolarisSDKContextManager { public AssemblyAPI getAssemblyAPI() { return assemblyAPI; } + + public SDKContext getConfigSDKContext() { + initConfig(); + return configSDKContext; + } + + public void initConfig() { + // get modifiers for configuration. + List configModifierList = new ArrayList<>(); + for (PolarisConfigModifier modifier : modifierList) { + if (modifier instanceof PolarisConfigurationConfigModifier) { + configModifierList.add(modifier); + } + } + if (null == configSDKContext && CollectionUtils.isNotEmpty(configModifierList)) { + try { + // init config SDKContext + configSDKContext = SDKContext.initContextByConfig(properties.configuration(configModifierList, + () -> environment.getProperty("spring.cloud.client.ip-address"), + () -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0))); + configSDKContext.init(); + + // add shutdown hook + Runtime.getRuntime().addShutdownHook(new Thread(PolarisSDKContextManager::innerConfigDestroy)); + LOG.info("create Polaris config SDK context successfully. properties: {}, ", properties); + } + catch (Throwable throwable) { + LOG.error("create Polaris config SDK context failed. properties: {}, ", properties, throwable); + throw throwable; + } + } + } } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java index 498af60d1..3dad08fce 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java @@ -76,6 +76,7 @@ public class PolarisContextProperties { /** * polaris service name. */ + @Value("${spring.cloud.polaris.service:${spring.application.name:}}") private String service; public Configuration configuration(List modifierList, Supplier ipAddressSupplier, Supplier portSupplier) {