如何在spring boot health中添加自定义运行状况检查?

sha*_*njo 24 spring spring-boot spring-boot-actuator

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

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

{
    "status":"UP",
    "diskSpace": {
        "status":"UP",
        "free":56443746,
        "threshold":1345660
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在spring boot health中添加自定义运行状况检查?

sha*_*njo 28

添加自定义运行状况检查很容易.只需创建一个新的Java类,从AbstractHealthIndicator扩展它并实现doHealthCheck方法.该方法通过一些有用的方法获取构建器.如果您的健康状况良好,请调用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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这足以激活新的运行状况检查(确保@ComponentScan在您的应用程序中).重新启动应用程序并将浏览器定位到/ health端点,您将看到新添加的运行状况检查.

{
    "status":"UP",
    "CustomHealthCheck": {
        "status":"UP"
    },
    "diskSpace": {
        "status":"UP",
        "free":56443746,
        "threshold":1345660
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 对于 k8s 就绪检查,还需要将 `custom` 添加到 application.properties 文件键值 `management.endpoint.health.group.readiness.include="custom,readinessState"` (2认同)

Tob*_*ske 8

从Spring Boot 2.X开始

如@ yuranos87所述,执行器的概念在Spring Boot 2.X中已更改,但是您仍然可以通过实施或针对反应性应用轻松添加自定义运行状况检查HealthIndicatorReactiveHealthIndicator

@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();      
}
Run Code Online (Sandbox Code Playgroud)

要么

@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();
        });
   }
}
Run Code Online (Sandbox Code Playgroud)

此外,您可以使用或添加或扩展任何端点。这里的端点是,还有更多。因此,您可以通过使用添加自定义运行状况检查,但是使用起来要容易得多。@Endpoint@EndpointWebExtensioninfohealth@EndpointHealthIndicator

您可以在Spring Boot文档中找到有关自定义运行状况检查自定义端点的更多信息。

  • 还需要添加以下属性,以便在响应“management.endpoint.health.show-details=always”中添加自定义消息 (4认同)

yur*_*s87 6

Spring Boot 2.X大大改变了执行器。通过启用了一种更好的新机制来扩展现有端点@EndpointWebExtension

话虽这么说,健康端点的扩展有点棘手,因为执行器本身即开即用地为其提供了一个扩展。如果不操纵bean的初始化过程,您的应用程序将无法启动,因为它将看到2个扩展名,并且不知道选择哪个扩展名。一个更简单的方法是改为使用信息并对其进行扩展:

@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() {
Run Code Online (Sandbox Code Playgroud)

不要忘记,您也可以重新映射URL。在我来说,我更喜欢/状态/健康,不希望/驱动器/路径:

management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.info=status
Run Code Online (Sandbox Code Playgroud)

我更喜欢/ info的另一个原因是因为我没有这个嵌套的结构,这是/ health的默认结构:

{
"status": {
    "status": "ON",
Run Code Online (Sandbox Code Playgroud)