栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

更新资料

  • 关于新的弹簧执行器端点的文档不是很清楚。它试图以现有的运行状况端点为例来说明新的端点基础结构。
  • 新的端点ID必须是 唯一的, 并且不应与现有的执行器端点相同。如果尝试将下面显示的示例的ID更改为

    health
    ,则将出现以下异常:

    java.lang.IllegalStateException: Found two endpoints with the id 'health'
  • 上面有关使用

    @Bean
    注释声明端点类的注释是正确的。

  • health
    在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的配置,

@Configurationpublic 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"}

在这里可以找到完整的工作示例。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/390724.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号