【问题标题】:Spring Boot Actuator - Custom EndpointsSpring Boot Actuator - 自定义端点
【发布时间】:2021-04-24 15:13:41
【问题描述】:

我在我的项目中使用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 公开它们?

【问题讨论】:

  • 还可以查看 Endpoint 的 Javadoc 和相关/链接的类:docs.spring.io/spring-boot/docs/current/api/org/springframework/…
  • 不清楚您要解决什么问题。想看spring-boot-actuator的源码吗?它在此处可用:github.com/spring-projects/spring-boot/tree/master/…,查看并查看使用 @Endpoint 注释的 IDE。您会看到 EndpointRequestMatcher 依次由执行器引擎调用,但我怀疑这些信息是否足以解决您面临的问题(无论是什么 :))
  • 感谢 Puce 和 Mark Bramnik 为我指明了正确的方向。感谢指导!我添加了一个答案作为我的问题的解决方案,以防人们将来需要类似的东西。

标签: java spring spring-boot spring-boot-actuator


【解决方案1】:

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

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

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

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

参考文档:Implementing Custom Endpoints

参考示例:

@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

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

【讨论】:

  • 你为什么在方法 greet() 中使用@Bean?
猜你喜欢
  • 2015-02-22
  • 1970-01-01
  • 2023-03-10
  • 2020-06-01
  • 2016-06-01
  • 1970-01-01
  • 2017-01-21
  • 2020-09-15
  • 2016-09-07
相关资源
最近更新 更多