源码地址:https://gitee.com/xiaoyao777/spring-boot-actuator.git
1.首先创建一个SpringBoot工程,然后引入一下Jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--这个就是SpringBootActuator核心包了-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.启动项目
![](https://img.haomeiwen.com/i4717137/bf7951597f04b57c.png)
有这个说明Actuator算是加载上了
3.访问SpringBootActuator
http://127.0.0.1:8080/actuator
![](https://img.haomeiwen.com/i4717137/8dd086e9ce7bd342.png)
3.health端点
可以看到有很多端点我们看一下health
http://localhost:8080/actuator/health
![](https://img.haomeiwen.com/i4717137/3d4a3dc161eccf7f.png)
health这个是健康检查,那么检查的是啥子东西呢?
检查的是资源,啥子资源。我们加一个配置。
#展示health的详情
management.endpoint.health.show-details=always
![](https://img.haomeiwen.com/i4717137/22f0b1668af51e58.png)
我们重新启动一下项目再次访问health。
![](https://img.haomeiwen.com/i4717137/16ff021a1daf4224.png)
{
"status": "UP",
"components": {
//磁盘
"diskSpace": {
"status": "UP",
"details": {
//磁盘总容量
"total": 510770802688,
//磁盘剩余容量
"free": 428996915200,
//磁盘可用最小空间
"threshold": 10485760,
"exists": true
}
},
"ping": {
"status": "UP"
}
}
}
更多想起的参数配置可以看官方文档
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#actuator.endpoints.enabling
如果看不懂英文文档也没有问题还有中文文档,而且中文文档还可以下载
https://www.bookstack.cn/read/springboot/spilt.2.pages-production-ready.md#8rnaz8
![](https://img.haomeiwen.com/i4717137/76e4c906b0f0bb14.png)
然后我们看一下status值代表的什么意思
![](https://img.haomeiwen.com/i4717137/f82e0313e1af5960.png)
也就是status有四个取值
UP:正常
DOWN:遇到问题,也就是所谓down机了
OUT_OF_SERVICE:资源未在使用,或者不能使用
UNKNOWN:我也不知道啥问题
4.Actuator 的 REST 接口
Spring Boot Actuator 的关键特性是在应用程序里提供众多 Web 接口,通过它们了解应用程序运行时的内部状况。Actuator 提供了 13 个接口,可以分为三大类:配置接口、度量接口和其它接口,具体如下表所示。
GET请求:
/autoconfig:提供了一份自动配置报告,记录哪些自动配置条件通过了,哪些没通过
/configprops:描述配置属性(包含默认值)如何注入Bean
/beans:描述应用程序上下文里全部的Bean,以及它们的关系
/dump:获取线程活动的快照
/env:获取全部环境属性
/env/{name}:根据名称获取特定的环境属性值
/health:报告应用程序的健康指标,这些值由HealthIndicator的实现类提供
/info:获取应用程序的定制信息,这些信息由info打头的属性提供
/mappings:描述全部的URI路径,以及它们和控制器(包含Actuator端点)的映射关系
/metrics:报告各种应用程序度量信息,比如内存用量和HTTP请求计数
/metrics/{name}:报告指定名称的应用程序度量值
/trace:提供基本的HTTP请求跟踪信息(时间戳、HTTP头等)
POST请求:
/shutdown:关闭应用程序,要求endpoints.shutdown.enabled设置为true
5.激活所有的端点
#可以看到除了/shutdown之外的其他所有端点。
management.endpoints.web.exposure.include=*
![](https://img.haomeiwen.com/i4717137/17e9e958af23d5a7.png)
![](https://img.haomeiwen.com/i4717137/5d6bc1a837fabbcb.png)
如果开放几个端点就用逗号隔开好了
#可以看到除了/shutdown之外的其他所有端点。
#management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.include=health,info
![](https://img.haomeiwen.com/i4717137/4f85e0da3e71ed24.png)
源码地址:
https://gitee.com/xiaoyao777/spring-boot-actuator.git
网友评论