diff --git a/CHANGELOG.md b/CHANGELOG.md index c736b92ac8..e3eddf034f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,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) 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 c8487ea26a..82e64d426e 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 @@ -77,6 +77,12 @@ public class PolarisDiscoveryProperties { */ private Boolean enabled = true; + /** + * Enable heartbeat or not. + */ + @Value("${spring.cloud.polaris.discovery.heartbeat.enabled:true}") + private Boolean heartbeatEnabled = true; + /** * If instance registered. */ @@ -279,6 +285,14 @@ public void setDetect(Detect detect) { this.detect = detect; } + public Boolean getHeartbeatEnabled() { + return heartbeatEnabled; + } + + public void setHeartbeatEnabled(Boolean heartbeatEnabled) { + this.heartbeatEnabled = heartbeatEnabled; + } + @Override public String toString() { return "PolarisDiscoveryProperties{" + @@ -290,6 +304,7 @@ public String toString() { ", version='" + version + '\'' + ", protocol='" + protocol + '\'' + ", enabled=" + enabled + + ", heartbeatEnabled=" + heartbeatEnabled + ", registerEnabled=" + registerEnabled + ", heartbeatInterval=" + heartbeatInterval + ", healthCheckUrl='" + healthCheckUrl + '\'' + 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 40e5ab59a6..6eae53041f 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.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; @@ -77,7 +79,6 @@ 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. - 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(); - } } } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json index aa4fe5f874..d35c2294e5 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -18,6 +18,12 @@ "defaultValue": true, "description": "enable polaris discovery or not." }, + { + "name": "spring.cloud.polaris.discovery.heartbeat.enabled", + "type": "java.lang.Boolean", + "defaultValue": true, + "description": "enable polaris heartbeat or not." + }, { "name": "spring.cloud.polaris.discovery.register", "type": "java.lang.Boolean", 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 9736254257..1fa63d14ce 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: