在 Spring Boot Actuator 的体系里,其实并没有一个官方叫"静态检查"的名词。但结合你之前的排查经历(Eureka 注册、数据库连接报错),你所说的"静态检查",在微服务和运维领域的标准术语是 "健康检查(Health Check)" 和 "端点监控(Endpoints)"。
简单来说,Actuator 就像是汽车的仪表盘,而"静态检查"就是仪表盘上的各种指示灯(比如机油灯、胎压灯)。它不负责开动汽车,但能告诉你汽车当前的"静态"组件是否正常。
下面我手把手教你 Actuator 的静态检查是如何工作的,以及怎么用。
一、 为什么需要 Actuator 的静态检查?
在没有 Actuator 之前,你的服务启动成功了,但底层数据库可能连不上(就像你刚才遇到的 Public Key Retrieval 错误);或者虽然连上了,但 Redis 挂了。如果没人主动去调用接口,你根本不知道服务其实已经"带病运行"了。
Actuator 的静态检查能实现:
- 随时探活:通过一个 URL 就能知道应用是否还活着。
- 依赖检查:自动检查数据库、Redis、磁盘空间等基础组件是否可用。
- 配合 K8s/运维平台:云平台可以通过调用这个检查接口,自动重启挂掉的服务。
二、 Actuator 的"静态检查"是怎么做的?
核心机制是 HealthIndicator(健康指示器) 。
Spring Boot 在启动时,会自动扫描所有实现了 HealthIndicator 接口的类,并把这些检查结果汇总到 /actuator/health 这个接口里。
1. 内置的静态检查
只要你引入了对应的依赖,Spring Boot 就会自动配置这些检查:
DiskSpaceHealthIndicator:静态检查磁盘剩余空间。DataSourceHealthIndicator:静态检查数据库连接池(你去请求一下,它会去数据库select 1看看通不通)。RedisHealthIndicator:检查 Redis 是否能 ping 通。EurekaHealthIndicator:检查是否成功注册到 Eureka。
2. 如何查看这些静态检查?
你需要修改 application.yml,把检查的详细信息暴露出来(默认只显示 UP 或 DOWN,不显示细节):
yaml
management:
endpoints:
web:
exposure:
include: health,info # 暴露 health 和 info 端点
endpoint:
health:
show-details: always # 重点:始终显示详细的静态检查信息
启动你的 8002 服务,然后在浏览器访问:http://localhost:8002/actuator/health
如果一切正常,你会看到类似这样的 JSON:
json
{
"status": "UP",
"components": {
"db": {
"status": "UP",
"details": {
"database": "MySQL",
"validationQuery": "isValid()"
}
},
"diskSpace": {
"status": "UP",
"details": {
"total": 500000000000,
"free": 300000000000,
"threshold": 10485760
}
},
"discoveryComposite": {
"status": "UP",
"components": {
"eureka": {
"status": "UP",
"details": {
"applications": {
"CLOUD-PROVIDERPAYMENT8002": 1
}
}
}
}
}
}
}
(注意:如果在刚才你数据库报错的时候你访问这个接口,db 那一项肯定是 DOWN,这比看满屏红色报错直观多了!)
三、 进阶:如何自定义一个"静态检查"?
假设你的业务要求:必须存在 config/my-config.txt 这个配置文件,系统才能算健康。 你就可以自己写一个检查器。
第一步:新建一个类实现 HealthIndicator 接口
java
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
import java.io.File;
@Component("configFile") // 这个就是组件在 JSON 里的 key
public class ConfigFileHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// 你的静态检查逻辑
File file = new File("config/my-config.txt");
if (file.exists() && file.isFile()) {
// 检查通过
return Health.up()
.withDetail("path", file.getAbsolutePath())
.withDetail("message", "配置文件检查正常")
.build();
} else {
// 检查失败
return Health.down()
.withDetail("path", file.getAbsolutePath())
.withDetail("error", "找不到核心配置文件!")
.build();
}
}
}
第二步:重启服务并访问
再次访问 http://localhost:8002/actuator/health,你会发现在 components 里多了一个 configFile:
json
"configFile": {
"status": "UP",
"details": {
"path": "G:\\CodeForPractice\\SpringCloud-01\\config\\my-config.txt",
"message": "配置文件检查正常"
}
}
四、 总结:Actuator 到底帮你干了啥?
- 开发阶段 :帮你快速定位问题。比如你刚才排错时,如果早点用
/actuator/health,一眼就能看出是db挂了,而不是盲猜。 - 运维阶段 :配合 Prometheus + Grafana 做数据大屏,或者让 Kubernetes 的 Liveness Probe(存活探针)定期来拨测这个接口。如果接口返回
DOWN(比如返回 503 状态码),K8s 就会自动干掉这个 Pod 并重启一个新的。
你现在可以试着在你 8002 的配置文件里把 show-details 打开,然后去浏览器里看看你的 MySQL、磁盘和 Eureka 到底是怎么报告的。