将弹簧引导执行器的健康状态从"向上/向下"转换为"真/假"



有没有办法将状态字段值从向上/向下更改

{"status":"UP"}

真/假,如下所示:

{"status":true}

我想使用弹簧执行器使用的相同检查逻辑,不需要自定义检查逻辑,只想更新状态值。

以下代码将注册一个新的执行器终结点/healthy,该终结点使用与默认/health终结点相同的机制。

package com.example;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.health.HealthEndpoint;
import org.springframework.boot.actuate.health.Status;
import org.springframework.stereotype.Component;
@Component
@Endpoint(id = "healthy") // Change this to expose the endpoint under a different name
public class BooleanHealthEndpoint {
HealthEndpoint healthEndpoint;
public BooleanHealthEndpoint(HealthEndpoint healthEndpoint) {
this.healthEndpoint = healthEndpoint;
}
@ReadOperation
public Health getHealth() {
Boolean healthy = healthEndpoint.health().getStatus().equals(Status.UP);
return new Health(healthy);
}
public static class Health {
private Boolean status;
public Health(Boolean status) {
this.status = status;
}
public Boolean getStatus() {
return status;
}
}
}

如果不想添加自定义的/healthy终结点并继续使用默认的/health终结点,可以在属性文件中添加以下设置,然后将其映射到默认设置:

management.endpoints.web.path-mapping.health=internal/health
management.endpoints.web.path-mapping.healthy=/health

假设您的公司建立了新的API标准,因为涉及许多不同的框架构成了应用程序的范围,我们不只是在谈论Spring Boot应用程序(因为否则会很烦人(:

只需在/actuator/customstatus下实现您自己的@Endpoint,并在其下聚合HealthIndicator的所有状态。您可能希望从Spring BootsHealthEndpointCompositeHealthIndicator课程中获得有关如何做到这一点的灵感。(主题HealthAggregator(

最新更新