From 0007b4a95ba6f3abfdd4c2a3b99865b071a9760c Mon Sep 17 00:00:00 2001 From: wulingxiao <1251605638@qqcom> Date: Sun, 10 Jul 2022 12:18:22 +0800 Subject: [PATCH] configdata --- .../adapter/PolarisConfigFilePuller.java | 174 ++++++++++++++++++ .../configdata/PolarisConfigDataLoader.java | 158 ++-------------- .../PolarisConfigDataLocationResolver.java | 4 +- .../src/main/resources/application.yml | 2 +- 4 files changed, 194 insertions(+), 144 deletions(-) create mode 100644 spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java 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 new file mode 100644 index 000000000..8c145a888 --- /dev/null +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePuller.java @@ -0,0 +1,174 @@ +package com.tencent.cloud.polaris.config.adapter; + +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import com.tencent.cloud.polaris.config.config.ConfigFileGroup; +import com.tencent.cloud.polaris.config.enums.ConfigFileFormat; +import com.tencent.cloud.polaris.context.config.PolarisContextProperties; +import com.tencent.polaris.configuration.api.core.ConfigFileMetadata; +import com.tencent.polaris.configuration.api.core.ConfigFileService; +import com.tencent.polaris.configuration.api.core.ConfigKVFile; +import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + + +/** + * Pull configFile from Polaris + * + * @author wlx + */ +public class PolarisConfigFilePuller { + + private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigFileLocator.class); + + private final PolarisContextProperties polarisContextProperties; + + private final ConfigFileService configFileService; + + private final PolarisPropertySourceManager polarisPropertySourceManager; + + private Environment environment; + + public PolarisConfigFilePuller(PolarisContextProperties polarisContextProperties, + ConfigFileService configFileService, + PolarisPropertySourceManager polarisPropertySourceManager, + Environment environment) { + this.polarisContextProperties = polarisContextProperties; + this.configFileService = configFileService; + this.polarisPropertySourceManager = polarisPropertySourceManager; + this.environment = environment; + } + + public PolarisConfigFilePuller(PolarisContextProperties polarisContextProperties, + ConfigFileService configFileService, + PolarisPropertySourceManager polarisPropertySourceManager) { + this.polarisContextProperties = polarisContextProperties; + this.configFileService = configFileService; + this.polarisPropertySourceManager = polarisPropertySourceManager; + } + + public void initInternalConfigFiles(CompositePropertySource compositePropertySource) { + List internalConfigFiles = getInternalConfigFiles(); + + for (ConfigFileMetadata configFile : internalConfigFiles) { + PolarisPropertySource polarisPropertySource = loadPolarisPropertySource( + configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); + + compositePropertySource.addPropertySource(polarisPropertySource); + + polarisPropertySourceManager.addPropertySource(polarisPropertySource); + + LOGGER.info("[SCT Config] Load and inject polaris config file. file = {}", configFile); + } + } + + public List getInternalConfigFiles() { + String namespace = polarisContextProperties.getNamespace(); + String serviceName = polarisContextProperties.getService(); + if (!StringUtils.hasText(serviceName)) { + serviceName = environment.getProperty("spring.application.name"); + } + + List internalConfigFiles = new LinkedList<>(); + + // priority: application-${profile} > application > boostrap-${profile} > boostrap + String[] activeProfiles = environment.getActiveProfiles(); + + for (String activeProfile : activeProfiles) { + if (!StringUtils.hasText(activeProfile)) { + continue; + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".yml")); + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml")); + + for (String activeProfile : activeProfiles) { + if (!StringUtils.hasText(activeProfile)) { + continue; + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".yml")); + } + + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties")); + internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml")); + + + return internalConfigFiles; + } + + + public void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, + List configFileGroups) { + configFileGroups.forEach( + configFileGroup -> initCustomPolarisConfigFile(compositePropertySource, configFileGroup) + ); + } + + public void initCustomPolarisConfigFile(CompositePropertySource compositePropertySource, + ConfigFileGroup configFileGroup) { + String namespace = polarisContextProperties.getNamespace(); + + String group = configFileGroup.getName(); + + if (!StringUtils.hasText(group)) { + throw new IllegalArgumentException("polaris config group name cannot be empty."); + } + + List files = configFileGroup.getFiles(); + if (CollectionUtils.isEmpty(files)) { + return; + } + + for (String fileName : files) { + PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(namespace, group, fileName); + + compositePropertySource.addPropertySource(polarisPropertySource); + + polarisPropertySourceManager.addPropertySource(polarisPropertySource); + + 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) { + ConfigKVFile configKVFile; + // unknown extension is resolved as properties file + if (ConfigFileFormat.isPropertyFile(fileName) + || ConfigFileFormat.isUnknownFile(fileName)) { + configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName); + } else if (ConfigFileFormat.isYamlFile(fileName)) { + configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); + } else { + 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"); + } + + Map map = new ConcurrentHashMap<>(); + for (String key : configKVFile.getPropertyNames()) { + map.put(key, configKVFile.getProperty(key, null)); + } + return new PolarisPropertySource(namespace, group, fileName, configKVFile, map); + } + +} 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 0f289ed6b..83a2bd602 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 @@ -1,5 +1,6 @@ package com.tencent.cloud.polaris.config.configdata; +import com.tencent.cloud.polaris.config.adapter.PolarisConfigFilePuller; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource; import com.tencent.cloud.polaris.config.adapter.PolarisPropertySourceManager; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; @@ -63,22 +64,16 @@ public class PolarisConfigDataLoader implements ConfigDataLoader configFileGroups = polarisConfigProperties.getGroups(); - if (CollectionUtils.isEmpty(configFileGroups)) { - return compositePropertySource; - } - initCustomPolarisConfigFiles(compositePropertySource, configFileGroups, bootstrapContext); - + SDKContext sdkContext = bootstrapContext.get(SDKContext.class); + ConfigFileService configFileService = ConfigFileServiceFactory.createConfigFileService(sdkContext); + PolarisConfigFilePuller puller = new PolarisConfigFilePuller(resource.getPolarisContextProperties() + ,configFileService, + bootstrapContext.get(PolarisPropertySourceManager.class)); + puller.initCustomPolarisConfigFile(compositePropertySource, configFileGroup(resource)); return compositePropertySource; } @@ -89,136 +84,19 @@ public class PolarisConfigDataLoader implements ConfigDataLoader internalConfigFiles = getInternalConfigFiles(bootstrapContext,resource); - - for (ConfigFileMetadata configFile : internalConfigFiles) { - PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(bootstrapContext, - configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName()); - - compositePropertySource.addPropertySource(polarisPropertySource); - - polarisPropertySourceManager.addPropertySource(polarisPropertySource); - - log.info("[SCT Config] Load and inject polaris config file. file = " + configFile); - } + private ConfigFileGroup configFileGroup(PolarisConfigDataResource polarisConfigDataResource) { + String fileName = polarisConfigDataResource.getFileName(); + String serviceName = polarisConfigDataResource.getServiceName(); + ConfigFileGroup configFileGroup = new ConfigFileGroup(); + configFileGroup.setName(serviceName); + List flies = new ArrayList<>(); + flies.add(fileName); + configFileGroup.setFiles(flies); + return configFileGroup; } - private List getInternalConfigFiles(ConfigurableBootstrapContext bootstrapContext, - PolarisConfigDataResource resource) { - PolarisContextProperties polarisContextProperties = bootstrapContext.get(PolarisContextProperties.class); - String namespace = polarisContextProperties.getNamespace(); - String serviceName = polarisContextProperties.getService(); - if (!StringUtils.hasText(serviceName)) { - serviceName = resource.getServiceName(); - } - - List internalConfigFiles = new LinkedList<>(); - - // priority: application-${profile} > application > boostrap-${profile} > boostrap - List activeProfiles = resource.getProfiles().getActive(); - - for (String activeProfile : activeProfiles) { - if (!StringUtils.hasText(activeProfile)) { - continue; - } - - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + activeProfile + ".yml")); - } - - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml")); - - for (String activeProfile : activeProfiles) { - if (!StringUtils.hasText(activeProfile)) { - continue; - } - - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + activeProfile + ".yml")); - } - - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties")); - internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml")); - - - return internalConfigFiles; - } - - - private void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, - List configFileGroups, - ConfigurableBootstrapContext bootstrapContext - ) { - PolarisContextProperties polarisContextProperties = bootstrapContext.get(PolarisContextProperties.class); - String namespace = polarisContextProperties.getNamespace(); - PolarisPropertySourceManager polarisPropertySourceManager = - bootstrapContext.get(PolarisPropertySourceManager.class); - for (ConfigFileGroup configFileGroup : configFileGroups) { - String group = configFileGroup.getName(); - - if (!StringUtils.hasText(group)) { - throw new IllegalArgumentException("polaris config group name cannot be empty."); - } - - List files = configFileGroup.getFiles(); - if (CollectionUtils.isEmpty(files)) { - return; - } - - for (String fileName : files) { - PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(bootstrapContext, - namespace, group, fileName); - - compositePropertySource.addPropertySource(polarisPropertySource); - - polarisPropertySourceManager.addPropertySource(polarisPropertySource); - - String loggerFormat = "[SCT Config] Load and inject polaris config file success. " + - "namespace = %s, group = %s, fileName = %s"; - log.info(String.format(loggerFormat,namespace, group, fileName)); - } - } - } - - private PolarisPropertySource loadPolarisPropertySource( - ConfigurableBootstrapContext bootstrapContext, - String namespace, String group, String fileName) { - - SDKContext sdkContext = bootstrapContext.get(SDKContext.class); - - ConfigKVFile configKVFile; - ConfigFileService configFileService = ConfigFileServiceFactory.createConfigFileService(sdkContext); - - // unknown extension is resolved as properties file - if (ConfigFileFormat.isPropertyFile(fileName) - || ConfigFileFormat.isUnknownFile(fileName)) { - configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName); - } - else if (ConfigFileFormat.isYamlFile(fileName)) { - configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); - } - else { - String loggerFormat = "[SCT Config] Unsupported config file. namespace = %s, group = %s, fileName = %s"; - log.warn(String.format(loggerFormat,namespace, group, fileName) ); - throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" - + " can be injected into the spring context"); - } - - Map map = new ConcurrentHashMap<>(); - for (String key : configKVFile.getPropertyNames()) { - map.put(key, configKVFile.getProperty(key, null)); - } - - return new PolarisPropertySource(namespace, group, fileName, configKVFile, map); - } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java index 53598f738..a933c37ba 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java @@ -115,10 +115,8 @@ public class PolarisConfigDataLocationResolver implements // stop sdkContext and register PolarisPropertySourceManager to context bootstrapContext.addCloseListener( event -> { - event.getApplicationContext().getBeanFactory().registerSingleton( - "sdkContext", event.getBootstrapContext().get(SDKContext.class) + event.getBootstrapContext().get(SDKContext.class).destroy(); - ); event.getApplicationContext().getBeanFactory().registerSingleton( "polarisPropertySourceManager", event.getBootstrapContext().get(PolarisPropertySourceManager.class) diff --git a/spring-cloud-tencent-examples/polaris-config-data-example/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-config-data-example/src/main/resources/application.yml index 6951d94d4..c0184db9f 100644 --- a/spring-cloud-tencent-examples/polaris-config-data-example/src/main/resources/application.yml +++ b/spring-cloud-tencent-examples/polaris-config-data-example/src/main/resources/application.yml @@ -14,7 +14,7 @@ spring: files: [ "config/application.properties", "config/bootstrap.yml" ] config: import: - - optional:polaris:test.yml + - optional:polaris:application.properties management: endpoints: web: