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

Posted

技术标签:

【中文标题】如何在 Spring Boot Health 中添加自定义健康检查?【英文标题】:How to add a custom health check in spring boot health? 【发布时间】:2017-12-04 14:20:05 【问题描述】:
<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中添加自定义健康检查?

【问题讨论】:

【参考方案1】:

添加自定义运行状况检查很容易。只需创建一个新的 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();
        
    

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


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

【讨论】:

对于k8s的ready检查,还需要在application.properties文件key-value management.endpoint.health.group.readiness.include="custom,readinessState"添加custom【参考方案2】:

自 Spring Boot 2.X 起

正如@yuranos87 所述,执行器概念在 Spring Boot 2.X 中发生了变化,但您仍然可以通过实现 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 文档中找到有关 custom health checks 和 custom endpoints 的更多信息。

【讨论】:

还需要添加以下属性,以便在响应中添加自定义消息management.endpoint.health.show-details=always【参考方案3】:

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

话虽如此,健康端点的扩展有点棘手,因为它的一个扩展是由执行器本身开箱即用提供的。如果不操纵 bean 初始化过程,您的应用程序将无法启动,因为它会看到 2 个扩展并且不知道该选择哪一个。 一种更简单的方法是使用 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",

【讨论】:

【参考方案4】:

如果您想要自定义状态消息,那么您可以在此处查看答案 - https://***.com/a/66985769/4952800

【讨论】:

以上是关于如何在 Spring Boot Health 中添加自定义健康检查?的主要内容,如果未能解决你的问题,请参考以下文章

如何在不杀死 /health 的情况下将我的 Spring Boot 应用程序默认为 application/xml?

我的 Spring Boot 应用程序的 /health 端点正在发出有关在同一台机器上运行的 Consul 服务器的所有信息。如何禁用此功能?

在 Spring Boot Actuator 中为 /health 端点启用 CORS

Spring Boot - 将 /health 端点的位置更改为 /ping/me

如何更改 Spring Boot 健康执行器结果以进行测试?

使用 OAuth2 身份验证启用 Spring Boot Health Actuator