其他分享
首页 > 其他分享> > 如何使`@Endpoint(id =“health”)`在Spring Boot 2.0中工作?

如何使`@Endpoint(id =“health”)`在Spring Boot 2.0中工作?

作者:互联网

我已经尝试了在Spring Boot 2.0.0.M5中自定义健康执行器的新方法,如下所述:https://spring.io/blog/2017/08/22/introducing-actuator-endpoints-in-spring-boot-2-0

@Endpoint(id = "health")
public class HealthEndpoint {
    @ReadOperation
    public Health health() {
        return new Health.Builder()
            .up()
            .withDetail("MyStatus", "is happy")
            .build();
    }
}

但是,当我运行HTTP GET到localhost:port / application / health时,我仍然获得标准的默认健康信息.我的代码完全被忽略了.

当我使用通过HealthIndicator实现自定义健康信息的“传统方式”时,它按预期工作,健康信息使用给定的详细信息进行修饰:

@Component
public class MyHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        return new Health.Builder()
            .up()
            .withDetail("MyStatus 1.1", "is happy")
            .withDetail("MyStatus 1.2", "is also happy")
            .build();
    }
}

问题:为了使@Endpoint(id =“health”)解决方案有效,我还应该配置和/或实现什么?

我的意图不是创建自定义执行器myhealth,而是定制现有的健康执行器.基于文档,我希望通过实现HealthIndicator获得相同的结果.我错在那个假设吗?

Maven配置pom.xml包含:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.M5</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

Spring Boot配置application.properties包含:

endpoints.health.enabled=true
endpoints.autoconfig.enabled=true
endpoints.autoconfig.web.enabled=true

解决方法:

更新

>新型弹簧执行器端点上的documentation不是很清晰.它试图以现有的健康端点为例解释新的端点基础设施.
>新的端点ID必须是唯一的,并且不应与现有的执行器端点相同.如果尝试将下面显示的示例的ID更改为运行状况,则会出现以下异常:

java.lang.IllegalStateException: Found two endpoints with the id 'health'

>上面关于使用@Bean注释声明端点类的注释是正确的.
>在Spring Boot 2.0中,自定义运行状况端点没有更改.您仍然必须实现HealthIndicator以添加自定义值.

自定义执行器端点

以下是在Spring Boot 2.0中创建自定义Actuator端点所需的更改.

模型

包含您的自定义信息的域.

@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class MyHealth {

    private Map<String, Object> details;

    @JsonAnyGetter
    public Map<String, Object> getDetails() {
        return this.details;
    }
}

我的健康终点

声明myhealth端点,

@Endpoint(id = "myhealth")
public class MyHealthEndpoint {

    @ReadOperation
    public MyHealth health() {
        Map<String, Object> details = new LinkedHashMap<>();
        details.put("MyStatus", "is happy");
        MyHealth health = new MyHealth();
        health.setDetails(details);

        return health;
    }
}

我的健康延伸

myhealth端点的扩展,

@WebEndpointExtension(endpoint = MyHealthEndpoint.class)
public class MyHealthWebEndpointExtension {

    private final MyHealthEndpoint delegate;

    public MyHealthWebEndpointExtension(MyHealthEndpoint delegate) {
        this.delegate = delegate;
    }

    @ReadOperation
    public WebEndpointResponse<MyHealth> getHealth() {
        MyHealth health = delegate.health();
        return new WebEndpointResponse<>(health, 200);
    }
}

执行器配置

配置将两个新创建的执行器类暴露为bean,

@Configuration
public class ActuatorConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint
    public MyHealthEndpoint myHealthEndpoint() {
        return new MyHealthEndpoint();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint
    @ConditionalOnBean({MyHealthEndpoint.class})
    public MyHealthWebEndpointExtension myHealthWebEndpointExtension(
            MyHealthEndpoint delegate) {
        return new MyHealthWebEndpointExtension(delegate);
    }
}

应用属性

对application.yml的更改,

endpoints:
  myhealth:
    enabled: true

启动应用程序后,您应该能够访问位于http://< host>:< port> / application / myhealth的新执行器端点.

您应该期望类似于下面所示的响应,

{
  "MyStatus": "is happy"
}

完整的工作示例可以在here找到.

标签:java,spring,spring-boot-2,spring-boot-actuator
来源: https://codeday.me/bug/20190627/1305919.html