From f7720f594291561492c7eb52a1dde5d7750a7cd8 Mon Sep 17 00:00:00 2001 From: yangjuanying <531948963@qq.com> Date: Thu, 3 Aug 2023 16:34:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=AE=9A=E4=B9=89?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E9=85=8D=E7=BD=AE=E5=B1=82=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=EF=BC=8C=E8=B0=83=E6=95=B4=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=9A=84checkstyle=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PolarisConfigCustomExtensionLayer.java | 27 +++++++++ .../adapter/PolarisConfigFileLocator.java | 59 +++++++++++-------- ...arisConfigRefreshOptimizationListener.java | 17 +++--- spring-cloud-tencent-dependencies/pom.xml | 2 +- 4 files changed, 71 insertions(+), 34 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigCustomExtensionLayer.java 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 new file mode 100644 index 000000000..76cf840db --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigCustomExtensionLayer.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 www.tencent.com. + * All Rights Reserved. + * This program is the confidential and proprietary information of + * www.tencent.com ("Confidential Information"). You shall not disclose such + * Confidential Information and shall use it only in accordance with + * the terms of the license agreement you entered into with www.tencent.com. + * + */ + +package com.tencent.cloud.polaris.config.adapter; + +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.Environment; + +import com.tencent.polaris.configuration.api.core.ConfigFileService; + + +/** + * @Date Jul 23, 2023 2:57:49 PM + * @author juanyinyang + */ +public interface PolarisConfigCustomExtensionLayer { + void execute(String namespace, Environment environment, CompositePropertySource compositePropertySource, + PolarisPropertySourceManager polarisPropertySourceManager, ConfigFileService configFileService); + +} 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 495352a6b..740f697bc 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 @@ -20,9 +20,11 @@ package com.tencent.cloud.polaris.config.adapter; import java.util.ArrayList; import java.util.Arrays; +import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.ServiceLoader; import java.util.concurrent.ConcurrentHashMap; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; @@ -68,12 +70,20 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { private final PolarisPropertySourceManager polarisPropertySourceManager; private final Environment environment; + // 此类给一些客户定制化逻辑做一些特殊业务分组文件的配置处理 + private PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer; + + { + ServiceLoader polarisConfigCustomExtensionLayerLoader = ServiceLoader.load(PolarisConfigCustomExtensionLayer.class); + Iterator polarisConfigCustomExtensionLayerIterator = polarisConfigCustomExtensionLayerLoader.iterator(); + // 一般就一个实现类,如果有多个,那么加载的是最后一个 + while (polarisConfigCustomExtensionLayerIterator.hasNext()) { + polarisConfigCustomExtensionLayer = polarisConfigCustomExtensionLayerIterator.next(); + LOGGER.info("[SCT Config] PolarisConfigFileLocator init polarisConfigCustomExtensionLayer:{}", polarisConfigCustomExtensionLayer); + } + } - public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, - PolarisContextProperties polarisContextProperties, - ConfigFileService configFileService, - PolarisPropertySourceManager polarisPropertySourceManager, - Environment environment) { + public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) { this.polarisConfigProperties = polarisConfigProperties; this.polarisContextProperties = polarisContextProperties; this.configFileService = configFileService; @@ -83,9 +93,10 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { @Override public PropertySource locate(Environment environment) { - CompositePropertySource compositePropertySource = new CompositePropertySource( - POLARIS_CONFIG_PROPERTY_SOURCE_NAME); + CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME); + // load custom config extension files + initCustomPolarisConfigExtensionFiles(compositePropertySource); // load spring boot default config files initInternalConfigFiles(compositePropertySource); @@ -99,12 +110,21 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { return compositePropertySource; } + private void initCustomPolarisConfigExtensionFiles(CompositePropertySource compositePropertySource) { + if (polarisConfigCustomExtensionLayer == null) { + LOGGER.info("[SCT Config] PolarisAdaptorTsfConfigExtensionLayer is not init, ignore the following execution steps"); + return; + } + String namespace = polarisContextProperties.getNamespace(); + polarisConfigCustomExtensionLayer.execute(namespace, environment, compositePropertySource, polarisPropertySourceManager, configFileService); + LOGGER.info("[SCT Config] InitCustomPolarisConfigExtensionFiles finished, namespace:{}", namespace); + } + private void initInternalConfigFiles(CompositePropertySource compositePropertySource) { List internalConfigFiles = getInternalConfigFiles(); for (ConfigFileMetadata configFile : internalConfigFiles) { - PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( - configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); + PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); compositePropertySource.addPropertySource(polarisPropertySource); @@ -141,8 +161,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { return internalConfigFiles; } - private void buildInternalApplicationConfigFiles( - List internalConfigFiles, String namespace, String serviceName, List profileList) { + private void buildInternalApplicationConfigFiles(List internalConfigFiles, String namespace, String serviceName, List profileList) { for (String profile : profileList) { if (!StringUtils.hasText(profile)) { continue; @@ -157,8 +176,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yaml")); } - private void buildInternalBootstrapConfigFiles( - List internalConfigFiles, String namespace, String serviceName, List profileList) { + private void buildInternalBootstrapConfigFiles(List internalConfigFiles, String namespace, String serviceName, List profileList) { for (String profile : profileList) { if (!StringUtils.hasText(profile)) { continue; @@ -173,8 +191,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yaml")); } - private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, - List configFileGroups) { + private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, List configFileGroups) { String namespace = polarisContextProperties.getNamespace(); for (ConfigFileGroup configFileGroup : configFileGroups) { @@ -196,14 +213,12 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { polarisPropertySourceManager.addPropertySource(polarisPropertySource); - LOGGER.info("[SCT Config] Load and inject polaris config file success." - + " namespace = {}, group = {}, fileName = {}", namespace, group, fileName); + LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); } } } - private PolarisPropertySource loadPolarisPropertySource(String namespace, - String group, String fileName) { + private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName) { ConfigKVFile configKVFile; // unknown extension is resolved as properties file if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) { @@ -213,11 +228,9 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); } else { - LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", - namespace, group, fileName); + LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); - throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" - + " can be injected into the spring context"); + throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context"); } Map map = new ConcurrentHashMap<>(); 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 95c3798de..30e256dca 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 @@ -65,11 +65,11 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList private static final String REFRESH_CONTEXT_REFRESHER_BEAN_NAME = "polarisRefreshContextPropertySourceAutoRefresher"; + @Override public void onApplicationEvent(@NonNull ContextRefreshedEvent event) { ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) event.getApplicationContext(); - PolarisConfigRefreshScopeAnnotationDetector detector = applicationContext - .getBean(PolarisConfigRefreshScopeAnnotationDetector.class); + PolarisConfigRefreshScopeAnnotationDetector detector = applicationContext.getBean(PolarisConfigRefreshScopeAnnotationDetector.class); boolean isRefreshScopeAnnotationUsed = detector.isRefreshScopeAnnotationUsed(); String annotatedRefreshScopeBeanName = detector.getAnnotatedRefreshScopeBeanName(); // using System.setProperty to set spring.cloud.polaris.config.refresh-type @@ -78,12 +78,10 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList // a bean is using @RefreshScope, but the config refresh type is still [reflect], switch automatically if (isRefreshScopeAnnotationUsed || isSystemSetRefreshType) { if (isRefreshScopeAnnotationUsed) { - LOGGER.warn("Detected that the bean [{}] is using @RefreshScope annotation, but the config refresh type is still [reflect]. " - + "[SCT] will automatically switch to [refresh_context].", annotatedRefreshScopeBeanName); + LOGGER.warn("Detected that the bean [{}] is using @RefreshScope annotation, but the config refresh type is still [reflect]. " + "[SCT] will automatically switch to [refresh_context].", annotatedRefreshScopeBeanName); } if (isSystemSetRefreshType) { - LOGGER.warn("Detected that using System.setProperty to set spring.cloud.polaris.config.refresh-type = refresh_context, but the config refresh type is still [reflect]. " - + "[SCT] will automatically switch to [refresh_context]."); + LOGGER.warn("Detected that using System.setProperty to set spring.cloud.polaris.config.refresh-type = refresh_context, but the config refresh type is still [reflect]. " + "[SCT] will automatically switch to [refresh_context]."); } switchConfigRefreshTypeProperty(applicationContext); modifyPolarisConfigPropertiesBean(applicationContext); @@ -98,8 +96,7 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList private void switchConfigRefreshTypeProperty(ConfigurableApplicationContext applicationContext) { MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources(); - propertySources.addFirst(new MapPropertySource(CONFIG_REFRESH_TYPE_PROPERTY, - Collections.singletonMap(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.REFRESH_CONTEXT))); + propertySources.addFirst(new MapPropertySource(CONFIG_REFRESH_TYPE_PROPERTY, Collections.singletonMap(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.REFRESH_CONTEXT))); } private void modifyPolarisConfigPropertiesBean(ConfigurableApplicationContext applicationContext) { @@ -127,9 +124,9 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList beanFactory.registerBeanDefinition(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, beanDefinition); } + private void addRefresherBeanAsListener(ConfigurableApplicationContext applicationContext) { - PolarisRefreshEntireContextRefresher refresher = (PolarisRefreshEntireContextRefresher) applicationContext - .getBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME); + PolarisRefreshEntireContextRefresher refresher = (PolarisRefreshEntireContextRefresher) applicationContext.getBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME); applicationContext.addApplicationListener(refresher); } } diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 4b6fd9fa2..e49d7c556 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -73,7 +73,7 @@ 1.12.0-Hoxton.SR12-SNAPSHOT - 1.13.0 + 1.14.0-SNAPSHOT 32.0.1-jre 1.2.11 4.5.1