feat:support consul config. (#1319)

* feat:support consul config.

* feat:support consul config.

* feat:support consul config.
pull/1329/head
Haotian Zhang 5 months ago committed by GitHub
parent 74f500b60d
commit ff5a1a027f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -18,3 +18,4 @@
- [fix:fix no registry when lossless is disabled.](https://github.com/Tencent/spring-cloud-tencent/pull/1311) - [fix:fix no registry when lossless is disabled.](https://github.com/Tencent/spring-cloud-tencent/pull/1311)
- [fix:fix the ratelimit bug for 2022](https://github.com/Tencent/spring-cloud-tencent/pull/1314) - [fix:fix the ratelimit bug for 2022](https://github.com/Tencent/spring-cloud-tencent/pull/1314)
- [feat:add Tencent Cloud TSF support.](https://github.com/Tencent/spring-cloud-tencent/pull/1317) - [feat:add Tencent Cloud TSF support.](https://github.com/Tencent/spring-cloud-tencent/pull/1317)
- [feat:support consul config.](https://github.com/Tencent/spring-cloud-tencent/pull/1319)

@ -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.config.config.PolarisCryptoConfigProperties;
import com.tencent.cloud.polaris.context.PolarisConfigurationConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigurationConfigModifier;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties; 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.ConfigurationImpl;
import com.tencent.polaris.factory.config.configuration.ConfigFilterConfigImpl; import com.tencent.polaris.factory.config.configuration.ConfigFilterConfigImpl;
import com.tencent.polaris.factory.config.configuration.ConnectorConfigImpl;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 { public class ConfigurationModifier implements PolarisConfigurationConfigModifier {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigurationModifier.class); 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 PolarisConfigProperties polarisConfigProperties;
private final PolarisCryptoConfigProperties polarisCryptoConfigProperties; private final PolarisCryptoConfigProperties polarisCryptoConfigProperties;
@ -68,15 +67,8 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier
if (!polarisContextProperties.getEnabled() || !polarisConfigProperties.isEnabled()) { if (!polarisContextProperties.getEnabled() || !polarisConfigProperties.isEnabled()) {
return; return;
} }
if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_POLARIS)) {
initByPolarisDataSource(configuration); initDataSource(configuration);
}
else if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_LOCAL)) {
initByLocalDataSource(configuration);
}
else {
throw new RuntimeException("Unsupported config data source");
}
ConfigFilterConfigImpl configFilterConfig = configuration.getConfigFile().getConfigFilterConfig(); ConfigFilterConfigImpl configFilterConfig = configuration.getConfigFile().getConfigFilterConfig();
configFilterConfig.setEnable(polarisCryptoConfigProperties.isEnabled()); configFilterConfig.setEnable(polarisCryptoConfigProperties.isEnabled());
@ -86,19 +78,16 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier
} }
} }
private void initByLocalDataSource(ConfigurationImpl configuration) { private void initDataSource(ConfigurationImpl configuration) {
configuration.getConfigFile().getServerConnector().setConnectorType("localFile"); // set connector type
configuration.getConfigFile().getServerConnector().setConnectorType(polarisConfigProperties.getDataSource());
if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), LOCAL_FILE_CONNECTOR_TYPE)) {
String localFileRootPath = polarisConfigProperties.getLocalFileRootPath(); String localFileRootPath = polarisConfigProperties.getLocalFileRootPath();
configuration.getConfigFile().getServerConnector().setPersistDir(localFileRootPath); configuration.getConfigFile().getServerConnector().setPersistDir(localFileRootPath);
LOGGER.info("[SCT] Run spring cloud tencent config with local data source. localFileRootPath = {}", localFileRootPath); LOGGER.info("[SCT] Run spring cloud tencent config with local data source. localFileRootPath = {}", localFileRootPath);
return;
} }
private void initByPolarisDataSource(ConfigurationImpl configuration) {
// set connector type
configuration.getConfigFile().getServerConnector().setConnectorType("polaris");
// set config server address // set config server address
List<String> configAddresses; List<String> configAddresses;
String configAddressesStr = polarisConfigProperties.getAddress(); String configAddressesStr = polarisConfigProperties.getAddress();
@ -119,6 +108,11 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier
configuration.getConfigFile().getServerConnector().setAddresses(configAddresses); 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."); LOGGER.info("[SCT] Run spring cloud tencent config in polaris data source.");
} }

@ -191,8 +191,12 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
String namespace = polarisContextProperties.getNamespace(); String namespace = polarisContextProperties.getNamespace();
for (ConfigFileGroup configFileGroup : configFileGroups) { 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)) { if (!StringUtils.hasText(group)) {
throw new IllegalArgumentException("polaris config group name cannot be empty."); throw new IllegalArgumentException("polaris config group name cannot be empty.");
} }
@ -203,26 +207,26 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
} }
for (String fileName : files) { for (String fileName : files) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(namespace, group, fileName); PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(groupNamespace, group, fileName);
compositePropertySource.addPropertySource(polarisPropertySource); compositePropertySource.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.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) { private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName) {
ConfigKVFile configKVFile; ConfigKVFile configKVFile;
// unknown extension is resolved as properties file // unknown extension is resolved as yaml file
if (ConfigFileFormat.isPropertyFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) { if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isYamlFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
} }
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else { else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName); LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);

@ -112,7 +112,10 @@ public final class PolarisConfigFilePuller {
*/ */
public void initCustomPolarisConfigFile(CompositePropertySource compositePropertySource, public void initCustomPolarisConfigFile(CompositePropertySource compositePropertySource,
ConfigFileGroup configFileGroup) { ConfigFileGroup configFileGroup) {
String namespace = polarisContextProperties.getNamespace(); String groupNamespace = configFileGroup.getNamespace();
if (!StringUtils.hasText(groupNamespace)) {
groupNamespace = polarisContextProperties.getNamespace();
}
String group = configFileGroup.getName(); String group = configFileGroup.getName();
if (!StringUtils.hasText(group)) { if (!StringUtils.hasText(group)) {
throw new IllegalArgumentException("polaris config group name cannot be empty."); throw new IllegalArgumentException("polaris config group name cannot be empty.");
@ -122,25 +125,24 @@ public final class PolarisConfigFilePuller {
return; return;
} }
for (String fileName : files) { for (String fileName : files) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(namespace, group, fileName); PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(groupNamespace, group, fileName);
compositePropertySource.addPropertySource(polarisPropertySource); compositePropertySource.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(polarisPropertySource); PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info( LOGGER.info(
"[SCT Config] Load and inject polaris config file success. namespace = {}, group = {}, fileName = {}", "[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) { private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName) {
ConfigKVFile configKVFile; ConfigKVFile configKVFile;
// unknown extension is resolved as properties file // unknown extension is resolved as yaml file
if (ConfigFileFormat.isPropertyFile(fileName) if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
|| ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isYamlFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName); configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
} }
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else { else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace,
group, fileName); group, fileName);

@ -26,6 +26,8 @@ import java.util.List;
*/ */
public class ConfigFileGroup { public class ConfigFileGroup {
private String namespace;
/** /**
* group name. * group name.
*/ */
@ -36,6 +38,14 @@ public class ConfigFileGroup {
*/ */
private List<String> files; private List<String> files;
public String getNamespace() {
return namespace;
}
public void setNamespace(String namespace) {
this.namespace = namespace;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -54,6 +64,10 @@ public class ConfigFileGroup {
@Override @Override
public String toString() { public String toString() {
return "ConfigFileGroup{" + "name='" + name + '\'' + ", file=" + files + '}'; return "ConfigFileGroup{" +
"namespace='" + namespace + '\'' +
", name='" + name + '\'' +
", files=" + files +
'}';
} }
} }

@ -50,6 +50,8 @@ public class PolarisConfigProperties {
@Value("${spring.cloud.polaris.config.port:#{'8093'}}") @Value("${spring.cloud.polaris.config.port:#{'8093'}}")
private int port = 8093; private int port = 8093;
private String token;
/** /**
* Whether to automatically update to the spring context when the configuration file. * Whether to automatically update to the spring context when the configuration file.
* is updated * is updated
@ -113,6 +115,14 @@ public class PolarisConfigProperties {
this.port = port; this.port = port;
} }
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public boolean isAutoRefresh() { public boolean isAutoRefresh() {
return autoRefresh; return autoRefresh;
} }
@ -168,4 +178,21 @@ public class PolarisConfigProperties {
public void setLocalFileRootPath(String localFileRootPath) { public void setLocalFileRootPath(String localFileRootPath) {
this.localFileRootPath = 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 + '\'' +
'}';
}
} }

@ -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.config.tsf.controller.PolarisAdaptorTsfConfigController;
import com.tencent.cloud.polaris.context.tsf.ConditionalOnTsfEnabled; import com.tencent.cloud.polaris.context.tsf.ConditionalOnTsfEnabled;
import com.tencent.cloud.polaris.context.tsf.config.TsfCoreProperties; 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 com.tencent.tsf.consul.config.watch.TsfConsulConfigRefreshEventListener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -71,7 +72,7 @@ public class PolarisAdaptorTsfConfigAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public TsfConfigurationModifier tsfConfigModifier(TsfCoreProperties tsfCoreProperties, PolarisConfigProperties polarisConfigProperties) { public TsfConfigurationModifier tsfConfigModifier(TsfCoreProperties tsfCoreProperties, TsfConsulProperties tsfConsulProperties, PolarisConfigProperties polarisConfigProperties) {
return new TsfConfigurationModifier(tsfCoreProperties, polarisConfigProperties); return new TsfConfigurationModifier(tsfCoreProperties, tsfConsulProperties, polarisConfigProperties);
} }
} }

@ -17,10 +17,15 @@
package com.tencent.cloud.polaris.config.tsf; 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.common.constant.OrderConstant;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties; import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.PolarisConfigModifier; import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.tsf.config.TsfCoreProperties; 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; import com.tencent.polaris.factory.config.ConfigurationImpl;
/** /**
@ -33,17 +38,36 @@ public class TsfConfigurationModifier implements PolarisConfigModifier {
private final TsfCoreProperties tsfCoreProperties; private final TsfCoreProperties tsfCoreProperties;
private final TsfConsulProperties tsfConsulProperties;
private final PolarisConfigProperties polarisConfigProperties; private final PolarisConfigProperties polarisConfigProperties;
public TsfConfigurationModifier(TsfCoreProperties tsfCoreProperties, PolarisConfigProperties polarisConfigProperties) { public TsfConfigurationModifier(TsfCoreProperties tsfCoreProperties, TsfConsulProperties tsfConsulProperties, PolarisConfigProperties polarisConfigProperties) {
this.tsfCoreProperties = tsfCoreProperties; this.tsfCoreProperties = tsfCoreProperties;
this.tsfConsulProperties = tsfConsulProperties;
this.polarisConfigProperties = polarisConfigProperties; this.polarisConfigProperties = polarisConfigProperties;
} }
@Override @Override
public void modify(ConfigurationImpl configuration) { public void modify(ConfigurationImpl configuration) {
if (polarisConfigProperties != null && tsfCoreProperties != null) { 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() + "/");
}
} }
} }

@ -21,6 +21,13 @@
"description": "The polaris configuration server port.", "description": "The polaris configuration server port.",
"sourceType": "com.tencent.cloud.polaris.config.config.PolarisConfigProperties" "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", "name": "spring.cloud.polaris.config.auto-refresh",
"type": "java.lang.Boolean", "type": "java.lang.Boolean",

@ -79,9 +79,9 @@ public class TsfDiscoveryConfigModifier implements PolarisConfigModifier {
// namespace id // namespace id
polarisDiscoveryProperties.setHeartbeatInterval(Long.valueOf(tsfHeartbeatProperties.computeHearbeatInterval() polarisDiscoveryProperties.setHeartbeatInterval(Long.valueOf(tsfHeartbeatProperties.computeHearbeatInterval()
.toStandardDuration().getMillis()).intValue()); .toStandardDuration().getMillis()).intValue());
polarisContextProperties.setNamespace(tsfDiscoveryProperties.getTsfNamespaceId()); polarisContextProperties.setNamespace(tsfCoreProperties.getTsfNamespaceId());
polarisDiscoveryProperties.setNamespace(tsfDiscoveryProperties.getTsfNamespaceId()); polarisDiscoveryProperties.setNamespace(tsfCoreProperties.getTsfNamespaceId());
System.setProperty("spring.cloud.polaris.namespace", tsfDiscoveryProperties.getTsfNamespaceId()); System.setProperty("spring.cloud.polaris.namespace", tsfCoreProperties.getTsfNamespaceId());
// application id // application id
polarisDiscoveryProperties.setVersion(tsfDiscoveryProperties.getTsfProgVersion()); polarisDiscoveryProperties.setVersion(tsfDiscoveryProperties.getTsfProgVersion());
@ -134,8 +134,8 @@ public class TsfDiscoveryConfigModifier implements PolarisConfigModifier {
String appName = RegistrationUtil.getAppName(tsfDiscoveryProperties, context.getEnvironment()); String appName = RegistrationUtil.getAppName(tsfDiscoveryProperties, context.getEnvironment());
metadata.put(ConsulConstant.MetadataMapKey.SERVICE_NAME_KEY, RegistrationUtil.normalizeForDns(appName)); metadata.put(ConsulConstant.MetadataMapKey.SERVICE_NAME_KEY, RegistrationUtil.normalizeForDns(appName));
metadata.put(ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY, RegistrationUtil.getInstanceId(tsfDiscoveryProperties, context)); metadata.put(ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY, RegistrationUtil.getInstanceId(tsfDiscoveryProperties, context));
if (StringUtils.isNotBlank(tsfDiscoveryProperties.getAclToken())) { if (StringUtils.isNotBlank(tsfConsulProperties.getAclToken())) {
serverConnectorConfig.setToken(tsfDiscoveryProperties.getAclToken()); serverConnectorConfig.setToken(tsfConsulProperties.getAclToken());
} }
metadata.put(ConsulConstant.MetadataMapKey.TAGS_KEY, JacksonUtils.serialize2Json(RegistrationUtil.createTags(tsfDiscoveryProperties))); metadata.put(ConsulConstant.MetadataMapKey.TAGS_KEY, JacksonUtils.serialize2Json(RegistrationUtil.createTags(tsfDiscoveryProperties)));
if (StringUtils.isNotBlank(tsfDiscoveryProperties.getDefaultQueryTag())) { if (StringUtils.isNotBlank(tsfDiscoveryProperties.getDefaultQueryTag())) {

@ -43,9 +43,6 @@ public class TsfDiscoveryProperties {
private InetUtils.HostInfo hostInfo; 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. * Tags to use when registering service.
*/ */
@ -217,28 +214,6 @@ public class TsfDiscoveryProperties {
*/ */
private Boolean healthCheckTlsSkipVerify; 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. * tsf service consul registration tags.
* *
@ -321,14 +296,6 @@ public class TsfDiscoveryProperties {
this.hostInfo = hostInfo; this.hostInfo = hostInfo;
} }
public String getAclToken() {
return aclToken;
}
public void setAclToken(String aclToken) {
this.aclToken = aclToken;
}
public List<String> getTags() { public List<String> getTags() {
return tags; return tags;
} }
@ -586,30 +553,6 @@ public class TsfDiscoveryProperties {
this.healthCheckTlsSkipVerify = 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() { public String getTsfProgVersion() {
return tsfProgVersion; return tsfProgVersion;
} }
@ -686,7 +629,6 @@ public class TsfDiscoveryProperties {
public String toString() { public String toString() {
return "ConsulDiscoveryProperties{" + return "ConsulDiscoveryProperties{" +
"hostInfo=" + hostInfo + "hostInfo=" + hostInfo +
", aclToken='" + aclToken + '\'' +
", tags=" + tags + ", tags=" + tags +
", enabled=" + enabled + ", enabled=" + enabled +
", managementTags=" + managementTags + ", managementTags=" + managementTags +

@ -19,6 +19,7 @@ package com.tencent.cloud.polaris.tsf.registry;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager; import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.tsf.ConditionalOnTsfEnabled; 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.registry.PolarisServiceRegistryAutoConfiguration;
import com.tencent.cloud.polaris.tsf.TsfDiscoveryProperties; import com.tencent.cloud.polaris.tsf.TsfDiscoveryProperties;
import com.tencent.cloud.polaris.tsf.TsfHeartbeatProperties; import com.tencent.cloud.polaris.tsf.TsfHeartbeatProperties;
@ -43,10 +44,12 @@ import org.springframework.context.annotation.Configuration;
@ConditionalOnTsfEnabled @ConditionalOnTsfEnabled
@AutoConfigureBefore(PolarisServiceRegistryAutoConfiguration.class) @AutoConfigureBefore(PolarisServiceRegistryAutoConfiguration.class)
public class TsfDiscoveryRegistryAutoConfiguration { public class TsfDiscoveryRegistryAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public TsfMetadataPolarisRegistrationCustomizer tsfMetadataPolarisRegistrationCustomizer(TsfDiscoveryProperties tsfDiscoveryProperties) { public TsfMetadataPolarisRegistrationCustomizer tsfMetadataPolarisRegistrationCustomizer(
return new TsfMetadataPolarisRegistrationCustomizer(tsfDiscoveryProperties); TsfCoreProperties tsfCoreProperties, TsfDiscoveryProperties tsfDiscoveryProperties) {
return new TsfMetadataPolarisRegistrationCustomizer(tsfCoreProperties, tsfDiscoveryProperties);
} }
@Bean @Bean

@ -21,12 +21,23 @@ import java.util.Map;
import com.tencent.cloud.common.constant.SdkVersion; import com.tencent.cloud.common.constant.SdkVersion;
import com.tencent.cloud.common.util.JacksonUtils; 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.PolarisRegistration;
import com.tencent.cloud.polaris.registry.PolarisRegistrationCustomizer; import com.tencent.cloud.polaris.registry.PolarisRegistrationCustomizer;
import com.tencent.cloud.polaris.tsf.TsfDiscoveryProperties; import com.tencent.cloud.polaris.tsf.TsfDiscoveryProperties;
import com.tencent.cloud.polaris.tsf.consts.WarmupCons; import com.tencent.cloud.polaris.tsf.consts.WarmupCons;
import com.tencent.cloud.polaris.tsf.util.RegistrationUtil; 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 { public class TsfMetadataPolarisRegistrationCustomizer implements PolarisRegistrationCustomizer {
private final TsfCoreProperties tsfCoreProperties;
private final TsfDiscoveryProperties tsfDiscoveryProperties; private final TsfDiscoveryProperties tsfDiscoveryProperties;
public TsfMetadataPolarisRegistrationCustomizer(TsfDiscoveryProperties tsfDiscoveryProperties) { public TsfMetadataPolarisRegistrationCustomizer(TsfCoreProperties tsfCoreProperties, TsfDiscoveryProperties tsfDiscoveryProperties) {
this.tsfCoreProperties = tsfCoreProperties;
this.tsfDiscoveryProperties = tsfDiscoveryProperties; this.tsfDiscoveryProperties = tsfDiscoveryProperties;
} }
@ -44,17 +58,17 @@ public class TsfMetadataPolarisRegistrationCustomizer implements PolarisRegistra
public void customize(PolarisRegistration registration) { public void customize(PolarisRegistration registration) {
Map<String, String> metadata = registration.getMetadata(); Map<String, String> metadata = registration.getMetadata();
metadata.put("TSF_APPLICATION_ID", tsfDiscoveryProperties.getTsfApplicationId()); metadata.put(TSF_APPLICATION_ID, tsfCoreProperties.getTsfApplicationId());
metadata.put("TSF_PROG_VERSION", tsfDiscoveryProperties.getTsfProgVersion()); metadata.put(TSF_PROG_VERSION, tsfDiscoveryProperties.getTsfProgVersion());
metadata.put("TSF_GROUP_ID", tsfDiscoveryProperties.getTsfGroupId()); metadata.put(TSF_GROUP_ID, tsfCoreProperties.getTsfGroupId());
metadata.put("TSF_NAMESPACE_ID", tsfDiscoveryProperties.getTsfNamespaceId()); metadata.put(TSF_NAMESPACE_ID, tsfCoreProperties.getTsfNamespaceId());
metadata.put("TSF_INSTNACE_ID", tsfDiscoveryProperties.getInstanceId()); metadata.put(TSF_INSTNACE_ID, tsfDiscoveryProperties.getInstanceId());
metadata.put("TSF_REGION", tsfDiscoveryProperties.getTsfRegion()); metadata.put(TSF_REGION, tsfDiscoveryProperties.getTsfRegion());
metadata.put("TSF_ZONE", tsfDiscoveryProperties.getTsfZone()); metadata.put(TSF_ZONE, tsfDiscoveryProperties.getTsfZone());
// 处理预热相关的参数 // 处理预热相关的参数
metadata.put(WarmupCons.TSF_START_TIME, String.valueOf(System.currentTimeMillis())); metadata.put(WarmupCons.TSF_START_TIME, String.valueOf(System.currentTimeMillis()));
metadata.put("TSF_SDK_VERSION", SdkVersion.get()); metadata.put(TSF_SDK_VERSION, SdkVersion.get());
metadata.put("TSF_TAGS", JacksonUtils.serialize2Json(RegistrationUtil.createTags(tsfDiscoveryProperties))); metadata.put(TSF_TAGS, JacksonUtils.serialize2Json(RegistrationUtil.createTags(tsfDiscoveryProperties)));
RegistrationUtil.appendMetaIpAddress(metadata); RegistrationUtil.appendMetaIpAddress(metadata);
} }
} }

@ -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() {
}
}

@ -162,18 +162,6 @@ public class PolarisSDKContextManager {
initConfig(); 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() { public SDKContext getSDKContext() {
initService(); initService();
return serviceSdkContext; return serviceSdkContext;
@ -218,11 +206,30 @@ public class PolarisSDKContextManager {
return configSDKContext; 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() { public void initService() {
if (null == serviceSdkContext) { if (null == serviceSdkContext) {
try { try {
// get modifiers for service.
List<PolarisConfigModifier> serviceModifierList = new ArrayList<>();
for (PolarisConfigModifier modifier : modifierList) {
if (!(modifier instanceof PolarisConfigurationConfigModifier)) {
serviceModifierList.add(modifier);
}
}
// init SDKContext // 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.client.ip-address"),
() -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0))); () -> environment.getProperty("spring.cloud.polaris.local-port", Integer.class, 0)));
serviceSdkContext.init(); serviceSdkContext.init();

@ -37,6 +37,28 @@ public class TsfCoreProperties {
@Value("${tse_polaris_enable:false}") @Value("${tse_polaris_enable:false}")
private boolean tsePolarisEnable = false; private boolean tsePolarisEnable = false;
/**
* tsf service consul registration tags.
* <p>
* applicationId Id
*/
@Value("${tsf_application_id:}")
private String tsfApplicationId;
/**
* tsf service consul registration tags.
* <p>
* groupId Id
*/
@Value("${tsf_group_id:}")
private String tsfGroupId;
/**
* 使.
*/
@Value("${tsf_namespace_id:}")
private String tsfNamespaceId;
public String getTsePolarisIp() { public String getTsePolarisIp() {
return tsePolarisIp; return tsePolarisIp;
} }
@ -61,12 +83,39 @@ public class TsfCoreProperties {
this.tsePolarisEnable = 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 @Override
public String toString() { public String toString() {
return "TsfCoreProperties{" + return "TsfCoreProperties{" +
"tsePolarisIp='" + tsePolarisIp + '\'' + "tsePolarisIp='" + tsePolarisIp + '\'' +
", tsfConsulEnable=" + tsfConsulEnable + ", tsfConsulEnable=" + tsfConsulEnable +
", tsePolarisEnable=" + tsePolarisEnable + ", tsePolarisEnable=" + tsePolarisEnable +
", tsfApplicationId='" + tsfApplicationId + '\'' +
", tsfGroupId='" + tsfGroupId + '\'' +
", tsfNamespaceId='" + tsfNamespaceId + '\'' +
'}'; '}';
} }
} }

@ -53,6 +53,9 @@ public class TsfConsulProperties {
@Value("${tsf_consul_ttl_read_timeout:5000}") @Value("${tsf_consul_ttl_read_timeout:5000}")
private int ttlReadTimeout = 5000; // default 5s 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() { public String getHost() {
return host; return host;
} }
@ -93,15 +96,23 @@ public class TsfConsulProperties {
this.ttlReadTimeout = ttlReadTimeout; this.ttlReadTimeout = ttlReadTimeout;
} }
public String getAclToken() {
return aclToken;
}
public void setAclToken(String aclToken) {
this.aclToken = aclToken;
}
@Override @Override
public String toString() { public String toString() {
return "ConsulProperties{" + return "TsfConsulProperties{" +
"host='" + host + '\'' + "host='" + host + '\'' +
", scheme='" + scheme + '\'' + ", scheme='" + scheme + '\'' +
", port=" + port + ", port=" + port +
", enabled=" + enabled + ", enabled=" + enabled +
", ttlReadTimeout=" + ttlReadTimeout + ", ttlReadTimeout=" + ttlReadTimeout +
", aclToken='" + aclToken + '\'' +
'}'; '}';
} }
} }

@ -21,10 +21,12 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.tencent.polaris.api.utils.StringUtils; import com.tencent.polaris.api.utils.StringUtils;
import org.apache.commons.logging.Log;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor; import org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor;
import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.logging.DeferredLogFactory;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
@ -34,13 +36,19 @@ import org.springframework.core.env.MapPropertySource;
* *
* @author Haotian Zhang * @author Haotian Zhang
*/ */
public class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
/** /**
* run before {@link ConfigDataEnvironmentPostProcessor}. * run before {@link ConfigDataEnvironmentPostProcessor}.
*/ */
public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER - 1; public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER - 1;
private final Log LOGGER;
private TsfCoreEnvironmentPostProcessor(DeferredLogFactory logFactory) {
this.LOGGER = logFactory.getLog(getClass());
}
@Override @Override
public int getOrder() { public int getOrder() {
return ORDER; return ORDER;
@ -52,9 +60,58 @@ public class TsfCoreEnvironmentPostProcessor implements EnvironmentPostProcessor
if (StringUtils.isNotBlank(tsfAppId)) { if (StringUtils.isNotBlank(tsfAppId)) {
Map<String, Object> defaultProperties = new HashMap<>(); Map<String, Object> 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 // 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 // tse_polaris_ip
defaultProperties.put("spring.cloud.polaris.address", "grpc://" + environment.getProperty("tse_polaris_ip", "") + ":8091"); defaultProperties.put("spring.cloud.polaris.address", "grpc://" + environment.getProperty("tse_polaris_ip", "") + ":8091");

Loading…
Cancel
Save