【发布时间】:2017-06-03 12:43:33
【问题描述】:
我正在尝试将 Spring Boot Actuator 与我公司现有的基础架构集成。为此,我需要能够自定义状态消息。例如,如果应用程序启动并正常运行,我需要从健康执行器端点返回 200 和纯文本正文“HAPPY”。
目前可以进行这种定制吗?由于 Status 类是 final 我不能扩展它,但我认为这会起作用。
【问题讨论】:
标签: spring-boot spring-boot-actuator
我正在尝试将 Spring Boot Actuator 与我公司现有的基础架构集成。为此,我需要能够自定义状态消息。例如,如果应用程序启动并正常运行,我需要从健康执行器端点返回 200 和纯文本正文“HAPPY”。
目前可以进行这种定制吗?由于 Status 类是 final 我不能扩展它,但我认为这会起作用。
【问题讨论】:
标签: spring-boot spring-boot-actuator
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 方法。
【讨论】: