From 7d5b931ba8fb3db5ff4e1d0e977ef92284d75991 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 25 Apr 2024 10:26:18 +0800 Subject: [PATCH 01/11] feat:add zero protection. --- CHANGELOG.md | 1 + .../polaris/config/ConfigurationModifier.java | 3 ++ .../adapter/PolarisConfigFileLocator.java | 31 ++++++++++--------- .../adapter/PolarisConfigFileLocatorTest.java | 3 ++ .../contract/PolarisContractReporter.java | 2 +- .../polaris/DiscoveryConfigModifier.java | 4 +++ .../polaris/PolarisDiscoveryProperties.java | 30 ++++++++++++++++++ .../extend/consul/ConsulConfigModifier.java | 3 ++ .../consul/ConsulContextProperties.java | 19 +++++++++--- ...itional-spring-configuration-metadata.json | 14 ++++----- .../PolarisDiscoveryPropertiesTest.java | 4 ++- .../cloud/common/constant/OrderConstant.java | 2 +- .../src/main/resources/application.yml | 3 ++ .../cloud/polaris/context/ModifyAddress.java | 10 ++++++ 14 files changed, 101 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b45c5d152..75148a421 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,4 +11,5 @@ - [fix:fix nearby router properties loading bug.](https://github.com/Tencent/spring-cloud-tencent/pull/1273) - [refactor:let the configuration SDK context stand alone.](https://github.com/Tencent/spring-cloud-tencent/pull/1275) - [fix: fix grammar issues for lane router example & optimize the gateway dependency](https://github.com/Tencent/spring-cloud-tencent/pull/1276) +- [feat:add zero protection.](https://github.com/Tencent/spring-cloud-tencent/pull/1278) - [fix: fix lossless deregister failed when no healthcheck configured](https://github.com/Tencent/spring-cloud-tencent/pull/1281) 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..999bd5168 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,9 @@ public ConfigurationModifier(PolarisConfigProperties polarisConfigProperties, @Override public void modify(ConfigurationImpl configuration) { + if (!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 PolarisConfigFileLocator(PolarisConfigProperties polarisConfigProperties, @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 b2f98817c..9a397ac49 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 @@ -89,6 +89,7 @@ public void testLoadApplicationPropertiesFile() { 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[] {}); @@ -136,6 +137,7 @@ public void testActiveProfileFilesPriorityBiggerThanDefault() { 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"}); @@ -173,6 +175,7 @@ public void testGetCustomFiles() { 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..1d8305af8 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 @@ -129,7 +129,7 @@ private List getInterfaceDescriptorFromSwagger(OpenAPI open InterfaceDescriptor interfaceDescriptor = new InterfaceDescriptor(); interfaceDescriptor.setPath(p.getKey()); interfaceDescriptor.setMethod(o.getKey()); - interfaceDescriptor.setContent(JacksonUtils.serialize2Json(p.getValue())); + interfaceDescriptor.setContent(JacksonUtils.serialize2Json(o.getValue())); interfaceDescriptorList.add(interfaceDescriptor); } } 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 void modify(ConfigurationImpl configuration) { // 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 void setHeartbeatInterval(Integer heartbeatInterval) { 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 String toString() { ", 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 void modify(ConfigurationImpl configuration) { 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 boolean isRegister() { 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 boolean isPreferIpAddress() { 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/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 8e5b628af..a8ebda064 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -67,16 +67,16 @@ "description": "Millis interval of refresh of service info list. Default: 60000." }, { - "name": "spring.cloud.polaris.loadbalancer.enabled", + "name": "spring.cloud.polaris.discovery.zero-protection.enabled", "type": "java.lang.Boolean", - "defaultValue": "true", - "description": "polaris loadbalancer." + "defaultValue": false, + "description": "Zero protection switch. Default: false." }, { - "name": "spring.cloud.polaris.loadbalancer.strategy", - "type": "java.lang.String", - "defaultValue": "roundRobin", - "description": "loadbalancer strategy." + "name": "spring.cloud.polaris.discovery.zero-protection.is-need-test-connectivity", + "type": "java.lang.Boolean", + "defaultValue": false, + "description": "Zero protection test connectivity switch. Default: false." }, { "name": "spring.cloud.nacos.discovery.enabled", diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/PolarisDiscoveryPropertiesTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/PolarisDiscoveryPropertiesTest.java index 1a5e875dc..0345bf43f 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/PolarisDiscoveryPropertiesTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/PolarisDiscoveryPropertiesTest.java @@ -101,6 +101,8 @@ public void testGetAndSet() { + ", registerEnabled=true" + ", heartbeatInterval=20" + ", healthCheckUrl='/health'" - + ", serviceListRefreshInterval=1000}"); + + ", serviceListRefreshInterval=1000" + + ", zeroProtectionEnabled=false" + + ", zeroProtectionNeedTestConnectivity=false}"); } } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java index ee0d8e763..8c75f5256 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java @@ -125,7 +125,7 @@ public static final class Modifier { /** * Address modifier order. */ - public static Integer ADDRESS_ORDER = Integer.MIN_VALUE; + public static Integer ADDRESS_ORDER = Integer.MIN_VALUE + 10; /** * Discovery config modifier order. diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/application.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/application.yml index 6383b4505..8af26fd76 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/application.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-caller-service/src/main/resources/application.yml @@ -16,6 +16,9 @@ spring: heartbeat: enabled: true health-check-url: /quickstart/caller/healthCheck + zero-protection: + enabled: true + is-need-test-connectivity: true contract: exposure: true report: diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java index 68e9f2c4c..bb830b80d 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/ModifyAddress.java @@ -18,6 +18,7 @@ package com.tencent.cloud.polaris.context; +import java.util.ArrayList; import java.util.List; import com.tencent.cloud.common.constant.OrderConstant; @@ -26,6 +27,8 @@ import com.tencent.polaris.factory.config.ConfigurationImpl; import org.apache.commons.lang.StringUtils; +import org.springframework.util.CollectionUtils; + /** * Modify polaris server address. * @@ -48,6 +51,13 @@ public void modify(ConfigurationImpl configuration) { List 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 From 5b7acf92b6ba5494a0ee259e04a5cef3369867b3 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 25 Apr 2024 10:26:55 +0800 Subject: [PATCH 02/11] feat:add zero protection. --- .../PolarisConfigDataLocationResolver.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java index 0fa303286..ebe3dcefe 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java @@ -139,8 +139,19 @@ public List resolveProfileSpecific( } // prepare and init earlier Polaris SDKContext to pull config files from remote. - prepareAndInitEarlierPolarisSdkContext(resolverContext, polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties); - + try { + prepareAndInitEarlierPolarisSdkContext(resolverContext, polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties); + } + catch (Throwable throwable) { + if (location.isOptional()) { + log.warn("create earlier polaris SDK context failed.", throwable); + return new ArrayList<>(); + } + else { + log.error("create earlier polaris SDK context failed.", throwable); + throw throwable; + } + } bootstrapContext.registerIfAbsent(PolarisConfigProperties.class, BootstrapRegistry.InstanceSupplier.of(polarisConfigProperties)); From fdac64c285e35dc35c2d5f075e46f66540b3059c Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 25 Apr 2024 10:32:56 +0800 Subject: [PATCH 03/11] feat:add zero protection. --- .../polaris/config/ConfigurationModifier.java | 4 +- .../PolarisConfigDataLocationResolver.java | 13 ++- .../contract/PolarisContractReporter.java | 6 +- .../PolarisSwaggerAutoConfiguration.java | 4 +- .../main/resources/META-INF/spring.factories | 2 +- ...ot.autoconfigure.AutoConfiguration.imports | 2 +- .../polaris/registry/PolarisRegistration.java | 10 +++ .../registry/PolarisServiceRegistry.java | 1 + .../tencent/cloud/common/util/GzipUtil.java | 84 +++++++++++++++++++ .../config/PolarisContextProperties.java | 2 +- 10 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java 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 999bd5168..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,7 +63,9 @@ public ConfigurationModifier(PolarisConfigProperties polarisConfigProperties, @Override public void modify(ConfigurationImpl configuration) { - if (!polarisConfigProperties.isEnabled()) { + configuration.getGlobal().getAPI().setReportEnable(false); + + if (!polarisContextProperties.getEnabled() || !polarisConfigProperties.isEnabled()) { return; } if (StringUtils.equalsIgnoreCase(polarisConfigProperties.getDataSource(), DATA_SOURCE_POLARIS)) { diff --git a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java index ebe3dcefe..98db039cf 100644 --- a/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java +++ b/spring-cloud-starter-tencent-polaris-config/src/main/java/com/tencent/cloud/polaris/config/configdata/PolarisConfigDataLocationResolver.java @@ -89,9 +89,16 @@ public boolean isResolvable(ConfigDataLocationResolverContext context, ConfigDat if (!location.hasPrefix(PREFIX)) { return false; } - return context.getBinder() + + boolean contextEnabled = context.getBinder() + .bind("spring.cloud.polaris.enabled", Boolean.class) + .orElse(true); + + boolean configEnabled = context.getBinder() .bind("spring.cloud.polaris.config.enabled", Boolean.class) .orElse(true); + + return contextEnabled && configEnabled; } @Override @@ -138,6 +145,10 @@ public List resolveProfileSpecific( polarisContextProperties = new PolarisContextProperties(); } + if (!polarisContextProperties.getEnabled() || !polarisConfigProperties.isEnabled()) { + return Collections.emptyList(); + } + // prepare and init earlier Polaris SDKContext to pull config files from remote. try { prepareAndInitEarlierPolarisSdkContext(resolverContext, polarisConfigProperties, polarisCryptoConfigProperties, polarisContextProperties); 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 1d8305af8..29954cda6 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 @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; +import com.tencent.cloud.common.util.GzipUtil; import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.contract.config.PolarisContractProperties; @@ -97,13 +98,16 @@ else if (multipleOpenApiWebFluxResource != null) { request.setVersion(polarisDiscoveryProperties.getVersion()); List interfaceDescriptorList = getInterfaceDescriptorFromSwagger(openAPI); request.setInterfaceDescriptors(interfaceDescriptorList); + String jsonValue = JacksonUtils.serialize2Json(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 { 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 e0af4daf1..0550b95b8 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 @@ -85,8 +85,8 @@ public GroupedOpenApi polarisGroupedOpenApi(PolarisContractProperties polarisCon 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")); } 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 e725a0ce2..bd639443e 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,4 +1,4 @@ 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-contract/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index c695c214c..78cf3d959 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1,2 +1,2 @@ com.tencent.cloud.polaris.contract.config.PolarisSwaggerAutoConfiguration -com.tencent.cloud.polaris.contract.config.PolarisContractProperties +com.tencent.cloud.polaris.contract.config.PolarisContractPropertiesAutoConfiguration 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 PolarisRegistration( this.metadata = instanceMetadata; } + this.extendedMetadata = new HashMap<>(); + // generate registerEnabled if (null != polarisDiscoveryProperties) { registerEnabled = polarisDiscoveryProperties.isRegisterEnabled(); @@ -217,6 +220,13 @@ public Map getMetadata() { 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 void register(PolarisRegistration registration) { instanceRegisterRequest.setCampus(staticMetadataManager.getCampus()); instanceRegisterRequest.setTtl(polarisDiscoveryProperties.getHeartbeatInterval()); instanceRegisterRequest.setMetadata(registration.getMetadata()); + instanceRegisterRequest.setExtendedMetadata(registration.getExtendedMetadata()); instanceRegisterRequest.setProtocol(polarisDiscoveryProperties.getProtocol()); instanceRegisterRequest.setVersion(polarisDiscoveryProperties.getVersion()); instanceRegisterRequest.setInstanceId(polarisDiscoveryProperties.getInstanceId()); diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java new file mode 100644 index 000000000..7843b112a --- /dev/null +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java @@ -0,0 +1,84 @@ +/* + * 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.util; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Base64; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +/** + * @author kysonli + * @date 2018/9/7 15:35 + */ +public final class GzipUtil { + + private GzipUtil() { + } + + public static byte[] compress(String data, String charsetName) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + GZIPOutputStream gzip = new GZIPOutputStream(bos); + gzip.write(data.getBytes(charsetName)); + gzip.finish(); + gzip.close(); + byte[] ret = bos.toByteArray(); + bos.close(); + return ret; + } + + public static String compressBase64Encode(String data, String charsetName) throws IOException { + byte[] compressData = compress(data, charsetName); + return new String(Base64.getEncoder().encode(compressData), charsetName); + } + + public static String compressBase64Encode(byte[] byteData, String charsetName) throws IOException { + byte[] compressData = compress(new String(byteData, charsetName), charsetName); + return Base64.getEncoder().encodeToString(compressData); + } + + + public static byte[] decompress(byte[] zipData) throws IOException { + ByteArrayInputStream bis = new ByteArrayInputStream(zipData); + GZIPInputStream gzip = new GZIPInputStream(bis); + byte[] buf = new byte[256]; + int num = -1; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + while ((num = gzip.read(buf, 0, buf.length)) != -1) { + bos.write(buf, 0, num); + } + gzip.close(); + bis.close(); + byte[] ret = bos.toByteArray(); + bos.flush(); + bos.close(); + return ret; + } + + public static String base64DecodeDecompress(String data, String charsetName) throws IOException { + byte[] base64DecodeData = Base64.getDecoder().decode(data); + return new String(decompress(base64DecodeData), charsetName); + } + + public static String base64DecodeDecompress(String data) throws IOException { + return base64DecodeDecompress(data, "utf-8"); + } + +} 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. From e88ab95b8be54a31b620989e406c561b98cf7e9b Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 25 Apr 2024 11:16:35 +0800 Subject: [PATCH 04/11] feat:add zero protection. --- .../cloud/polaris/contract/config/PolarisContractModifier.java | 3 ++- .../polaris/contract/config/PolarisContractProperties.java | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractModifier.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractModifier.java index f48a01d24..b1b50f518 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractModifier.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractModifier.java @@ -41,7 +41,8 @@ public PolarisContractModifier(PolarisContractProperties polarisContractProperti public void modify(ConfigurationImpl configuration) { List 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..9b442a942 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 @@ -141,6 +141,9 @@ public void setExposure(boolean exposure) { @Override public boolean isReportEnabled() { + if (Objects.nonNull(extendContractProperties)) { + return extendContractProperties.isReportEnabled(); + } return reportEnabled; } From 976558cd9b3cd21215df6b3252544dcc19f66a79 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Thu, 25 Apr 2024 15:00:49 +0800 Subject: [PATCH 05/11] feat:add zero protection. --- .../cloud/polaris/contract/PolarisContractReporter.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) 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 29954cda6..ff14e424b 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,6 +17,7 @@ package com.tencent.cloud.polaris.contract; +import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -133,7 +134,12 @@ private List getInterfaceDescriptorFromSwagger(OpenAPI open InterfaceDescriptor interfaceDescriptor = new InterfaceDescriptor(); interfaceDescriptor.setPath(p.getKey()); interfaceDescriptor.setMethod(o.getKey()); - interfaceDescriptor.setContent(JacksonUtils.serialize2Json(o.getValue())); + try { + interfaceDescriptor.setContent(GzipUtil.compressBase64Encode(JacksonUtils.serialize2Json(o.getValue()), "utf-8")); + } + catch (IOException ioe) { + LOG.warn("Encode operation [{}] failed.", o.getValue(), ioe); + } interfaceDescriptorList.add(interfaceDescriptor); } } From 43127f5ff387e88e6d9ea9d1a89a31e53e5df96a Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 7 May 2024 10:03:20 +0800 Subject: [PATCH 06/11] feat:add zero protection. --- .../contract/PolarisContractReporter.java | 18 ++++++++++++++++-- .../PolarisSwaggerAutoConfiguration.java | 5 +++-- 2 files changed, 19 insertions(+), 4 deletions(-) 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 ff14e424b..10aebf678 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 @@ -23,6 +23,8 @@ import java.util.List; import java.util.Map; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.ObjectMapper; import com.tencent.cloud.common.util.GzipUtil; import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; @@ -39,6 +41,7 @@ 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; @@ -64,15 +67,18 @@ public class PolarisContractReporter implements ApplicationListener interfaceDescriptorList = getInterfaceDescriptorFromSwagger(openAPI); request.setInterfaceDescriptors(interfaceDescriptorList); - String jsonValue = JacksonUtils.serialize2Json(openAPI); + 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); 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 0550b95b8..885c9ffd8 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.License; import org.springdoc.core.configuration.SpringDocConfiguration; import org.springdoc.core.models.GroupedOpenApi; +import org.springdoc.core.providers.ObjectMapperProvider; import org.springdoc.webflux.api.MultipleOpenApiWebFluxResource; import org.springdoc.webmvc.api.MultipleOpenApiWebMvcResource; @@ -98,9 +99,9 @@ public PolarisContractReporter polarisContractReporter( @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 From 04668c092583081c86644bcc88a41839f01bde05 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 7 May 2024 11:33:00 +0800 Subject: [PATCH 07/11] feat:add zero protection. --- .../polaris/contract/PolarisContractReporter.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) 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 10aebf678..502759576 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 @@ -26,7 +26,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.tencent.cloud.common.util.GzipUtil; -import com.tencent.cloud.common.util.JacksonUtils; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; import com.tencent.cloud.polaris.contract.config.PolarisContractProperties; import com.tencent.polaris.api.core.ProviderAPI; @@ -149,7 +148,16 @@ private List getInterfaceDescriptorFromSwagger(OpenAPI open interfaceDescriptor.setPath(p.getKey()); interfaceDescriptor.setMethod(o.getKey()); try { - interfaceDescriptor.setContent(GzipUtil.compressBase64Encode(JacksonUtils.serialize2Json(o.getValue()), "utf-8")); + String jsonValue; + if (springdocObjectMapperProvider != null && springdocObjectMapperProvider.jsonMapper() != null) { + jsonValue = springdocObjectMapperProvider.jsonMapper().writeValueAsString(o.getValue()); + } + else { + ObjectMapper mapper = new ObjectMapper(); + mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + jsonValue = mapper.writeValueAsString(o.getValue()); + } + interfaceDescriptor.setContent(GzipUtil.compressBase64Encode(jsonValue, "utf-8")); } catch (IOException ioe) { LOG.warn("Encode operation [{}] failed.", o.getValue(), ioe); From e2c0f0c3ffcc75e5edd4d697833722a1864cf56b Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Sat, 11 May 2024 19:47:17 +0800 Subject: [PATCH 08/11] feat:add zero protection. --- .../cloud/rpc/enhancement/stat/config/StatConfigModifier.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 void modify(ConfigurationImpl configuration) { // 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()); From 075f89ee040bbf296afd4ba3fff6a351cc0bfccd Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 13 May 2024 22:07:55 +0800 Subject: [PATCH 09/11] feat:add zero protection. --- .../polaris/contract/PolarisContractReporter.java | 7 ++++++- .../contract/config/ContractProperties.java | 4 ++++ .../contract/config/PolarisContractProperties.java | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) 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 502759576..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 @@ -32,6 +32,7 @@ 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; @@ -97,7 +98,11 @@ else if (multipleOpenApiWebFluxResource != null) { } if (openAPI != null) { ReportServiceContractRequest request = new ReportServiceContractRequest(); - request.setName(polarisDiscoveryProperties.getService()); + String name = polarisContractProperties.getName(); + if (StringUtils.isBlank(name)) { + name = polarisDiscoveryProperties.getService(); + } + request.setName(name); request.setNamespace(polarisDiscoveryProperties.getNamespace()); request.setService(polarisDiscoveryProperties.getService()); request.setProtocol("http"); diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/ContractProperties.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/ContractProperties.java index 7a34c8360..21d098cae 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/ContractProperties.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/ContractProperties.java @@ -51,4 +51,8 @@ public interface ContractProperties { boolean isReportEnabled(); void setReportEnabled(boolean reportEnabled); + + String getName(); + + void setName(String name); } 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 9b442a942..2be391352 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; } @@ -151,4 +153,16 @@ public boolean isReportEnabled() { public void setReportEnabled(boolean reportEnabled) { this.reportEnabled = reportEnabled; } + + public String getName() { + if (Objects.nonNull(extendContractProperties)) { + return extendContractProperties.getName(); + } + return name; + } + + @Override + public void setName(String name) { + this.name = name; + } } From 152aa35e6f2bc70826976f3a7d7ae1976bef40d6 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 13 May 2024 22:32:14 +0800 Subject: [PATCH 10/11] feat:add zero protection. --- .../config/PolarisContractProperties.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) 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 2be391352..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 @@ -73,6 +73,9 @@ public boolean isEnabled() { @Override public void setEnabled(boolean enabled) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setEnabled(enabled); + } this.enabled = enabled; } @@ -86,6 +89,9 @@ public String getBasePackage() { @Override public void setBasePackage(String basePackage) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setBasePackage(basePackage); + } this.basePackage = basePackage; } @@ -99,6 +105,9 @@ public String getExcludePath() { @Override public void setExcludePath(String excludePath) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setExcludePath(excludePath); + } this.excludePath = excludePath; } @@ -112,6 +121,9 @@ public String getGroup() { @Override public void setGroup(String group) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setGroup(group); + } this.group = group; } @@ -125,6 +137,9 @@ public String getBasePath() { @Override public void setBasePath(String basePath) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setBasePath(basePath); + } this.basePath = basePath; } @@ -138,6 +153,9 @@ public boolean isExposure() { @Override public void setExposure(boolean exposure) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setExposure(exposure); + } this.exposure = exposure; } @@ -151,6 +169,9 @@ public boolean isReportEnabled() { @Override public void setReportEnabled(boolean reportEnabled) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setReportEnabled(reportEnabled); + } this.reportEnabled = reportEnabled; } @@ -163,6 +184,9 @@ public String getName() { @Override public void setName(String name) { + if (Objects.nonNull(extendContractProperties)) { + extendContractProperties.setName(name); + } this.name = name; } } From 1f9e2ed55a977d2aa1c5727724f9ac8625934399 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 21 May 2024 13:33:09 +0800 Subject: [PATCH 11/11] feat:add zero protection. --- .../tencent/cloud/common/util/GzipUtil.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java index 7843b112a..89b1d37f3 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/util/GzipUtil.java @@ -24,24 +24,29 @@ import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + /** * @author kysonli - * @date 2018/9/7 15:35 */ public final class GzipUtil { + private static final Logger LOG = LoggerFactory.getLogger(GzipUtil.class); + private GzipUtil() { } public static byte[] compress(String data, String charsetName) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - GZIPOutputStream gzip = new GZIPOutputStream(bos); - gzip.write(data.getBytes(charsetName)); - gzip.finish(); - gzip.close(); - byte[] ret = bos.toByteArray(); - bos.close(); - return ret; + try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(bos)) { + gzip.write(data.getBytes(charsetName)); + gzip.finish(); + return bos.toByteArray(); + } + catch (IOException e) { + LOG.error("compress data [{}] error", data, e); + throw e; + } } public static String compressBase64Encode(String data, String charsetName) throws IOException { @@ -56,20 +61,19 @@ public static String compressBase64Encode(byte[] byteData, String charsetName) t public static byte[] decompress(byte[] zipData) throws IOException { - ByteArrayInputStream bis = new ByteArrayInputStream(zipData); - GZIPInputStream gzip = new GZIPInputStream(bis); - byte[] buf = new byte[256]; - int num = -1; - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - while ((num = gzip.read(buf, 0, buf.length)) != -1) { - bos.write(buf, 0, num); + try (ByteArrayInputStream bis = new ByteArrayInputStream(zipData); GZIPInputStream gzip = new GZIPInputStream(bis); ByteArrayOutputStream bos = new ByteArrayOutputStream()) { + byte[] buf = new byte[256]; + int num; + while ((num = gzip.read(buf)) != -1) { + bos.write(buf, 0, num); + } + bos.flush(); + return bos.toByteArray(); + } + catch (IOException e) { + LOG.error("decompress zip data error", e); + throw e; } - gzip.close(); - bis.close(); - byte[] ret = bos.toByteArray(); - bos.flush(); - bos.close(); - return ret; } public static String base64DecodeDecompress(String data, String charsetName) throws IOException {