如何在不调用执行器端点的情况下获取健康检查状态?

How to get healthcheck status without calling actuator endpoint?

在 springboot 应用程序中,我想生成 /version 端点,它结合了一些 /actuator/health/actuator/info 数据。 从健康端点我需要总体状态 UP/DOWN.

如何在 java 代码中检索 申请状态

基于 我试图检索所有 HealthIndicator 个 beans:

@RestController
public class AppStatusRestController {
    private final List<HealthIndicator> healthIndicators;

    public AppStatusRestController(List<HealthIndicator> healthIndicators) {
        this.healthIndicators = healthIndicators;
    }

    @GetMapping("/status")
    public String status() {
        return "status: " + getStatus();
    }
    private String getStatus() {
        return isUp() ? Status.UP.getCode() : Status.DOWN.getCode();

    }

    private boolean isUp() {
        return this.healthIndicators.stream().allMatch(healthIndicator -> healthIndicator.getHealth(false).getStatus() == Status.UP);
    }

}

但它不适用于某些组件,例如RabbitMQ

{
  status: "DOWN", // how can I get application status 
  components: {
    db: {
      status: "UP",
...
    },
    diskSpace: {
      status: "UP",
...
      }
    },
    ping: {
      status: "UP"
    },
    rabbit: {
      status: "DOWN",
      details: {
        error: "org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect"
      }
    },
    myCustomHealthIndicator: {
      status: "UP",
...
    }
  }
}

请注意,我根本不需要组件状态。我只想检索我的应用程序的总体状态。

我发现 HealthEndpoint 正是我要找的。

@RestController
public class AppStatusRestController {
    private final HealthEndpoint healthEndpoint;

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

    @GetMapping("/status")
    public String status() {
        return "status: " + healthEndpoint.health().getStatus();
    }

}