diff --git a/CHANGELOG.md b/CHANGELOG.md index cde3a78d2..55f71c8cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,4 +3,5 @@ - [Bugfix: optimize ratelimit actuator](https://github.com/Tencent/spring-cloud-tencent/pull/420) - [Optimize: add EncodeTransferMedataRestTemplateInterceptor to RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/440) -- [Feature: add rate limit filter debug log](https://github.com/Tencent/spring-cloud-tencent/pull/437) \ No newline at end of file +- [Feature: add rate limit filter debug log](https://github.com/Tencent/spring-cloud-tencent/pull/437) +- [Add configurable heartbeat interval support](https://github.com/Tencent/spring-cloud-tencent/pull/444) 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 8ceadf3d2..70c752df7 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 @@ -30,6 +30,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; +import static com.tencent.cloud.common.constant.ContextConstant.DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; + /** * Properties for Polaris. * @@ -95,6 +97,13 @@ public class PolarisDiscoveryProperties { @Value("${spring.cloud.polaris.discovery.heartbeat.enabled:#{true}}") private Boolean heartbeatEnabled = true; + /** + * Heart beat interval (The time interval must be greater than zero). + * Time unit: millisecond. Default: 5000. + * @see ContextConstant#DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL + */ + private Integer heartbeatInterval = 5000; + /** * Custom health check url to override default. */ @@ -203,6 +212,17 @@ public class PolarisDiscoveryProperties { this.serviceListRefreshInterval = serviceListRefreshInterval; } + public Integer getHeartbeatInterval() { + if (this.heartbeatEnabled && this.heartbeatInterval <= 0) { + return DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; + } + return heartbeatInterval; + } + + public void setHeartbeatInterval(Integer heartbeatInterval) { + this.heartbeatInterval = heartbeatInterval; + } + @Override public String toString() { return "PolarisDiscoveryProperties{" + @@ -216,6 +236,7 @@ public class PolarisDiscoveryProperties { ", enabled=" + enabled + ", registerEnabled=" + registerEnabled + ", heartbeatEnabled=" + heartbeatEnabled + + ", heartbeatInterval=" + heartbeatInterval + ", healthCheckUrl='" + healthCheckUrl + '\'' + ", serviceListRefreshInterval=" + serviceListRefreshInterval + '}'; 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 36b755100..e92d81f1c 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 @@ -20,7 +20,6 @@ package com.tencent.cloud.polaris.registry; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; import com.tencent.cloud.common.metadata.StaticMetadataManager; import com.tencent.cloud.polaris.PolarisDiscoveryProperties; @@ -42,6 +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 org.springframework.util.ReflectionUtils.rethrowRuntimeException; /** @@ -53,8 +53,6 @@ public class PolarisServiceRegistry implements ServiceRegistry { private static final Logger LOGGER = LoggerFactory.getLogger(PolarisServiceRegistry.class); - private static final int TTL = 5; - private final PolarisDiscoveryProperties polarisDiscoveryProperties; private final PolarisDiscoveryHandler polarisDiscoveryHandler; @@ -98,7 +96,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { instanceRegisterRequest.setZone(staticMetadataManager.getZone()); instanceRegisterRequest.setCampus(staticMetadataManager.getCampus()); if (null != heartbeatExecutor) { - instanceRegisterRequest.setTtl(TTL); + instanceRegisterRequest.setTtl(polarisDiscoveryProperties.getHeartbeatInterval()); } instanceRegisterRequest.setMetadata(registration.getMetadata()); instanceRegisterRequest.setProtocol(polarisDiscoveryProperties.getProtocol()); @@ -216,6 +214,6 @@ public class PolarisServiceRegistry implements ServiceRegistry { catch (Exception e) { LOGGER.error("polaris heartbeat runtime error", e); } - }, TTL, TTL, TimeUnit.SECONDS); + }, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), MILLISECONDS); } } 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 390c6ea24..b371fac02 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 @@ -41,6 +41,10 @@ public class PolarisDiscoveryPropertiesTest { polarisDiscoveryProperties.setHeartbeatEnabled(true); assertThat(polarisDiscoveryProperties.isHeartbeatEnabled()).isTrue(); + // HeartbeatEnabled + polarisDiscoveryProperties.setHeartbeatInterval(2000); + assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(2000); + // Namespace polarisDiscoveryProperties.setNamespace(NAMESPACE_TEST); assertThat(polarisDiscoveryProperties.getNamespace()).isEqualTo(NAMESPACE_TEST); @@ -96,6 +100,7 @@ public class PolarisDiscoveryPropertiesTest { + ", enabled=true" + ", registerEnabled=true" + ", heartbeatEnabled=true" + + ", heartbeatInterval=2000" + ", 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 e2b2608b3..8117ef8f4 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 @@ -36,6 +36,11 @@ public final class ContextConstant { */ public static final String UTF_8 = StandardCharsets.UTF_8.name(); + /** + * Default registry heartbeat time interval, default: 5000 (ms). + */ + public static final Integer DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL = 5000; + private ContextConstant() { }