Skip to content

Commit

Permalink
feat:support consul config. (#1352)
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyuwei01 authored Jul 25, 2024
1 parent 2bc8964 commit 4bc8c9e
Show file tree
Hide file tree
Showing 18 changed files with 368 additions and 138 deletions.
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/1347)
- [fix:fix the ratelimit bug for 2022](https://github.com/Tencent/spring-cloud-tencent/pull/1348)
- [feat:add Tencent Cloud TSF support.](https://github.com/Tencent/spring-cloud-tencent/pull/1350)
- [feat:support consul config.](https://github.com/Tencent/spring-cloud-tencent/pull/1352)
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
Expand Up @@ -112,7 +112,10 @@ public void initCustomPolarisConfigFiles(CompositePropertySource compositeProper
*/
public void initCustomPolarisConfigFile(CompositePropertySource compositePropertySource,
ConfigFileGroup configFileGroup) {
String namespace = polarisContextProperties.getNamespace();
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.");
Expand All @@ -122,25 +125,24 @@ public void initCustomPolarisConfigFile(CompositePropertySource compositePropert
return;
}
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);
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
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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class PolarisConfigProperties {
@Value("${spring.cloud.polaris.config.port:#{'8093'}}")
private int port = 8093;

private String token;

/**
* Whether to automatically update to the spring context when the configuration file.
* is updated
Expand Down Expand Up @@ -113,6 +115,14 @@ public void setPort(int port) {
this.port = port;
}

public String getToken() {
return token;
}

public void setToken(String token) {
this.token = token;
}

public boolean isAutoRefresh() {
return autoRefresh;
}
Expand Down Expand Up @@ -168,4 +178,21 @@ public String getLocalFileRootPath() {
public void setLocalFileRootPath(String localFileRootPath) {
this.localFileRootPath = localFileRootPath;
}

@Override
public String toString() {
return "PolarisConfigProperties{" +
"enabled=" + enabled +
", address='" + address + '\'' +
", port=" + port +
", token='" + token + '\'' +
", autoRefresh=" + autoRefresh +
", shutdownIfConnectToConfigServerFailed=" + shutdownIfConnectToConfigServerFailed +
", preference=" + preference +
", refreshType=" + refreshType +
", groups=" + groups +
", dataSource='" + dataSource + '\'' +
", localFileRootPath='" + localFileRootPath + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.tsf.controller.PolarisAdaptorTsfConfigController;
import com.tencent.cloud.polaris.context.tsf.config.TsfCoreProperties;
import com.tencent.cloud.polaris.context.tsf.consul.TsfConsulProperties;
import com.tencent.tsf.consul.config.watch.TsfConsulConfigRefreshEventListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -71,7 +72,7 @@ public PolarisAdaptorTsfConfigController polarisAdaptorTsfConfigController() {

@Bean
@ConditionalOnMissingBean
public TsfConfigurationModifier tsfConfigModifier(TsfCoreProperties tsfCoreProperties, PolarisConfigProperties polarisConfigProperties) {
return new TsfConfigurationModifier(tsfCoreProperties, polarisConfigProperties);
public TsfConfigurationModifier tsfConfigModifier(TsfCoreProperties tsfCoreProperties, TsfConsulProperties tsfConsulProperties, PolarisConfigProperties polarisConfigProperties) {
return new TsfConfigurationModifier(tsfCoreProperties, tsfConsulProperties, polarisConfigProperties);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@

package com.tencent.cloud.polaris.config.tsf;

import java.util.ArrayList;
import java.util.List;

import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.tsf.config.TsfCoreProperties;
import com.tencent.cloud.polaris.context.tsf.consul.TsfConsulProperties;
import com.tencent.polaris.factory.config.ConfigurationImpl;

/**
Expand All @@ -33,17 +38,36 @@ public class TsfConfigurationModifier implements PolarisConfigModifier {

private final TsfCoreProperties tsfCoreProperties;

private final TsfConsulProperties tsfConsulProperties;

private final PolarisConfigProperties polarisConfigProperties;

public TsfConfigurationModifier(TsfCoreProperties tsfCoreProperties, PolarisConfigProperties polarisConfigProperties) {
public TsfConfigurationModifier(TsfCoreProperties tsfCoreProperties, TsfConsulProperties tsfConsulProperties, PolarisConfigProperties polarisConfigProperties) {
this.tsfCoreProperties = tsfCoreProperties;
this.tsfConsulProperties = tsfConsulProperties;
this.polarisConfigProperties = polarisConfigProperties;
}

@Override
public void modify(ConfigurationImpl configuration) {
if (polarisConfigProperties != null && tsfCoreProperties != null) {
polarisConfigProperties.setEnabled(tsfCoreProperties.isTsePolarisEnable());
polarisConfigProperties.setEnabled(true);
if (!tsfCoreProperties.isTsePolarisEnable()) {
polarisConfigProperties.setDataSource("consul");
polarisConfigProperties.setAddress("http://" + tsfConsulProperties.getHost() + ":" + tsfConsulProperties.getPort());
polarisConfigProperties.setPort(tsfConsulProperties.getPort());
polarisConfigProperties.setToken(tsfConsulProperties.getAclToken());
List<ConfigFileGroup> groups = new ArrayList<>();
polarisConfigProperties.setGroups(groups);
groups.clear();
ConfigFileGroup tsfGroup = new ConfigFileGroup();
tsfGroup.setNamespace("config");
tsfGroup.setName("application");
List<String> files = new ArrayList<>();
tsfGroup.setFiles(files);
files.add(tsfCoreProperties.getTsfNamespaceId() + "/");
files.add(tsfCoreProperties.getTsfApplicationId() + "/" + tsfCoreProperties.getTsfGroupId() + "/");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
"description": "The polaris configuration server port.",
"sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties"
},
{
"name": "spring.cloud.polaris.config.token",
"type": "java.lang.String",
"defaultValue": "",
"description": "The polaris configuration server token.",
"sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties"
},
{
"name": "spring.cloud.polaris.config.auto-refresh",
"type": "java.lang.Boolean",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public void modify(ConfigurationImpl configuration) {
// namespace id
polarisDiscoveryProperties.setHeartbeatInterval(Long.valueOf(tsfHeartbeatProperties.computeHearbeatInterval()
.toStandardDuration().getMillis()).intValue());
polarisContextProperties.setNamespace(tsfDiscoveryProperties.getTsfNamespaceId());
polarisDiscoveryProperties.setNamespace(tsfDiscoveryProperties.getTsfNamespaceId());
System.setProperty("spring.cloud.polaris.namespace", tsfDiscoveryProperties.getTsfNamespaceId());
polarisContextProperties.setNamespace(tsfCoreProperties.getTsfNamespaceId());
polarisDiscoveryProperties.setNamespace(tsfCoreProperties.getTsfNamespaceId());
System.setProperty("spring.cloud.polaris.namespace", tsfCoreProperties.getTsfNamespaceId());

// application id
polarisDiscoveryProperties.setVersion(tsfDiscoveryProperties.getTsfProgVersion());
Expand Down Expand Up @@ -134,8 +134,8 @@ public void modify(ConfigurationImpl configuration) {
String appName = RegistrationUtil.getAppName(tsfDiscoveryProperties, context.getEnvironment());
metadata.put(ConsulConstant.MetadataMapKey.SERVICE_NAME_KEY, RegistrationUtil.normalizeForDns(appName));
metadata.put(ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY, RegistrationUtil.getInstanceId(tsfDiscoveryProperties, context));
if (StringUtils.isNotBlank(tsfDiscoveryProperties.getAclToken())) {
serverConnectorConfig.setToken(tsfDiscoveryProperties.getAclToken());
if (StringUtils.isNotBlank(tsfConsulProperties.getAclToken())) {
serverConnectorConfig.setToken(tsfConsulProperties.getAclToken());
}
metadata.put(ConsulConstant.MetadataMapKey.TAGS_KEY, JacksonUtils.serialize2Json(RegistrationUtil.createTags(tsfDiscoveryProperties)));
if (StringUtils.isNotBlank(tsfDiscoveryProperties.getDefaultQueryTag())) {
Expand Down
Loading

0 comments on commit 4bc8c9e

Please sign in to comment.