有没有办法将状态字段值从向上/向下更改
{"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 BootsHealthEndpoint
和CompositeHealthIndicator
课程中获得有关如何做到这一点的灵感。(主题HealthAggregator
(