From b0e13132fa85a7415312626d2a596344974c1f49 Mon Sep 17 00:00:00 2001 From: DerekYRC <44155264+DerekYRC@users.noreply.github.com> Date: Wed, 2 Nov 2022 20:16:46 +0800 Subject: [PATCH] Register the service with the ProviderAPI#registerInstance method (#687) * publish spring event named ConfigChangeSpringEvent when the configuration is changed * update changelog * register the service with the ProviderAPI#registerInstance method * update changelog * delete PolarisDiscoveryProperties#getHeartbeatEnabled Co-authored-by: Haotian Zhang <928016560@qq.com> --- CHANGELOG.md | 2 +- .../polaris/PolarisDiscoveryProperties.java | 22 +------- .../registry/PolarisServiceRegistry.java | 50 +++++++++---------- .../PolarisDiscoveryPropertiesTest.java | 5 -- 4 files changed, 25 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2412268b5..715b9f1a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,5 +2,5 @@ --- - [Optimize:optimize spring value processor](https://github.com/Tencent/spring-cloud-tencent/pull/669) +- [Optimize: Register the service with the ProviderAPI#registerInstance method.](https://github.com/Tencent/spring-cloud-tencent/pull/687) - [Optimize:optimize spring annotation.](https://github.com/Tencent/spring-cloud-tencent/pull/689) - 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 731676858..cf1b9dfff 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 @@ -84,12 +84,6 @@ public class PolarisDiscoveryProperties { @Value("${spring.cloud.polaris.discovery.register:#{true}}") private Boolean registerEnabled; - /** - * If heartbeat enabled. - */ - @Value("${spring.cloud.polaris.discovery.heartbeat.enabled:#{true}}") - private Boolean heartbeatEnabled = true; - /** * Heartbeat interval ( 0 < interval <= 60). * Time unit: second. Default: 5. @@ -108,15 +102,6 @@ public class PolarisDiscoveryProperties { */ private Long serviceListRefreshInterval = 60000L; - - public boolean isHeartbeatEnabled() { - return heartbeatEnabled; - } - - public void setHeartbeatEnabled(Boolean heartbeatEnabled) { - this.heartbeatEnabled = heartbeatEnabled; - } - public String getNamespace() { return namespace; } @@ -206,7 +191,7 @@ public class PolarisDiscoveryProperties { } public Integer getHeartbeatInterval() { - if (this.heartbeatEnabled && (this.heartbeatInterval <= 0 || this.heartbeatInterval > 60)) { + if (this.heartbeatInterval <= 0 || this.heartbeatInterval > 60) { heartbeatInterval = DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; } return heartbeatInterval; @@ -228,10 +213,6 @@ public class PolarisDiscoveryProperties { this.registerEnabled = registerEnabled; } - public Boolean getHeartbeatEnabled() { - return heartbeatEnabled; - } - @Override public String toString() { return "PolarisDiscoveryProperties{" + @@ -244,7 +225,6 @@ public class PolarisDiscoveryProperties { ", port=" + port + ", enabled=" + enabled + ", registerEnabled=" + registerEnabled + - ", heartbeatEnabled=" + heartbeatEnabled + ", heartbeatInterval=" + heartbeatInterval + ", healthCheckUrl='" + healthCheckUrl + '\'' + ", serviceListRefreshInterval=" + serviceListRefreshInterval + 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 a704e8d1b..53d8500e9 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 @@ -68,7 +68,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { try { String healthCheckEndpoint = polarisDiscoveryProperties.getHealthCheckUrl(); - // First determine whether health-check-url is configured. - // If configured, the service instance health check needs to be executed - // first. // If the health check passes, the heartbeat will be reported. // If it does not pass, the heartbeat will not be reported. - if (StringUtils.isNotBlank(healthCheckEndpoint)) { - if (!healthCheckEndpoint.startsWith("/")) { - healthCheckEndpoint = "/" + healthCheckEndpoint; - } - - String healthCheckUrl = String.format("http://%s:%s%s", - heartbeatRequest.getHost(), heartbeatRequest.getPort(), healthCheckEndpoint); - - if (!OkHttpUtil.get(healthCheckUrl, null)) { - LOGGER.error("backend service health check failed. health check endpoint = {}", healthCheckEndpoint); - return; - } + if (!healthCheckEndpoint.startsWith("/")) { + healthCheckEndpoint = "/" + healthCheckEndpoint; + } + + String healthCheckUrl = String.format("http://%s:%s%s", heartbeatRequest.getHost(), + heartbeatRequest.getPort(), healthCheckEndpoint); + + if (!OkHttpUtil.get(healthCheckUrl, null)) { + LOGGER.error("backend service health check failed. health check endpoint = {}", + healthCheckEndpoint); + return; } polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest); diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/PolarisDiscoveryPropertiesTest.java b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/PolarisDiscoveryPropertiesTest.java index 52a264868..6b7ed6e47 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/PolarisDiscoveryPropertiesTest.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/test/java/com/tencent/cloud/polaris/PolarisDiscoveryPropertiesTest.java @@ -37,10 +37,6 @@ public class PolarisDiscoveryPropertiesTest { public void testGetAndSet() { PolarisDiscoveryProperties polarisDiscoveryProperties = new PolarisDiscoveryProperties(); - // HeartbeatEnabled - polarisDiscoveryProperties.setHeartbeatEnabled(true); - assertThat(polarisDiscoveryProperties.isHeartbeatEnabled()).isTrue(); - // HeartbeatEnabled polarisDiscoveryProperties.setHeartbeatInterval(200); assertThat(polarisDiscoveryProperties.getHeartbeatInterval()).isEqualTo(5); @@ -103,7 +99,6 @@ public class PolarisDiscoveryPropertiesTest { + ", port=9091" + ", enabled=true" + ", registerEnabled=true" - + ", heartbeatEnabled=true" + ", heartbeatInterval=20" + ", healthCheckUrl='/health'" + ", serviceListRefreshInterval=1000}");