SpringBoot3 Actuator使用如何以及自定义端点

参考: https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn

作用: 对线上应用进行观测、监控、预警…
比如:
● 健康状况【组件状态、存活状态】Health
● 运行指标【cpu、内存、垃圾回收、吞吐量、响应成功率…】Metrics
● 链路追踪

1.使用

1.场景引入

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

2.暴露指标
在application.properties里面进行设置

#暴露所有端点信息  
management.endpoints.web.exposure.include=*
#以web方式暴露  
management.endpoint.health.enabled=true

3.访问数据
● 访问 http://localhost:8080/actuator;展示出所有可以用的监控端点
● http://localhost:8080/actuator/beans
● http://localhost:8080/actuator/configprops
● http://localhost:8080/actuator/metrics
● http://localhost:8080/actuator/metrics/jvm.gc.pause
● http://localhost:8080/actuator/endpointName/detailPath

2.Endpoint

常用端点: 默认提供了各种端点,来展示应用的详细信息
包括看有多少线程及线程运行情况,生成堆dump等各种端点
参考:
https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn?inner=u8Ebe

3.如何自定义端点

自定义HealthEndpoint和MetricsEndpoint

3.1 HealthEndpoint

1.自定义健康端点
例如监控一个组件的状态,存活还是死亡
方法: 实现HealthIndicator接口或者继承AbstractHealthIndicator来实现
1)实现HealthIndicator接口–>类名必须以HealthIndicator结尾,并且实现HealthIndicator接口,spring boot就能够确定这个组件是用来监控的

一句话总结下面的代码: 实现HealthIndicator接口的health方法,返回Health对象

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class MyHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

}

//构建Health
Health build = Health.down()
                .withDetail("msg", "error service")
                .withDetail("code", "500")
                .withException(new RuntimeException())
                .build();

需要下面的配置来开启健康管理以及展示详细信息: application.properties里面进行配置:

management.endpoint.health.show-details=always

2)继承AbstractHealthIndicator来实现

例子: 自定义一个健康监控的端点来监控某个组件的健康状态
一句话总结下面的代码: 继承AbstractHealthIndicator,实现里面的doHealthCheck方法,通过builder创建好Health对象

/**
 * @author lfy
 * @Description
 * @create 2023-05-08 22:59
 *
 * 1、实现 HealthIndicator 接口来定制组件的健康状态对象(Health) 返回
 * 2、
 */
@Component
public class MyHahaHealthIndicator extends AbstractHealthIndicator {

    @Autowired
    MyHahaComponent myHahaComponent; // 要检查的组件,会从容器中找,自动注入进来
    /**
     * 健康检查
     * @param builder
     * @throws Exception
     */
    @Override
    protected void doHealthCheck(Health.Builder builder) throws Exception {
        //调用组件自定义的检查方法,来检查应用的状态
        int check = myHahaComponent.check();
        if(check == 1){
            //存活
            builder.up()
                    .withDetail("code","1000")
                    .withDetail("msg","活的很健康")
                    .withDetail("data","我的名字叫haha")
                    .build();
        }else {
            //下线
            builder.down()
                    .withDetail("code","1001")
                    .withDetail("msg","死的很健康")
                    .withDetail("data","我的名字叫haha完蛋")
                    .build();
        }
    }
}

同样需要下面的配置来开启健康管理以及展示详细信息: application.properties里面进行配置:

management.endpoint.health.show-details=always

3.2 MetricsEndpoint

例子:自定义一个metric,来监控某个方法被调用了多少次?
总结:自定义metric是通过注入的MeterRegistry实现,实现一个有参构造方法,参数是MeterRegistry,然后通过MeterRegistry里面提供的方法来实现,比如计数器counter

@Component
public class MyHahaComponent {
    Counter counter = null;

    /**
     * 注入 meterRegistry 来保存和统计所有指标
     * @param meterRegistry
     */
    public MyHahaComponent(MeterRegistry meterRegistry){ // 只有一个有参构造方法,参数自动从容器中找然后注入meterRegistry
        //得到一个名叫 myhaha.hello 的计数器
        //这个myhaha.hello会被展示在metrics中,通过这个指标就能得到hello方法被访问了多少次
        counter = meterRegistry.counter("myhaha.hello");
    }
    public  int check(){
        //业务代码判断这个组件是否该是存活状态
        return 1;
    }

    // hello被调用一次,就记一次数
    public void hello(){
        System.out.println("hello");
        // 调用一次就记一次数
        counter.increment();
    }
}

4.还可以整合Prometheus + Grafana来展示这些端点提供的数据

大概得步骤是: 引入Prometheus 和 Grafana的场景,分别做一些配置,设置Prometheus数据源,配置Grafana监控面板
https://www.yuque.com/leifengyang/springboot3/wsx0br0dalot1pqn

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值