From 882b23ef297bf9dbd1978feb11de62b166bceeec Mon Sep 17 00:00:00 2001 From: bruceppeng Date: Mon, 24 Jan 2022 20:31:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=9C=8D=E5=8A=A1=E5=AE=9E?= =?UTF-8?q?=E4=BE=8B=E8=87=AA=E8=BA=AB=E5=81=A5=E5=BA=B7=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/bootstrap.yml | 2 +- .../caller/DiscoveryCallerController.java | 10 +++ .../src/main/resources/bootstrap.yml | 4 + .../cloud/polaris/PolarisProperties.java | 15 ++++ .../registry/PolarisServiceRegistry.java | 8 ++ .../cloud/polaris/util/OkHttpUtil.java | 83 +++++++++++++++++++ ...itional-spring-configuration-metadata.json | 6 ++ 7 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml index ea3cd0aa8..c40c21016 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-callee-service/src/main/resources/bootstrap.yml @@ -1,6 +1,6 @@ server: session-timeout: 1800 - port: 48081 + port: 48084 spring: application: name: DiscoveryCalleeService diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/java/com/tencent/cloud/polaris/discovery/service/caller/DiscoveryCallerController.java b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/java/com/tencent/cloud/polaris/discovery/service/caller/DiscoveryCallerController.java index af338861f..ae7b2f30b 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/java/com/tencent/cloud/polaris/discovery/service/caller/DiscoveryCallerController.java +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/java/com/tencent/cloud/polaris/discovery/service/caller/DiscoveryCallerController.java @@ -59,4 +59,14 @@ public class DiscoveryCallerController { return restTemplate.getForObject("http://DiscoveryCalleeService/discovery/service/callee/info", String.class); } + + /** + * health check + * + * @return 信息 + */ + @GetMapping("/healthCheck") + public String healthCheck() { + return "pk ok"; + } } diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml index 43930793e..949fae7c6 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml @@ -7,6 +7,10 @@ spring: cloud: polaris: address: grpc://127.0.0.1:8091 + discovery: + heartbeat: + enabled: true + health-check-url: http://localhost:48080/discovery/service/caller/healthCheck consul: port: 8500 host: 127.0.0.1 diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java index be31339d4..e71be74d3 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/PolarisProperties.java @@ -98,6 +98,12 @@ public class PolarisProperties { @Value("${spring.cloud.polaris.discovery.heartbeat.enabled:#{false}}") private Boolean heartbeatEnabled = true; + /** + * Custom health check url to override default + */ + @Value("${spring.cloud.polaris.discovery.health-check-url:}") + private String healthCheckUrl; + @Autowired private Environment environment; @@ -212,6 +218,14 @@ public class PolarisProperties { this.port = port; } + public String getHealthCheckUrl() { + return healthCheckUrl; + } + + public void setHealthCheckUrl(String healthCheckUrl) { + this.healthCheckUrl = healthCheckUrl; + } + @Override @SuppressWarnings("checkstyle:all") public String toString() { @@ -227,6 +241,7 @@ public class PolarisProperties { ", policy='" + policy + '\'' + ", registerEnabled=" + registerEnabled + ", heartbeatEnabled=" + heartbeatEnabled + + ", healthCheckUrl=" + healthCheckUrl + ", environment=" + environment + '}'; } diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java index d99f30b9b..8e7263a32 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisServiceRegistry.java @@ -21,6 +21,7 @@ import static org.springframework.util.ReflectionUtils.rethrowRuntimeException; import com.tencent.cloud.metadata.config.MetadataLocalProperties; import com.tencent.cloud.polaris.PolarisProperties; +import com.tencent.cloud.polaris.util.OkHttpUtil; import com.tencent.polaris.api.core.ProviderAPI; import com.tencent.polaris.api.exception.PolarisException; import com.tencent.polaris.api.pojo.Instance; @@ -33,6 +34,7 @@ import com.tencent.cloud.polaris.discovery.PolarisDiscoveryHandler; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; +import org.apache.logging.log4j.util.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; @@ -179,6 +181,12 @@ public class PolarisServiceRegistry implements ServiceRegistry { @Override public void run() { try { + String healthCheckUrl = polarisProperties.getHealthCheckUrl(); + //先判断是否配置了health-check-url,如果配置了,需要先进行服务实例健康检查,如果健康检查通过,则进行心跳上报,如果不通过,则不上报心跳 + if (Strings.isNotEmpty(healthCheckUrl) && !OkHttpUtil.get(healthCheckUrl, null)){ + log.error("polaris health check failed"); + return; + } polarisDiscoveryHandler.getProviderAPI().heartbeat(heartbeatRequest); } catch (PolarisException e) { log.error("polaris heartbeat[{}]", e.getCode(), e); diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java new file mode 100644 index 000000000..71020f006 --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/util/OkHttpUtil.java @@ -0,0 +1,83 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ +package com.tencent.cloud.polaris.util; + +import com.squareup.okhttp.MediaType; +import com.squareup.okhttp.Request; +import com.squareup.okhttp.Response; +import com.squareup.okhttp.OkHttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.Map; +import java.util.Objects; +/** + * okhttp util. + * + * @author kan peng + */ +public class OkHttpUtil { + public final static Logger logger = LoggerFactory.getLogger(OkHttpUtil.class); + /** + * JSON format + */ + public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8"); + /** + * client + */ + private final static OkHttpClient HTTP_CLIENT = new OkHttpClient(); + + /** + * get request. + * + * @param url url + * @param headers headers + * @return response + */ + public static boolean get(String url, Map headers) { + try { + Request.Builder builder = new Request.Builder(); + buildHeader(builder, headers); + Request request = builder.url(url).build(); + Response response = HTTP_CLIENT.newCall(request).execute(); + + if (response.isSuccessful() && Objects.nonNull(response.body())) { + String result = response.body().string(); + logger.info("exec get request, url: {} success,response data: {}", url, result); + return true; + } + } catch (Exception e) { + logger.error("exec get request,url: {} failed!", url, e); + } + return false; + } + + /** + * build header. + * + * @param builder builder + * @param headers headers + */ + private static void buildHeader(Request.Builder builder, Map headers) { + if (Objects.nonNull(headers) && headers.size() > 0) { + headers.forEach((k, v) -> { + if (Objects.nonNull(k) && Objects.nonNull(v)) { + builder.addHeader(k, v); + } + }); + } + } +} \ No newline at end of file diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json index ba5ec8078..f7f5d0e52 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-starters/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.health-check-url", + "type": "java.lang.String", + "defaultValue": "", + "description": "heal check url." + }, { "name": "spring.cloud.polaris.discovery.instance-enabled", "type": "java.lang.Boolean",