有没有办法从 UP/DOWN 更改状态字段值
{"status":"UP"}
为 TRUE/FALSE,如下所示:
{"status":true}
我想使用与弹簧执行器相同的检查逻辑,不需要自定义检查逻辑,只想更新状态值。
有没有办法从 UP/DOWN 更改状态字段值
{"status":"UP"}
为 TRUE/FALSE,如下所示:
{"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 应用程序(因为否则会很烦人):
只需在其下实现您自己@Endpoint
的/actuator/customstatus
并聚合所有HealthIndicator
的状态。您可能希望从 Spring BootsHealthEndpoint
和CompositeHealthIndicator
课程中获得有关如何做到这一点的灵感。(主题HealthAggregator
)