From 444804a2fdaf42e309fb338d9802b8f2ab69e7b4 Mon Sep 17 00:00:00 2001 From: zihenz <1290829032@qq.com> Date: Wed, 18 Jun 2025 11:31:23 +0800 Subject: [PATCH] fix optimize heartbeat control in service registry --- .../registry/PolarisServiceRegistry.java | 240 ++++++++++-------- 1 file changed, 137 insertions(+), 103 deletions(-) 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 dd9649e9e..2bbad41a9 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 @@ -55,6 +55,8 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.DisposableBean; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent; +import org.springframework.context.event.EventListener; import org.springframework.http.HttpHeaders; import static java.util.concurrent.TimeUnit.SECONDS; @@ -87,17 +89,10 @@ public class PolarisServiceRegistry implements ServiceRegistry { + try { + // Check if heartbeat should still be running + if (!polarisDiscoveryProperties.getHeartbeatEnabled() || + StringUtils.isBlank(polarisDiscoveryProperties.getHealthCheckUrl())) { + LOGGER.info("Heartbeat conditions no longer met, stopping heartbeat task."); + stopHeartbeat(); + return; + } + + // Perform health check + Map headers = new HashMap<>(1); + headers.put(HttpHeaders.USER_AGENT, "polaris"); + if (!OkHttpUtil.checkUrl(heartbeatRequest.getHost(), heartbeatRequest.getPort(), + polarisDiscoveryProperties.getHealthCheckUrl(), headers)) { + LOGGER.error("Backend service health check failed. health check endpoint = {}", + polarisDiscoveryProperties.getHealthCheckUrl()); + return; + } + + // Send heartbeat + polarisSDKContextManager.getProviderAPI().heartbeat(heartbeatRequest); + LOGGER.debug("Polaris heartbeat is sent successfully."); + } + catch (PolarisException e) { + LOGGER.error("Polaris heartbeat error with code [{}]", e.getCode(), e); + } + catch (Exception e) { + LOGGER.error("Polaris heartbeat runtime error", e); + } + }, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), SECONDS); + + LOGGER.info("Heartbeat task scheduled with interval {} seconds.", polarisDiscoveryProperties.getHeartbeatInterval()); + } + + private void stopHeartbeat() { + if (heartbeatExecutor != null) { + try { + heartbeatExecutor.shutdownNow(); + if (heartbeatExecutor.awaitTermination(5, SECONDS)) { + LOGGER.info("Polaris heartbeat task stopped gracefully."); + } else { + LOGGER.warn("Polaris heartbeat task did not terminate in time."); + } + } catch (InterruptedException e) { + LOGGER.warn("Interrupted while stopping heartbeat task.", e); + Thread.currentThread().interrupt(); + } finally { + heartbeatExecutor = null; + } + } + } + + @EventListener(RefreshScopeRefreshedEvent.class) + public void onPropertyRefreshed() { + if (!polarisDiscoveryProperties.getHeartbeatEnabled()) { + LOGGER.info("Heartbeat disabled via property refresh, stopping heartbeat task."); + stopHeartbeat(); } } @@ -196,6 +284,8 @@ public class PolarisServiceRegistry implements ServiceRegistry { - try { - // Skip heartbeat if disabled or health check URL is not set - if (!polarisDiscoveryProperties.getHeartbeatEnabled() || - StringUtils.isBlank(polarisDiscoveryProperties.getHealthCheckUrl())) { - return; - } - - Map headers = new HashMap<>(1); - headers.put(HttpHeaders.USER_AGENT, "polaris"); - if (!OkHttpUtil.checkUrl(heartbeatRequest.getHost(), heartbeatRequest.getPort(), - polarisDiscoveryProperties.getHealthCheckUrl(), headers)) { - LOGGER.error("backend service health check failed. health check endpoint = {}", - polarisDiscoveryProperties.getHealthCheckUrl()); - return; - } - - polarisSDKContextManager.getProviderAPI().heartbeat(heartbeatRequest); - LOGGER.trace("Polaris heartbeat is sent"); - } - catch (PolarisException e) { - LOGGER.error("polaris heartbeat error with code [{}]", e.getCode(), e); - } - catch (Exception e) { - LOGGER.error("polaris heartbeat runtime error", e); - } - }, polarisDiscoveryProperties.getHeartbeatInterval(), polarisDiscoveryProperties.getHeartbeatInterval(), SECONDS); - } - @Override public void destroy() { - if (heartbeatExecutor != null) { - heartbeatExecutor.shutdown(); - } + stopHeartbeat(); } }