diff --git a/CHANGELOG.md b/CHANGELOG.md index fb80840ca..f83741744 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) 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.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 @@ 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 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()); @@ -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 configAddresses; @@ -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."); } 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 @@ 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."); } @@ -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); 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 @@ */ 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 void setFiles(List files) { @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 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; } @@ -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 + '\'' + + '}'; + } } 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 7c4f8ac9a..11f75ff44 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.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 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); } } 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/java/com/tencent/cloud/polaris/config/tsf/adaptor/PolarisAdaptorTsfConfigExtensionLayer.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/adaptor/PolarisAdaptorTsfConfigExtensionLayer.java index 94d32cff0..505c793f6 100755 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/adaptor/PolarisAdaptorTsfConfigExtensionLayer.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/tsf/adaptor/PolarisAdaptorTsfConfigExtensionLayer.java @@ -95,9 +95,7 @@ public boolean isEnabled() { } /** - * @see PolarisConfigCustomExtensionLayer#initConfigFiles(CompositePropertySource, - * PolarisPropertySourceManager, - * ConfigFileService) + * @see PolarisConfigCustomExtensionLayer#initConfigFiles(Environment , CompositePropertySource , ConfigFileService ) */ @Override public void initConfigFiles(Environment environment, CompositePropertySource compositePropertySource, 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 f0c000ff2..9f3e87c40 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 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()); @@ -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())) { 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 @@ private void setHostInfo(InetUtils.HostInfo hostInfo) { 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 void setHealthCheckTlsSkipVerify(Boolean healthCheckTlsSkipVerify) { 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 void setTestConnectivityTimeout(int testConnectivityTimeout) { 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 39f8e3b44..8e9cc36cf 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 @@ -20,6 +20,7 @@ import com.tencent.cloud.common.tsf.ConditionalOnTsfEnabled; import com.tencent.cloud.polaris.context.PolarisSDKContextManager; +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; @@ -45,8 +46,9 @@ 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 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 @@ */ 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 TsfMetadataPolarisRegistrationCustomizer(TsfDiscoveryProperties tsfDiscov 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 743444a49..31b0c3c07 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 void init() { 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 SDKContext getConfigSDKContext() { 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 void setTsePolarisEnable(boolean tsePolarisEnable) { 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 334b14f85..a87329b5a 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 void setTtlReadTimeout(int ttlReadTimeout) { 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 bb3f56c07..56d71668d 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 @@ -59,7 +59,7 @@ public void postProcessEnvironment(ConfigurableEnvironment environment, SpringAp String tsfAppId = environment.getProperty("tsf_app_id"); if (StringUtils.isNotBlank(tsfAppId)) { Map defaultProperties = new HashMap<>(); - //默认开启polaris + //enable polaris as default defaultProperties.put("spring.cloud.polaris.enabled", "true"); // tsf_application_id String tsfApplicationId = environment.getProperty("tsf_application_id");