
健康检查(Health check)是确保软件平台可靠性的关键机制,它通过主动监控系统组件的运行状态,及时发现潜在问题并触发告警或自动恢复。一个完善的健康检查机制需要遵循以下核心需遵循以下原则:
实际应用时,建议采用渐进式检查策略:先执行快速的基础层检查,再按需触发深层诊断。例如云服务提供商通常将健康检查分为:
以Spring Boot微服务为例,典型场景包括:
SELECT 1)验证连接池状态。/ping)。默认提供健康检查端点/actuator/health,可通过配置扩展:
# application.yml
management:
endpoint:
health:
show-details: always
health:
diskspace:
enabled: true
path: / # 监控根目录
threshold: 10MB实现HealthIndicator接口,添加业务逻辑检查:
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class OrderServiceHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int queueSize = getOrderQueueSize();
if (queueSize > 100) {
return Health.down()
.withDetail("error", "订单队列积压: " + queueSize)
.build();
}
return Health.up().withDetail("queueSize", queueSize).build();
}
private int getOrderQueueSize() {
// 模拟获取订单队列长度
return 42;
}
}在K8s部署中,使用Actuator端点作为就绪探针:
apiVersion: apps/v1
kind: Deployment
spec:
template:
spec:
containers:
- name: app
livenessProbe:
httpGet:
path: /actuator/health
port: 8080
initialDelaySeconds: 30
periodSeconds: 10LIVENESS(重启服务)和READINESS(停止流量)探针逻辑。根据故障严重程度设计差异化探针响应: LIVENESS探针(存活检查):检测服务是否崩溃(如进程挂起),触发自动重启。典型检查项包括:进程状态、死锁检测、内存泄漏监控。READINESS探针(就绪检查):检测服务是否过载(如线程池耗尽),触发流量摘除。典型检查项包括:依赖服务连通性、队列积压量、CPU负载阈值。/health端点暴露细粒度状态(如{"db":"OK","cache":"DEGRADED"})RoutePredicateFactory集成健康检查API,每30秒聚合一次后端服务状态。通过以上设计,可构建适应高可用需求的健康检查体系。