From de91e16e1aac828870e7ef2f4fc4b576e463ba63 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Mon, 5 Sep 2022 11:01:46 +0800 Subject: [PATCH] fix:fix heartbeat interval different configuration from polaris-java SDK. (#558) --- CHANGELOG.md | 3 ++- .../cloud/polaris/PolarisDiscoveryProperties.java | 10 +++++----- .../cloud/polaris/registry/PolarisServiceRegistry.java | 9 +++++---- .../additional-spring-configuration-metadata.json | 4 ++-- .../cloud/polaris/PolarisDiscoveryPropertiesTest.java | 10 +++++++--- .../tencent/cloud/common/constant/ContextConstant.java | 4 ++-- 6 files changed, 23 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ad169944..7db6c0252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,4 +28,5 @@ - [feature:add @ConditionalOnConfigReflectEnabled annotation](https://github.com/Tencent/spring-cloud-tencent/pull/538) - [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/544) - [Optimize: add switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/549) -- [Report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/555) \ No newline at end of file +- [Report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/555) +- [fix:fix heartbeat interval different configuration from polaris-java SDK.](https://github.com/Tencent/spring-cloud-tencent/pull/558) 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 05b9bcfee..05b9cacc8 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 @@ -98,11 +98,11 @@ public class PolarisDiscoveryProperties { private Boolean heartbeatEnabled = true; /** - * Heart beat interval (The time interval must be greater than zero). - * Time unit: millisecond. Default: 5000. + * Heartbeat interval ( 0 < interval <= 60). + * Time unit: second. Default: 5. * @see ContextConstant#DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL */ - private Integer heartbeatInterval = 5000; + private Integer heartbeatInterval = DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; /** * Custom health check url to override default. @@ -212,8 +212,8 @@ public class PolarisDiscoveryProperties { } public Integer getHeartbeatInterval() { - if (this.heartbeatEnabled && this.heartbeatInterval <= 0) { - return DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; + if (this.heartbeatEnabled && (this.heartbeatInterval <= 0 || this.heartbeatInterval > 60)) { + heartbeatInterval = DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; } return heartbeatInterval; } 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 e9bd0fb6e..aad9ff412 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 @@ -41,7 +41,7 @@ import org.springframework.beans.BeanUtils; import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; -import static java.util.concurrent.TimeUnit.MILLISECONDS; +import static java.util.concurrent.TimeUnit.SECONDS; import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; /** @@ -70,7 +70,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { if (polarisDiscoveryProperties.isHeartbeatEnabled()) { this.heartbeatExecutor = Executors - .newSingleThreadScheduledExecutor(new NamedThreadFactory("spring-cloud-heartbeat")); + .newSingleThreadScheduledExecutor(new NamedThreadFactory("polaris-heartbeat")); } else { this.heartbeatExecutor = null; @@ -209,13 +209,14 @@ public class PolarisServiceRegistry implements ServiceRegistry { } polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest); + log.trace("Polaris heartbeat is sent"); } catch (PolarisException e) { - log.error("polaris heartbeat[{}]", e.getCode(), e); + log.error("polaris heartbeat error with code [{}]", e.getCode(), e); } catch (Exception e) { log.error("polaris heartbeat runtime error", e); } - }, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), MILLISECONDS); + }, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), SECONDS); } } 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 93372d6dc..22f6e5363 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 @@ -27,8 +27,8 @@ { "name": "spring.cloud.polaris.discovery.heartbeat-interval", "type": "java.lang.Integer", - "defaultValue": "5000", - "description": "Millis interval of Heart beat. Default: 5000." + "defaultValue": "5", + "description": "Seconds interval of Heart beat. Default: 5." }, { "name": "spring.cloud.polaris.discovery.health-check-url", 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 b371fac02..52a264868 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 @@ -42,8 +42,12 @@ public class PolarisDiscoveryPropertiesTest { assertThat(polarisDiscoveryProperties.isHeartbeatEnabled()).isTrue(); // HeartbeatEnabled - polarisDiscoveryProperties.setHeartbeatInterval(2000); - assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(2000); + polarisDiscoveryProperties.setHeartbeatInterval(200); + assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(5); + polarisDiscoveryProperties.setHeartbeatInterval(0); + assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(5); + polarisDiscoveryProperties.setHeartbeatInterval(20); + assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(20); // Namespace polarisDiscoveryProperties.setNamespace(NAMESPACE_TEST); @@ -100,7 +104,7 @@ public class PolarisDiscoveryPropertiesTest { + ", enabled=true" + ", registerEnabled=true" + ", heartbeatEnabled=true" - + ", heartbeatInterval=2000" + + ", heartbeatInterval=20" + ", healthCheckUrl='/health'" + ", serviceListRefreshInterval=1000}"); } diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java index 8fa44f2fa..66a8f08b5 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/ContextConstant.java @@ -37,9 +37,9 @@ public final class ContextConstant { public static final String UTF_8 = StandardCharsets.UTF_8.name(); /** - * Default registry heartbeat time interval, default: 5000 (ms). + * Default registry heartbeat time interval: 5 (s). */ - public static final Integer DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL = 5000; + public static final Integer DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL = 5; private ContextConstant() { }