【问题标题】:Convert Spring Boot Actuator health status from UP/DOWN to TRUE/FALSE将 Spring Boot Actuator 健康状态从 UP/DOWN 转换为 TRUE/FALSE
【发布时间】:2019-12-12 17:59:16
【问题描述】:

有没有办法将状态字段值从 UP/DOWN 更改

{"status":"UP"}

到 TRUE/FALSE 如下所示:

{"status":true}

我想使用与弹簧执行器相同的检查逻辑,不需要自定义检查逻辑,只想更新状态值。

【问题讨论】:

  • 这听起来并不自然:“你的身份是什么?是的”......而且很可能是 XY 问题
  • 很遗憾这是我公司的新 API 标准,他们希望看到真/假而不是 UP/DOWN。
  • 很高兴没有人必须遵循制定的标准

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


【解决方案1】:

以下代码将注册一个新的执行器端点/healthy,它使用与默认/health端点相同的机制。

package com.example;

import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "healthy") // Change this to expose the endpoint under a different name
public class BooleanHealthEndpoint {

    HealthEndpoint healthEndpoint;

    public BooleanHealthEndpoint(HealthEndpoint healthEndpoint) {
        this.healthEndpoint = healthEndpoint;
    }

    @ReadOperation
    public Health getHealth() {
        Boolean healthy = healthEndpoint.health().getStatus().equals(Status.UP);
        return new Health(healthy);
    }

    public static class Health {
        private Boolean status;

        public Health(Boolean status) {
            this.status = status;
        }

        public Boolean getStatus() {
            return status;
        }
    }
}

如果您不想添加自定义的/healthy 端点并继续使用默认的/health 端点,您可以在属性文件中添加以下设置,然后它将映射到默认值:

management.endpoints.web.path-mapping.health=internal/health
management.endpoints.web.path-mapping.healthy=/health

【讨论】:

  • 感谢您的回答!!完全忘记了这个问题。我能够让它与 HealthAggregator 一起工作,但现在由于这个类在较新的 springboot 版本中已被弃用,我不得不想出一种新方法,你的解决方案绝对有效!!
  • 只是对任何可能感兴趣的人的更新,如果您想将 /health 端点与默认端点相同,我使用属性映射更新了答案
【解决方案2】:

假设您的公司建立了新的 API 标准,因为涉及构成应用程序范围的大量不同框架,我们并不是只讨论 Spring Boot 应用程序(因为否则会很烦人):

只需在/actuator/customstatus 下实现您自己的@Endpoint 并在其下聚合所有HealthIndicators status'。您可能希望从 Spring Boots HealthEndpoint 和 CompositeHealthIndicator 课程中获得灵感,了解如何做到这一点。 (话题HealthAggregator)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-05
    • 2023-03-10
    • 2015-08-24
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多