diff --git a/CHANGELOG.md b/CHANGELOG.md index e53681b5d..8b0dda90a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,4 @@ - [Add metadata transfer example.](https://github.com/Tencent/spring-cloud-tencent/pull/184) - [Feature: Support metadata router.](https://github.com/Tencent/spring-cloud-tencent/pull/191) - [Feature: Misc optimize metadata router.](https://github.com/Tencent/spring-cloud-tencent/pull/192) +- [Feature: Load application.yml and application-${profile}.yml from polaris server.](https://github.com/Tencent/spring-cloud-tencent/pull/199) 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 2abe30442..1a7e05ac9 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 @@ -31,6 +31,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.core.env.Environment; /** * polaris config module auto configuration at bootstrap phase. @@ -64,10 +65,11 @@ public class PolarisConfigBootstrapAutoConfiguration { PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, - PolarisPropertySourceManager polarisPropertySourceManager) { + PolarisPropertySourceManager polarisPropertySourceManager, + Environment environment) { return new PolarisConfigFileLocator(polarisConfigProperties, polarisContextProperties, configFileService, - polarisPropertySourceManager); + polarisPropertySourceManager, environment); } @Bean 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 6ab0b4530..cad31d1de 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 @@ -1,23 +1,24 @@ /* * 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. + * 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 + * 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 + * 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. + * 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.concurrent.ConcurrentHashMap; @@ -26,8 +27,10 @@ import com.tencent.cloud.polaris.config.config.ConfigFileGroup; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.enums.ConfigFileFormat; import com.tencent.cloud.polaris.context.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; @@ -62,14 +65,18 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { private final PolarisPropertySourceManager polarisPropertySourceManager; + private final Environment environment; + public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, PolarisContextProperties polarisContextProperties, ConfigFileService configFileService, - PolarisPropertySourceManager polarisPropertySourceManager) { + PolarisPropertySourceManager polarisPropertySourceManager, + Environment environment) { this.polarisConfigProperties = polarisConfigProperties; this.polarisContextProperties = polarisContextProperties; this.configFileService = configFileService; this.polarisPropertySourceManager = polarisPropertySourceManager; + this.environment = environment; } @Override @@ -82,12 +89,70 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { return compositePropertySource; } - initPolarisConfigFiles(compositePropertySource, configFileGroups); + initInternalConfigFiles(compositePropertySource); + + initCustomPolarisConfigFiles(compositePropertySource, configFileGroups); return compositePropertySource; } - private void initPolarisConfigFiles(CompositePropertySource compositePropertySource, + private 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); + } + } + + private List getInternalConfigFiles() { + String namespace = polarisContextProperties.getNamespace(); + String serviceName = polarisContextProperties.getService(); + if (StringUtils.isEmpty(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.isEmpty(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.isEmpty(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) { String namespace = polarisContextProperties.getNamespace(); diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java index 95c1c2672..3df1731b5 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisContextProperties.java @@ -134,11 +134,11 @@ public class PolarisContextProperties { this.namespace = namespace; } - String getService() { + public String getService() { return service; } - void setService(String service) { + public void setService(String service) { this.service = service; }