如何在spring boot health中添加自定义运行状况检查?

时间:2017-06-30 15:06:19

标签: spring spring-boot spring-boot-actuator

+---------+----------+
|    name | duration | 
+---------+----------+
| object1 |        4 |
+---------+----------+

这将为您的应用程序添加几个有用的端点。其中之一是/健康。当您启动应用程序并导航到/ health端点时,您将看到它已返回一些数据。

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

如何在spring boot health中添加自定义运行状况检查?

4 个答案:

答案 0 :(得分:24)

添加自定义运行状况检查很简单。只需创建一个新的Java类,从AbstractHealthIndicator扩展它并实现doHealthCheck方法。该方法通过一些有用的方法获取构建器。如果您的健康状况良好,请调用builder.up();如果不健康,则调用builder.down()。你做什么检查健康完全取决于你。也许你想ping一些服务器或检查一些文件。

@Component
public class CustomHealthCheck extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder bldr) throws Exception {
        // TODO implement some check
        boolean running = true;
        if (running) {
          bldr.up();
        } else {
          bldr.down();
        }
    }
}

这足以激活新的运行状况检查(确保@ComponentScan在您的应用程序中)。重新启动应用程序并将浏览器定位到/ health端点,您将看到新添加的运行状况检查。

{
    "status":"UP",
    "CustomHealthCheck": {
        "status":"UP"
    },
    "diskSpace": {
        "status":"UP",
        "free":56443746,
        "threshold":1345660
    }
}

答案 1 :(得分:6)

Spring Boot 2.X大大改变了执行器。通过@EndpointWebExtension启用了一种更好的扩展现有端点的新机制。

话虽这么说,健康端点的扩展有点棘手,因为执行器本身提供了一个扩展名。如果不操纵bean的初始化过程,您的应用程序将无法启动,因为它将看到2个扩展名,并且不知道选择哪个扩展名。 一种更简单的方法是改用信息并对其进行扩展:

@Component
@EndpointWebExtension(endpoint = InfoEndpoint.class)
public class InfoWebEndpointExtension {
   @Value("${info.build.version}")
   private String versionNumber;
   @Value("${git.commit.id}")
   private String gitCommit;
   @Value("${info.build.name}")
   private String applicationName;
   ...
   @ReadOperation
   public WebEndpointResponse<Map> info() {

别忘了您还可以重新映射URL。就我而言,我更喜欢 / status 而不是 / health ,并且不希望路径中的 / actuator /

management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.info=status

我更喜欢 / info 的另一个原因是因为我没有此嵌套结构,这是 / health 的默认结构:

{
"status": {
    "status": "ON",

答案 2 :(得分:3)

自Spring Boot 2.X起

如@ yuranos87所述,执行器概念在Spring Boot 2.X中已更改,但是您仍然可以通过实施HealthIndicator或针对反应性应用{{1}来轻松添加自定义运行状况检查 }:

ReactiveHealthIndicator

@Component
public class CacheHealthIndicator implements HealthIndicator {

@Override
public Health health() {
    long result = checkSomething())           
    if (result <= 0) {
        return Health.down().withDetail("Something Result", result).build();
    }
    return Health.up().build();      
}

此外,您可以使用@Component public class CacheHealthIndicator implements ReactiveHealthIndicator { @Override public Mono<Health> health() { return Mono.fromCallable(() -> checkSomething()) .map(result -> { if (result <= 0) { return Health.down().withDetail("Something Result", result).build(); } return Health.up().build(); }); } } @Endpoint添加或扩展任何端点。这里的端点是@EndpointWebExtensioninfo等。因此,您可以使用health添加自定义健康检查,但是使用@Endpoint则容易得多。

您可以在Spring Boot文档中找到有关custom health checkscustom endpoints的更多信息。

答案 3 :(得分:0)

如果您想要自定义状态消息,那么您可以在此处查看答案 - https://stackoverflow.com/a/66985769/4952800