一、简介
spring-boot-starter-actuator模块是一个spring提供的监控模块。我们在开运行发过程中,需要实时和定时监控服务的各项状态和可用性。
Spring Boot的spring-boot-starter-actuator 模块(健康监控)功能提供了很多监控所需的接口,可以对应用系统进行配置查看、相关功能统计等。
五、其他端点介绍:
二、实现
在maven的pom中引入:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
如果使用HTTP调用的方式,还需要这个依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
三、配置
1、health:配置
server:
port: 8082 // 服务端口
management:
port: 54001 // 指定监听端口,不指定则语server端口一直
health:
db:
enabled: true
mail:
enabled: true
security:
enabled: false // 指定访问信息不进行用户验证
访问:
http://localhost:54001/health
status "UP"
diskSpace
status "UP"
total 67548213248
free 13226700800
threshold 10485760
2、info:配置
info:
app:
name: "@project.name@" #从pom.xml中获取
description: "@project.description@"
version: "@project.version@"
spring-boot-version: "@project.parent.version@"
访问:
http://localhost:54001/info
{
"app":{
"name":"SpringBootStart Maven Webapp",
"description":"Parent pom providing dependency and plugin management for applications built with Maven",
"version":"1.0-SNAPSHOT",
"spring-boot-version":"1.0-SNAPSHOT"
}
}
四、自定义health指标
实现
HealthIndicator接口,重写health方法,在此方法中输出相关信息即可。
{
"status":"UP",
"applicationStart":{
"status":"UP",
"hello":"world"
},
"diskSpace":{
"status":"UP",
"total":67548213248,
"free":13226696704,
"threshold":10485760}
}
五、其他端点介绍:
HTTP方法 | 路径 | 描述 | 鉴权 |
---|---|---|---|
GET | /autoconfig | 查看自动配置的使用情况 | true |
GET | /configprops | 查看配置属性,包括默认配置 | true |
GET | /beans | 查看bean及其关系列表 | true |
GET | /dump | 打印线程栈 | true |
GET | /env | 查看所有环境变量 | true |
GET | /env/{name} | 查看具体变量值 | true |
GET | /health | 查看应用健康指标 | false |
GET | /info | 查看应用信息(需要自己在application.properties里头添加信息,比如info.contact.email=easonjim@163.com) | false |
GET | /mappings | 查看所有url映射 | true |
GET | /metrics | 查看应用基本指标 | true |
GET | /metrics/{name} | 查看具体指标 | true |
POST | /shutdown | 关闭应用(要真正生效,得配置文件开启endpoints.shutdown.enabled: true) | true |
GET | /trace | 查看基本追踪信息 | true |