fix:fix heartbeat interval different configuration from polaris-java SDK. (#558)

pull/564/head
Haotian Zhang 2 years ago committed by GitHub
parent 31bbaa61fc
commit de91e16e1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -28,4 +28,5 @@
- [feature:add @ConditionalOnConfigReflectEnabled annotation](https://github.com/Tencent/spring-cloud-tencent/pull/538) - [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) - [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) - [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) - [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)

@ -98,11 +98,11 @@ public class PolarisDiscoveryProperties {
private Boolean heartbeatEnabled = true; private Boolean heartbeatEnabled = true;
/** /**
* Heart beat interval (The time interval must be greater than zero). * Heartbeat interval ( 0 < interval <= 60).
* Time unit: millisecond. Default: 5000. * Time unit: second. Default: 5.
* @see ContextConstant#DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL * @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. * Custom health check url to override default.
@ -212,8 +212,8 @@ public class PolarisDiscoveryProperties {
} }
public Integer getHeartbeatInterval() { public Integer getHeartbeatInterval() {
if (this.heartbeatEnabled && this.heartbeatInterval <= 0) { if (this.heartbeatEnabled && (this.heartbeatInterval <= 0 || this.heartbeatInterval > 60)) {
return DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; heartbeatInterval = DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL;
} }
return heartbeatInterval; return heartbeatInterval;
} }

@ -41,7 +41,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.client.serviceregistry.ServiceRegistry; 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; import static org.springframework.util.ReflectionUtils.rethrowRuntimeException;
/** /**
@ -70,7 +70,7 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
if (polarisDiscoveryProperties.isHeartbeatEnabled()) { if (polarisDiscoveryProperties.isHeartbeatEnabled()) {
this.heartbeatExecutor = Executors this.heartbeatExecutor = Executors
.newSingleThreadScheduledExecutor(new NamedThreadFactory("spring-cloud-heartbeat")); .newSingleThreadScheduledExecutor(new NamedThreadFactory("polaris-heartbeat"));
} }
else { else {
this.heartbeatExecutor = null; this.heartbeatExecutor = null;
@ -209,13 +209,14 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
} }
polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest); polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest);
log.trace("Polaris heartbeat is sent");
} }
catch (PolarisException e) { catch (PolarisException e) {
log.error("polaris heartbeat[{}]", e.getCode(), e); log.error("polaris heartbeat error with code [{}]", e.getCode(), e);
} }
catch (Exception e) { catch (Exception e) {
log.error("polaris heartbeat runtime error", e); log.error("polaris heartbeat runtime error", e);
} }
}, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), MILLISECONDS); }, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), SECONDS);
} }
} }

@ -27,8 +27,8 @@
{ {
"name": "spring.cloud.polaris.discovery.heartbeat-interval", "name": "spring.cloud.polaris.discovery.heartbeat-interval",
"type": "java.lang.Integer", "type": "java.lang.Integer",
"defaultValue": "5000", "defaultValue": "5",
"description": "Millis interval of Heart beat. Default: 5000." "description": "Seconds interval of Heart beat. Default: 5."
}, },
{ {
"name": "spring.cloud.polaris.discovery.health-check-url", "name": "spring.cloud.polaris.discovery.health-check-url",

@ -42,8 +42,12 @@ public class PolarisDiscoveryPropertiesTest {
assertThat(polarisDiscoveryProperties.isHeartbeatEnabled()).isTrue(); assertThat(polarisDiscoveryProperties.isHeartbeatEnabled()).isTrue();
// HeartbeatEnabled // HeartbeatEnabled
polarisDiscoveryProperties.setHeartbeatInterval(2000); polarisDiscoveryProperties.setHeartbeatInterval(200);
assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(2000); assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(5);
polarisDiscoveryProperties.setHeartbeatInterval(0);
assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(5);
polarisDiscoveryProperties.setHeartbeatInterval(20);
assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(20);
// Namespace // Namespace
polarisDiscoveryProperties.setNamespace(NAMESPACE_TEST); polarisDiscoveryProperties.setNamespace(NAMESPACE_TEST);
@ -100,7 +104,7 @@ public class PolarisDiscoveryPropertiesTest {
+ ", enabled=true" + ", enabled=true"
+ ", registerEnabled=true" + ", registerEnabled=true"
+ ", heartbeatEnabled=true" + ", heartbeatEnabled=true"
+ ", heartbeatInterval=2000" + ", heartbeatInterval=20"
+ ", healthCheckUrl='/health'" + ", healthCheckUrl='/health'"
+ ", serviceListRefreshInterval=1000}"); + ", serviceListRefreshInterval=1000}");
} }

@ -37,9 +37,9 @@ public final class ContextConstant {
public static final String UTF_8 = StandardCharsets.UTF_8.name(); 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() { private ContextConstant() {
} }

Loading…
Cancel
Save