From ce0ce68b2f04e4ae99efb8523392860ed1ac4000 Mon Sep 17 00:00:00 2001 From: Fishtail <49390359+fuyuwei01@users.noreply.github.com> Date: Thu, 15 Aug 2024 11:55:35 +0800 Subject: [PATCH] feat:add zero protection. (#1386) Co-authored-by: Haotian Zhang --- CHANGELOG.md | 4 +- .../polaris/config/ConfigurationModifier.java | 5 ++ .../adapter/PolarisConfigFileLocator.java | 31 ++++--- .../adapter/PolarisConfigFileLocatorTest.java | 3 + .../contract/PolarisContractReporter.java | 47 ++++++++-- .../contract/config/ContractProperties.java | 4 + .../config/PolarisContractModifier.java | 3 +- .../config/PolarisContractProperties.java | 41 +++++++++ .../PolarisSwaggerAutoConfiguration.java | 15 +++- .../main/resources/META-INF/spring.factories | 5 +- .../polaris/DiscoveryConfigModifier.java | 4 + .../polaris/PolarisDiscoveryProperties.java | 30 +++++++ .../extend/consul/ConsulConfigModifier.java | 3 + .../consul/ConsulContextProperties.java | 19 +++- .../polaris/registry/PolarisRegistration.java | 10 +++ .../registry/PolarisServiceRegistry.java | 1 + ...itional-spring-configuration-metadata.json | 14 +-- .../PolarisDiscoveryPropertiesTest.java | 4 +- .../cloud/common/constant/OrderConstant.java | 2 +- .../tencent/cloud/common/util/GzipUtil.java | 88 +++++++++++++++++++ .../src/main/resources/bootstrap.yml | 3 + .../cloud/polaris/context/ModifyAddress.java | 10 +++ .../config/PolarisContextProperties.java | 2 +- .../stat/config/StatConfigModifier.java | 2 +- 24 files changed, 308 insertions(+), 42 deletions(-) create mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f820ba57..31cfccfde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,4 +12,6 @@ - [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1381) - [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1382) - [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1383) -- [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1385) \ No newline at end of file +- [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1385) +- [fix:fix ApplicationContextAwareUtils NPE bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1386) +- [feat:add zero protection.](https://github.com/Tencent/spring-cloud-tencent/pull/1386) \ No newline at end of file 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 1df53b537..a6c5f4989 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 @@ -63,6 +63,11 @@ public class ConfigurationModifier implements PolarisConfigurationConfigModifier @Override public void modify(ConfigurationImpl configuration) { + configuration.getGlobal().getAPI().setReportEnable(false); + + if (!polarisContextProperties.getEnabled() || !polarisConfigProperties.isEnabled()) { + return; + } if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_POLARIS)) { initByPolarisDataSource(configuration); } 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 974aff31b..824f47cab 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 @@ -78,23 +78,26 @@ public class PolarisConfigFileLocator implements PropertySourceLocator { @Override public PropertySource locate(Environment environment) { - CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME); - try { - // load custom config extension files - initCustomPolarisConfigExtensionFiles(compositePropertySource); - // load spring boot default config files - initInternalConfigFiles(compositePropertySource); - // load custom config files - List configFileGroups = polarisConfigProperties.getGroups(); - if (CollectionUtils.isEmpty(configFileGroups)) { + if (polarisConfigProperties.isEnabled()) { + CompositePropertySource compositePropertySource = new CompositePropertySource(POLARIS_CONFIG_PROPERTY_SOURCE_NAME); + try { + // load custom config extension files + initCustomPolarisConfigExtensionFiles(compositePropertySource); + // load spring boot default config files + initInternalConfigFiles(compositePropertySource); + // load custom config files + List configFileGroups = polarisConfigProperties.getGroups(); + if (CollectionUtils.isEmpty(configFileGroups)) { + return compositePropertySource; + } + initCustomPolarisConfigFiles(compositePropertySource, configFileGroups); return compositePropertySource; } - initCustomPolarisConfigFiles(compositePropertySource, configFileGroups); - return compositePropertySource; - } - finally { - afterLocatePolarisConfigExtension(compositePropertySource); + finally { + afterLocatePolarisConfigExtension(compositePropertySource); + } } + return null; } private void initCustomPolarisConfigExtensionFiles(CompositePropertySource compositePropertySource) { diff --git a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java index ec264aa0f..bf6e396f9 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java +++ b/spring-cloud-starter-tencent-polaris-config/src/test/java/com/tencent/cloud/polaris/config/adapter/PolarisConfigFileLocatorTest.java @@ -90,6 +90,7 @@ public class PolarisConfigFileLocatorTest { when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap.yaml")).thenReturn(emptyConfigFile); + when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.getGroups()).thenReturn(null); when(environment.getActiveProfiles()).thenReturn(new String[] {}); @@ -137,6 +138,7 @@ public class PolarisConfigFileLocatorTest { when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yml")).thenReturn(emptyConfigFile); when(configFileService.getConfigYamlFile(testNamespace, testServiceName, "bootstrap-dev.yaml")).thenReturn(emptyConfigFile); + when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.getGroups()).thenReturn(null); when(environment.getActiveProfiles()).thenReturn(new String[] {"dev"}); @@ -174,6 +176,7 @@ public class PolarisConfigFileLocatorTest { configFileGroup.setFiles(Lists.newArrayList(customFile1, customFile2)); customFiles.add(configFileGroup); + when(polarisConfigProperties.isEnabled()).thenReturn(true); when(polarisConfigProperties.getGroups()).thenReturn(customFiles); when(environment.getActiveProfiles()).thenReturn(new String[] {}); diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java index bee9704fb..c380be0c3 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java @@ -17,18 +17,22 @@ package com.tencent.cloud.polaris.contract; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import com.tencent.cloud.common.util.JacksonUtils; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.tencent.cloud.common.util.GzipUtil; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.contract.config.PolarisContractProperties; import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.api.plugin.server.InterfaceDescriptor; import com.tencent.polaris.api.plugin.server.ReportServiceContractRequest; import com.tencent.polaris.api.plugin.server.ReportServiceContractResponse; +import com.tencent.polaris.api.utils.StringUtils; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.PathItem; @@ -37,6 +41,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springdoc.api.AbstractOpenApiResource; import org.springdoc.api.AbstractOpenApiResourceUtil; +import org.springdoc.core.providers.ObjectMapperProvider; import org.springdoc.webflux.api.OpenApiWebFluxUtil; import org.springdoc.webmvc.api.OpenApiWebMvcUtil; @@ -62,15 +67,18 @@ public class PolarisContractReporter implements ApplicationListener interfaceDescriptorList = getInterfaceDescriptorFromSwagger(openAPI); request.setInterfaceDescriptors(interfaceDescriptorList); + String jsonValue; + if (springdocObjectMapperProvider != null && springdocObjectMapperProvider.jsonMapper() != null) { + jsonValue = springdocObjectMapperProvider.jsonMapper().writeValueAsString(openAPI); + } + else { + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + jsonValue = mapper.writeValueAsString(openAPI); + } + String serviceApiMeta = GzipUtil.compressBase64Encode(jsonValue, "utf-8"); + request.setContent(serviceApiMeta); ReportServiceContractResponse response = providerAPI.reportServiceContract(request); LOG.info("Service contract [Namespace: {}. Name: {}. Service: {}. Protocol:{}. Version: {}. API counter: {}] is reported.", request.getNamespace(), request.getName(), request.getService(), request.getProtocol(), request.getVersion(), request.getInterfaceDescriptors().size()); if (LOG.isDebugEnabled()) { - String jsonValue = JacksonUtils.serialize2Json(openAPI); LOG.debug("OpenApi json data: {}", jsonValue); + LOG.debug("OpenApi json base64 data: {}", serviceApiMeta); } } else { @@ -129,7 +152,21 @@ public class PolarisContractReporter implements ApplicationListener registerConfigs = configuration.getProvider().getRegisters(); for (RegisterConfigImpl registerConfig : registerConfigs) { - registerConfig.setReportServiceContractEnable(polarisContractProperties.isEnabled()); + registerConfig.setReportServiceContractEnable( + polarisContractProperties.isEnabled() && polarisContractProperties.isReportEnabled()); } } diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractProperties.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractProperties.java index 2b43226a5..0fdc0a1d4 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractProperties.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractProperties.java @@ -57,6 +57,8 @@ public class PolarisContractProperties implements ContractProperties { @Value("${spring.cloud.polaris.contract.report.enabled:true}") private boolean reportEnabled = true; + private String name; + public PolarisContractProperties(@Nullable ExtendedContractProperties extendContractProperties) { this.extendContractProperties = extendContractProperties; } @@ -71,6 +73,9 @@ public class PolarisContractProperties implements ContractProperties { @Override public void setEnabled(boolean enabled) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setEnabled(enabled); + } this.enabled = enabled; } @@ -84,6 +89,9 @@ public class PolarisContractProperties implements ContractProperties { @Override public void setBasePackage(String basePackage) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setBasePackage(basePackage); + } this.basePackage = basePackage; } @@ -97,6 +105,9 @@ public class PolarisContractProperties implements ContractProperties { @Override public void setExcludePath(String excludePath) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setExcludePath(excludePath); + } this.excludePath = excludePath; } @@ -110,6 +121,9 @@ public class PolarisContractProperties implements ContractProperties { @Override public void setGroup(String group) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setGroup(group); + } this.group = group; } @@ -123,6 +137,9 @@ public class PolarisContractProperties implements ContractProperties { @Override public void setBasePath(String basePath) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setBasePath(basePath); + } this.basePath = basePath; } @@ -136,16 +153,40 @@ public class PolarisContractProperties implements ContractProperties { @Override public void setExposure(boolean exposure) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setExposure(exposure); + } this.exposure = exposure; } @Override public boolean isReportEnabled() { + if (Objects.nonNull(extendContractProperties)) { + return extendContractProperties.isReportEnabled(); + } return reportEnabled; } @Override public void setReportEnabled(boolean reportEnabled) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setReportEnabled(reportEnabled); + } this.reportEnabled = reportEnabled; } + + public String getName() { + if (Objects.nonNull(extendContractProperties)) { + return extendContractProperties.getName(); + } + return name; + } + + @Override + public void setName(String name) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setName(name); + } + this.name = name; + } } diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisSwaggerAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisSwaggerAutoConfiguration.java index 75eee6c1d..6f2f76660 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisSwaggerAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisSwaggerAutoConfiguration.java @@ -30,6 +30,7 @@ import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; import org.springdoc.core.GroupedOpenApi; import org.springdoc.core.SpringDocConfiguration; +import org.springdoc.core.providers.ObjectMapperProvider; import org.springdoc.webflux.api.MultipleOpenApiWebFluxResource; import org.springdoc.webmvc.api.MultipleOpenApiWebMvcResource; @@ -56,6 +57,12 @@ import static com.tencent.cloud.polaris.contract.utils.PackageUtil.SPLITTER; @Import(SpringDocConfiguration.class) public class PolarisSwaggerAutoConfiguration { + static { + // After springboot2.6.x, the default path matching strategy of spring MVC is changed from ANT_PATH_MATCHER + // mode to PATH_PATTERN_PARSER mode, causing an error. The solution is to switch to the original ANT_PATH_MATCHER mode. + System.setProperty("spring.mvc.pathmatch.matching-strategy", "ant-path-matcher"); + } + @Bean public GroupedOpenApi polarisGroupedOpenApi(PolarisContractProperties polarisContractProperties) { String basePackage = PackageUtil.scanPackage(polarisContractProperties.getBasePackage()); @@ -79,8 +86,8 @@ public class PolarisSwaggerAutoConfiguration { public OpenAPI polarisOpenAPI() { return new OpenAPI() .info(new Info() - .title("Polaris Swagger API") - .description("This is to show polaris api description.") + .title("Polaris Contract") + .description("This is to show polaris contract description.") .license(new License().name("BSD-3-Clause").url("https://opensource.org/licenses/BSD-3-Clause")) .version("1.0.0")); } @@ -92,9 +99,9 @@ public class PolarisSwaggerAutoConfiguration { @Nullable MultipleOpenApiWebMvcResource multipleOpenApiWebMvcResource, @Nullable MultipleOpenApiWebFluxResource multipleOpenApiWebFluxResource, PolarisContractProperties polarisContractProperties, PolarisSDKContextManager polarisSDKContextManager, - PolarisDiscoveryProperties polarisDiscoveryProperties) { + PolarisDiscoveryProperties polarisDiscoveryProperties, ObjectMapperProvider springdocObjectMapperProvider) { return new PolarisContractReporter(multipleOpenApiWebMvcResource, multipleOpenApiWebFluxResource, - polarisContractProperties, polarisSDKContextManager.getProviderAPI(), polarisDiscoveryProperties); + polarisContractProperties, polarisSDKContextManager.getProviderAPI(), polarisDiscoveryProperties, springdocObjectMapperProvider); } @Bean diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/spring.factories b/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/spring.factories index 86a23f5be..406e1aba4 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/spring.factories @@ -1,7 +1,8 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.tencent.cloud.polaris.contract.config.PolarisSwaggerAutoConfiguration,\ - com.tencent.cloud.polaris.contract.config.PolarisContractProperties + com.tencent.cloud.polaris.contract.config.PolarisContractProperties,\ + com.tencent.cloud.polaris.contract.config.PolarisContractPropertiesAutoConfiguration org.springframework.cloud.bootstrap.BootstrapConfiguration=\ - com.tencent.cloud.polaris.contract.config.PolarisContractPropertiesAutoConfiguration + com.tencent.cloud.polaris.contract.config.PolarisContractPropertiesBootstrapConfiguration org.springframework.context.ApplicationListener=\ com.tencent.cloud.polaris.contract.PolarisSwaggerApplicationListener diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryConfigModifier.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryConfigModifier.java index 1725519f3..73f4e1071 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryConfigModifier.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/DiscoveryConfigModifier.java @@ -51,6 +51,10 @@ public class DiscoveryConfigModifier implements PolarisConfigModifier { // Set ServiceRefreshInterval configuration.getConsumer().getLocalCache() .setServiceListRefreshInterval(polarisDiscoveryProperties.getServiceListRefreshInterval()); + + configuration.getConsumer().getZeroProtection().setEnable(polarisDiscoveryProperties.isZeroProtectionEnabled()); + configuration.getConsumer().getZeroProtection() + .setNeedTestConnectivity(polarisDiscoveryProperties.isZeroProtectionNeedTestConnectivity()); } @Override diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java index 0477ebfb9..83c089e5a 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisDiscoveryProperties.java @@ -101,6 +101,18 @@ public class PolarisDiscoveryProperties { */ private Long serviceListRefreshInterval = 60000L; + /** + * Zero protection switch. + */ + @Value("${spring.cloud.polaris.discovery.zero-protection.enabled:false}") + private boolean zeroProtectionEnabled = false; + + /** + * Zero protection test connectivity switch. + */ + @Value("${spring.cloud.polaris.discovery.zero-protection.is-need-test-connectivity:false}") + private boolean zeroProtectionNeedTestConnectivity = false; + public String getInstanceId() { return instanceId; } @@ -192,6 +204,22 @@ public class PolarisDiscoveryProperties { this.heartbeatInterval = heartbeatInterval; } + public boolean isZeroProtectionEnabled() { + return zeroProtectionEnabled; + } + + public void setZeroProtectionEnabled(boolean zeroProtectionEnabled) { + this.zeroProtectionEnabled = zeroProtectionEnabled; + } + + public boolean isZeroProtectionNeedTestConnectivity() { + return zeroProtectionNeedTestConnectivity; + } + + public void setZeroProtectionNeedTestConnectivity(boolean zeroProtectionNeedTestConnectivity) { + this.zeroProtectionNeedTestConnectivity = zeroProtectionNeedTestConnectivity; + } + public Boolean getEnabled() { return enabled; } @@ -227,6 +255,8 @@ public class PolarisDiscoveryProperties { ", heartbeatInterval=" + heartbeatInterval + ", healthCheckUrl='" + healthCheckUrl + '\'' + ", serviceListRefreshInterval=" + serviceListRefreshInterval + + ", zeroProtectionEnabled=" + zeroProtectionEnabled + + ", zeroProtectionNeedTestConnectivity=" + zeroProtectionNeedTestConnectivity + '}'; } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulConfigModifier.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulConfigModifier.java index af8431a5c..13094e78c 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulConfigModifier.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulConfigModifier.java @@ -90,6 +90,9 @@ public class ConsulConfigModifier implements PolarisConfigModifier { if (StringUtils.isNotBlank(consulContextProperties.getInstanceId())) { metadata.put(ConsulConstant.MetadataMapKey.INSTANCE_ID_KEY, consulContextProperties.getInstanceId()); } + if (StringUtils.isNotBlank(consulContextProperties.getAclToken())) { + serverConnectorConfig.setToken(consulContextProperties.getAclToken()); + } if (consulContextProperties.isPreferIpAddress() && StringUtils.isNotBlank(consulContextProperties.getIpAddress())) { metadata.put(ConsulConstant.MetadataMapKey.PREFER_IP_ADDRESS_KEY, diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java index ad98e009b..7c650cb59 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/extend/consul/ConsulContextProperties.java @@ -55,6 +55,9 @@ public class ConsulContextProperties { @Value("${spring.cloud.consul.discovery.prefer-ip-address:#{'false'}}") private boolean preferIpAddress; + @Value("${consul.token:${CONSUL_TOKEN:${spring.cloud.consul.token:${SPRING_CLOUD_CONSUL_TOKEN:${spring.cloud.consul.discovery.acl-token:}}}}}") + private String aclToken; + public String getHost() { return host; } @@ -83,14 +86,14 @@ public class ConsulContextProperties { return register; } - public boolean isDiscoveryEnabled() { - return discoveryEnabled; - } - public void setRegister(boolean register) { this.register = register; } + public boolean isDiscoveryEnabled() { + return discoveryEnabled; + } + public void setDiscoveryEnabled(boolean discoveryEnabled) { this.discoveryEnabled = discoveryEnabled; } @@ -126,4 +129,12 @@ public class ConsulContextProperties { public void setPreferIpAddress(boolean preferIpAddress) { this.preferIpAddress = preferIpAddress; } + + public String getAclToken() { + return aclToken; + } + + public void setAclToken(String aclToken) { + this.aclToken = aclToken; + } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index 0a3d09ae8..dd6390403 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -67,6 +67,7 @@ public class PolarisRegistration implements Registration { private final List customizers; private boolean registerEnabled = false; private Map metadata; + private Map> extendedMetadata; private int port; private String instanceId; @@ -132,6 +133,8 @@ public class PolarisRegistration implements Registration { this.metadata = instanceMetadata; } + this.extendedMetadata = new HashMap<>(); + // generate registerEnabled if (null != polarisDiscoveryProperties) { registerEnabled = polarisDiscoveryProperties.isRegisterEnabled(); @@ -217,6 +220,13 @@ public class PolarisRegistration implements Registration { return metadata; } + public Map> getExtendedMetadata() { + if (extendedMetadata == null) { + extendedMetadata = new HashMap<>(); + } + return extendedMetadata; + } + @Override public String getInstanceId() { return instanceId; diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index 43aeaa8c7..80b17a1c2 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -117,6 +117,7 @@ public class PolarisServiceRegistry implements ServiceRegistry addresses = AddressUtils.parseAddressList(properties.getAddress()); configuration.getGlobal().getServerConnector().setAddresses(addresses); + if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors())) { + configuration.getGlobal().setServerConnectors(new ArrayList<>()); + } + if (CollectionUtils.isEmpty(configuration.getGlobal().getServerConnectors()) + && null != configuration.getGlobal().getServerConnector()) { + configuration.getGlobal().getServerConnectors().add(configuration.getGlobal().getServerConnector()); + } } @Override diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java index 3dad08fce..f92d065fb 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextProperties.java @@ -65,7 +65,7 @@ public class PolarisContextProperties { * If polaris enabled. */ @Value("${spring.cloud.polaris.enabled:#{'true'}}") - private Boolean enabled; + private Boolean enabled = true; /** * polaris namespace. diff --git a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java index abaac1c93..0a8ff8f15 100644 --- a/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java +++ b/spring-cloud-tencent-rpc-enhancement/src/main/java/com/tencent/cloud/rpc/enhancement/stat/config/StatConfigModifier.java @@ -62,7 +62,7 @@ public class StatConfigModifier implements PolarisConfigModifier { // pull metrics prometheusHandlerConfig.setType("pull"); if (!StringUtils.hasText(polarisStatProperties.getHost())) { - polarisStatProperties.setHost(environment.getProperty("spring.cloud.client.ip-address")); + polarisStatProperties.setHost("0.0.0.0"); } prometheusHandlerConfig.setHost(polarisStatProperties.getHost()); prometheusHandlerConfig.setPort(polarisStatProperties.getPort());