【问题标题】:Spring Boot Actuator: have a customized status as plain text?Spring Boot Actuator:具有纯文本的自定义状态?
【发布时间】:2017-06-03 12:43:33
【问题描述】:

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

目前可以进行这种定制吗?由于 Status 类是 final 我不能扩展它,但我认为这会起作用。

【问题讨论】:

    标签: spring-boot spring-boot-actuator


    【解决方案1】:

    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 中现有的 HealthMvcEndpointclass 用作超类并覆盖其 invoke 方法。

    【讨论】:

    • 其实这并不能回答我的问题。这仍然返回一个 JSON 结构。我只需要一个纯文本字符串“HAPPY”,仅此而已。因为这个 aggregate() 返回一个 Health 对象,所以我看不到这样做的方法。健康和状态都是最终的,所以我不确定如何自定义它们的行为。
    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2015-02-22
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多