From da2c08a3029917a5217cd43ab305d31697df3a83 Mon Sep 17 00:00:00 2001 From: Haotian Zhang <928016560@qq.com> Date: Tue, 17 Jun 2025 19:29:56 +0800 Subject: [PATCH 1/4] docs:update CHANGELOG. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edd6a05ac..e3eddf034 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,6 @@ # Change Log --- -- [fix: polaris.discovery.heartbeat.enabled not effective.](https://github.com/Tencent/spring-cloud-tencent/pull/xxxx) - [fix: add gateway context config example.](https://github.com/Tencent/spring-cloud-tencent/pull/1563) - [feat:support config empty protection.](https://github.com/Tencent/spring-cloud-tencent/pull/1585) - [feat:upgrade springframework version.](https://github.com/Tencent/spring-cloud-tencent/pull/1590) @@ -10,3 +9,4 @@ - [feat:support config all recover enabled.](https://github.com/Tencent/spring-cloud-tencent/pull/1604) - [feat:support stat reporting path aggregation.](https://github.com/Tencent/spring-cloud-tencent/pull/1608) - [feat:support instance detect.](https://github.com/Tencent/spring-cloud-tencent/pull/1617) +- [fix: polaris.discovery.heartbeat.enabled not effective.](https://github.com/Tencent/spring-cloud-tencent/pull/1621) From d94c7a00a3b2f925b7eec5096aaf9086ea10f730 Mon Sep 17 00:00:00 2001 From: zihenz <1290829032@qq.com> Date: Tue, 17 Jun 2025 20:20:46 +0800 Subject: [PATCH 2/4] fix heartbeat control logic and property name --- .../tencent/cloud/polaris/PolarisDiscoveryProperties.java | 1 + .../cloud/polaris/registry/PolarisServiceRegistry.java | 5 ++--- 2 files changed, 3 insertions(+), 3 deletions(-) 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 b940919b8..82e64d426 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 @@ -80,6 +80,7 @@ public class PolarisDiscoveryProperties { /** * Enable heartbeat or not. */ + @Value("${spring.cloud.polaris.discovery.heartbeat.enabled:true}") private Boolean heartbeatEnabled = true; /** 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 e09b2e67a..4fc5a8efa 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 @@ -87,8 +87,7 @@ public class PolarisServiceRegistry implements ServiceRegistry Date: Tue, 17 Jun 2025 21:27:34 +0800 Subject: [PATCH 3/4] fix --- .../registry/PolarisServiceRegistry.java | 42 +++++++++++++------ .../src/main/resources/application.yml | 2 + 2 files changed, 31 insertions(+), 13 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 4fc5a8efa..dd9649e9e 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 @@ -77,7 +77,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { try { - // If the health check passes, the heartbeat will be reported. - // If it does not pass, the heartbeat will not be reported. + // 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(), diff --git a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml index 973625425..1fa63d14c 100644 --- a/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml +++ b/spring-cloud-tencent-examples/quickstart-example/quickstart-callee-service-a/src/main/resources/application.yml @@ -13,6 +13,8 @@ spring: discovery: enabled: true register: true + heartbeat: + enabled: false contract: exposure: true report: From bf58604ad3bef693d7c48b7f609a0de4c158cb9f Mon Sep 17 00:00:00 2001 From: zihenz <1290829032@qq.com> Date: Wed, 18 Jun 2025 11:31:23 +0800 Subject: [PATCH 4/4] 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(); } }