【问题标题】:How can I invoke the health check of another Spring Boot application from within my Spring Boot health endpoint?如何从我的 Spring Boot 健康端点中调用另一个 Spring Boot 应用程序的健康检查?
【发布时间】:2021-09-24 15:45:12
【问题描述】:

我想知道如何调用自定义健康指标:

  1. 在同一个应用程序中
  2. 另一个 Spring Boot 应用程序

我的应用程序分为一个基本应用程序(而不是一个配置)A,它实现了几乎所有的功能(没有 main 方法)和另一个应用程序 B(有一个 main 方法;-))具有基本配置作为依赖项POM。

在 A 中,我实现了一个自定义 HealthIndicator:

@Component
@RequiredArgsConstructor
public class AdapterDownstreamHealthIndicator implements HealthIndicator {

    private RestTemplate restTemplate;
    private String downStreamUrl = "http://localhost:8081/actuator";

    public AdapterDownstreamHealthIndicator(RestTemplate restTemplate, String downStreamUrl) {
        this.restTemplate = restTemplate;
        this.downStreamUrl = downStreamUrl;
    }

    @Override
    public Health health() {
//        try {
//            JsonNode resp = restTemplate.getForObject(downStreamUrl + "/health", JsonNode.class);
//            if (resp.get("status").asText().equalsIgnoreCase("UP")) {
//                System.out.println("JUHUUUUUUUUUUU!!!!");
//                return Health.up().build();
//            }
//        } catch (Exception ex) {
//            return Health.down(ex).build();
//        }
        return Health.down().build();
    }
}

在我的application.properties 中,我有一些执行器属性:

management.endpoints.web.exposure.include=health,info,prometheus,adapterDownstream
spring.jackson.serialization.INDENT_OUTPUT=true
management.endpoint.health.show-details=always

当我在浏览器中输入 http://localhost:9091/actuator/health/adapterDownstream 时,调试器不会在 health() 方法中停止,我只是显示一个空白页面。

我已经尝试扩展AbstractHealthIndicator,而不是实现HealthIndicator 接口。

自定义运行状况指示器无法识别,我做错了什么?

最后我想进行某种深度健康检查来测试我的应用程序中使用的所有组件。或许应该用CompositeHealthContributor???

【问题讨论】:

    标签: spring-boot-actuator indicator contributor compositehealthcontributor


    【解决方案1】:

    正如我所描述的,我有一个依赖项 A,它具有 NO main 方法,它作为 POM 中的依赖项加载到我的应用程序 B 中.到目前为止,我尝试在此依赖项/模块 A 中实现自定义运行状况检查类/运行状况指示器。

    简单的解决方案是添加一个 @ComponentScan(basePackages = "path.to.actuator") 应用程序的 main 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多