springboot自定义Actuator

本文介绍了如何在Spring Boot 2.x应用中自定义Actuator端点。通过添加依赖,定义端点类并配置相应的操作(如@WriteOperation、@DeleteOperation和@ReadOperation),可以创建自定义的监控和管理功能。此外,文章还提到了2.x版本与1.5.x版本在构建上的差异,以及启用所有端点的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring Boot的Actuator在应用程序中提供众多Web端点,我们可以通过它们了解应用程序允许的状态,也可以通过提供的rest接口可以获取整个依赖于spring体系中bean如何组装在一起,也可以掌握应用程序允许的环境属性消息等

如何启用Actuator的端点,只需要在项目中引入Actuator的依赖:

在Maven中构建 :

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

其中springboot2.x版本与spirngboot1.5x构建有很大不同

springboot2.x:

@Endpoint(id = "dtc-refresh"):@Endpoint设置为端点类id设置端点访问路径

@WriteOperation:类比于post请求作用

@DeleteOperation:类比于delete请求作用

@ReadOperation:类比于get请求作用

@Selector:接收请求参数

第一步定义端点类:

@Endpoint(id = "test")
public class TestEndpoint{


    /**
     *test
     */
    @WriteOperation
    public void test1() {
        //编写代码
    }

}

@ReadOperation
public void test2(@Selector String type) {
    //

}

2将端点注入到spring中(使用@Componen注解或使用@bean)

3:在2.x系列 actuator默认只会开启info,health,如果需要开启所有端点访问(不开启反问会404):

###springboot2.x Actuator只暴露了health和info端点
##如果需要则使用下面配置
management:
  endpoints:
    web:
      exposure:
        include: "*"

4:最终访问http://localhost:8866/actuator/test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值