💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 46.6.2 编写自定义HealthIndicators 你可以注册实现[HealthIndicator](https://github.com/spring-projects/spring-boot/tree/v1.4.1.RELEASE/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthIndicator.java)接口的Spring beans来提供自定义健康信息。你需要实现`health()`方法,并返回一个`Health`响应,该响应需要包含一个`status`和其他用于展示的详情。 ```java import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class MyHealth implements HealthIndicator { @Override public Health health() { int errorCode = check(); // perform some specific health check if (errorCode != 0) { return Health.down().withDetail("Error Code", errorCode).build(); } return Health.up().build(); } } ``` **注** 对于给定`HealthIndicator`的标识是bean name去掉`HealthIndicator`后缀剩下的部分。在以上示例中,可以在`my`的实体中获取健康信息。 除Spring Boot预定义的[`Status`](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Status.java)类型,`Health`也可以返回一个代表新的系统状态的自定义`Status`。在这种情况下,你需要提供一个[`HealthAggregator`](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/HealthAggregator.java)接口的自定义实现,或使用`management.health.status.order`属性配置默认实现。 例如,假设一个新的,代码为`FATAL`的`Status`被用于你的一个`HealthIndicator`实现中。为了配置严重性级别,你需要将以下配置添加到application属性文件中: ```java management.health.status.order=DOWN, OUT_OF_SERVICE, UNKNOWN, UP ``` 如果使用HTTP访问health端点,你可能想要注册自定义的status,并使用`HealthMvcEndpoint`进行映射。例如,你可以将`FATAL`映射为`HttpStatus.SERVICE_UNAVAILABLE`。