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

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

@ -30,3 +30,4 @@
- [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/545) - [fix:set error handler named EnhancedRestTemplateReporter for RestTemplate](https://github.com/Tencent/spring-cloud-tencent/pull/545)
- [Optimize: add switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/550) - [Optimize: add switch for report call result and default false](https://github.com/Tencent/spring-cloud-tencent/pull/550)
- [Report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/556) - [Report the labels in request when report the result of invocation by Feign](https://github.com/Tencent/spring-cloud-tencent/pull/556)
- [fix:fix heartbeat interval different configuration from polaris-java SDK.](https://github.com/Tencent/spring-cloud-tencent/pull/559)

@ -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.
@ -213,8 +213,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;
/** /**
@ -69,8 +69,8 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
this.staticMetadataManager = staticMetadataManager; this.staticMetadataManager = staticMetadataManager;
if (polarisDiscoveryProperties.isHeartbeatEnabled()) { if (polarisDiscoveryProperties.isHeartbeatEnabled()) {
this.heartbeatExecutor = Executors.newSingleThreadScheduledExecutor( this.heartbeatExecutor = Executors
new NamedThreadFactory("spring-cloud-heartbeat")); .newSingleThreadScheduledExecutor(new NamedThreadFactory("polaris-heartbeat"));
} }
else { else {
this.heartbeatExecutor = null; this.heartbeatExecutor = null;
@ -207,13 +207,14 @@ public class PolarisServiceRegistry implements ServiceRegistry<Registration> {
} }
polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest); polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest);
LOGGER.trace("Polaris heartbeat is sent");
} }
catch (PolarisException e) { catch (PolarisException e) {
LOGGER.error("polaris heartbeat[{}]", e.getCode(), e); LOGGER.error("polaris heartbeat error with code [{}]", e.getCode(), e);
} }
catch (Exception e) { catch (Exception e) {
LOGGER.error("polaris heartbeat runtime error", e); LOGGER.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