参考: https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn
作用: 对线上应用进行观测、监控、预警…
比如:
● 健康状况【组件状态、存活状态】Health
● 运行指标【cpu、内存、垃圾回收、吞吐量、响应成功率…】Metrics
● 链路追踪
1.使用
1.场景引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.暴露指标
在application.properties里面进行设置
#暴露所有端点信息
management.endpoints.web.exposure.include=*
#以web方式暴露
management.endpoint.health.enabled=true
3.访问数据
● 访问 http://localhost:8080/actuator;展示出所有可以用的监控端点
● http://localhost:8080/actuator/beans
● http://localhost:8080/actuator/configprops
● http://localhost:8080/actuator/metrics
● http://localhost:8080/actuator/metrics/jvm.gc.pause
● http://localhost:8080/actuator/endpointName/detailPath
2.Endpoint
常用端点: 默认提供了各种端点,来展示应用的详细信息
包括看有多少线程及线程运行情况,生成堆dump等各种端点
参考:
https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn?inner=u8Ebe
3.如何自定义端点
自定义HealthEndpoint和MetricsEndpoint
3.1 HealthEndpoint
1.自定义健康端点
例如监控一个组件的状态,存活还是死亡
方法: 实现HealthIndicator接口或者继承AbstractHealthIndicator来实现
1)实现HealthIndicator接口–>类名必须以HealthIndicator结尾,并且实现HealthIndicator接口,spring boot就能够确定这个组件是用来监控的
一句话总结下面的代码: 实现HealthIndicator接口的health方法,返回Health对象
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator