consur运行状况指示器未显示在springboot的执行器运行状况端点中

juzqafwq  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(435)

我有一个基于springboot的web应用程序,它公开了一个consur健康指示器bean。
bean是通过springboot的自动配置正确创建和初始化的,但是,尽管关联的配置属性“management.health.consur.enabled”设置为true,指示器仍没有显示在actuator health端点中:

{
   "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());
    }

在这里设置一个断点,我看到实际上consultHealthIndicator bean没有列在applicationcontext.getbeansoftype(healthcontributor.class)调用的输出中,如下所示:

但是,当我用父应用程序上下文测试同一调用时,得到以下结果:

有人能解释一下为什么这个特定的bean出现在根上下文中而不是子上下文中吗?
有没有办法强制它在子上下文中初始化,以便它在健康端点中正确注册?
我附上了一个样本项目,允许复制的问题。我还提供了应用程序使用的consul配置示例(您可以通过consumport命令导入它)。运行上面的示例并转到运行状况端点(localhost:8080/monitoring/health)您将清楚地看到列表中缺少consur组件。
先谢谢你。

oyt4ldly

oyt4ldly1#

看起来,consol health indicator没有注册到health contributor注册表,您可以通过自己注册consol 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"
    }
  }
}

相关问题