我正在尝试将Spring Boot Actuator与我公司现有的基础架构集成。为此,我需要能够自定义状态消息。例如,如果应用程序启动并正常运行,我需要从健康执行器端点返回200和纯文本体“HAPPY”。
目前可以进行此类定制吗?由于Status类是final,我无法扩展它,但我认为这样可行。
答案 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
方法。