Spring Boot Actuator:具有纯文本的自定义状态?

时间:2017-01-18 23:24:18

标签: spring-boot spring-boot-actuator

我正在尝试将Spring Boot Actuator与我公司现有的基础架构集成。为此,我需要能够自定义状态消息。例如,如果应用程序启动并正常运行,我需要从健康执行器端点返回200和纯文本体“HAPPY”。

目前可以进行此类定制吗?由于Status类是final,我无法扩展它,但我认为这样可行。

1 个答案:

答案 0 :(得分:0)

Spring Boot使用HealthAggregator将各个运行状况指示器中的所有状态聚合到整个应用程序的单个运行状况中。您可以插入委派给Boot的默认聚合器OrderedHealthAggregator的自定义聚合器,然后将UP映射到HAPPY

@Bean
public HealthAggregator healthAggregator() {
    return new HappyHealthAggregator(new OrderedHealthAggregator());
}

static class HappyHealthAggregator implements HealthAggregator {

    private final HealthAggregator delegate;

    HappyHealthAggregator(HealthAggregator delegate) {
        this.delegate = delegate;
    }

    @Override
    public Health aggregate(Map<String, Health> healths) {
        Health result = this.delegate.aggregate(healths);
        if (result.getStatus() == Status.UP) {
            return new Health.Builder(new Status("HAPPY"), result.getDetails())
                    .build();
        }
        return result;
    }

}

如果要完全控制响应的格式,那么您需要编写自己的MVC端点实现。您可以将Spring Boot中现有的HealthMvcEndpoint类用作超类,并覆盖其invoke方法。