load application.yml and application-${profile}.yml from polaris server ()

pull/201/head
lepdou 3 years ago committed by GitHub
parent 70786309ea
commit 9378bb2878
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,4 +7,6 @@
- [Add metadata transfer example.](https://github.com/Tencent/spring-cloud-tencent/pull/184) - [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: 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: 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)
- [feat:add rate limit of unirate.](https://github.com/Tencent/spring-cloud-tencent/pull/197) - [feat:add rate limit of unirate.](https://github.com/Tencent/spring-cloud-tencent/pull/197)

@ -31,6 +31,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
/** /**
* polaris config module auto configuration at bootstrap phase. * polaris config module auto configuration at bootstrap phase.
@ -64,10 +65,11 @@ public class PolarisConfigBootstrapAutoConfiguration {
PolarisConfigProperties polarisConfigProperties, PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties, PolarisContextProperties polarisContextProperties,
ConfigFileService configFileService, ConfigFileService configFileService,
PolarisPropertySourceManager polarisPropertySourceManager) { PolarisPropertySourceManager polarisPropertySourceManager,
Environment environment) {
return new PolarisConfigFileLocator(polarisConfigProperties, return new PolarisConfigFileLocator(polarisConfigProperties,
polarisContextProperties, configFileService, polarisContextProperties, configFileService,
polarisPropertySourceManager); polarisPropertySourceManager, environment);
} }
@Bean @Bean

@ -1,23 +1,24 @@
/* /*
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available. * 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"); * Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * 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 * 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 * 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 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License. * specific language governing permissions and limitations under the License.
* *
*/ */
package com.tencent.cloud.polaris.config.adapter; package com.tencent.cloud.polaris.config.adapter;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; 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.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.enums.ConfigFileFormat; import com.tencent.cloud.polaris.config.enums.ConfigFileFormat;
import com.tencent.cloud.polaris.context.PolarisContextProperties; 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.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile; import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -62,14 +65,18 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
private final PolarisPropertySourceManager polarisPropertySourceManager; private final PolarisPropertySourceManager polarisPropertySourceManager;
private final Environment environment;
public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, public PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties, PolarisContextProperties polarisContextProperties,
ConfigFileService configFileService, ConfigFileService configFileService,
PolarisPropertySourceManager polarisPropertySourceManager) { PolarisPropertySourceManager polarisPropertySourceManager,
Environment environment) {
this.polarisConfigProperties = polarisConfigProperties; this.polarisConfigProperties = polarisConfigProperties;
this.polarisContextProperties = polarisContextProperties; this.polarisContextProperties = polarisContextProperties;
this.configFileService = configFileService; this.configFileService = configFileService;
this.polarisPropertySourceManager = polarisPropertySourceManager; this.polarisPropertySourceManager = polarisPropertySourceManager;
this.environment = environment;
} }
@Override @Override
@ -82,12 +89,70 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
return compositePropertySource; return compositePropertySource;
} }
initPolarisConfigFiles(compositePropertySource, configFileGroups); initInternalConfigFiles(compositePropertySource);
initCustomPolarisConfigFiles(compositePropertySource, configFileGroups);
return compositePropertySource; return compositePropertySource;
} }
private void initPolarisConfigFiles(CompositePropertySource compositePropertySource, private void initInternalConfigFiles(CompositePropertySource compositePropertySource) {
List<ConfigFileMetadata> 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<ConfigFileMetadata> getInternalConfigFiles() {
String namespace = polarisContextProperties.getNamespace();
String serviceName = polarisContextProperties.getService();
if (StringUtils.isEmpty(serviceName)) {
serviceName = environment.getProperty("spring.application.name");
}
List<ConfigFileMetadata> 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<ConfigFileGroup> configFileGroups) { List<ConfigFileGroup> configFileGroups) {
String namespace = polarisContextProperties.getNamespace(); String namespace = polarisContextProperties.getNamespace();

@ -134,11 +134,11 @@ public class PolarisContextProperties {
this.namespace = namespace; this.namespace = namespace;
} }
String getService() { public String getService() {
return service; return service;
} }
void setService(String service) { public void setService(String service) {
this.service = service; this.service = service;
} }

Loading…
Cancel
Save