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 e0d7b3eff..53a3c7fd1 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 @@ -47,12 +47,15 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { private final PolarisConfigFilePuller polarisConfigFilePuller; + private final Environment environment; + public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) { this.polarisConfigProperties = polarisConfigProperties; + this.environment = environment; this.polarisConfigFilePuller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, - polarisPropertySourceManager, environment); + polarisPropertySourceManager); } @Override @@ -61,7 +64,9 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { POLARIS_CONFIG_PROPERTY_SOURCE_NAME); // load spring boot default config files - polarisConfigFilePuller.initInternalConfigFiles(compositePropertySource); + String[] activeProfiles = environment.getActiveProfiles(); + String serviceName = environment.getProperty("spring.application.name"); + polarisConfigFilePuller.initInternalConfigFiles(compositePropertySource,activeProfiles,serviceName); // load custom config files List configFileGroups = polarisConfigProperties.getGroups(); 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 bc9c1627c..bff11cc85 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 @@ -1 +1 @@ -/* * 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.config.adapter; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import com.google.common.collect.Lists; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader; 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.boot.context.config.Profiles; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.Environment; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** * PolarisConfigFilePuller pull configFile from Polaris. * * @author wlx */ public final class PolarisConfigFilePuller { private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigFileLocator.class); private PolarisContextProperties polarisContextProperties; private ConfigFileService configFileService; private PolarisPropertySourceManager polarisPropertySourceManager; private Environment environment; private PolarisConfigFilePuller() { } /** * InitInternalConfigFiles for {@link PolarisConfigFileLocator}. * * @param compositePropertySource compositePropertySource */ 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); } } /** * InitInternalConfigFiles for {@link PolarisConfigDataLoader}. * * @param compositePropertySource compositePropertySource * @param profiles profiles * @param serviceName serviceName */ public void initInternalConfigFiles(CompositePropertySource compositePropertySource, Profiles profiles, String serviceName) { List internalConfigFiles = getInternalConfigFiles(profiles, serviceName); 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); } } /** * Init multiple CustomPolarisConfigFile. * * @param compositePropertySource compositePropertySource * @param configFileGroups configFileGroups */ public void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, List configFileGroups) { configFileGroups.forEach( configFileGroup -> initCustomPolarisConfigFile(compositePropertySource, configFileGroup) ); } /** * Init single CustomPolarisConfigFile. * * @param compositePropertySource compositePropertySource * @param configFileGroup 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); } private List getInternalConfigFiles() { String namespace = polarisContextProperties.getNamespace(); String serviceName = polarisContextProperties.getService(); if (Objects.isNull(this.environment)) { return Lists.newArrayList(); } if (!StringUtils.hasText(serviceName)) { serviceName = environment.getProperty("spring.application.name"); } // priority: application-${profile} > application > boostrap-${profile} > boostrap String[] activeProfiles = environment.getActiveProfiles(); return getInternalConfigFiles(activeProfiles, namespace, serviceName); } private List getInternalConfigFiles(Profiles profiles, String serviceName) { String namespace = polarisContextProperties.getNamespace(); if (StringUtils.hasText(polarisContextProperties.getService())) { serviceName = polarisContextProperties.getService(); } // priority: application-${profile} > application > boostrap-${profile} > boostrap List active = profiles.getActive(); String[] activeProfiles = active.toArray(new String[]{}); return getInternalConfigFiles(activeProfiles, namespace, serviceName); } private List getInternalConfigFiles(String[] activeProfiles, String namespace, String serviceName) { List internalConfigFiles = new LinkedList<>(); 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; } /** * Factory method to create PolarisConfigFilePuller for * {@link PolarisConfigFileLocator}. * * @param polarisContextProperties polarisContextProperties * @param configFileService configFileService * @param polarisPropertySourceManager polarisPropertySourceManager * @param environment environment * @return PolarisConfigFilePuller instance */ public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisPropertySourceManager polarisPropertySourceManager, Environment environment) { PolarisConfigFilePuller puller = new PolarisConfigFilePuller(); puller.polarisContextProperties = polarisContextProperties; puller.configFileService = configFileService; puller.polarisPropertySourceManager = polarisPropertySourceManager; puller.environment = environment; return puller; } /** * Factory method to create PolarisConfigFilePuller for * {@link PolarisConfigDataLoader}. * * @param polarisContextProperties polarisContextProperties * @param configFileService configFileService * @param polarisPropertySourceManager polarisPropertySourceManager * @return PolarisConfigFilePuller instance */ public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisPropertySourceManager polarisPropertySourceManager) { PolarisConfigFilePuller puller = new PolarisConfigFilePuller(); puller.polarisContextProperties = polarisContextProperties; puller.configFileService = configFileService; puller.polarisPropertySourceManager = polarisPropertySourceManager; return puller; } } \ No newline at end of file +/* * 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.config.adapter; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import com.google.common.collect.Lists; import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader; 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.boot.context.config.Profiles; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.Environment; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** * PolarisConfigFilePuller pull configFile from Polaris. * * @author wlx */ public final class PolarisConfigFilePuller { private static final Logger LOGGER = LoggerFactory.getLogger(PolarisConfigFileLocator.class); private PolarisContextProperties polarisContextProperties; private ConfigFileService configFileService; private PolarisPropertySourceManager polarisPropertySourceManager; private PolarisConfigFilePuller() { } /** * InitInternalConfigFiles for {@link PolarisConfigDataLoader}. * * @param compositePropertySource compositePropertySource * @param activeProfiles activeProfiles * @param serviceName serviceName */ public void initInternalConfigFiles(CompositePropertySource compositePropertySource, String[] activeProfiles, String serviceName) { List internalConfigFiles = getInternalConfigFiles(activeProfiles, serviceName); 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); } } /** * Init multiple CustomPolarisConfigFile. * * @param compositePropertySource compositePropertySource * @param configFileGroups configFileGroups */ public void initCustomPolarisConfigFiles(CompositePropertySource compositePropertySource, List configFileGroups) { configFileGroups.forEach( configFileGroup -> initCustomPolarisConfigFile(compositePropertySource, configFileGroup) ); } /** * Init single CustomPolarisConfigFile. * * @param compositePropertySource compositePropertySource * @param configFileGroup 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); } private List getInternalConfigFiles(String[] activeProfiles, String serviceName) { String namespace = polarisContextProperties.getNamespace(); if (StringUtils.hasText(polarisContextProperties.getService())) { serviceName = polarisContextProperties.getService(); } // priority: application-${profile} > application > boostrap-${profile} > boostrap return getInternalConfigFiles(activeProfiles, namespace, serviceName); } private List getInternalConfigFiles(String[] activeProfiles, String namespace, String serviceName) { List internalConfigFiles = new LinkedList<>(); 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; } /** * Factory method to create PolarisConfigFilePuller for * {@link PolarisConfigDataLoader},{@link PolarisConfigFileLocator}. * * @param polarisContextProperties polarisContextProperties * @param configFileService configFileService * @param polarisPropertySourceManager polarisPropertySourceManager * @return PolarisConfigFilePuller instance */ public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, PolarisPropertySourceManager polarisPropertySourceManager) { PolarisConfigFilePuller puller = new PolarisConfigFilePuller(); puller.polarisContextProperties = polarisContextProperties; puller.configFileService = configFileService; puller.polarisPropertySourceManager = polarisPropertySourceManager; return puller; } } \ No newline at end of file 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 f9477c7d9..7dc4f9e9f 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 @@ -90,9 +90,7 @@ public class PolarisConfigDataLoader implements ConfigDataLoader> propertySources = new ArrayList<>(); - propertySources.add(compositePropertySource); - return new ConfigData(propertySources, getOptions(resource)); + return new ConfigData(compositePropertySource.getPropertySources(), getOptions(resource)); } private CompositePropertySource locate(ConfigurableBootstrapContext bootstrapContext, @@ -110,7 +108,9 @@ public class PolarisConfigDataLoader implements ConfigDataLoader profilesActive = profiles.getActive(); + String[] activeProfiles = profilesActive.toArray(new String[]{}); + this.puller.initInternalConfigFiles(compositePropertySource, activeProfiles, resource.getServiceName()); } PolarisConfigProperties polarisConfigProperties = resource.getPolarisConfigProperties(); 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 81ec971ff..e41b645e8 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 @@ -252,30 +252,10 @@ public class PolarisConfigDataLocationResolver implements private SDKContext sdkContext(ConfigDataLocationResolverContext resolverContext, PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties) { - - // 1. Read user-defined polaris.yml configuration - ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory - .defaultConfig(ConfigProvider.DEFAULT_CONFIG); - - // 2. Override user-defined polaris.yml configuration with SCT configuration - - String defaultHost = polarisContextProperties.getLocalIpAddress(); - if (StringUtils.isBlank(defaultHost)) { - defaultHost = loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address"); - } - - configuration.getGlobal().getAPI().setBindIP(defaultHost); - - Collection modifiers = modifierList(polarisConfigProperties, polarisContextProperties); - modifiers = modifiers.stream() - .sorted(Comparator.comparingInt(PolarisConfigModifier::getOrder)) - .collect(Collectors.toList()); - if (!CollectionUtils.isEmpty(modifiers)) { - for (PolarisConfigModifier modifier : modifiers) { - modifier.modify(configuration); - } - } - return SDKContext.initContextByConfig(configuration); + List modifierList = modifierList(polarisConfigProperties, polarisContextProperties); + return SDKContext.initContextByConfig(polarisContextProperties.configuration(modifierList, () -> { + return loadPolarisConfigProperties(resolverContext, String.class, "spring.cloud.client.ip-address"); + })); } private List modifierList(PolarisConfigProperties polarisConfigProperties, diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFilePullerTest.java index 2dcaa412d..06f9367aa 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 @@ -20,7 +20,6 @@ import org.mockito.junit.MockitoJUnitRunner; import org.springframework.boot.context.config.Profiles; import org.springframework.core.env.CompositePropertySource; -import org.springframework.core.env.Environment; import static org.mockito.Mockito.when; @@ -32,153 +31,19 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PolarisConfigFilePullerTest { - @Mock - private PolarisConfigProperties polarisConfigProperties; @Mock private PolarisContextProperties polarisContextProperties; @Mock private ConfigFileService configFileService; @Mock private PolarisPropertySourceManager polarisPropertySourceManager; - @Mock - private Environment environment; - @Mock - private Profiles profiles; private final String testNamespace = "testNamespace"; private final String testServiceName = "testServiceName"; private final String polarisConfigPropertySourceName = "polaris-config"; @Test - public void testBootstrapPullInternalConfigFiles() { - PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, - polarisPropertySourceManager, environment); - - when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); - when(polarisContextProperties.getService()).thenReturn(testServiceName); - - // application.properties - Map applicationProperties = new HashMap<>(); - applicationProperties.put("k1", "v1"); - applicationProperties.put("k2", "v2"); - applicationProperties.put("k3", "v3"); - ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties); - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")) - .thenReturn(propertiesFile); - - Map emptyMap = new HashMap<>(); - ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile); - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); - - when(polarisConfigProperties.getGroups()).thenReturn(null); - when(environment.getActiveProfiles()).thenReturn(new String[]{}); - - CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); - puller.initInternalConfigFiles(compositePropertySource); - - Assert.assertEquals("v1", compositePropertySource.getProperty("k1")); - Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); - Assert.assertEquals("v3", compositePropertySource.getProperty("k3")); - } - - @Test - public void testBootstrapPullInternalConfigFilesWithProfile() { - PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, - polarisPropertySourceManager, environment); - - when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); - when(polarisContextProperties.getService()).thenReturn(testServiceName); - - // application.properties - Map applicationProperties = new HashMap<>(); - applicationProperties.put("k1", "v1"); - applicationProperties.put("k2", "v2"); - applicationProperties.put("k3", "v3"); - ConfigKVFile propertiesFile = new MockedConfigKVFile(applicationProperties); - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")) - .thenReturn(propertiesFile); - - // application-dev.properties - Map devProperties = new HashMap<>(); - devProperties.put("k1", "v11"); - ConfigKVFile devFile = new MockedConfigKVFile(devProperties); - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application-dev.properties")) - .thenReturn(devFile); - - Map emptyMap = new HashMap<>(); - ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application-dev.yml")).thenReturn(emptyConfigFile); - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile); - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile); - - when(polarisConfigProperties.getGroups()).thenReturn(null); - when(environment.getActiveProfiles()).thenReturn(new String[]{"dev"}); - - CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); - puller.initInternalConfigFiles(compositePropertySource); - - Assert.assertEquals("v11", compositePropertySource.getProperty("k1")); - Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); - Assert.assertEquals("v3", compositePropertySource.getProperty("k3")); - } - - @Test - public void testBootstrapPullCustomConfigFilesWithProfile() { - PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, - polarisPropertySourceManager, environment); - - when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); - when(polarisContextProperties.getService()).thenReturn(testServiceName); - - Map emptyMap = new HashMap<>(); - ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap); - - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")).thenReturn(emptyConfigFile); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile); - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); - - List customFiles = new LinkedList<>(); - ConfigFileGroup configFileGroup = new ConfigFileGroup(); - String customGroup = "group1"; - configFileGroup.setName(customGroup); - String customFile1 = "file1.properties"; - String customFile2 = "file2.properties"; - configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2)); - customFiles.add(configFileGroup); - - when(polarisConfigProperties.getGroups()).thenReturn(customFiles); - when(environment.getActiveProfiles()).thenReturn(new String[]{}); - - // file1.properties - Map file1Map = new HashMap<>(); - file1Map.put("k1", "v1"); - file1Map.put("k2", "v2"); - ConfigKVFile file1 = new MockedConfigKVFile(file1Map); - when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile1)).thenReturn(file1); - - // file2.properties - Map file2Map = new HashMap<>(); - file2Map.put("k1", "v11"); - file2Map.put("k3", "v3"); - ConfigKVFile file2 = new MockedConfigKVFile(file2Map); - when(configFileService.getConfigPropertiesFile(testNamespace, customGroup, customFile2)).thenReturn(file2); - - CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); - puller.initCustomPolarisConfigFiles(compositePropertySource, customFiles); - - Assert.assertEquals("v1", compositePropertySource.getProperty("k1")); - Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); - Assert.assertEquals("v3", compositePropertySource.getProperty("k3")); - } - - @Test - public void testConfigDataPullInternalConfigFiles() { + public void testPullInternalConfigFiles() { PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, polarisPropertySourceManager); @@ -199,13 +64,9 @@ public class PolarisConfigFilePullerTest { when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); - - when(polarisConfigProperties.getGroups()).thenReturn(null); - - when(profiles.getActive()).thenReturn(Lists.newArrayList()); - CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); - puller.initInternalConfigFiles(compositePropertySource, profiles, testServiceName); + + puller.initInternalConfigFiles(compositePropertySource, new String[]{}, testServiceName); Assert.assertEquals("v1", compositePropertySource.getProperty("k1")); Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); @@ -213,7 +74,7 @@ public class PolarisConfigFilePullerTest { } @Test - public void testConfigDataPullInternalConfigFilesWithProfile() { + public void testPullInternalConfigFilesWithProfile() { PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, polarisPropertySourceManager); @@ -244,14 +105,11 @@ public class PolarisConfigFilePullerTest { when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap-dev.properties")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile); - - when(polarisConfigProperties.getGroups()).thenReturn(null); List active = new ArrayList<>(); active.add("dev"); - when(profiles.getActive()).thenReturn(active); - + String[] activeProfiles = active.toArray(new String[]{}); CompositePropertySource compositePropertySource = new CompositePropertySource(polarisConfigPropertySourceName); - puller.initInternalConfigFiles(compositePropertySource, profiles, testServiceName); + puller.initInternalConfigFiles(compositePropertySource, activeProfiles, testServiceName); Assert.assertEquals("v11", compositePropertySource.getProperty("k1")); Assert.assertEquals("v2", compositePropertySource.getProperty("k2")); @@ -259,20 +117,11 @@ public class PolarisConfigFilePullerTest { } @Test - public void testConfigDataPullCustomConfigFilesWithProfile() { + public void testPullCustomConfigFilesWithProfile() { PolarisConfigFilePuller puller = PolarisConfigFilePuller.get(polarisContextProperties, configFileService, polarisPropertySourceManager); when(polarisContextProperties.getNamespace()).thenReturn(testNamespace); - when(polarisContextProperties.getService()).thenReturn(testServiceName); - - Map emptyMap = new HashMap<>(); - ConfigKVFile emptyConfigFile = new MockedConfigKVFile(emptyMap); - - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "application.properties")).thenReturn(emptyConfigFile); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "application.yml")).thenReturn(emptyConfigFile); - when(configFileService.getConfigPropertiesFile(testNamespace, testServiceName, "bootstrap.properties")).thenReturn(emptyConfigFile); - when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); List customFiles = new LinkedList<>(); ConfigFileGroup configFileGroup = new ConfigFileGroup(); @@ -283,9 +132,6 @@ public class PolarisConfigFilePullerTest { configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2)); customFiles.add(configFileGroup); - when(polarisConfigProperties.getGroups()).thenReturn(customFiles); - when(profiles.getActive()).thenReturn(Lists.newArrayList()); - // file1.properties Map file1Map = new HashMap<>(); file1Map.put("k1", "v1"); diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java index f2a1a3f07..2dcc53883 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java @@ -46,7 +46,9 @@ public class PolarisContextAutoConfiguration { public SDKContext polarisContext(PolarisContextProperties properties, Environment environment, List modifierList) throws PolarisException { - return SDKContext.initContextByConfig(properties.configuration(environment, modifierList)); + return SDKContext.initContextByConfig(properties.configuration(modifierList,() -> { + return environment.getProperty("spring.cloud.client.ip-address"); + })); } @Bean 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 f2dad57f8..eef2a042f 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 @@ -21,6 +21,7 @@ package com.tencent.cloud.polaris.context.config; import java.util.Collection; import java.util.Comparator; import java.util.List; +import java.util.function.Supplier; import java.util.stream.Collectors; import com.tencent.cloud.polaris.context.PolarisConfigModifier; @@ -31,7 +32,6 @@ import com.tencent.polaris.factory.config.ConfigurationImpl; import org.apache.commons.lang.StringUtils; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.core.env.Environment; import org.springframework.util.CollectionUtils; /** @@ -67,7 +67,7 @@ public class PolarisContextProperties { */ private String service; - public Configuration configuration(Environment environment, List modifierList) { + public Configuration configuration(List modifierList, Supplier ipAddressSupplier) { // 1. Read user-defined polaris.yml configuration ConfigurationImpl configuration = (ConfigurationImpl) ConfigAPIFactory .defaultConfig(ConfigProvider.DEFAULT_CONFIG); @@ -75,7 +75,7 @@ public class PolarisContextProperties { // 2. Override user-defined polaris.yml configuration with SCT configuration String defaultHost = this.localIpAddress; if (StringUtils.isBlank(localIpAddress)) { - defaultHost = environment.getProperty("spring.cloud.client.ip-address"); + defaultHost = ipAddressSupplier.get(); } configuration.getGlobal().getAPI().setBindIP(defaultHost);