Consul Health 指标未显示在 Spring Boot 的执行器健康端点中

Consul Health indicator not showing in SpringBoot' actuator health endpoint

我有一个基于 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) 调用的输出中列出显示如下:

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

有人可以解释为什么这个特定的 bean 出现在 root context but not in the child context 中吗?

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

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

提前致谢。

Consul health indicator 似乎没有注册到 health contributor registry,你可以通过自己注册 consul health check 来解决这个问题。您可以在任何配置文件中添加这样的代码段。

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