This example shows how to perform health check and metadata query through Layotto Actuator's Http API
In the production environment, the status of the application needs to be monitored, and Layotto has built-in a monitoring function, which is called Actuator.
Using Layotto Actuator can help you monitor and manage Layotto and the applications behind Layotto, such as health checks, query runtime metadata, etc.
All these features can be accessed through the HTTP API.
After downloading the project source code, change directory and compile:
cd ${projectpath}/cmd/layotto
go build
After completion, the layotto file will be generated in the directory, run it:
./layotto start -c ../../configs/config_apollo_health.json
Visit /actuator/health/liveness
curl http://127.0.0.1:34999/actuator/health/liveness
return:
{
"components": {
"apollo": {
"status": "UP"
},
"runtime_startup": {
"status": "UP",
"details": {
"reason": ""
}
}
},
"status": "UP"
}
In the above json,"status": "UP" means the status is healthy. The Http status code returned is 200.
Visit /actuator/info
curl http://127.0.0.1:34999/actuator/info
return:
{
"app": {
"name": "Layotto",
"version": "0.1.0",
"compiled": "2021-05-20T14:32:40.522057+08:00"
}
}
If a configuration error causes Layotto unavailable after startup, it can be discovered in time through the health check function.
We can simulate a configuration error scenario by starting Layotto with an incorrect configuration file:
./layotto start -c ../../configs/wrong/config_apollo_health.json
There isn't an 'open_api_token' field in the configuration file,which is required to access apollo.
Access the health check API (note that the port configured here is 34888, which is different from the previous example):
curl http://127.0.0.1:34888/actuator/health/liveness
return:
{
"components": {
"apollo": {
"status": "DOWN",
"details": {
"reason": "configuration illegal:no open_api_token"
}
},
"runtime_startup": {
"status": "DOWN",
"details": {
"reason": "configuration illegal:no open_api_token"
}
}
},
"status": "DOWN"
}
"status": "DOWN" in json means the current status is unhealthy. The Http status code returned this time is 503.
Layotto provides two built-in health check API: /actuator/health/readiness and /actuator/health/liveness, corresponding to the two semantics of Readiness and Liveness in the Kubernetes health check feature.
Therefore, you can refer to Kubernetes documentation to integrate these two API into the Kubernetes ecosystem.
If you are implementing your own Layotto component, you can add health check capabilities to it. You can refer to the implementation of the apollo component (the code is at components/configstores/apollo/indicator.go), implement the info.Indicator interface, and inject it into the Actuator.
If you are interested in the implementation principle, or want to extend some functions in Actuator, you can read Actuator's design document