0

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

如何在 java 代码中检索应用程序状态?

基于这个答案,我尝试检索所有HealthIndicatorbean:

@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",
...
    }
  }
}

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

4

1 回答 1

0

我发现这里就是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();
    }

}
于 2022-02-17T11:26:18.900 回答