【问题标题】:Check that all HealthIndicator avaliable on application startup检查应用程序启动时所有可用的 HealthIndicator
【发布时间】:2021-09-30 11:15:56
【问题描述】:

目前,Actuator 不会检查组中的所有指标是否在上下文中可用,它还会在运行时检查时跳过不存在的指标。

例如,application.yaml - 如果我描述 checkServiceUnknown

management:
  endpoint:
    health:
      group:
        readiness:
          include: checkService1, checkServiceUnknown

它只是跳过 HealthEndpointSupport 中的 checkServiceUnknown。发生这种情况是因为它由 NamedContributor 循环,而不是由组成员循环:

for (NamedContributor<C> namedContributor : namedContributors) {
            String name = namedContributor.getName();
            C contributor = namedContributor.getContributor();
            if (group.isMember(name) || isNested) {
...

如何检查所有描述的指标是否真的会在 Spring 启动时被调用/可用?

【问题讨论】:

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


    【解决方案1】:

    我的解决方法

    @Autowired
    HealthEndpointProperties healthEndpointProperties;
    
    @Autowired
    ApplicationContext applicationContext;
    
    @PostConstruct
    public void init() {
        Set<String> healthContributors = applicationContext.getBeansOfType(HealthContributor.class).keySet().stream()
                .map(HealthContributorNameFactory.INSTANCE)
                .collect(Collectors.toSet());
    
        healthEndpointProperties.getGroup().values().stream()
                .flatMap(it -> it.getInclude().stream())
                .forEach(it -> {
                    if (!healthContributors.contains(it)) {
                        throw new IllegalArgumentException("Unknown health indicator " + it);
                    }
                });
    }
    

    【讨论】:

      猜你喜欢
      • 2016-02-24
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 2021-08-19
      • 2012-09-23
      • 1970-01-01
      相关资源
      最近更新 更多