[Feature] Add configurable heartbeat interval support (2021). (#444)

pull/447/head
VOPEN.XYZ 2 years ago committed by GitHub
parent 2aa50f3c6e
commit 719235ec8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,3 +4,4 @@
- [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)
- [Add configurable heartbeat interval support](https://github.com/Tencent/spring-cloud-tencent/pull/444)

@ -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 +
'}';

@ -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<Registration> {
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<Registration> {
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<Registration> {
catch (Exception e) {
LOGGER.error("polaris heartbeat runtime error", e);
}
}, TTL, TTL, TimeUnit.SECONDS);
}, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), MILLISECONDS);
}
}

@ -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}");
}

@ -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() {
}

Loading…
Cancel
Save