如何在Spring Boot Health中添加自定义健康检查?

42
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

这将为您的应用程序添加几个有用的端点。其中一个是/health。当您启动应用程序并导航到/health端点时,您将看到它已经返回了一些数据。

{
    "status":"UP",
    "diskSpace": {
        "status":"UP",
        "free":56443746,
        "threshold":1345660
    }
}

如何在Spring Boot Health中添加自定义健康检查?

3个回答

41

添加自定义健康检查很容易。只需创建一个新的Java类,将其扩展自AbstractHealthIndicator并实现doHealthCheck方法即可。该方法将传递一个builder,其中包含一些有用的方法。如果您的健康状况良好,请调用builder.up(),如果不行,请调用builder.down()。检查健康状况所采取的措施完全由您决定。也许您想ping一些服务器或检查一些文件。

@Component
public class CustomHealthCheck extends AbstractHealthIndicator {
    @Override
    protected void doHealthCheck(Health.Builder bldr) throws Exception {
        // TODO implement some check
        boolean running = true;
        if (running) {
          bldr.up();
        } else {
          bldr.down();
        }
    }
}

这已足够激活新的健康检查(确保你的应用程序上有@ComponentScan)。重新启动应用程序,将您的浏览器定位到/health端点,您将看到新添加的健康检查。

{
    "status":"UP",
    "CustomHealthCheck": {
        "status":"UP"
    },
    "diskSpace": {
        "status":"UP",
        "free":56443746,
        "threshold":1345660
    }
}

2
对于k8s的准备检查,还需要在application.properties文件的键值中添加“custom”,如下:management.endpoint.health.group.readiness.include="custom,readinessState" - 袁文涛

20

自从Spring Boot 2.X版本以后

正如@yuranos87所述,在Spring Boot 2.X中,actuator的概念已经发生了改变,但是您仍然可以通过实现HealthIndicator或对于响应式应用程序使用ReactiveHealthIndicator来轻松添加自定义健康检查

@Component
public class CacheHealthIndicator implements HealthIndicator {

@Override
public Health health() {
    long result = checkSomething();
    if (result <= 0) {
        return Health.down().withDetail("Something Result", result).build();
    }
    return Health.up().build();      
  }
}
或者
@Component
public class CacheHealthIndicator implements ReactiveHealthIndicator {

@Override
public Mono<Health> health() {
    return Mono.fromCallable(() -> checkSomething())
        .map(result -> {
            if (result <= 0) {
                return Health.down().withDetail("Something Result", result).build();
            }
            return Health.up().build();
        });
   }
}
此外,你可以使用@Endpoint@EndpointWebExtension添加或扩展任何端点。这里的端点包括infohealth等等。因此,你可以使用@Endpoint添加自定义健康检查,但使用HealthIndicator更加简单。 在 Spring Boot 文档中,你可以找到更多关于自定义健康检查自定义端点的信息。 不要忘记设置应用程序属性,否则你将无法看到你的自定义健康指标:
management.endpoint.health.show-details=always

4
需要添加以下属性,以便在响应中添加自定义消息:management.endpoint.health.show-details=always - Minnow

9

Spring Boot 2.X显著改变了actuator。通过@EndpointWebExtension,启用了一种新的、更好的机制来扩展现有的端点。

话虽如此,健康端点的扩展有些棘手,因为actuator本身提供了一个用于它的扩展。如果不操纵bean初始化过程,您的应用程序将无法启动,因为它将看到两个扩展,并且不知道选择哪一个。 一个更简单的方法是使用info而不是扩展它:

@Component
@EndpointWebExtension(endpoint = InfoEndpoint.class)
public class InfoWebEndpointExtension {
   @Value("${info.build.version}")
   private String versionNumber;
   @Value("${git.commit.id}")
   private String gitCommit;
   @Value("${info.build.name}")
   private String applicationName;
   ...
   @ReadOperation
   public WebEndpointResponse<Map> info() {

别忘了您还可以重新映射URL。在我的情况下,我更喜欢使用/status而不是/health,并且不希望在路径中出现/actuator/

management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.info=status

我更喜欢使用 /info 的另一个原因是,它没有默认的嵌套结构(即 /health),这一点让我感到困惑。

{
"status": {
    "status": "ON",

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接