Skip to content

feat:support consul config. #1394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
- [fix:fix no registry when lossless is disabled.](https://github.com/Tencent/spring-cloud-tencent/pull/1388)
- [fix:fix the ratelimit bug](https://github.com/Tencent/spring-cloud-tencent/pull/1389)
- [feat:add Tencent Cloud TSF support.](https://github.com/Tencent/spring-cloud-tencent/pull/1391)
- [feat:support consul config.](https://github.com/Tencent/spring-cloud-tencent/pull/1394)
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
import com.tencent.cloud.polaris.config.config.PolarisCryptoConfigProperties;
import com.tencent.cloud.polaris.context.PolarisConfigurationConfigModifier;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.factory.config.configuration.ConfigFilterConfigImpl;
import com.tencent.polaris.factory.config.configuration.ConnectorConfigImpl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.util.CollectionUtils;
import static com.tencent.polaris.api.config.plugin.DefaultPlugins.LOCAL_FILE_CONNECTOR_TYPE;


/**
Expand All @@ -44,9 +46,6 @@
public class ConfigurationModifier implements PolarisConfigurationConfigModifier {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class);

private static final String DATA_SOURCE_POLARIS = "polaris";
private static final String DATA_SOURCE_LOCAL = "local";

private final PolarisConfigProperties polarisConfigProperties;

private final PolarisCryptoConfigProperties polarisCryptoConfigProperties;
Expand All @@ -68,15 +67,8 @@ public void modify(ConfigurationImpl configuration) {
if (!polarisContextProperties.getEnabled() || !polarisConfigProperties.isEnabled()) {
return;
}
if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_POLARIS)) {
initByPolarisDataSource(configuration);
}
else if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_LOCAL)) {
initByLocalDataSource(configuration);
}
else {
throw new RuntimeException("Unsupported config data source");
}

initDataSource(configuration);

ConfigFilterConfigImpl configFilterConfig = configuration.getConfigFile().getConfigFilterConfig();
configFilterConfig.setEnable(polarisCryptoConfigProperties.isEnabled());
Expand All @@ -86,18 +78,15 @@ else if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), D
}
}

private void initByLocalDataSource(ConfigurationImpl configuration) {
configuration.getConfigFile().getServerConnector().setConnectorType("localFile");

String localFileRootPath = polarisConfigProperties.getLocalFileRootPath();
configuration.getConfigFile().getServerConnector().setPersistDir(localFileRootPath);

LOGGER.info("[SCT] Run spring cloud tencent config with local data source. localFileRootPath = {}", localFileRootPath);
}

private void initByPolarisDataSource(ConfigurationImpl configuration) {
private void initDataSource(ConfigurationImpl configuration) {
// set connector type
configuration.getConfigFile().getServerConnector().setConnectorType("polaris");
configuration.getConfigFile().getServerConnector().setConnectorType(polarisConfigProperties.getDataSource());
if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), LOCAL_FILE_CONNECTOR_TYPE)) {
String localFileRootPath = polarisConfigProperties.getLocalFileRootPath();
configuration.getConfigFile().getServerConnector().setPersistDir(localFileRootPath);
LOGGER.info("[SCT] Run spring cloud tencent config with local data source. localFileRootPath = {}", localFileRootPath);
return;
}

// set config server address
List<String> configAddresses;
Expand All @@ -119,6 +108,11 @@ private void initByPolarisDataSource(ConfigurationImpl configuration) {

configuration.getConfigFile().getServerConnector().setAddresses(configAddresses);

if (StringUtils.isNotEmpty(polarisConfigProperties.getToken())) {
ConnectorConfigImpl connectorConfig = configuration.getConfigFile().getServerConnector();
connectorConfig.setToken(polarisConfigProperties.getToken());
}

LOGGER.info("[SCT] Run spring cloud tencent config in polaris data source.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,12 @@ private void initCustomPolarisConfigFiles(CompositePropertySource compositePrope
String namespace = polarisContextProperties.getNamespace();

for (ConfigFileGroup configFileGroup : configFileGroups) {
String group = configFileGroup.getName();
String groupNamespace = configFileGroup.getNamespace();
if (!StringUtils.hasText(groupNamespace)) {
groupNamespace = namespace;
}

String group = configFileGroup.getName();
if (!StringUtils.hasText(group)) {
throw new IllegalArgumentException("polaris config group name cannot be empty.");
}
Expand All @@ -203,26 +207,26 @@ private void initCustomPolarisConfigFiles(CompositePropertySource compositePrope
}

for (String fileName : files) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(namespace, group, fileName);
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(groupNamespace, 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);
LOGGER.info("[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", groupNamespace, 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)) {
// unknown extension is resolved as yaml file
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
/*
* 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.ArrayList;
import java.util.Arrays;
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.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.core.env.CompositePropertySource;
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 PolarisConfigFilePuller() {
}

/**
* Factory method to create PolarisConfigFilePuller for
* {@link PolarisConfigFileLocator}.
*
* @param polarisContextProperties polarisContextProperties
* @param configFileService configFileService
* @return PolarisConfigFilePuller instance
*/
public static PolarisConfigFilePuller get(PolarisContextProperties polarisContextProperties, ConfigFileService configFileService) {
PolarisConfigFilePuller puller = new PolarisConfigFilePuller();
puller.polarisContextProperties = polarisContextProperties;
puller.configFileService = configFileService;
return puller;
}

/**
* InitInternalConfigFiles for {@link PolarisConfigDataLoader}.
*
* @param compositePropertySource compositePropertySource
* @param activeProfiles activeProfiles
* @param defaultProfiles defaultProfiles
* @param serviceName serviceName
*/
public void initInternalConfigFiles(CompositePropertySource compositePropertySource, String[] activeProfiles,
String[] defaultProfiles, String serviceName) {
List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles(activeProfiles, defaultProfiles, 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<ConfigFileGroup> configFileGroups) {
configFileGroups.forEach(
configFileGroup -> initCustomPolarisConfigFile(compositePropertySource, configFileGroup)
);
}

/**
* Init single CustomPolarisConfigFile.
*
* @param compositePropertySource compositePropertySource
* @param configFileGroup configFileGroup
*/
public void initCustomPolarisConfigFile(CompositePropertySource compositePropertySource,
ConfigFileGroup configFileGroup) {
String groupNamespace = configFileGroup.getNamespace();
if (!StringUtils.hasText(groupNamespace)) {
groupNamespace = polarisContextProperties.getNamespace();
}
String group = configFileGroup.getName();
if (!StringUtils.hasText(group)) {
throw new IllegalArgumentException("polaris config group name cannot be empty.");
}
List<String> files = configFileGroup.getFiles();
if (CollectionUtils.isEmpty(files)) {
return;
}
for (String fileName : files) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(groupNamespace, group, fileName);
compositePropertySource.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info(
"[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}",
groupNamespace, group, fileName);
}
}

private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName) {
ConfigKVFile configKVFile;
// unknown extension is resolved as yaml file
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(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<String, Object> 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<ConfigFileMetadata> getInternalConfigFiles(
String[] activeProfiles, String[] defaultProfiles, String serviceName) {
String namespace = polarisContextProperties.getNamespace();
if (StringUtils.hasText(polarisContextProperties.getService())) {
serviceName = polarisContextProperties.getService();
}
// priority: application-${profile} > application > boostrap-${profile} > boostrap
return getInternalConfigFiles(activeProfiles, defaultProfiles, namespace, serviceName);
}

private List<ConfigFileMetadata> getInternalConfigFiles(
String[] activeProfiles, String[] defaultProfiles, String namespace, String serviceName) {
List<String> profileList = new ArrayList<>();
if (ArrayUtils.isNotEmpty(activeProfiles)) {
profileList.addAll(Arrays.asList(activeProfiles));
}
else if (ArrayUtils.isNotEmpty(defaultProfiles)) {
profileList.addAll(Arrays.asList(defaultProfiles));
}

List<ConfigFileMetadata> internalConfigFiles = new LinkedList<>();
// build application config files
buildInternalApplicationConfigFiles(internalConfigFiles, namespace, serviceName, profileList);
// build bootstrap config files
buildInternalBootstrapConfigFiles(internalConfigFiles, namespace, serviceName, profileList);

return internalConfigFiles;
}

private void buildInternalApplicationConfigFiles(
List<ConfigFileMetadata> internalConfigFiles, String namespace, String serviceName, List<String> profiles) {
for (String profile : profiles) {
if (!StringUtils.hasText(profile)) {
continue;
}
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".properties"));
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".yml"));
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application-" + profile + ".yaml"));
}
// build default config properties files.
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.properties"));
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yml"));
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "application.yaml"));
}

private void buildInternalBootstrapConfigFiles(
List<ConfigFileMetadata> internalConfigFiles, String namespace, String serviceName, List<String> profiles) {
for (String profile : profiles) {
if (!StringUtils.hasText(profile)) {
continue;
}
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".properties"));
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".yml"));
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap-" + profile + ".yaml"));
}
// build default config properties files.
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.properties"));
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yml"));
internalConfigFiles.add(new DefaultConfigFileMetadata(namespace, serviceName, "bootstrap.yaml"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
public class ConfigFileGroup {

private String namespace;

/**
* group name.
*/
Expand All @@ -36,6 +38,14 @@ public class ConfigFileGroup {
*/
private List<String> files;

public String getNamespace() {
return namespace;
}

public void setNamespace(String namespace) {
this.namespace = namespace;
}

public String getName() {
return name;
}
Expand All @@ -54,6 +64,10 @@ public void setFiles(List<String> files) {

@Override
public String toString() {
return "ConfigFileGroup{" + "name='" + name + '\'' + ", file=" + files + '}';
return "ConfigFileGroup{" +
"namespace='" + namespace + '\'' +
", name='" + name + '\'' +
", files=" + files +
'}';
}
}
Loading
Loading