1

有没有办法从 UP/DOWN 更改状态字段值

{"status":"UP"}

为 TRUE/FALSE,如下所示:

{"status":true}

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

4

2 回答 2

2

以下代码将注册一个新的执行器端点,该端点/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
于 2019-12-18T20:51:10.423 回答
0

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

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

于 2019-12-13T13:06:26.873 回答