diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ec220ce5..c20bfa318 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,3 +14,4 @@ - [fix: fix ConfigChangeListener and unit test](https://github.com/Tencent/spring-cloud-tencent/pull/1655) - [feat: support spring-retry and feign config refresh and feign eager load support schema](https://github.com/Tencent/spring-cloud-tencent/pull/1650) - [fix: fix ConfigChangeListener ut bug](https://github.com/Tencent/spring-cloud-tencent/pull/1660) +- [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 93794f2de..4eb8d1fe6 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 @@ -78,22 +78,22 @@ public class PolarisDiscoveryProperties { private Boolean enabled = true; /** - * Enable heartbeat or not. + * If instance registered. */ - @Value("${spring.cloud.polaris.discovery.heartbeat-enabled:true}") - private Boolean heartbeatEnabled = true; + @Value("${spring.cloud.polaris.discovery.register:#{true}}") + private Boolean registerEnabled; /** - * Heartbeat interval (seconds). + * Enable heartbeat or not. */ - @Value("${spring.cloud.polaris.discovery.heartbeat-interval:5}") - private Integer heartbeatInterval = 5; + private Boolean heartbeatEnabled = true; /** - * If instance registered. + * Heartbeat interval ( 0 < interval <= 60). + * Time unit: second. Default: 5. + * @see ContextConstant#DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL */ - @Value("${spring.cloud.polaris.discovery.register:#{true}}") - private Boolean registerEnabled; + private Integer heartbeatInterval = DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; /** * Custom health check url to override default. @@ -213,6 +213,14 @@ public class PolarisDiscoveryProperties { this.serviceListRefreshInterval = serviceListRefreshInterval; } + public Boolean getHeartbeatEnabled() { + return heartbeatEnabled; + } + + public void setHeartbeatEnabled(Boolean heartbeatEnabled) { + this.heartbeatEnabled = heartbeatEnabled; + } + public Integer getHeartbeatInterval() { if (this.heartbeatInterval <= 0 || this.heartbeatInterval > 60) { heartbeatInterval = DEFAULT_REGISTRY_HEARTBEAT_TIME_INTERVAL; @@ -284,14 +292,6 @@ public class PolarisDiscoveryProperties { this.detect = detect; } - public Boolean getHeartbeatEnabled() { - return heartbeatEnabled; - } - - public void setHeartbeatEnabled(Boolean heartbeatEnabled) { - this.heartbeatEnabled = heartbeatEnabled; - } - @Override public String toString() { return "PolarisDiscoveryProperties{" + @@ -303,8 +303,8 @@ public class PolarisDiscoveryProperties { ", version='" + version + '\'' + ", protocol='" + protocol + '\'' + ", enabled=" + enabled + - ", heartbeatEnabled=" + heartbeatEnabled + ", 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 8fda5966b..c224e7ef2 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 @@ -22,7 +22,6 @@ import java.util.Map; import java.util.Objects; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; import com.tencent.cloud.common.metadata.MetadataContext; import com.tencent.cloud.common.metadata.StaticMetadataManager; @@ -56,13 +55,10 @@ 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; import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; -import org.springframework.beans.factory.annotation.Value; /** * Service registry of Polaris. @@ -81,6 +77,7 @@ public class PolarisServiceRegistry implements ServiceRegistry { - try { - boolean canSendHeartbeat = true; - if (StringUtils.isNotBlank(healthCheckUrl)) { - Map headers = new HashMap<>(1); - headers.put(HttpHeaders.USER_AGENT, "polaris"); - canSendHeartbeat = OkHttpUtil.checkUrl( - instanceRegisterRequest.getHost(), - instanceRegisterRequest.getPort(), - healthCheckUrl, - headers - ); + } + registration.setInstanceId(instanceRegisterResponse.getInstanceId()); + LOGGER.info("polaris registry, {} {} {} {}:{} {} {} {} {} register finished", polarisDiscoveryProperties.getNamespace(), + registration.getServiceId(), registration.getInstanceId(), registration.getHost(), registration.getPort(), + staticMetadataManager.getRegion(), staticMetadataManager.getZone(), staticMetadataManager.getCampus(), + staticMetadataManager.getMergedStaticMetadata()); + if (Objects.nonNull(polarisStatProperties) && polarisStatProperties.isEnabled()) { + try { + StatReporter statReporter = (StatReporter) polarisSDKContextManager.getSDKContext().getPlugins() + .getPlugin(PluginTypes.STAT_REPORTER.getBaseType(), StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS); + if (Objects.nonNull(statReporter)) { + ReporterMetaInfo reporterMetaInfo = statReporter.metaInfo(); + if (reporterMetaInfo.getPort() != null) { + LOGGER.info("Stat server started on port: " + reporterMetaInfo.getPort() + " (http)"); } - if (!canSendHeartbeat) { - LOGGER.warn("Health check failed, skip heartbeat this round. health check endpoint = {}", healthCheckUrl); - return; + else { + LOGGER.info("Stat server is set to type of Push gateway"); } - InstanceHeartbeatRequest heartbeatRequest = new InstanceHeartbeatRequest(); - BeanUtils.copyProperties(instanceRegisterRequest, heartbeatRequest); - heartbeatRequest.setInstanceID(instanceRegisterResponse.getInstanceId()); - providerClient.heartbeat(heartbeatRequest); - LOGGER.debug("Polaris heartbeat is sent successfully."); - } catch (Exception e) { - LOGGER.error("Polaris heartbeat runtime error", e); } - }, - polarisDiscoveryProperties.getHeartbeatInterval(), - polarisDiscoveryProperties.getHeartbeatInterval(), - TimeUnit.SECONDS); - } - - registration.setInstanceId(instanceRegisterResponse.getInstanceId()); - LOGGER.info("polaris registry, {} {} {} {}:{} {} {} {} {} register finished", - instanceRegisterRequest.getNamespace(), - instanceRegisterRequest.getService(), - instanceRegisterResponse.getInstanceId(), - instanceRegisterRequest.getHost(), - instanceRegisterRequest.getPort(), - instanceRegisterRequest.getRegion(), - instanceRegisterRequest.getZone(), - instanceRegisterRequest.getCampus(), - instanceRegisterRequest.getMetadata()); - - // Start stat server and configure service - if (polarisStatProperties.isEnabled()) { - startStatServer(); + else { + LOGGER.warn("Plugin StatReporter not found"); + } + } + catch (Exception e) { + LOGGER.warn("Stat server started error, ", e); + } } ServiceConfigImpl serviceConfig = (ServiceConfigImpl) polarisSDKContextManager.getSDKContext().getConfig() @@ -200,29 +190,8 @@ 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 5b12c9290..f7d88842a 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 @@ -19,16 +19,16 @@ "description": "enable polaris discovery or not." }, { - "name": "spring.cloud.polaris.discovery.heartbeat-enabled", + "name": "spring.cloud.polaris.discovery.register", "type": "java.lang.Boolean", "defaultValue": true, - "description": "enable polaris heartbeat or not." + "description": "enable polaris registration or not." }, { - "name": "spring.cloud.polaris.discovery.register", + "name": "spring.cloud.polaris.discovery.heartbeat-enabled", "type": "java.lang.Boolean", "defaultValue": true, - "description": "enable polaris registration or not." + "description": "enable polaris heartbeat or not." }, { "name": "spring.cloud.polaris.discovery.heartbeat-interval",