Spring Boot Actuator的使用

本文专为SpringBoot2.0以上版本设计,介绍如何利用SpringBoot Actuator进行系统监控,包括端点设置、配置方法及常见端点示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

主要用来确认系统状态和各种meta数据的插件包
本文只针对Spring Boot 2.0以上版本,2.0以下版本请自行百度。

详细文档
https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

导入

compile('org.springframework.boot:spring-boot-starter-actuator')

只是运用默认设置的话,简单导入后就可以直接使用了。

端点运用

监视端点的端口默认是和应用端口一样,如果希望改变,可以设置
management.server.port

属性设置-application.properties

Spring Boot Actuator的各端口需要设置 有效-enable,开放-exposure
通过设置有效/无效—设置指定端口的使用
通过设置开放/关闭—设置指定端口可否通过http访问
除了shutdown,其他端口默认有效
除了health,info,其他端口默认关闭

management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
management.endpoint.info.enabled=false

endpoint

提供了各种用来确认系统状态的各种端点,
以下例:(8080端口是当前应用的设置端口,自行设置。)

//auto-configuration设定确认
http://localhost:8080/actuator/autoconfig
//container内登录的bean一览
http://localhost:8080/actuator/beans
//thread dump情报
http://localhost:8080/actuator/dump
//环境设置信息
http://localhost:8080/actuator/env
//healthcheck 缺省"ok"返回,可自行指定
http://localhost:8080/actuator/health
//@RequestMapping情报
http://localhost:8080/actuator/mappings
//shutdown,缺省无效,可在配置文件中设置有效
http://localhost:8080/actuator/shutdown

等其他。

### Spring Boot Actuator 使用教程 #### 添加依赖项 为了在项目中启用Spring Boot Actuator,需向`pom.xml`文件添加如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> ``` 此操作使得能够利用Actuator提供的各种特性[^1]。 #### 启用内置端点 通过修改`application.properties`或`application.yml`配置文件中的设置,可激活所需的服务端口。例如,在YAML格式下,可以通过下面的方式公开所有默认端点: ```yaml management: endpoints: web: exposure: include: "*" ``` 上述配置允许访问所有的预定义健康检查和其他诊断信息接口[^4]。 #### 访问管理端点 一旦完成以上两步,启动应用程序之后就可以通过HTTP请求来获取有关应用的状态数据。比如要查看当前系统的健康状况,只需发送GET请求到`http://localhost:<port>/actuator/health`即可获得JSON响应体描述的应用程序健康详情。 #### 自定义健康指示器 除了标准的健康检测外,还可以创建自定义的HealthIndicator类以报告特定业务逻辑层面的信息。这有助于更全面地掌握整个软件栈的工作情况。 ```java @Component public class CustomHealthIndicator implements HealthIndicator { @Override public Health health() { int errorCode = check(); // perform some specific health check if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } } ``` 这段代码展示了如何编写一个简单的自定义健康监测组件并将其集成至现有的框架内[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值