SpringBoot自定义metric指标(仅做部分参考,细节处还得看你自己框架用了什么)

本文介绍了如何在SpringBoot项目中自定义metric指标,包括添加Maven依赖、配置yml文件,以及创建自定义指标代码示例。示例中涉及DemoTable实体、DemoTableService接口及其实现类,通过TestMetric.java解释了指标的使用。

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

Maven依赖(根据个人情况使用具体版本,我这儿因为框架关系,有默认版本,可不写)

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

 配置文件(yml为例)

management:
  endpoints:
    web:
      exposure:
        include: '*' # info, health, beans, env, metrics, mappings, scheduledtasks, threaddump,prometheus
      base-path: /actuator
  metrics:
    tags:
      application: ${artifactId} # 公共标签

全局标签

@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
  return registry -> registry.config().commonTags("application", applicationName);
}

 自定义指标代码示例(DemoTable是个实体,DemoTableService是个借口,DemoTableServiceImpl是个接口实现类,缺的东西要么自行创建,要么根据情况忽略掉也可以)

TestMetric.java(代码中对指标的阐释-摘抄自互联网,,,忘了哪儿了。。。)

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.capitek.cloud.sub.server.entity.DemoTable;
import com.capitek.cloud.sub.server.service.DemoTableService;
import io.micrometer.core.instrument.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class TestMetric {

    @Autowired
    private DemoTableService demoTableService;

    @Autowired
    private MeterRegistry meterRegistry;

    // 只增的指标
    // 以下4个counter指标名称相同,标签不同,根据标签可区分彼此
    public Counter boyJavaCounter;
    public Counter boyCPlusCounter;
    public Count
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值