使用Layotto Actuator进行健康检查和元数据查询
该示例展示了如何通过Layotto Actuator的Http API进行健康检查和元数据查询
什么是Layotto Actuator
在生产环境中,需要对应用程序的状态进行监控,而Layotto已经内置了一个监控功能,它叫Actuator。 使用Layotto Actuator可以帮助你监控和管理Layotto和Layotto服务的应用,比如健康检查、查询运行时元数据等。 所有的这些特性可以通过HTTP接口来访问。
快速开始
运行Layotto server 端
将项目代码下载到本地后,切换代码目录:
cd ${project_path}/cmd/layotto
构建:
go build -o layotto
完成后目录下会生成 layotto 文件,运行它:
./layotto start -c ../../configs/config_standalone.json
访问健康检查接口
访问 /actuator/health/liveness
curl http://127.0.0.1:34999/actuator/health/liveness
返回:
{
"components": {
"apollo": {
"status": "UP"
},
"runtime_startup": {
"status": "UP",
"details": {
"reason": ""
}
}
},
"status": "UP"
}
其中"status": "UP"代表状态健康。此时返回的Http状态码是200。 如果状态不健康,这个值会返回"DOWN",返回的Http状态码是503。
查询元数据
访问 /actuator/info
curl http://127.0.0.1:34999/actuator/info
返回:
{
"app": {
"name": "Layotto",
"version": "0.1.0",
"compiled": "2021-05-20T14:32:40.522057+08:00"
}
}
下一步
集成进Kubernetes健康检查
Layotto内置提供了/actuator/health/readiness和/actuator/health/liveness 两个健康检查接口,对应Kubernetes健康检查功能中Readiness和Liveness两个语义。
因此,您可以参照Kubernetes的文档 ,将这两个接口集成进Kubernetes健康检查。
为您的组件添加健康检查或元数据查询能力
如果您实现了自己的Layotto组件,可以为其添加健康检查能力。可以参考apollo组件的实现(文件在components/configstores/apollo/indicator.go),实现info.Indicator接口,并将其注入进Actuator即可。
了解Actuator实现原理
如果您对实现原理感兴趣,或者想在Actuator扩展一些功能,可以阅读Actuator的设计文档