Spring Boot Actuator
节选自《Netkiller Spring Cloud 手札》
健康检查、审计、统计和监控
Maven 依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
与 Spring Boot Actuator 有关的配置
application.properties
跨域配置
management.endpoints.web.cors.allowed-origins=https://example.com
management.endpoints.web.cors.allowed-methods=GET,POST
禁用HTTP端点
如果您不想通过HTTP公开端点,则可以将管理端口设置为-1,如以下示例所示:
management.server.port=-1
actuator 接口
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator | jq
{
"_links": {
"self": {
"href": "http://localhost:8080/actuator",
"templated": false
},
"health": {
"href": "http://localhost:8080/actuator/health",
"templated": false
},
"health-component": {
"href": "http://localhost:8080/actuator/health/{component}",
"templated": true
},
"health-component-instance": {
"href": "http://localhost:8080/actuator/health/{component}/{instance}",
"templated": true
},
"info": {
"href": "http://localhost:8080/actuator/info",
"templated": false
}
}
}
健康状态
curl localhost:8080/actuator/health
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator/health | jq
{
"status": "UP"
}
健康状态
详细的健康状态信息
management.endpoint.health.show-details=always
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator/health | jq
{
"status": "UP",
"details": {
"diskSpace": {
"status": "UP",
"details": {
"total": 250790436864,
"free": 23556112384,
"threshold": 10485760
}
}
}
}
关机
配置文件中加入
management.endpoint.shutdown.enabled=true
curl -X POST localhost:8080/actuator/shutdown
info 配置信息
返回 application.properties 文件中定义的 info 配置信息,如:
# info端点信息配置
info.app.name=spring-boot-example
info.app.version=v1.0.0
neo@MacBook-Pro ~ % curl -s http://localhost:8080/actuator/info | jq
{
"app": {
"name": "spring-boot-example",
"version": "v1.0.0"
}
}
计划任务
http://localhost:8080/actuator/scheduledtasks