首页 > 解决方案 > Spring Boot Actuator - 自定义端点

问题描述

Spring Boot Actuator在我的项目中使用模块,它公开 REST 端点 URL 来监视和管理生产环境中的应用程序使用情况,而无需对其中任何一个进行编码和配置。

默认情况下,仅公开/health/info端点。

我正在根据application.properties我的用例通过文件自定义端点。

application.properties.

#To expose all endpoints
management.endpoints.web.exposure.include=*
 
#To expose only selected endpoints
management.endpoints.jmx.exposure.include=health,info,env,beans

我想了解,Spring Boot 究竟在哪里创建实际端点,/health以及/info它如何通过 HTTP 公开它们?

标签: javaspringspring-bootspring-boot-actuator

解决方案


感谢@Puce 和@MarkBramnik 帮助我参考文档和代码存储库。

我想了解端点是如何工作的以及它们是如何通过 HTTP 公开的,以便我可以创建自定义端点以在我的应用程序中使用。

Spring Framework 的一大特点是它很容易扩展,我也能做到这一点。

要创建自定义执行器端点,请在类上使用 @Endpoint 注释。然后根据需要利用方法上的@ReadOperation//注释将它们公开为执行器端点 bean @WriteOperation@DeleteOperation

参考文档:实现自定义端点

参考示例:

@Endpoint(id="custom_endpoint")
@Component
public class MyCustomEndpoint {

    @ReadOperation
    @Bean
    public String greet() {
        return "Hello from custom endpoint";
    }
}

端点 id 即 custom_endpoint 需要在要启用的执行器端点列表中进行配置。

application.properties

management.endpoints.web.exposure.include=health,info,custom_endpoint

重新启动后,端点就像一个魅力!


推荐阅读