1.Actuator简介
SpringBoot大部分模块都是用于开发业务功能或者连接外部资源的。除此之外,SpringBoot还提供了Actuator模块来管理和监控应用,Actuator是一个用于暴露自身信息的模块,可以有效地减少监控系统在采集应用指标时的开发量。
Actuator提供了监控和管理端点以及一些常用的扩展和配置方式,具体如下表所示。
路径(端点名) | 描述 | 鉴权 |
---|---|---|
/health | 显示应用监控指标 | false |
/beans | 查看Bean及其关系列表 | false |
/info | 查看应用信息 | false |
/trace | 查看基本追踪信息 | true |
/env | 查看所有环境变量 | true |
/env/{name} | 查看具体变量值 | true |
/mappings | 查看所有URL映射 | true |
/autoconfig | 查看当前应用的所有自动配置 | true |
/configprops | 查看应用所有配置属性 | true |
/shutdown | 关闭应用(默认关闭) | true |
/metrics | 查看应用基本指标 | true |
/metrics/{name} | 查看应用具体指标 | true |
/dump | 打印线程栈 | true |
2.引入依赖
新建一个SpringBoot工程,并在pom.xml文件中添加集成Actuator所需要的dependency。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3.添加配置
在application.properties文件中添加如下配置信息。
############################################################
#
# 应用监控配置
#
############################################################
### 指定访问监控方法的端口
management.server.port = 8080
### 开启所有端点
management.endpoints.web.exposure.include = *
4.测试
启动项目,然后在浏览器中请求“http://localhost:8080/actuator”,可以查询到如下图所示的信息。
点击端点路径即可查看端点详情,如点击“http://localhost:8080/actuator/health”查看应用的健康状态,“UP”表示应用是健康状态,测试结果如下图所示。
5.其它配置详解
### 设置beans端点的缓存生存时间
management.endpoint.beans.cache.time-to-live = 10s
### 将/actuator/health重新映射到/healthCheck
management.endpoints.web.path-mapping.health = healthCheck
### 更改基本路径
management.endpoints.web.base-path = /
### 启用shutdown端点
management.endpoint.shutdown.enabled = true
### 启用info端点并禁用其它所有端点
management.endpoints.enabled-by-default = false
management.endpoints.info.enabled = true
### 查看详细的应用健康信息
management.endpoints.health.show-detail = always
- management.endpoint.beans.cache.time-to-live:对于不带任何参数的读取操作,端点自动缓存对其的响应。要配置端点缓存响应时间,需使用cache.time-to-live属性,如management.endpoint.beans.cache.time-to-live = 10s表示将beans端点缓存生存时间设置为10秒。
- management.endpoints.web.path-mapping.health:默认情况下,端点通过使用端点的ID在/actuactor路径下的HTTP上公开,如beans端点暴露在/actuator/beans下。如果需要改变端点映射,需使用management.endpoints.web.path-mapping属性。如management.endpoints.web.path-mapping.health = healthCheck表示将/actuator/health重新映射到/healthCheck。
- management.endpoints.web.base-path:更改基本路径。
- management.endpoint.shutdown.enabled:如果需要配置单个端点的启用,需使用management.endpoint.{id}.enabled属性。如management.endpoint.shutdown.enabled = true表示启动shutdown端点。
- management.endpoints.enabled-by-default:修改全局端口的默认配置。
- management.endpoints.health.show-detail:查看详细的应用健康信息,值除了always之外,还有when-authorized和never,默认是never。
6.工程目录结构