【问题标题】:SpringBoot : Consul Health indicator missing from the Health endpointSpring Boot:Health 端点中缺少 Consul Health 指示器
【发布时间】:2020-08-19 15:24:19
【问题描述】:

我有一个基于 SpringBoot 的 Web 应用程序,它公开了一个 consul 健康指示器 bean。
bean 由 springboot 的自动配置正确创建和初始化,但是,尽管关联的配置属性“management.health.consul.enabled”设置为 true,但指标并未显示在执行器健康端点中:

{
   "status": "UP",
   "components": {
        "Kafka": {...},
        "SchemaRegistry": {...},
        "discoveryComposite": {...},
        "diskSpace": {...},
        "ping": {...},
        "refreshScope": {...}
    }
}

经过进一步检查,我发现了负责获取所有可用指标的波纹管 sn-p (HealthEndpointConfiguration.java):

    @Bean
    @ConditionalOnMissingBean
    HealthContributorRegistry healthContributorRegistry(ApplicationContext applicationContext,
            HealthEndpointGroups groups) {
        Map<String, HealthContributor> healthContributors = new LinkedHashMap<>(
                applicationContext.getBeansOfType(HealthContributor.class));
        if (ClassUtils.isPresent("reactor.core.publisher.Flux", applicationContext.getClassLoader())) {
            healthContributors.putAll(new AdaptedReactiveHealthContributors(applicationContext).get());
        }
        return new AutoConfiguredHealthContributorRegistry(healthContributors, groups.getNames());
    }

在那里设置断点,我看到 ConsulHealthIndicator bean 确实没有列在 applicationContext.getBeansOfType(HealthContributor.class) 调用的输出中,如下所示:

但是当我使用父应用程序上下文测试相同的调用时,我得到以下信息:

有人能解释一下为什么root context 中出现了这个特定的 bean,而child context 中没有出现吗?

有没有办法强制它在子上下文中初始化,以便在健康端点中正确注册?

我正在使用

  • SpringBoot 2.3.1.RELEASE
  • spring-cloud-starter-consul-all 2.2.4.RELEASE

提前谢谢你。


编辑

我已附加 a sample project 以允许重现该问题。
我还包含了应用程序使用的 consul 配置(您可以通过 consul import 命令导入它)。
运行上面的示例并转到健康端点(localhost:8080/monitoring/health),您会清楚地看到列表中缺少 consul 组件。

【问题讨论】:

    标签: java spring spring-boot spring-boot-actuator


    【解决方案1】:

    为了让 consul 指示器正常工作,我必须提供自己的 HealthContributorRegistry,在执行 HealthContributor bean 查找时我会在其中考虑父上下文:

      @Bean
      HealthContributorRegistry healthContributorRegistry(
          ApplicationContext applicationContext, HealthEndpointGroups groups) {
        Map<String, HealthContributor> healthContributors =
            new LinkedHashMap<>(applicationContext.getBeansOfType(HealthContributor.class));
        ApplicationContext parent = applicationContext.getParent();
        while (parent != null) {
          healthContributors.putAll(parent.getBeansOfType(HealthContributor.class));
          parent = parent.getParent();
        }
        return new DefaultHealthContributorRegistry(healthContributors);
      }
    

    这是一种临时解决方法,理想情况下,领事指示器应该像其他健康贡献者一样开箱即用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-20
      • 1970-01-01
      • 2022-10-22
      • 1970-01-01
      • 1970-01-01
      • 2021-12-02
      • 2019-02-03
      • 2022-12-21
      相关资源
      最近更新 更多