java - 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 公开它们?
解决方案
感谢@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
重新启动后,端点就像一个魅力!
推荐阅读
- python - 使用 RDKit 将 SEQUENCE 转换为 SMILES 的问题
- ethereum - 以太坊合约调用方法返回 Gas 不足消息?
- ruby-on-rails - 在 Ruby 中重命名临时文件
- python - InvalidArgumentError: 2 root error(s) found.(0) Invalid argument: You must feed a value for placeholder tensor 'Placeholder_22'
- powershell - 奇怪的学校作业,关于在 powershell 中显示表情符号
- python - Flask 内部服务器错误:如何在发布表单数据以正确从表单中提取值时处理对服务器的请求?
- android - RecyclerView 在Android中单击时选择了多个项目
- mysql - Apexcharts 不显示来自 Web 服务的数据
- java - 使用正则表达式从字符串中删除不在允许列表中的所有内容
- flutter - Flutter Riverpod:使用状态通知器刷新屏幕外页面