From 850a105522e399046183a9fb5e65bd331cb222ad Mon Sep 17 00:00:00 2001 From: Fishtail <49390359+fuyuwei01@users.noreply.github.com> Date: Tue, 27 Aug 2024 15:58:47 +0800 Subject: [PATCH] feat:support consul config. (#1410) Co-authored-by: Haotian Zhang --- CHANGELOG.md | 1 + .../polaris/config/ConfigurationModifier.java | 42 +++++------ .../adapter/PolarisConfigFileLocator.java | 20 +++-- .../adapter/PolarisConfigFilePuller.java | 20 ++--- .../config/config/ConfigFileGroup.java | 16 +++- .../config/PolarisConfigProperties.java | 27 +++++++ ...arisAdaptorTsfConfigAutoConfiguration.java | 5 +- .../config/tsf/TsfConfigurationModifier.java | 28 ++++++- ...itional-spring-configuration-metadata.json | 7 ++ .../tsf/TsfDiscoveryConfigModifier.java | 10 +-- .../polaris/tsf/TsfDiscoveryProperties.java | 58 --------------- ...TsfDiscoveryRegistryAutoConfiguration.java | 7 +- ...MetadataPolarisRegistrationCustomizer.java | 34 ++++++--- .../tencent/cloud/common/tsf/TsfConstant.java | 73 +++++++++++++++++++ .../context/PolarisSDKContextManager.java | 33 +++++---- .../context/tsf/config/TsfCoreProperties.java | 49 +++++++++++++ .../tsf/consul/TsfConsulProperties.java | 13 +++- .../env/TsfCoreEnvironmentPostProcessor.java | 63 +++++++++++++++- 18 files changed, 368 insertions(+), 138 deletions(-) create mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/tsf/TsfConstant.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 511a58bad..0f9d98411 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,3 +19,4 @@ - [feat:add scg-mvc example](https://github.com/Tencent/spring-cloud-tencent/pull/1367) - [feat:add zero protection.](https://github.com/Tencent/spring-cloud-tencent/pull/1408) - [feat:add Tencent Cloud TSF support.](https://github.com/Tencent/spring-cloud-tencent/pull/1409) +- [feat:support consul config.](https://github.com/Tencent/spring-cloud-tencent/pull/1410) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java index a6c5f4989..993b3f978 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/ConfigurationModifier.java @@ -27,13 +27,15 @@ import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; 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; /** @@ -44,9 +46,6 @@ import org.springframework.util.CollectionUtils; 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; @@ -68,15 +67,8 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier 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()); @@ -86,18 +78,15 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier } } - 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 configAddresses; @@ -119,6 +108,11 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier 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."); } 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 824f47cab..e50eb6bae 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 @@ -191,8 +191,12 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { 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."); } @@ -203,26 +207,26 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { } 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); 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 2a369171e..94dbaa513 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 @@ -112,7 +112,10 @@ public final class PolarisConfigFilePuller { */ 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."); @@ -122,25 +125,24 @@ public final class PolarisConfigFilePuller { 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); diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java index b52ffd13e..80cff02b8 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/ConfigFileGroup.java @@ -26,6 +26,8 @@ import java.util.List; */ public class ConfigFileGroup { + private String namespace; + /** * group name. */ @@ -36,6 +38,14 @@ public class ConfigFileGroup { */ private List files; + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + public String getName() { return name; } @@ -54,6 +64,10 @@ public class ConfigFileGroup { @Override public String toString() { - return "ConfigFileGroup{" + "name='" + name + '\'' + ", file=" + files + '}'; + return "ConfigFileGroup{" + + "namespace='" + namespace + '\'' + + ", name='" + name + '\'' + + ", files=" + files + + '}'; } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java index 6a014aa04..38687ddd8 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/config/PolarisConfigProperties.java @@ -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 @@ -113,6 +115,14 @@ public class PolarisConfigProperties { this.port = port; } + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + public boolean isAutoRefresh() { return autoRefresh; } @@ -168,4 +178,21 @@ public class PolarisConfigProperties { 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 + '\'' + + '}'; + } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/PolarisAdaptorTsfConfigAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/PolarisAdaptorTsfConfigAutoConfiguration.java index fbb1ad02b..9c05d2eb0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/PolarisAdaptorTsfConfigAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/PolarisAdaptorTsfConfigAutoConfiguration.java @@ -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.ConditionalOnTsfEnabled; 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; @@ -71,7 +72,7 @@ public class PolarisAdaptorTsfConfigAutoConfiguration { @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); } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/TsfConfigurationModifier.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/TsfConfigurationModifier.java index 9799e872b..a15583ca0 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/TsfConfigurationModifier.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/TsfConfigurationModifier.java @@ -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; /** @@ -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 groups = new ArrayList<>(); + polarisConfigProperties.setGroups(groups); + groups.clear(); + ConfigFileGroup tsfGroup = new ConfigFileGroup(); + tsfGroup.setNamespace("config"); + tsfGroup.setName("application"); + List files = new ArrayList<>(); + tsfGroup.setFiles(files); + files.add(tsfCoreProperties.getTsfNamespaceId() + "/"); + files.add(tsfCoreProperties.getTsfApplicationId() + "/" + tsfCoreProperties.getTsfGroupId() + "/"); + } } } diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 9ca79a7ab..cbef57639 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-config/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/TsfDiscoveryConfigModifier.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/TsfDiscoveryConfigModifier.java index d20774205..f086f940e 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/TsfDiscoveryConfigModifier.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/TsfDiscoveryConfigModifier.java @@ -79,9 +79,9 @@ public class TsfDiscoveryConfigModifier implements PolarisConfigModifier { // 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()); @@ -134,8 +134,8 @@ public class TsfDiscoveryConfigModifier implements PolarisConfigModifier { 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())) { diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/TsfDiscoveryProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/TsfDiscoveryProperties.java index 7e2e1f92f..9b113ec51 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/TsfDiscoveryProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/TsfDiscoveryProperties.java @@ -43,9 +43,6 @@ public class TsfDiscoveryProperties { private InetUtils.HostInfo hostInfo; - @Value("${tsf_token:${consul.token:${CONSUL_TOKEN:${spring.cloud.consul.token:${SPRING_CLOUD_CONSUL_TOKEN:}}}}}") - private String aclToken; - /** * Tags to use when registering service. */ @@ -217,28 +214,6 @@ public class TsfDiscoveryProperties { */ private Boolean healthCheckTlsSkipVerify; - /** - * tsf service consul registration tags. - * - * applicationId 应用Id - */ - @Value("${tsf_application_id:}") - private String tsfApplicationId; - - /** - * tsf service consul registration tags. - * - * groupId 部署组Id - */ - @Value("${tsf_group_id:}") - private String tsfGroupId; - - /** - * 仅本地测试时使用. - */ - @Value("${tsf_namespace_id:}") - private String tsfNamespaceId; - /** * tsf service consul registration tags. * @@ -321,14 +296,6 @@ public class TsfDiscoveryProperties { this.hostInfo = hostInfo; } - public String getAclToken() { - return aclToken; - } - - public void setAclToken(String aclToken) { - this.aclToken = aclToken; - } - public List getTags() { return tags; } @@ -586,30 +553,6 @@ public class TsfDiscoveryProperties { this.healthCheckTlsSkipVerify = healthCheckTlsSkipVerify; } - public String getTsfApplicationId() { - return tsfApplicationId; - } - - public void setTsfApplicationId(final String tsfApplicationId) { - this.tsfApplicationId = tsfApplicationId; - } - - public String getTsfGroupId() { - return tsfGroupId; - } - - public void setTsfGroupId(final String tsfGroupId) { - this.tsfGroupId = tsfGroupId; - } - - public String getTsfNamespaceId() { - return tsfNamespaceId; - } - - public void setTsfNamespaceId(String tsfNamespaceId) { - this.tsfNamespaceId = tsfNamespaceId; - } - public String getTsfProgVersion() { return tsfProgVersion; } @@ -686,7 +629,6 @@ public class TsfDiscoveryProperties { public String toString() { return "ConsulDiscoveryProperties{" + "hostInfo=" + hostInfo + - ", aclToken='" + aclToken + '\'' + ", tags=" + tags + ", enabled=" + enabled + ", managementTags=" + managementTags + diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfDiscoveryRegistryAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfDiscoveryRegistryAutoConfiguration.java index 461b1e05b..8e0c66da0 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfDiscoveryRegistryAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfDiscoveryRegistryAutoConfiguration.java @@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.tsf.registry; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.tsf.ConditionalOnTsfEnabled; +import com.tencent.cloud.polaris.context.tsf.config.TsfCoreProperties; import com.tencent.cloud.polaris.registry.PolarisServiceRegistryAutoConfiguration; import com.tencent.cloud.polaris.tsf.TsfDiscoveryProperties; import com.tencent.cloud.polaris.tsf.TsfHeartbeatProperties; @@ -43,10 +44,12 @@ import org.springframework.context.annotation.Configuration; @ConditionalOnTsfEnabled @AutoConfigureBefore(PolarisServiceRegistryAutoConfiguration.class) public class TsfDiscoveryRegistryAutoConfiguration { + @Bean @ConditionalOnMissingBean - public TsfMetadataPolarisRegistrationCustomizer tsfMetadataPolarisRegistrationCustomizer(TsfDiscoveryProperties tsfDiscoveryProperties) { - return new TsfMetadataPolarisRegistrationCustomizer(tsfDiscoveryProperties); + public TsfMetadataPolarisRegistrationCustomizer tsfMetadataPolarisRegistrationCustomizer( + TsfCoreProperties tsfCoreProperties, TsfDiscoveryProperties tsfDiscoveryProperties) { + return new TsfMetadataPolarisRegistrationCustomizer(tsfCoreProperties, tsfDiscoveryProperties); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfMetadataPolarisRegistrationCustomizer.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfMetadataPolarisRegistrationCustomizer.java index 9b698c59f..b152afd54 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfMetadataPolarisRegistrationCustomizer.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/tsf/registry/TsfMetadataPolarisRegistrationCustomizer.java @@ -21,12 +21,23 @@ import java.util.Map; import com.tencent.cloud.common.constant.SdkVersion; import com.tencent.cloud.common.util.JacksonUtils; +import com.tencent.cloud.polaris.context.tsf.config.TsfCoreProperties; import com.tencent.cloud.polaris.registry.PolarisRegistration; import com.tencent.cloud.polaris.registry.PolarisRegistrationCustomizer; import com.tencent.cloud.polaris.tsf.TsfDiscoveryProperties; import com.tencent.cloud.polaris.tsf.consts.WarmupCons; import com.tencent.cloud.polaris.tsf.util.RegistrationUtil; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_APPLICATION_ID; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_GROUP_ID; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_INSTNACE_ID; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_NAMESPACE_ID; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_PROG_VERSION; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_REGION; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_SDK_VERSION; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_TAGS; +import static com.tencent.cloud.common.tsf.TsfConstant.TSF_ZONE; + /** * * @@ -34,9 +45,12 @@ import com.tencent.cloud.polaris.tsf.util.RegistrationUtil; */ public class TsfMetadataPolarisRegistrationCustomizer implements PolarisRegistrationCustomizer { + private final TsfCoreProperties tsfCoreProperties; + private final TsfDiscoveryProperties tsfDiscoveryProperties; - public TsfMetadataPolarisRegistrationCustomizer(TsfDiscoveryProperties tsfDiscoveryProperties) { + public TsfMetadataPolarisRegistrationCustomizer(TsfCoreProperties tsfCoreProperties, TsfDiscoveryProperties tsfDiscoveryProperties) { + this.tsfCoreProperties = tsfCoreProperties; this.tsfDiscoveryProperties = tsfDiscoveryProperties; } @@ -44,17 +58,17 @@ public class TsfMetadataPolarisRegistrationCustomizer implements PolarisRegistra public void customize(PolarisRegistration registration) { Map metadata = registration.getMetadata(); - metadata.put("TSF_APPLICATION_ID", tsfDiscoveryProperties.getTsfApplicationId()); - metadata.put("TSF_PROG_VERSION", tsfDiscoveryProperties.getTsfProgVersion()); - metadata.put("TSF_GROUP_ID", tsfDiscoveryProperties.getTsfGroupId()); - metadata.put("TSF_NAMESPACE_ID", tsfDiscoveryProperties.getTsfNamespaceId()); - metadata.put("TSF_INSTNACE_ID", tsfDiscoveryProperties.getInstanceId()); - metadata.put("TSF_REGION", tsfDiscoveryProperties.getTsfRegion()); - metadata.put("TSF_ZONE", tsfDiscoveryProperties.getTsfZone()); + metadata.put(TSF_APPLICATION_ID, tsfCoreProperties.getTsfApplicationId()); + metadata.put(TSF_PROG_VERSION, tsfDiscoveryProperties.getTsfProgVersion()); + metadata.put(TSF_GROUP_ID, tsfCoreProperties.getTsfGroupId()); + metadata.put(TSF_NAMESPACE_ID, tsfCoreProperties.getTsfNamespaceId()); + metadata.put(TSF_INSTNACE_ID, tsfDiscoveryProperties.getInstanceId()); + metadata.put(TSF_REGION, tsfDiscoveryProperties.getTsfRegion()); + metadata.put(TSF_ZONE, tsfDiscoveryProperties.getTsfZone()); // 处理预热相关的参数 metadata.put(WarmupCons.TSF_START_TIME, String.valueOf(System.currentTimeMillis())); - metadata.put("TSF_SDK_VERSION", SdkVersion.get()); - metadata.put("TSF_TAGS", JacksonUtils.serialize2Json(RegistrationUtil.createTags(tsfDiscoveryProperties))); + metadata.put(TSF_SDK_VERSION, SdkVersion.get()); + metadata.put(TSF_TAGS, JacksonUtils.serialize2Json(RegistrationUtil.createTags(tsfDiscoveryProperties))); RegistrationUtil.appendMetaIpAddress(metadata); } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/tsf/TsfConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/tsf/TsfConstant.java new file mode 100644 index 000000000..dfc8949fe --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/tsf/TsfConstant.java @@ -0,0 +1,73 @@ +/* + * 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.common.tsf; + +/** + * Constant for TSF. + * + * @author Haotian Zhang + */ +public final class TsfConstant { + /** + * tsf application id. + */ + public static String TSF_APPLICATION_ID = "TSF_APPLICATION_ID"; + + /** + * tsf program version. + */ + public static String TSF_PROG_VERSION = "TSF_PROG_VERSION"; + + /** + * tsf group id. + */ + public static String TSF_GROUP_ID = "TSF_GROUP_ID"; + + /** + * tsf namespace id. + */ + public static String TSF_NAMESPACE_ID = "TSF_NAMESPACE_ID"; + + /** + * tsf instance id. + */ + public static String TSF_INSTNACE_ID = "TSF_INSTNACE_ID"; + + /** + * tsf region. + */ + public static String TSF_REGION = "TSF_REGION"; + + /** + * tsf zone. + */ + public static String TSF_ZONE = "TSF_ZONE"; + + /** + * tsf SDK version. + */ + public static String TSF_SDK_VERSION = "TSF_SDK_VERSION"; + + /** + * tsf tags. + */ + public static String TSF_TAGS = "TSF_TAGS"; + + private TsfConstant() { + } +} diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java index a09bfd81c..8d4fd852a 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/PolarisSDKContextManager.java @@ -162,18 +162,6 @@ public class PolarisSDKContextManager { initConfig(); } - /** - * Used for config data. - */ - public static void setConfigSDKContext(SDKContext context) { - if (configSDKContext == null) { - configSDKContext = context; - // add shutdown hook - Runtime.getRuntime().addShutdownHook(new Thread(PolarisSDKContextManager::innerConfigDestroy)); - LOG.info("create Polaris config SDK context successfully."); - } - } - public SDKContext getSDKContext() { initService(); return serviceSdkContext; @@ -218,11 +206,30 @@ public class PolarisSDKContextManager { return configSDKContext; } + /** + * Used for config data. + */ + public static void setConfigSDKContext(SDKContext context) { + if (configSDKContext == null) { + configSDKContext = context; + // add shutdown hook + Runtime.getRuntime().addShutdownHook(new Thread(PolarisSDKContextManager::innerConfigDestroy)); + LOG.info("create Polaris config SDK context successfully."); + } + } + public void initService() { if (null == serviceSdkContext) { try { + // get modifiers for service. + List serviceModifierList = new ArrayList<>(); + for (PolarisConfigModifier modifier : modifierList) { + if (!(modifier instanceof PolarisConfigurationConfigModifier)) { + serviceModifierList.add(modifier); + } + } // init SDKContext - serviceSdkContext = SDKContext.initContextByConfig(properties.configuration(modifierList, + serviceSdkContext = SDKContext.initContextByConfig(properties.configuration(serviceModifierList, () -> environment.getProperty("spring.cloud.client.ip-address"), () -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0))); serviceSdkContext.init(); diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/config/TsfCoreProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/config/TsfCoreProperties.java index 376ae0e51..6a617639f 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/config/TsfCoreProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/config/TsfCoreProperties.java @@ -37,6 +37,28 @@ public class TsfCoreProperties { @Value("${tse_polaris_enable:false}") private boolean tsePolarisEnable = false; + /** + * tsf service consul registration tags. + *

+ * applicationId 应用Id + */ + @Value("${tsf_application_id:}") + private String tsfApplicationId; + + /** + * tsf service consul registration tags. + *

+ * groupId 部署组Id + */ + @Value("${tsf_group_id:}") + private String tsfGroupId; + + /** + * 仅本地测试时使用. + */ + @Value("${tsf_namespace_id:}") + private String tsfNamespaceId; + public String getTsePolarisIp() { return tsePolarisIp; } @@ -61,12 +83,39 @@ public class TsfCoreProperties { this.tsePolarisEnable = tsePolarisEnable; } + public String getTsfApplicationId() { + return tsfApplicationId; + } + + public void setTsfApplicationId(final String tsfApplicationId) { + this.tsfApplicationId = tsfApplicationId; + } + + public String getTsfGroupId() { + return tsfGroupId; + } + + public void setTsfGroupId(final String tsfGroupId) { + this.tsfGroupId = tsfGroupId; + } + + public String getTsfNamespaceId() { + return tsfNamespaceId; + } + + public void setTsfNamespaceId(String tsfNamespaceId) { + this.tsfNamespaceId = tsfNamespaceId; + } + @Override public String toString() { return "TsfCoreProperties{" + "tsePolarisIp='" + tsePolarisIp + '\'' + ", tsfConsulEnable=" + tsfConsulEnable + ", tsePolarisEnable=" + tsePolarisEnable + + ", tsfApplicationId='" + tsfApplicationId + '\'' + + ", tsfGroupId='" + tsfGroupId + '\'' + + ", tsfNamespaceId='" + tsfNamespaceId + '\'' + '}'; } } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/consul/TsfConsulProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/consul/TsfConsulProperties.java index 6cce59a42..e07291fc9 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/consul/TsfConsulProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/consul/TsfConsulProperties.java @@ -53,6 +53,9 @@ public class TsfConsulProperties { @Value("${tsf_consul_ttl_read_timeout:5000}") private int ttlReadTimeout = 5000; // default 5s + @Value("${tsf_token:${consul.token:${CONSUL_TOKEN:${spring.cloud.consul.token:${SPRING_CLOUD_CONSUL_TOKEN:}}}}}") + private String aclToken; + public String getHost() { return host; } @@ -93,15 +96,23 @@ public class TsfConsulProperties { this.ttlReadTimeout = ttlReadTimeout; } + public String getAclToken() { + return aclToken; + } + + public void setAclToken(String aclToken) { + this.aclToken = aclToken; + } @Override public String toString() { - return "ConsulProperties{" + + return "TsfConsulProperties{" + "host='" + host + '\'' + ", scheme='" + scheme + '\'' + ", port=" + port + ", enabled=" + enabled + ", ttlReadTimeout=" + ttlReadTimeout + + ", aclToken='" + aclToken + '\'' + '}'; } } diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java index c12c316ab..05a706c68 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/tsf/env/TsfCoreEnvironmentPostProcessor.java @@ -21,10 +21,12 @@ import java.util.HashMap; import java.util.Map; import com.tencent.polaris.api.utils.StringUtils; +import org.apache.commons.logging.Log; import org.springframework.boot.SpringApplication; import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.boot.logging.DeferredLogFactory; import org.springframework.core.Ordered; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MapPropertySource; @@ -34,13 +36,19 @@ import org.springframework.core.env.MapPropertySource; * * @author Haotian Zhang */ -public class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { +public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { /** * run before {@link ConfigDataEnvironmentPostProcessor}. */ public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER - 1; + private final Log LOGGER; + + private TsfCoreEnvironmentPostProcessor(DeferredLogFactory logFactory) { + this.LOGGER = logFactory.getLog(getClass()); + } + @Override public int getOrder() { return ORDER; @@ -52,9 +60,58 @@ public class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor if (StringUtils.isNotBlank(tsfAppId)) { Map defaultProperties = new HashMap<>(); - // TODO 接入consul配置后需要改动这个选项的判断 + // tsf_application_id + String tsfApplicationId = environment.getProperty("tsf_application_id"); + if (StringUtils.isBlank(tsfApplicationId)) { + LOGGER.error("tsf_application_id is empty"); + } + + // tsf_group_id + String tsfGroupId = environment.getProperty("tsf_group_id"); + if (StringUtils.isBlank(tsfGroupId)) { + LOGGER.error("tsf_group_id is empty"); + } + + // tsf_namespace_id + String tsfNamespaceId = environment.getProperty("tsf_namespace_id"); + if (StringUtils.isBlank(tsfNamespaceId)) { + LOGGER.error("tsf_namespace_id is empty"); + } + + // tsf_consul_ip + String tsfConsulIp = environment.getProperty("tsf_consul_ip"); + if (StringUtils.isBlank(tsfConsulIp)) { + LOGGER.error("tsf_consul_ip is empty"); + } + + // tsf_consul_port + String tsfConsulPort = environment.getProperty("tsf_consul_port"); + if (StringUtils.isBlank(tsfConsulPort)) { + LOGGER.error("tsf_consul_port is empty"); + } + + // tsf_token + String tsfConsulToken = environment.getProperty("tsf_token"); + if (StringUtils.isBlank(tsfConsulToken)) { + LOGGER.error("tsf_token is empty"); + } + // tse_polaris_enable - defaultProperties.put("spring.cloud.polaris.config.enabled", environment.getProperty("tse_polaris_enable", "false")); + String tsePolarisEnable = environment.getProperty("tse_polaris_enable", "false"); + if (StringUtils.equals(tsePolarisEnable, "true")) { + defaultProperties.put("spring.cloud.polaris.config.enabled", "true"); + } + else { + defaultProperties.put("spring.cloud.polaris.config.enabled", "true"); + defaultProperties.put("spring.cloud.polaris.config.data-source", "consul"); + defaultProperties.put("spring.cloud.polaris.config.address", "http://" + tsfConsulIp + ":" + tsfConsulPort); + defaultProperties.put("spring.cloud.polaris.config.port", tsfConsulPort); + defaultProperties.put("spring.cloud.polaris.config.token", tsfConsulToken); + defaultProperties.put("spring.cloud.polaris.config.groups[0].namespace", "config"); + defaultProperties.put("spring.cloud.polaris.config.groups[0].name", "application"); + defaultProperties.put("spring.cloud.polaris.config.groups[0].files[0]", tsfNamespaceId + "/"); + defaultProperties.put("spring.cloud.polaris.config.groups[0].files[1]", tsfApplicationId + "/" + tsfGroupId + "/"); + } // tse_polaris_ip defaultProperties.put("spring.cloud.polaris.address", "grpc://" + environment.getProperty("tse_polaris_ip", "") + ":8091");