Spring Boot执行器中是否存在检查子服务运行状况的标准方法?

问题描述

假设我有依赖于(调用)Spring Boot服务B的Spring Boot服务A。

A -> B

Spring Boot Actuators可以告诉我A是否启动。

https:// A / health

我想知道B是否通过调用A来启动。

https:// A / integratedhealth

我的问题是: Spring Boot执行器中是否存在检查子服务运行状况的标准方法 (或者我是否只需要构建自定义的执行器服务?)

解决方法

Spring Boot提供了许多开箱即用的健康指标。但是,您可以通过实现HealthIndicator界面(对于响应式应用程序,ReactiveHealthIndicator)来添加自己的自定义健康指标:

@Component
public class ServiceBHealthIndicator implements HealthIndicator {
    
    private final String message_key = "Service B";

    @Override
    public Health health() {
        if (!isRunningServiceB()) {
            return Health.down().withDetail(message_key,"Not Available").build();
        }
        return Health.up().withDetail(message_key,"Available").build();
    }
    private Boolean isRunningServiceB() {
        Boolean isRunning = true;
        // Your logic here
        return isRunning;
    }
}

如果将其与之前的其他健康指标结合使用,则可以通过以下方式获得健康终点响应:

{
   "status":"DOWN","details":{
      "serviceB":{
         "status":"UP","details":{
            "Service B":"Available"
         }
      },"serviceC":{
         "status":"DOWN","details":{
            "Service C":"Not Available"
         }
      }
   }
}

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