【第12章】SpringBoot之SpringBootActuator服务监控(上)


前言

SpringBootActuator是 Spring Boot 框架中的一个核心模块,它提供了生产级别的监控和管理功能,帮助开发者更好地理解和维护他们的 Spring Boot 应用。

SpringBootActuator 模块通过暴露一系列端点(Endpoints),允许外部系统或开发者通过 HTTP、JMX 或 SSH 等方式访问和监控应用的内部状态。这些端点提供了丰富的信息,如健康检查、度量指标、环境属性、日志配置等,从而帮助开发者快速定位问题并进行优化。


一、准备

1. 地址和端口配置

在单例SpringBoot应用程序中,Actuator默认使用主程序的端口号,不过我们可以通过

  • management.server.address
  • management.server.port

等外部配置自行定义,更多可配置信息参考ManagementServerProperties

2. 引入依赖

SpringBoot应用直接引入starter即可

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3. Actuator Properties

名称描述默认值路由
management.endpoint.beans.enabled是否启用bean端点。true/actuator/beans
management.endpoint.configprops.enabled是否启用configprops端点。true/actuator/configprops
management.endpoint.env.enabled是否启用env端点。true/actuator/env
management.endpoint.health.enabled是否启用health端点。true/actuator/health
management.endpoint.heapdump.enabled是否启用heapdump端点。true/actuator/heapdump
management.endpoint.mappings.enabled是否启用mappings端点。true/actuator/mappings
management.endpoint.metrics.enabled是否启用metrics端点。true/actuator/metrics
management.endpoint.startup.enabled是否启用startup端点。true/actuator/startup
management.endpoint.threaddump.enabled是否启用threaddump端点。true/actuator/threaddump

这些端点默认都是开启的,但是生产上为了保证服务安全,我们可能需要配合management.endpoints.enabled-by-defaultmanagement.endpoints.web.exposure.include配置合理使用需要开放的端点。

这里只列出一些常用的,更多配置信息请参考官网

二、使用

我这里为了方便,配置management.endpoints.web.exposure.include=*开启了所有端点
另外,如果应用程序中有登录拦截器之类的,需要对actuator请求进行放行,以下是可用的端点:

在这里插入图片描述

1. Beans (beans)

curl 'http://localhost:8080/actuator/beans' -i -X GET

2. Configuration Properties (configprops)

curl 'http://localhost:8080/actuator/configprops' -i -X GET

3. Environment (env)

curl 'http://localhost:8080/actuator/env' -i -X GET

4. Health (health)

curl 'http://localhost:8080/actuator/health' -i -X GET -H 'Accept: application/json'

5. Heap Dump (heapdump)

curl 'http://localhost:8080/actuator/heapdump' -O

在这里插入图片描述
生成的堆文件有60多M,我这刚启动,还什么都没做

6. Mappings (mappings)

curl 'http://localhost:8080/actuator/mappings' -i -X GET \
    -H 'accept-encoding: gzip' \
    -H 'user-agent: ReactorNetty/1.1.22' \
    -H 'accept: */*'

7. Metrics (metrics)

curl 'http://localhost:8080/actuator/metrics' -i -X GET

在这里插入图片描述

8. Thread Dump (threaddump)

curl 'http://localhost:8080/actuator/threaddump' -i -X GET \
    -H 'Accept: application/json'

总结

回到顶部

通过这些端点我们可以实时获取到应用程序的各类信息和指标,但是接口响应数据的格式是不利于观察的,所以我们需要一块可视化的工具,来帮助我们完成这些数据的结构化和可视化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值