前言
Actuator 是 Spring Boot 提供的对应用系统的自省和监控的集成功能,可以查看应用配置的详细信息,例如自动化配置信息、创建的 Spring beans 以及一些环境属性等。
一、pom.xml文件
引入web和actuator依赖,web依赖是必须引入的,否则不能启动。
<dependencies>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二、配置文件
info:
app:
name: spring-boot-actuator-test
version: 1.0.0
test: test
management:
endpoints:
web:
#Actuator 默认所有的监控点路径都在/actuator/*,当然如果有需要这个路径也支持定制。
base-path: /monitor
#Actuator 默认只开放了两个端点 /actuator/health 和 /actuator/info
exposure:
#打开所有的监控点
include: '*'
#屏蔽env,beans监控点
exclude: env,beans
endpoint:
health:
#详细显示health信息
show-details: always
#启用接口关闭 Spring Boot (post请求)
shutdown:
enabled: true
health:
#可以在配置文件中关闭特定的健康检查指标,比如关闭 redis 的健康检查(或者MongoDB)
redis:
enabled: false
三、测试
- 访问http://localhost:8080/monitor。默认路径为/actuator/*,配置中已经修改为base-path: /monitor。
image.png - 访问http://localhost:8080/monitor/info。 返回配置文件中info的内容。
image.png - 访问http://localhost:8080/monitor/health。显示 health 的细节。
image.png - 访问http://localhost:8080/monitor/shutdown (post请求)。关闭 Spring Boot。
image.png