首页 > 解决方案 > 使用 Spring Boot 应用程序的自定义健康指标实现健康检查休息 API

问题描述

我正在开发在8080以外的端口上运行的 Spring Boot Web 应用程序。我需要编写一个 rest API 来检查应用程序以及底层数据库的运行状况。

同样,我浏览过这篇文章:如何在 Spring Boot Health 中添加自定义健康检查?,但没有找到任何具体的实现。

其实我想做以下事情:

  1. 所有下游 API 的健康检查

  2. DB是否工作正常

那么有人可以帮我解决这个问题吗?谢谢。

标签: javaspringspring-boothealth-monitoring

解决方案


Spring boot actuator 可以自动为您创建常见的健康检查(也为您的数据库)和一个 REST API。

要开始添加以下依赖项:

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

然后 Spring Boot 将在下面公开一些执行器端点:

/actuator

与您最相关的可能是:

/actuator/health

要向此端点添加自定义运行状况检查,您可以按照链接帖子的答案中提供的指南进行操作。

更多详情,请参见https://www.baeldung.com/spring-boot-actuators


推荐阅读