spring boot的健康检查HealthIndicators

来源:互联网 发布:市政工程预算软件 编辑:程序博客网 时间:2024/05/14 16:31
想提供自定健康信息, 你可以注册实现HealthIndicator接口的Spring beans。 你需要提供一个health()方法的实现, 并返

回一个Health响应Health响应需要包含一个status和可的用于展示的情。

import org.springframework.boot.actuate.health.HealthIndicator;import org.springframework.stereotype.Component;@Componentpublic class MyHealth implements HealthIndicator {@Overridepublic Health health() {int errorCode = check(); // perform some specific health checkif (errorCode != 0) {return Health.down().withDetail("Error Code", errorCode).build();} return Health.up().build();}}

除了Spring BootStatus型,Health也可以返回一个代表新的系的自定Status。 在种情况下, 需要提供
一个
HealthAggregator接口的自定义实现, 或使用management.health.status.order属性配置默实现
例如, 假
一个新的, 代码为FATALStatus被用于你的一个HealthIndicator实现中。了配置重程度, 你需要将下面的配
置添加到
application属性文件中:
management.health.status.order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP
如果使用HTTP访问health端点, 你可能想要注册自定status, 并使用HealthMvcEndpoint行映射。 例如, 你可以将
FATAL映射HttpStatus.SERVICE_UNAVAILABLE


原创粉丝点击