首页 > 解决方案 > Consul Health 指示器未显示在 SpringBoot 的执行器健康端点中

问题描述

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

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

经过进一步检查,我发现了负责获取所有可用指标的波纹管片段(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)调用的输出中,如下所示:

在此处输入图像描述

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

有人可以解释一下为什么这个特定的bean出现在根上下文中而不是子上下文中吗?

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

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

先感谢您。

标签: javaspringspring-bootspring-boot-actuatorconsul

解决方案


看起来,Consul 健康指标没有注册到健康贡献者注册表,你可以通过自己注册 consul 健康检查来解决这个问题。您可以在任何配置文件中添加这样的片段。

@Autowired
  public void doRegister(
      ConsulHealthIndicator healthIndicator, HealthContributorRegistry healthContributorRegistry) {
    healthContributorRegistry.registerContributor("consul", healthIndicator);
  }

一旦添加,这应该会产生类似这样的东西

{
  "status": "UP",
  "components": {
    "consul": {
      "status": "UP",
      "details": {
        "leader": "127.0.0.1:8300",
        "services": {
          "consul": []
        }
      }
    },
    "discoveryComposite": {
      "description": "Discovery Client not initialized",
      "status": "UNKNOWN",
      "components": {
        "discoveryClient": {
          "description": "Discovery Client not initialized",
          "status": "UNKNOWN"
        }
      }
    },
    "diskSpace": {
      "status": "UP",
      "details": {
        "total": 250685575168,
        "free": 17967964160,
        "threshold": 10485760,
        "exists": true
      }
    },
    "ping": {
      "status": "UP"
    },
    "refreshScope": {
      "status": "UP"
    }
  }
}

推荐阅读