feat:support dynamic multi-discovery. (#1591)

pull/1592/head
Haotian Zhang 3 months ago committed by GitHub
parent ad3c537792
commit 96221289b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -3,3 +3,4 @@
- [fix: add gateway context config example.](https://github.com/Tencent/spring-cloud-tencent/pull/1562)
- [feat:support config empty protection.](https://github.com/Tencent/spring-cloud-tencent/pull/1581)
- [feat:support dynamic multi-discovery.](https://github.com/Tencent/spring-cloud-tencent/pull/1591)

@ -69,5 +69,10 @@
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-fault-tolerance</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-multi-discovery-plugin</artifactId>
</dependency>
</dependencies>
</project>

@ -21,20 +21,15 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.enums.ConfigFileFormat;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.api.utils.ClassUtils;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.client.internal.CompositeConfigFile;
import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -45,6 +40,9 @@ import org.springframework.core.env.CompositePropertySource;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertySource;
import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadGroupPolarisPropertySource;
import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadPolarisPropertySource;
/**
* Spring cloud reserved core configuration loading SPI.
* <p>
@ -74,64 +72,6 @@ public class PolarisConfigFileLocator implements PropertySourceLocator {
this.environment = environment;
}
public static PolarisPropertySource loadPolarisPropertySource(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile = loadConfigKVFile(configFileService, namespace, group, fileName);
Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : configKVFile.getPropertyNames()) {
map.put(key, configKVFile.getProperty(key, null));
}
return new PolarisPropertySource(namespace, group, fileName, configKVFile, map);
}
public static PolarisPropertySource loadGroupPolarisPropertySource(ConfigFileService configFileService, String namespace, String group) {
List<ConfigKVFile> configKVFiles = new ArrayList<>();
com.tencent.polaris.configuration.api.core.ConfigFileGroup remoteGroup = configFileService.getConfigFileGroup(namespace, group);
if (remoteGroup == null) {
return null;
}
for (ConfigFileMetadata configFile : remoteGroup.getConfigFileMetadataList()) {
String fileName = configFile.getFileName();
ConfigKVFile configKVFile = loadConfigKVFile(configFileService, namespace, group, fileName);
configKVFiles.add(configKVFile);
}
CompositeConfigFile compositeConfigFile = new CompositeConfigFile(configKVFiles);
Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : compositeConfigFile.getPropertyNames()) {
String value = compositeConfigFile.getProperty(key, null);
map.put(key, value);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("namespace='" + namespace + '\''
+ ", group='" + group + '\'' + ", fileName='" + compositeConfigFile + '\''
+ ", map='" + map + '\'');
}
return new PolarisPropertySource(namespace, group, "", compositeConfigFile, map);
}
public static ConfigKVFile loadConfigKVFile(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile;
// unknown extension is resolved as yaml file
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);
throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context");
}
return configKVFile;
}
/**
* order: spring boot default config files > custom config files > tsf default config group.

@ -21,24 +21,24 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.configdata.PolarisConfigDataLoader;
import com.tencent.cloud.polaris.config.enums.ConfigFileFormat;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.api.utils.ClassUtils;
import com.tencent.polaris.api.utils.CollectionUtils;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.client.internal.DefaultConfigFileMetadata;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.CompositePropertySource;
import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadGroupPolarisPropertySource;
import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadPolarisPropertySource;
/**
* PolarisConfigFilePuller pull configFile from Polaris.
*
@ -82,7 +82,7 @@ public final class PolarisConfigFilePuller {
String[] defaultProfiles, String serviceName) {
List<ConfigFileMetadata> internalConfigFiles = getInternalConfigFiles(activeProfiles, defaultProfiles, serviceName);
for (ConfigFileMetadata configFile : internalConfigFiles) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService,
configFile.getNamespace(), configFile.getFileGroup(), configFile.getFileName());
compositePropertySource.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
@ -124,7 +124,7 @@ public final class PolarisConfigFilePuller {
return;
}
for (String fileName : files) {
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(groupNamespace, group, fileName);
PolarisPropertySource polarisPropertySource = loadPolarisPropertySource(configFileService, groupNamespace, group, fileName);
compositePropertySource.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
LOGGER.info(
@ -133,27 +133,35 @@ public final class PolarisConfigFilePuller {
}
}
private PolarisPropertySource loadPolarisPropertySource(String namespace, String group, String fileName) {
ConfigKVFile configKVFile;
// unknown extension is resolved as yaml file
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
/**
* Init TSF config groups.
* @param compositePropertySource compositePropertySource
*/
public void initTsfConfigGroups(CompositePropertySource compositePropertySource) {
String tsfId = System.getProperty("tsf_id");
String tsfNamespaceName = System.getProperty("tsf_namespace_name");
String tsfGroupName = System.getProperty("tsf_group_name");
if (StringUtils.isEmpty(tsfNamespaceName) || StringUtils.isEmpty(tsfGroupName)) {
return;
}
else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace,
group, fileName);
String namespace = polarisContextProperties.getNamespace();
List<String> tsfConfigGroups = new ArrayList<>();
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".application_config_group");
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfNamespaceName + ".global_config_group");
throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml"
+ " can be injected into the spring context");
if (ClassUtils.isClassPresent("org.springframework.cloud.gateway.filter.GlobalFilter")) {
tsfConfigGroups.add((StringUtils.isNotBlank(tsfId) ? tsfId + "." : "") + tsfGroupName + ".gateway_config_group");
}
Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : configKVFile.getPropertyNames()) {
map.put(key, configKVFile.getProperty(key, null));
for (String tsfConfigGroup : tsfConfigGroups) {
PolarisPropertySource polarisPropertySource = loadGroupPolarisPropertySource(configFileService, namespace, tsfConfigGroup);
if (polarisPropertySource == null) {
// not register to polaris
continue;
}
compositePropertySource.addPropertySource(polarisPropertySource);
PolarisPropertySourceManager.addPropertySource(polarisPropertySource);
}
return new PolarisPropertySource(namespace, group, fileName, configKVFile, map);
}
private List<ConfigFileMetadata> getInternalConfigFiles(

@ -53,6 +53,9 @@ import org.springframework.core.env.PropertySource;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;
import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadGroupPolarisPropertySource;
import static com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils.loadPolarisPropertySource;
/**
* 1. Listen to the Polaris server configuration publishing event 2. Write the changed
* configuration content to propertySource 3. Refresh the context through contextRefresher
@ -153,7 +156,7 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
registeredPolarisPropertySets.add(entry.getKey());
LOGGER.info("[SCT Config] add polaris config file:{}", entry.getKey());
ConfigFileMetadata configFileMetadata = entry.getValue();
PolarisPropertySource p = PolarisConfigFileLocator.loadPolarisPropertySource(
PolarisPropertySource p = loadPolarisPropertySource(
configFileService, configFileMetadata.getNamespace(),
configFileMetadata.getFileGroup(), configFileMetadata.getFileName());
LOGGER.info("[SCT Config] changed property = {}", p.getSource().keySet());
@ -194,7 +197,7 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
PolarisPropertySource newGroupSource = null;
if (isGroupRefresh) {
newGroupSource = PolarisConfigFileLocator.loadGroupPolarisPropertySource(configFileService,
newGroupSource = loadGroupPolarisPropertySource(configFileService,
effectPolarisPropertySource.getNamespace(), effectPolarisPropertySource.getGroup());
}
@ -301,7 +304,8 @@ public abstract class PolarisConfigPropertyAutoRefresher implements ApplicationL
.withClientIp(context.getExtensions().getValueContext().getHost())
.withNamespace(polarisPropertySource.getNamespace())
.withConfigGroup(polarisPropertySource.getGroup())
.withConfigVersion(Optional.ofNullable(configKVFileChangeEvent.getConfigFile()).map(ConfigFile::getName).orElse(null))
.withConfigVersion(Optional.ofNullable(configKVFileChangeEvent.getConfigFile()).map(ConfigFile::getName)
.orElse(null))
.withConfigFileName(polarisPropertySource.getFileName());
BaseFlow.reportConfigEvent(context.getExtensions(), builder.build());

@ -130,6 +130,8 @@ public class PolarisConfigDataLoader implements ConfigDataLoader<PolarisConfigDa
log.info("loading config data config file, group:" + resource.getGroupName() + " file: " + resource.getFileName());
this.puller.initCustomPolarisConfigFile(compositePropertySource, configFileGroup(resource));
}
// load tsf default config group
this.puller.initTsfConfigGroups(compositePropertySource);
if (polarisConfigCustomExtensionLayer != null) {
polarisConfigCustomExtensionLayer.executeAfterLocateConfigReturning(compositePropertySource);
}

@ -17,7 +17,7 @@
package com.tencent.cloud.polaris.config.tsf;
import com.tencent.cloud.common.tsf.ConditionalOnTsfConsulEnabled;
import com.tencent.cloud.common.tsf.ConditionalOnOnlyTsfConsulEnabled;
import com.tencent.cloud.polaris.config.ConditionalOnPolarisConfigEnabled;
import com.tencent.cloud.polaris.config.tsf.controller.PolarisAdaptorTsfConfigController;
import com.tencent.tsf.consul.config.watch.TsfConsulConfigRefreshEventListener;
@ -33,7 +33,7 @@ import org.springframework.context.annotation.Configuration;
* @Date Jul 23, 2023 3:52:48 PM
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnTsfConsulEnabled
@ConditionalOnOnlyTsfConsulEnabled
@ConditionalOnPolarisConfigEnabled
public class PolarisAdaptorTsfConfigAutoConfiguration {

@ -17,6 +17,20 @@
package com.tencent.cloud.polaris.config.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import com.tencent.cloud.polaris.config.adapter.PolarisPropertySource;
import com.tencent.cloud.polaris.config.enums.ConfigFileFormat;
import com.tencent.polaris.configuration.api.core.ConfigFileMetadata;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
import com.tencent.polaris.configuration.client.internal.CompositeConfigFile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utils for PolarisPropertySource.
*
@ -24,6 +38,8 @@ package com.tencent.cloud.polaris.config.utils;
*/
public final class PolarisPropertySourceUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(PolarisPropertySourceUtils.class);
private PolarisPropertySourceUtils() {
}
@ -31,4 +47,63 @@ public final class PolarisPropertySourceUtils {
public static String generateName(String namespace, String group, String fileName) {
return namespace + "-" + group + "-" + fileName;
}
public static PolarisPropertySource loadPolarisPropertySource(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile = loadConfigKVFile(configFileService, namespace, group, fileName);
Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : configKVFile.getPropertyNames()) {
map.put(key, configKVFile.getProperty(key, null));
}
return new PolarisPropertySource(namespace, group, fileName, configKVFile, map);
}
public static PolarisPropertySource loadGroupPolarisPropertySource(ConfigFileService configFileService, String namespace, String group) {
List<ConfigKVFile> configKVFiles = new ArrayList<>();
com.tencent.polaris.configuration.api.core.ConfigFileGroup remoteGroup = configFileService.getConfigFileGroup(namespace, group);
if (remoteGroup == null) {
return null;
}
for (ConfigFileMetadata configFile : remoteGroup.getConfigFileMetadataList()) {
String fileName = configFile.getFileName();
ConfigKVFile configKVFile = loadConfigKVFile(configFileService, namespace, group, fileName);
configKVFiles.add(configKVFile);
}
CompositeConfigFile compositeConfigFile = new CompositeConfigFile(configKVFiles);
Map<String, Object> map = new ConcurrentHashMap<>();
for (String key : compositeConfigFile.getPropertyNames()) {
String value = compositeConfigFile.getProperty(key, null);
map.put(key, value);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("namespace='" + namespace + '\''
+ ", group='" + group + '\'' + ", fileName='" + compositeConfigFile + '\''
+ ", map='" + map + '\'');
}
return new PolarisPropertySource(namespace, group, "", compositeConfigFile, map);
}
public static ConfigKVFile loadConfigKVFile(ConfigFileService configFileService, String namespace, String group, String fileName) {
ConfigKVFile configKVFile;
// unknown extension is resolved as yaml file
if (ConfigFileFormat.isYamlFile(fileName) || ConfigFileFormat.isUnknownFile(fileName)) {
configKVFile = configFileService.getConfigYamlFile(namespace, group, fileName);
}
else if (ConfigFileFormat.isPropertyFile(fileName)) {
configKVFile = configFileService.getConfigPropertiesFile(namespace, group, fileName);
}
else {
LOGGER.warn("[SCT Config] Unsupported config file. namespace = {}, group = {}, fileName = {}", namespace, group, fileName);
throw new IllegalStateException("Only configuration files in the format of properties / yaml / yaml" + " can be injected into the spring context");
}
return configKVFile;
}
}

@ -28,6 +28,7 @@ import java.util.Map;
import com.tencent.cloud.polaris.config.config.ConfigFileGroup;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.config.enums.RefreshType;
import com.tencent.cloud.polaris.config.utils.PolarisPropertySourceUtils;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.api.core.ConfigKVFile;
@ -299,8 +300,8 @@ public class PolarisConfigFileLocatorTest {
when(mockPropertySource.getPropertySourceName()).thenReturn(expectedAppConfigGroup);
CompositePropertySource compositePropertySource = mock(CompositePropertySource.class);
try (MockedStatic<PolarisConfigFileLocator> mockedStatic = mockStatic(PolarisConfigFileLocator.class)) {
mockedStatic.when(() -> PolarisConfigFileLocator.loadGroupPolarisPropertySource(
try (MockedStatic<PolarisPropertySourceUtils> mockedStatic = mockStatic(PolarisPropertySourceUtils.class)) {
mockedStatic.when(() -> PolarisPropertySourceUtils.loadGroupPolarisPropertySource(
eq(configFileService),
eq(polarisNamespace),
any()

@ -237,6 +237,13 @@ public class PolarisRegistration implements Registration {
return registerEnabled;
}
public String getNamespace() {
if (polarisDiscoveryProperties != null) {
return polarisDiscoveryProperties.getNamespace();
}
return null;
}
public SDKContext getPolarisContext() {
return polarisContext;
}

@ -199,6 +199,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<PolarisRegistrati
}
InstanceDeregisterRequest deRegisterRequest = new InstanceDeregisterRequest();
deRegisterRequest.setInstanceID(registration.getInstanceId());
deRegisterRequest.setToken(polarisDiscoveryProperties.getToken());
deRegisterRequest.setNamespace(polarisDiscoveryProperties.getNamespace());
deRegisterRequest.setService(registration.getServiceId());

@ -17,7 +17,7 @@
package com.tencent.cloud.polaris.ratelimit.tsf;
import com.tencent.cloud.common.tsf.ConditionalOnTsfConsulEnabled;
import com.tencent.cloud.common.tsf.ConditionalOnOnlyTsfConsulEnabled;
import com.tencent.cloud.polaris.context.config.extend.consul.ConsulProperties;
import com.tencent.cloud.polaris.context.config.extend.tsf.TsfCoreProperties;
@ -32,7 +32,7 @@ import org.springframework.core.env.Environment;
* @author Haotian Zhang
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnTsfConsulEnabled
@ConditionalOnOnlyTsfConsulEnabled
public class TsfRateLimitAutoConfiguration {
@Bean

@ -17,7 +17,7 @@
package com.tencent.cloud.polaris.ratelimit.tsf;
import com.tencent.cloud.common.tsf.ConditionalOnTsfConsulEnabled;
import com.tencent.cloud.common.tsf.ConditionalOnOnlyTsfConsulEnabled;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@ -28,7 +28,7 @@ import org.springframework.context.annotation.Import;
* @author Haotian Zhang
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnTsfConsulEnabled
@ConditionalOnOnlyTsfConsulEnabled
@Import(TsfRateLimitAutoConfiguration.class)
public class TsfRateLimitBootstrapConfiguration {
}

@ -22,7 +22,6 @@ import java.util.Map;
import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.cloud.polaris.context.config.extend.consul.ConsulProperties;
import com.tencent.cloud.polaris.context.config.extend.tsf.TsfContextUtils;
import com.tencent.cloud.polaris.context.config.extend.tsf.TsfCoreProperties;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.ratelimit.client.sync.tsf.TsfRateLimitConstants;
@ -51,14 +50,12 @@ public class TsfRateLimitConfigModifier implements PolarisConfigModifier {
@Override
public void modify(ConfigurationImpl configuration) {
if (TsfContextUtils.isTsfConsulEnabled(environment)) {
Map<String, String> metadata = configuration.getProvider().getRateLimit().getMetadata();
metadata.put(TsfRateLimitConstants.RATE_LIMIT_MASTER_IP_KEY, tsfCoreProperties.getRatelimitMasterIp());
metadata.put(TsfRateLimitConstants.RATE_LIMIT_MASTER_PORT_KEY, String.valueOf(tsfCoreProperties.getRatelimitMasterPort()));
metadata.put(TsfRateLimitConstants.SERVICE_NAME_KEY, tsfCoreProperties.getServiceName());
metadata.put(TsfRateLimitConstants.INSTANCE_ID_KEY, tsfCoreProperties.getInstanceId());
metadata.put(TsfRateLimitConstants.TOKEN_KEY, consulProperties.getAclToken());
}
Map<String, String> metadata = configuration.getProvider().getRateLimit().getMetadata();
metadata.put(TsfRateLimitConstants.RATE_LIMIT_MASTER_IP_KEY, tsfCoreProperties.getRatelimitMasterIp());
metadata.put(TsfRateLimitConstants.RATE_LIMIT_MASTER_PORT_KEY, String.valueOf(tsfCoreProperties.getRatelimitMasterPort()));
metadata.put(TsfRateLimitConstants.SERVICE_NAME_KEY, tsfCoreProperties.getServiceName());
metadata.put(TsfRateLimitConstants.INSTANCE_ID_KEY, tsfCoreProperties.getInstanceId());
metadata.put(TsfRateLimitConstants.TOKEN_KEY, consulProperties.getAclToken());
}
@Override

@ -0,0 +1,47 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 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;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* Condition that if Only TSF Consul enabled.
*
* @author Haotian Zhang
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Conditional(ConditionalOnOnlyTsfConsulEnabled.OnTsfEnabledCondition.class)
public @interface ConditionalOnOnlyTsfConsulEnabled {
class OnTsfEnabledCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return TsfContextUtils.isOnlyTsfConsulEnabled(conditionContext.getEnvironment());
}
}
}

@ -22,16 +22,13 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.tencent.polaris.api.utils.StringUtils;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* Condition that if Polaris enabled.
* Condition that if TSF Consul enabled.
*
* @author Haotian Zhang
*/
@ -44,20 +41,7 @@ public @interface ConditionalOnTsfConsulEnabled {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
Environment environment = conditionContext.getEnvironment();
boolean tsfConsulEnable = false;
String tsfAppId = environment.getProperty("tsf_app_id");
if (StringUtils.isNotBlank(tsfAppId)) {
String tsfConsulIp = environment.getProperty("tsf_consul_ip");
String tsePolarisAddress = environment.getProperty("polaris_address");
if (StringUtils.isBlank(tsePolarisAddress) && StringUtils.isNotBlank(environment.getProperty("spring.cloud.polaris.address"))) {
tsePolarisAddress = environment.getProperty("spring.cloud.polaris.address");
}
tsfConsulEnable = StringUtils.isNotBlank(tsfConsulIp) && StringUtils.isBlank(tsePolarisAddress);
}
return tsfConsulEnable;
return TsfContextUtils.isTsfConsulEnabled(conditionContext.getEnvironment());
}
}
}

@ -0,0 +1,80 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 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;
import java.util.concurrent.atomic.AtomicBoolean;
import com.tencent.polaris.api.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
/**
* Utils for TSF.
*
* @author Haotian Zhang
*/
public final class TsfContextUtils {
private static final Logger LOG = LoggerFactory.getLogger(TsfContextUtils.class);
private static final AtomicBoolean isTsfConsulEnabledFirstConfiguration = new AtomicBoolean(true);
private static final AtomicBoolean isOnlyTsfConsulEnabledFirstConfiguration = new AtomicBoolean(true);
private static boolean tsfConsulEnabled = false;
private static boolean onlyTsfConsulEnabled = false;
private TsfContextUtils() {
}
public static boolean isTsfConsulEnabled(Environment environment) {
if (environment != null && isTsfConsulEnabledFirstConfiguration.compareAndSet(true, false)) {
if (isOnlyTsfConsulEnabled(environment)) {
tsfConsulEnabled = true;
}
else {
boolean consulEnabled = Boolean.parseBoolean(environment.getProperty("tsf_consul_enable", "true"));
String tsfConsulIp = environment.getProperty("tsf_consul_ip");
tsfConsulEnabled = consulEnabled && StringUtils.isNotBlank(tsfConsulIp);
if (tsfConsulEnabled) {
LOG.info("Tsf Consul is enabled: {}", tsfConsulIp);
}
}
}
return tsfConsulEnabled;
}
public static boolean isOnlyTsfConsulEnabled(Environment environment) {
if (environment != null && isOnlyTsfConsulEnabledFirstConfiguration.compareAndSet(true, false)) {
boolean consulEnabled = Boolean.parseBoolean(environment.getProperty("tsf_consul_enable", "true"));
String tsfConsulIp = environment.getProperty("tsf_consul_ip");
String polarisAddress = environment.getProperty("polaris_address");
if (StringUtils.isBlank(polarisAddress) && StringUtils.isNotBlank(environment.getProperty("spring.cloud.polaris.address"))) {
polarisAddress = environment.getProperty("spring.cloud.polaris.address");
}
onlyTsfConsulEnabled = consulEnabled && StringUtils.isNotBlank(tsfConsulIp) && StringUtils.isBlank(polarisAddress);
if (onlyTsfConsulEnabled) {
LOG.info("Only Tsf Consul is enabled: {}", tsfConsulIp);
}
}
return onlyTsfConsulEnabled;
}
}

@ -88,6 +88,12 @@
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-tencent-lossless-plugin</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-multi-discovery-plugin</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
<build>

@ -218,6 +218,12 @@
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-multi-discovery-plugin</artifactId>
<version>${revision}</version>
</dependency>
<!-- third part framework dependencies -->
<dependency>
<groupId>org.springdoc</groupId>

@ -22,6 +22,7 @@
<module>spring-cloud-starter-tencent-trace-plugin</module>
<module>spring-cloud-starter-tencent-fault-tolerance</module>
<module>spring-cloud-tencent-security-protection-plugin</module>
<module>spring-cloud-starter-tencent-multi-discovery-plugin</module>
</modules>
</project>

@ -0,0 +1,25 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>spring-cloud-tencent-plugin-starters</artifactId>
<groupId>com.tencent.cloud</groupId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-starter-tencent-multi-discovery-plugin</artifactId>
<name>Spring Cloud Tencent Trace Plugin</name>
<dependencies>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-config</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,46 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 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.plugin.discovery.multi.config;
import com.tencent.cloud.plugin.discovery.multi.listeners.ConsulDiscoveryConfigChangeListener;
import com.tencent.cloud.polaris.registry.PolarisRegistration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Auto configuration for multi discovery.
*
* @author Haotian Zhang
*/
@Configuration(proxyBeanMethods = false)
public class MultiDiscoveryAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(value = "spring.cloud.consul.enabled", havingValue = "true")
protected static class ConsulMultiDiscoveryConfig {
@Bean
@ConditionalOnMissingBean
public ConsulDiscoveryConfigChangeListener consulDiscoveryConfigChangeListener(PolarisRegistration polarisRegistration) {
return new ConsulDiscoveryConfigChangeListener(polarisRegistration);
}
}
}

@ -0,0 +1,114 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 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.plugin.discovery.multi.listeners;
import java.util.Set;
import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener;
import com.tencent.cloud.polaris.config.listener.ConfigChangeEvent;
import com.tencent.cloud.polaris.registry.PolarisRegistration;
import com.tencent.polaris.api.plugin.common.PluginTypes;
import com.tencent.polaris.api.plugin.server.ServerConnector;
import com.tencent.polaris.api.rpc.InstanceRegisterRequest;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.discovery.client.flow.RegisterStateManager;
import com.tencent.polaris.plugins.connector.common.DestroyableServerConnector;
import com.tencent.polaris.plugins.connector.composite.CompositeConnector;
import com.tencent.polaris.plugins.connector.consul.ConsulAPIConnector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Consul Discovery Config Listener .
*
* @author Haotian Zhang
*/
public final class ConsulDiscoveryConfigChangeListener {
private static final Logger LOG = LoggerFactory.getLogger(ConsulDiscoveryConfigChangeListener.class);
private final PolarisRegistration polarisRegistration;
private final SDKContext sdkContext;
private final ConsulAPIConnector consulAPIConnector;
private InstanceRegisterRequest instanceRegisterRequest;
public ConsulDiscoveryConfigChangeListener(PolarisRegistration polarisRegistration) {
this.polarisRegistration = polarisRegistration;
this.sdkContext = polarisRegistration.getPolarisContext();
ServerConnector connector = (ServerConnector) sdkContext.getPlugins()
.getPlugin(PluginTypes.SERVER_CONNECTOR.getBaseType(), sdkContext.getValueContext()
.getServerConnectorProtocol());
ConsulAPIConnector temp = null;
if (connector instanceof CompositeConnector) {
CompositeConnector compositeConnector = (CompositeConnector) connector;
for (DestroyableServerConnector sc : compositeConnector.getServerConnectors()) {
if (sc instanceof ConsulAPIConnector) {
temp = (ConsulAPIConnector) sc;
break;
}
}
}
else if (connector instanceof ConsulAPIConnector) {
temp = (ConsulAPIConnector) connector;
}
this.consulAPIConnector = temp;
}
@PolarisConfigKVFileChangeListener(interestedKeyPrefixes = "spring.cloud.consul.discovery")
public void onChange(ConfigChangeEvent event) {
if (consulAPIConnector != null) {
initInstanceRegisterRequest();
Set<String> changedKeys = event.changedKeys();
for (String changedKey : changedKeys) {
if (StringUtils.equals(changedKey, "spring.cloud.consul.discovery.enabled")) {
LOG.info("{} = {}", changedKey, event.getChange(changedKey));
boolean discoveryEnabled = !StringUtils.equals("false", event.getChange(changedKey).getNewValue()
.toString());
consulAPIConnector.setDiscoveryEnable(discoveryEnabled);
}
else if (StringUtils.equals(changedKey, "spring.cloud.consul.discovery.register")) {
LOG.info("{} = {}", changedKey, event.getChange(changedKey));
boolean registerEnabled = !StringUtils.equals("false", event.getChange(changedKey).getNewValue()
.toString());
if (registerEnabled) {
consulAPIConnector.registerInstance(RegisterStateManager.getRegisterState(sdkContext, instanceRegisterRequest)
.getInstanceRegisterRequest().getRequest(), null);
}
else {
consulAPIConnector.deregisterInstance(RegisterStateManager.getRegisterState(sdkContext, instanceRegisterRequest)
.getInstanceRegisterRequest().getRequest());
}
}
}
}
}
private void initInstanceRegisterRequest() {
if (instanceRegisterRequest == null) {
this.instanceRegisterRequest = new InstanceRegisterRequest();
instanceRegisterRequest.setNamespace(polarisRegistration.getNamespace());
instanceRegisterRequest.setService(polarisRegistration.getServiceId());
instanceRegisterRequest.setHost(polarisRegistration.getHost());
instanceRegisterRequest.setPort(polarisRegistration.getPort());
}
}
}

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tencent.cloud.plugin.discovery.multi.config.MultiDiscoveryAutoConfiguration

@ -17,7 +17,7 @@
package com.tencent.cloud.polaris.context.config.extend.tsf;
import com.tencent.cloud.common.tsf.ConditionalOnTsfConsulEnabled;
import com.tencent.cloud.common.tsf.ConditionalOnOnlyTsfConsulEnabled;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.cloud.polaris.context.config.extend.consul.ConsulProperties;
@ -33,7 +33,7 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(PolarisContextAutoConfiguration.class)
@ConditionalOnTsfConsulEnabled
@ConditionalOnOnlyTsfConsulEnabled
public class TsfContextAutoConfiguration {
@Bean

@ -1,58 +0,0 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.context.config.extend.tsf;
import java.util.concurrent.atomic.AtomicBoolean;
import com.tencent.polaris.api.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
/**
* Utils for TSF.
*
* @author Haotian Zhang
*/
public final class TsfContextUtils {
private static final Logger LOG = LoggerFactory.getLogger(TsfContextUtils.class);
private static final AtomicBoolean isFirstConfiguration = new AtomicBoolean(true);
private static boolean tsfConsulEnabled = false;
private TsfContextUtils() {
}
public static boolean isTsfConsulEnabled(Environment environment) {
if (environment != null && isFirstConfiguration.compareAndSet(true, false)) {
String tsfConsulIp = environment.getProperty("tsf_consul_ip");
String tsePolarisAddress = environment.getProperty("polaris_address");
if (StringUtils.isBlank(tsePolarisAddress) && StringUtils.isNotBlank(environment.getProperty("spring.cloud.polaris.address"))) {
tsePolarisAddress = environment.getProperty("spring.cloud.polaris.address");
}
tsfConsulEnabled = StringUtils.isNotBlank(tsfConsulIp) && StringUtils.isBlank(tsePolarisAddress);
if (tsfConsulEnabled) {
LOG.info("Tsf Consul is enabled: {}", tsfConsulIp);
}
}
return tsfConsulEnabled;
}
}

@ -20,6 +20,7 @@ package com.tencent.cloud.polaris.context.config.extend.tsf;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.common.tsf.TsfContextUtils;
import com.tencent.polaris.api.utils.StringUtils;
import org.apache.commons.logging.Log;
@ -64,13 +65,13 @@ public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostPro
// enabled
String polarisEnabled = environment.getProperty("spring.cloud.polaris.enabled");
if (StringUtils.isBlank(polarisEnabled)) {
defaultProperties.put("spring.cloud.polaris.enabled", true);
defaultProperties.put("spring.cloud.polaris.enabled", "true");
}
// lossless
String polarisAdminPort = environment.getProperty("polaris_admin_port");
if (StringUtils.isNotBlank(polarisAdminPort)) {
defaultProperties.put("spring.cloud.polaris.lossless.enabled", true);
defaultProperties.put("spring.cloud.polaris.lossless.enabled", environment.getProperty("spring.cloud.polaris.lossless.enabled", "true"));
}
if (TsfContextUtils.isTsfConsulEnabled(environment)) {
@ -114,16 +115,12 @@ public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostPro
// context
defaultProperties.put("spring.cloud.polaris.enabled", "true");
defaultProperties.put("spring.cloud.polaris.discovery.enabled", "false");
defaultProperties.put("spring.cloud.polaris.discovery.register", "false");
defaultProperties.put("spring.cloud.consul.enabled", "true");
defaultProperties.put("spring.cloud.consul.enabled", environment.getProperty("tsf_consul_enable", "true"));
defaultProperties.put("spring.cloud.consul.host", tsfConsulIp);
defaultProperties.put("spring.cloud.consul.port", tsfConsulPort);
defaultProperties.put("spring.cloud.consul.token", tsfConsulToken);
// discovery
defaultProperties.put("spring.cloud.consul.discovery.enabled", "true");
defaultProperties.put("spring.cloud.consul.discovery.register", "true");
defaultProperties.put("spring.cloud.consul.discovery.instance-id", tsfInstanceId);
defaultProperties.put("spring.cloud.polaris.discovery.instance-id", tsfInstanceId);
defaultProperties.put("spring.cloud.polaris.discovery.zero-protection.enabled",
@ -147,25 +144,31 @@ public final class TsfCoreEnvironmentPostProcessor implements EnvironmentPostPro
defaultProperties.put("spring.cloud.polaris.contract.report.enabled", environment.getProperty("tsf.swagger.enabled", "true"));
defaultProperties.put("spring.cloud.polaris.contract.name", tsfApplicationId);
// configuration
defaultProperties.put("spring.cloud.polaris.config.enabled", "true");
defaultProperties.put("spring.cloud.polaris.config.internal-enabled", "false");
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]", tsfApplicationId + "/" + tsfGroupId + "/");
defaultProperties.put("spring.cloud.polaris.config.groups[0].files[1]", tsfNamespaceId + "/");
defaultProperties.put("spring.cloud.polaris.config.refresh-type",
environment.getProperty("spring.cloud.polaris.config.refresh-type", "refresh_context"));
// router
defaultProperties.put("spring.cloud.polaris.router.rule-router.fail-over",
environment.getProperty("spring.cloud.polaris.router.rule-router.fail-over", "none"));
defaultProperties.put("spring.cloud.polaris.router.namespace-router.enabled",
environment.getProperty("spring.cloud.polaris.router.namespace-router.enabled", "true"));
if (TsfContextUtils.isOnlyTsfConsulEnabled(environment)) {
// context
defaultProperties.put("spring.cloud.polaris.discovery.enabled", "false");
defaultProperties.put("spring.cloud.polaris.discovery.register", "false");
// configuration
defaultProperties.put("spring.cloud.polaris.config.enabled", "true");
defaultProperties.put("spring.cloud.polaris.config.internal-enabled", "false");
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]", tsfApplicationId + "/" + tsfGroupId + "/");
defaultProperties.put("spring.cloud.polaris.config.groups[0].files[1]", tsfNamespaceId + "/");
defaultProperties.put("spring.cloud.polaris.config.refresh-type",
environment.getProperty("spring.cloud.polaris.config.refresh-type", "refresh_context"));
// router
defaultProperties.put("spring.cloud.polaris.router.rule-router.fail-over",
environment.getProperty("spring.cloud.polaris.router.rule-router.fail-over", "none"));
defaultProperties.put("spring.cloud.polaris.router.namespace-router.enabled",
environment.getProperty("spring.cloud.polaris.router.namespace-router.enabled", "true"));
}
}
MapPropertySource propertySource = new MapPropertySource("tsf-polaris-properties", defaultProperties);

@ -0,0 +1,74 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.context.config.extend.tsf;
import java.util.HashMap;
import java.util.Map;
import com.tencent.cloud.common.tsf.TsfContextUtils;
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;
/**
* Read TSF env.
*
* @author Haotian Zhang
*/
public final class TsfLastEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
/**
* run before {@link ConfigDataEnvironmentPostProcessor}.
*/
public static final int ORDER = ConfigDataEnvironmentPostProcessor.ORDER + 1;
private final Log LOGGER;
private TsfLastEnvironmentPostProcessor(DeferredLogFactory logFactory) {
this.LOGGER = logFactory.getLog(getClass());
}
@Override
public int getOrder() {
return ORDER;
}
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
String tsfAppId = environment.getProperty("tsf_app_id");
// TSF deploy
if (StringUtils.isNotBlank(tsfAppId)) {
Map<String, Object> defaultProperties = new HashMap<>();
if (TsfContextUtils.isTsfConsulEnabled(environment)) {
defaultProperties.put("spring.cloud.consul.discovery.enabled", environment.getProperty("spring.cloud.consul.discovery.enabled", "true"));
defaultProperties.put("spring.cloud.consul.discovery.register", environment.getProperty("spring.cloud.consul.discovery.register", "true"));
}
MapPropertySource tsfLastPropertySource = new MapPropertySource("tsf-last-properties", defaultProperties);
environment.getPropertySources().addLast(tsfLastPropertySource);
}
}
}

@ -10,4 +10,5 @@ org.springframework.context.ApplicationListener=\
com.tencent.cloud.polaris.context.listener.FailedEventApplicationListener
org.springframework.boot.env.EnvironmentPostProcessor=\
com.tencent.cloud.polaris.context.config.PolarisContextEnvironmentPostProcessor,\
com.tencent.cloud.polaris.context.config.extend.tsf.TsfCoreEnvironmentPostProcessor
com.tencent.cloud.polaris.context.config.extend.tsf.TsfCoreEnvironmentPostProcessor,\
com.tencent.cloud.polaris.context.config.extend.tsf.TsfLastEnvironmentPostProcessor

Loading…
Cancel
Save