如何在 Java 代码中访问 spring 执行器健康检查的结果?

How can I access the results from spring actuator healthcheck in Java code?

我已经使用端点 /actuator/health 设置了一个健康检查执行器,当您转到 URL:

时,它会为我的应用程序生成如下内容
{"status":"UP","app":{"status":"UP"},"db":{"status":"UP"}}

有没有一种方法可以使用 Spring API 在我的 Java 代码中访问这些结果?我正在监视任何发生故障的情况,并在发生故障时发送松弛通知,我对监视数据库何时发生故障特别感兴趣。我已经有了创建松弛通知的方法,我只需要已关闭的组件的名称(即 db)。

有人可以帮忙吗?

希望我的问题是有道理的,我对此还是很陌生。

编辑:我有@Override Health,如果整个应用程序出现故障,它会进入此阶段,不确定在这个阶段是否有办法获取当时出现的其他问题(数据库等)并通过进入松弛方法?:

    @Override
    public Health health() {
        System.out.println("HealthIndicator called at " + LocalDateTime.now() + " state=" + (state?"Up":"Down"));
        if(state) {
            return Health.up().build();
        } else { 
    triggerSlackNotifications(failedComponent, status);
            return Health.down().build();
        }
    }

要获取有关健康状况的更多详细信息,您可以在 application.properties

中添加以下内容
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always

之后,您将获得更多信息,如下所示。

{
    "status": "UP",
    "details": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 467848392704,
                "free": 69999702016,
                "threshold": 10485760
            }
        },
        "db": {
            "status": "UP",
            "details": {
                "database": "MySQL",
                "hello": 1
            }
        },
        "mail": {
            "status": "UP",
            "details": {
                "location": "smtp.gmail.com:<port>"
            }
        }
    }
}

解决方案

@GetMapping("/statusDB")
    public String healthCheck() throws IOException, JSONException
    {

          StringBuilder result = new StringBuilder();
          URL url = new URL("http://localhost:8080/actuator/health");
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.getResponseCode();
          BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
          String line;
          while ((line = rd.readLine()) != null) {
             result.append(line);
          }
          rd.close();
          System.out.println("Result: "+result.toString());

          JSONObject jsonObject =new JSONObject(result.toString());
          System.out.println("jsonObject: "+jsonObject);

          return "Status of Database is "+jsonObject.getJSONObject("details").getJSONObject("db").get("status");
    }

您不一定需要执行 Webendpoint 并抓取响应。更好且不易出错的方法是使用 Actuator Health Beans 本身。

例如

聚合状态

import org.springframework.stereotype.Component;
import org.springframework.boot.actuate.health.Status;
@Component
public class MyHealthStatusProvider {
    private final HealthEndpoint healthEndpoint;
    public MyHealthStatusProvider(HealthEndpoint healthEndpoint) {
        this.healthEndpoint = healthEndpoint;
    }
    public Status getHealthStatus() {
        return this.healthEndpoint.health().getStatus();
    }
}

组件级别状态

import org.springframework.boot.actuate.health.HealthIndicator;

@Component
public class MyComponentHealthStatusProvider {
    private final List<HealthIndicator> healthIndicators;

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

    public List<HealthIndicator> getHealthStatus() {
        return this.healthIndicators;
    }
}

就是这样。现在您可以非常轻松地调用这些方法并获取应用程序的健康状态。

您可以在此处查看有关我的 post 的更多信息: https://saggu.medium.com/how-to-get-spring-actuator-health-status-through-java-code-71864efacc73