本示例演示以下能力:
routes.HealthCheck()routes.Liveness()routes.Readiness()routes.Startup()routes.NamedProbe(...)
cd examples/probes
go run main.go服务默认监听 http://localhost:8080。
curl http://localhost:8080/health
curl http://localhost:8080/livez
curl http://localhost:8080/readyz
curl http://localhost:8080/startupz说明:
/health:最轻量的健康检查/livez:存活探针,只回答进程是否活着/readyz:就绪探针,检查database和cache/startupz:启动探针,示例中服务启动 5 秒后才会返回成功
curl http://localhost:8080/status返回示例:
{
"code": 200,
"message": "success",
"data": {
"cache_ready": true,
"database_ready": true,
"started": true
}
}让数据库探针失败:
curl -X POST http://localhost:8080/admin/database/down
curl http://localhost:8080/readyz恢复数据库:
curl -X POST http://localhost:8080/admin/database/up让缓存探针失败:
curl -X POST http://localhost:8080/admin/cache/down
curl http://localhost:8080/readyz手动切换启动状态:
curl -X POST http://localhost:8080/admin/startup/down
curl http://localhost:8080/startupz
curl -X POST http://localhost:8080/admin/startup/up当某个探针失败时,接口会返回 503 Service Unavailable,并在 checks 字段中列出失败原因。