如何实现自定义CompositeHealthContributor

时间:2019-12-02 12:06:48

标签: spring-boot spring-boot-actuator

我要显示以下健康信息:

{
   "status":"UP", 
   "components": {
          "myHealthComposite": {
                "status":"UP",
                "components": {
                        "myFirstHealthIndicator": { "status":"UP" },
                        "mySecondHealthIndicator": { "status":"UP" }
                }
          }
    },
    "groups":[]
}

从最新的Spring Boot Actuator文档中,我应该使用“ HealthIndicator”-接口实现“ myFirstHealthIndactor”和“ mySecondHealthIndicator”,并使用“ CompositeHealthContributor” -Interface将它们组合在一起。但是对于“ CompositeHealthContributor”,我需要一个示例,在“文档”中找不到对我有用的东西。

有人可以帮我吗?

问候

编辑:

myFirstHealthIndicator

@Component
public class MyFirstHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        return Health.up().build();
    }

}

mySecondHealthIndicator

@Component
public class MySecondHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        return Health.up().build();
    }

}

myHealthComposite

@Component
public class MyHealthComposite implements CompositeHealthContributor {

    @Override
    public HealthContributor getContributor(String name) {
        return null;
    }

    @Override
    public Iterator<NamedContributor<HealthContributor>> iterator() {
        return null;
    }

}

1 个答案:

答案 0 :(得分:0)

一种非常简单且经过硬编码的解决方案,可以输出预期的结果。

请从 MyFirstHealthIndicator MySecondHealthIndicator 中删除@Component批注。否则,它们也被列为单独的组件。

MyFirstHealthIndicator

public class MyFirstHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        return Health.up().build();
    }

}

MySecondHealthIndicator

public class MySecondHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        return Health.up().build();
    }

}

MyHealthComposite

@Component
public class MyHealthComposite implements CompositeHealthContributor {

    private Map<String, HealthContributor> contributors = new LinkedHashMap<>();

    MyHealthComposite() {
        this.contributors.put("myFirstHealthIndicator", new MyFirstHealthIndicator());
        this.contributors.put("mySecondHealthIndicator", new MySecondHealthIndicator());
    }

    @Override
    public HealthContributor getContributor(String name) {
        return contributors.get(name);
    }

    @Override
    public Iterator<NamedContributor<HealthContributor>> iterator() {
        return contributors.entrySet().stream()
                .map((entry) -> NamedContributor.of(entry.getKey(), entry.getValue())).iterator();
    }
}

请在您的management.endpoint.health.show-components=always中启用application.properties

输出如下:

{
  "status": "UP",
  "components": {
    "diskSpace": {
      "status": "UP"
    },
    "myHealthComposite": {
      "status": "UP",
      "components": {
        "myFirstHealthIndicator": {
          "status": "UP"
        },
        "mySecondHealthIndicator": {
          "status": "UP"
        }
      }
    },
    "ping": {
      "status": "UP"
    }
  }
}

但是,这不是我的解决方案,而是从BindersHealthIndicatorAutoConfiguration from spring-cloud-stream开始采用并适应要求。