增加自定义扩展配置层接口,调整部分代码的checkstyle格式

pull/1095/head
yangjuanying 2 years ago
parent f807ce1053
commit f7720f5942

@ -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);
}

@ -20,9 +20,11 @@ package com.tencent.cloud.polaris.config.adapter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
@ -68,12 +70,20 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
private final PolarisPropertySourceManager polarisPropertySourceManager; private final PolarisPropertySourceManager polarisPropertySourceManager;
private final Environment environment; private final Environment environment;
// 此类给一些客户定制化逻辑做一些特殊业务分组文件的配置处理
private PolarisConfigCustomExtensionLayer polarisConfigCustomExtensionLayer;
{
ServiceLoader<PolarisConfigCustomExtensionLayer> polarisConfigCustomExtensionLayerLoader = ServiceLoader.load(PolarisConfigCustomExtensionLayer.class);
Iterator<PolarisConfigCustomExtensionLayer> polarisConfigCustomExtensionLayerIterator = polarisConfigCustomExtensionLayerLoader.iterator();
// 一般就一个实现类,如果有多个,那么加载的是最后一个
while (polarisConfigCustomExtensionLayerIterator.hasNext()) {
polarisConfigCustomExtensionLayer = polarisConfigCustomExtensionLayerIterator.next();
LOGGER.info("[SCT Config] PolarisConfigFileLocator init polarisConfigCustomExtensionLayer:{}", polarisConfigCustomExtensionLayer);
}
}
public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) {
PolarisContextProperties polarisContextProperties,
ConfigFileService configFileService,
PolarisPropertySourceManager polarisPropertySourceManager,
Environment environment) {
this.polarisConfigProperties = polarisConfigProperties; this.polarisConfigProperties = polarisConfigProperties;
this.polarisContextProperties = polarisContextProperties; this.polarisContextProperties = polarisContextProperties;
this.configFileService = configFileService; this.configFileService = configFileService;
@ -83,9 +93,10 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
@Override @Override
public PropertySource<?> locate(Environment environment) { public PropertySource<?> locate(Environment environment) {
CompositePropertySource compositePropertySource = new CompositePropertySource( CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME);
POLARIS_CONFIG_PROPERTY_SOURCE_NAME);
// load custom config extension files
initCustomPolarisConfigExtensionFiles(compositePropertySource);
// load spring boot default config files // load spring boot default config files
initInternalConfigFiles(compositePropertySource); initInternalConfigFiles(compositePropertySource);
@ -99,12 +110,21 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
return compositePropertySource; 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) { private void initInternalConfigFiles(CompositePropertySource compositePropertySource) {
List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles(); List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles();
for (ConfigFileMetadata configFile : internalConfigFiles) { for (ConfigFileMetadata configFile : internalConfigFiles) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName());
configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName());
compositePropertySource.addPropertySource(polarisPropertySource); compositePropertySource.addPropertySource(polarisPropertySource);
@ -141,8 +161,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
return internalConfigFiles; return internalConfigFiles;
} }
private void buildInternalApplicationConfigFiles( private void buildInternalApplicationConfigFiles(List<ConfigFileMetadata> internalConfigFiles, String namespace, String serviceName, List<String> profileList) {
List<ConfigFileMetadata> internalConfigFiles, String namespace, String serviceName, List<String> profileList) {
for (String profile : profileList) { for (String profile : profileList) {
if (!StringUtils.hasText(profile)) { if (!StringUtils.hasText(profile)) {
continue; continue;
@ -157,8 +176,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yaml")); internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yaml"));
} }
private void buildInternalBootstrapConfigFiles( private void buildInternalBootstrapConfigFiles(List<ConfigFileMetadata> internalConfigFiles, String namespace, String serviceName, List<String> profileList) {
List<ConfigFileMetadata> internalConfigFiles, String namespace, String serviceName, List<String> profileList) {
for (String profile : profileList) { for (String profile : profileList) {
if (!StringUtils.hasText(profile)) { if (!StringUtils.hasText(profile)) {
continue; continue;
@ -173,8 +191,7 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yaml")); internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yaml"));
} }
private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, List<ConfigFileGroup> configFileGroups) {
List<ConfigFileGroup> configFileGroups) {
String namespace = polarisContextProperties.getNamespace(); String namespace = polarisContextProperties.getNamespace();
for (ConfigFileGroup configFileGroup : configFileGroups) { for (ConfigFileGroup configFileGroup : configFileGroups) {
@ -196,14 +213,12 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
polarisPropertySourceManager.addPropertySource(polarisPropertySource); polarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info("[SCT Config] Load and inject polaris config file success." LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);
+ " namespace = {}, group = {}, fileName = {}", namespace, group, fileName);
} }
} }
} }
private PolarisPropertySource loadPolarisPropertySource(String namespace, private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName) {
String group, String fileName) {
ConfigKVFile configKVFile; ConfigKVFile configKVFile;
// unknown extension is resolved as properties file // unknown extension is resolved as properties file
if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) { if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
@ -213,11 +228,9 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
} }
else { else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);
namespace, group, fileName);
throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context");
+ " can be injected into the spring context");
} }
Map<String, Object> map = new ConcurrentHashMap<>(); Map<String, Object> map = new ConcurrentHashMap<>();

@ -65,11 +65,11 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList
private static final String REFRESH_CONTEXT_REFRESHER_BEAN_NAME = "polarisRefreshContextPropertySourceAutoRefresher"; private static final String REFRESH_CONTEXT_REFRESHER_BEAN_NAME = "polarisRefreshContextPropertySourceAutoRefresher";
@Override @Override
public void onApplicationEvent(@NonNull ContextRefreshedEvent event) { public void onApplicationEvent(@NonNull ContextRefreshedEvent event) {
ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) event.getApplicationContext(); ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) event.getApplicationContext();
PolarisConfigRefreshScopeAnnotationDetector detector = applicationContext PolarisConfigRefreshScopeAnnotationDetector detector = applicationContext.getBean(PolarisConfigRefreshScopeAnnotationDetector.class);
.getBean(PolarisConfigRefreshScopeAnnotationDetector.class);
boolean isRefreshScopeAnnotationUsed = detector.isRefreshScopeAnnotationUsed(); boolean isRefreshScopeAnnotationUsed = detector.isRefreshScopeAnnotationUsed();
String annotatedRefreshScopeBeanName = detector.getAnnotatedRefreshScopeBeanName(); String annotatedRefreshScopeBeanName = detector.getAnnotatedRefreshScopeBeanName();
// using System.setProperty to set spring.cloud.polaris.config.refresh-type // 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 // a bean is using @RefreshScope, but the config refresh type is still [reflect], switch automatically
if (isRefreshScopeAnnotationUsed || isSystemSetRefreshType) { if (isRefreshScopeAnnotationUsed || isSystemSetRefreshType) {
if (isRefreshScopeAnnotationUsed) { if (isRefreshScopeAnnotationUsed) {
LOGGER.warn("Detected that the bean [{}] is using @RefreshScope annotation, but the config refresh type is still [reflect]. " 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);
+ "[SCT] will automatically switch to [refresh_context].", annotatedRefreshScopeBeanName);
} }
if (isSystemSetRefreshType) { 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]. " 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].");
+ "[SCT] will automatically switch to [refresh_context].");
} }
switchConfigRefreshTypeProperty(applicationContext); switchConfigRefreshTypeProperty(applicationContext);
modifyPolarisConfigPropertiesBean(applicationContext); modifyPolarisConfigPropertiesBean(applicationContext);
@ -98,8 +96,7 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList
private void switchConfigRefreshTypeProperty(ConfigurableApplicationContext applicationContext) { private void switchConfigRefreshTypeProperty(ConfigurableApplicationContext applicationContext) {
MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources(); MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
propertySources.addFirst(new MapPropertySource(CONFIG_REFRESH_TYPE_PROPERTY, propertySources.addFirst(new MapPropertySource(CONFIG_REFRESH_TYPE_PROPERTY, Collections.singletonMap(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.REFRESH_CONTEXT)));
Collections.singletonMap(POLARIS_CONFIG_REFRESH_TYPE, RefreshType.REFRESH_CONTEXT)));
} }
private void modifyPolarisConfigPropertiesBean(ConfigurableApplicationContext applicationContext) { private void modifyPolarisConfigPropertiesBean(ConfigurableApplicationContext applicationContext) {
@ -127,9 +124,9 @@ public class PolarisConfigRefreshOptimizationListener implements ApplicationList
beanFactory.registerBeanDefinition(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, beanDefinition); beanFactory.registerBeanDefinition(REFRESH_CONTEXT_REFRESHER_BEAN_NAME, beanDefinition);
} }
private void addRefresherBeanAsListener(ConfigurableApplicationContext applicationContext) { private void addRefresherBeanAsListener(ConfigurableApplicationContext applicationContext) {
PolarisRefreshEntireContextRefresher refresher = (PolarisRefreshEntireContextRefresher) applicationContext PolarisRefreshEntireContextRefresher refresher = (PolarisRefreshEntireContextRefresher) applicationContext.getBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME);
.getBean(REFRESH_CONTEXT_REFRESHER_BEAN_NAME);
applicationContext.addApplicationListener(refresher); applicationContext.addApplicationListener(refresher);
} }
} }

@ -73,7 +73,7 @@
<revision>1.12.0-Hoxton.SR12-SNAPSHOT</revision> <revision>1.12.0-Hoxton.SR12-SNAPSHOT</revision>
<!-- Dependencies --> <!-- Dependencies -->
<polaris.version>1.13.0</polaris.version> <polaris.version>1.14.0-SNAPSHOT</polaris.version>
<guava.version>32.0.1-jre</guava.version> <guava.version>32.0.1-jre</guava.version>
<logback.version>1.2.11</logback.version> <logback.version>1.2.11</logback.version>
<mocktio.version>4.5.1</mocktio.version> <mocktio.version>4.5.1</mocktio.version>

Loading…
Cancel
Save