服务健康检查

This commit is contained in:
2025-11-10 16:10:29 +08:00
parent a0fe94a09a
commit 314aa65b76
12 changed files with 161 additions and 108 deletions

View File

@@ -1,8 +1,9 @@
/*
package com.sdm.gateway2.health;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.cloud.client.loadbalancer.reactive.ReactorLoadBalancerExchangeFilterFunction;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientRequestException;
@@ -13,26 +14,28 @@ import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
*/
/**
* /actuator/health 查看整体健康状态
* 下游服务健康检查指示器
* 检查关键下游服务的可达性
*//*
*/
@Component
public class DownstreamServicesHealthIndicator implements HealthIndicator {
// 服务名称列表
private static final String[] SERVICE_NAMES = {
"approve", "capability", "data", "pbs",
"performance", "project", "system", "task"
"data", "project","system","task"
};
@Autowired
private ReactorLoadBalancerExchangeFilterFunction loadBalancerExchangeFilterFunction;
@Override
public Health health() {
Map<String, Object> servicesStatus = new HashMap<>();
boolean allServicesUp = true;
// 检查各个关键服务的状态
for (String serviceName : SERVICE_NAMES) {
boolean serviceUp = checkServiceStatus(serviceName);
@@ -41,7 +44,7 @@ public class DownstreamServicesHealthIndicator implements HealthIndicator {
allServicesUp = false;
}
}
if (allServicesUp) {
return Health.up()
.withDetail("downstreamServices", servicesStatus)
@@ -54,27 +57,28 @@ public class DownstreamServicesHealthIndicator implements HealthIndicator {
.build();
}
}
*/
/**
/**
* 检查特定服务的状态
* 通过调用服务的 /actuator/health 端点来检查服务的健康状况
*//*
*/
private boolean checkServiceStatus(String serviceName) {
try {
WebClient webClient = WebClient.builder()
.filter(loadBalancerExchangeFilterFunction)
.baseUrl("http://" + serviceName)
.build();
// 设置较短的超时时间,避免健康检查阻塞太久
String response = webClient.get()
.uri("/actuator/health")
.retrieve()
.bodyToMono(String.class)
.timeout(Duration.ofSeconds(5)) // 5秒超时
.timeout(Duration.ofSeconds(25)) // 25秒超时
.block();
// 如果能成功获取响应,则认为服务是可达的
return response != null && !response.isEmpty();
} catch (WebClientResponseException e) {
@@ -88,4 +92,4 @@ public class DownstreamServicesHealthIndicator implements HealthIndicator {
return false;
}
}
}*/
}

View File

@@ -85,18 +85,18 @@ management:
endpoints:
web:
exposure:
include: health,info,metrics,logfile
include: health,info
endpoint:
health:
show-details: always
enabled: true
probes:
enabled: true
info:
enabled: true
info:
env:
enabled: true
group:
readiness:
include: discoveryComposite,ping,refreshScope
health:
redis:
enabled: false
db:
enabled: false
logging:
level:

View File

@@ -85,18 +85,18 @@ management:
endpoints:
web:
exposure:
include: health,info,metrics,logfile
include: health,info
endpoint:
health:
show-details: always
enabled: true
probes:
enabled: true
info:
enabled: true
info:
env:
enabled: true
group:
readiness:
include: discoveryComposite,ping,refreshScope
health:
redis:
enabled: false
db:
enabled: false
logging:
level: