在Spring Boot Actuators中,有没有一种标准方法来检查子服务的健康状态?

huangapple go评论73阅读模式
英文:

Is there a standard way in Spring Boot Actuators of checking the health of child services?

问题

以下是要翻译的内容:

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

A -> B

Spring Boot Actuators可以告诉我A是否正常运行。

https://A/health

我想知道的是通过调用A来检查B是否正常运行。

https://A/integratedhealth

我的问题是:在Spring Boot Actuators中,是否有标准的方法来检查子服务的健康状态?
(还是我必须构建一个自定义的执行器服务?)

英文:

Suppose I have Spring Boot service A which depends on (calls) Spring Boot service B.

A -> B

Spring Boot Actuators can tell me if A is up.

https://A/health

What I want to know is if B is up, by calling A.

https://A/integratedhealth

My question is: Is there a standard way in Spring Boot Actuators of checking the health of child services?
(or do I just have to build a custom actuator service?)

答案1

得分: 1

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;
        // 在这里添加您的逻辑
        return isRunning;
    }
}

如果将其与之前的健康指标等组合,您可以获得类似以下方式的健康端点响应:

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

您可以在Spring Boot文档中找到有关自定义健康检查端点的更多信息。

英文:

Spring boot provides a lot of Health Indicators out of the box. However, you can add your own custom health indicator by implementing HealthIndicator interface (ReactiveHealthIndicator for reactive apps):

@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;
    }
}

If you combine it with other health indicators like the one before, you can get a health endpoint response in a way like this:

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

You can find more information about custom health checks and endpoints in the spring boot documentation.

huangapple
  • 本文由 发表于 2020年8月14日 11:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63406277.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定