项目的DEMO代码:https://github.com/heyu52/-spring-cloud
Actuator它是springboot提供对应用自身监控,以及对应用系统配置查看等功能。
springboot使用actuator的方式非常简单,只需要在项目中加入依赖spring-boot-starter-actuator。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Actuator暴露以下功能
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 | 查看应用信息 | false |
GET | /mappings | 查看所有url映射 | true |
GET | /metrics | 查看应用基本指标 | true |
GET | /metrics/{name} | 查看具体指标 | true |
POST | /shutdown | 关闭应用 | true |
GET | /trace | 查看基本追踪信息 | true |
在 Spring Boot 2.x 中为了安全期间,Actuator 只开放了两个端点 /actuator/health 和 /actuator/info,可以在 配置文件中设置打开。
我们在浏览器中输入 :http://127.0.0.1:8080/actuator/health
我们在浏览器中输入 :http://127.0.0.1:8080/actuator/info
#打开所有功能
management.endpoints.web.exposure.include=*
#打开特定功能
management.endpoints.web.exposure.exclude=beans,trace
Actuator 默认所有的监控点路径都在 /actuator/*,当然如果有需要这个路径也支持定制
management.endpoints.web.base-path=/csdn
我们在浏览器中输入 :http://127.0.0.1:8080/actuator/health
默认只是展示了简单的 UP 和 DOWN 状态,为了查询更详细的监控指标信息,可以在配置文件中添加以下配置:
management.endpoint.health.show-details=always
这里显示的磁盘的使用情况。
其实各个情况就不再一一写了,大家可以逐个访问,查看里的结果就可以了。