解锁Spring Boot、Prometheus与Grafana三合一:打造你的专属自定义指标炫酷大屏!

1. 集成Prometheus到Spring Boot

需要在Spring Boot应用中集成Prometheus。可以通过micrometer库来实现。micrometer是一个应用程序监控库,它支持多种监控系统,包括Prometheus。

一招制胜!Spring Boot、Prometheus和Grafana三剑合璧,称霸监控领域!

2. 编写自定义指标

需求: 统计接口访问次数

java 复制代码
/**
 * 功能描述: prometheus:自定义指标
 * 功能需求:
 * 来监控接口请求次数
 * 实际项目项目中,使用AOP,或拦截器方式统计接口的请求信息次数更为优雅
 *
 * @author Songxianyang
 * @date 2024-08-13 10:00
 */
@Slf4j
@RestController
@RequestMapping("/custom-metrics")
@RequiredArgsConstructor
@Api(tags = "自定义指标")
public class CustomMetricsController {
    private final MeterRegistry meterRegistry;

    public static final String METRICS = "metrics.kkxx-poi.request.count";

    /**
     * 详情接口请求统计
     */
    @ApiOperation("详情接口")
    @GetMapping("/get/{id}")
    public String get(@PathVariable("id") String id) {
        Counter.builder(METRICS).tags("apiCode", "get").register(meterRegistry).increment();
        return "详情接口请求统计id:" + id;
    }

    /**
     * 列表接口请求统计
     */
    @ApiOperation("列表接口")
    @PostMapping("/list")
    public String list() {
        Counter.builder(METRICS).tags("apiCode", "list").register(meterRegistry).increment();
        return "列表接口请求统计";
    }
}

可以通过使用AOP来实现自定义指标,可以更优雅地为Spring Boot应用中的方法提供监控支持,并且这些指标可以在Prometheus和Grafana中进行可视化展示。

2. 在Prometheus中调试展示指标

  1. 点击 Graph
  2. 输入 metrics_kkxx_poi_request_count_total 点击 Execute
  1. 点击 Graph

  2. 接口请求次数

4. 在Grafana中展示指标

  1. 创建仪表盘 :

    • 创建一个新的仪表盘(Dashboard)。
    • 添加一个新的面板(Panel)。
    • 在面板的查询部分输入你的自定义指标名称,例如metrics_kkxx_poi_request_count_total
    • 选择一个合适的图表类型(如Graph、Stat等)来展示数据。

      切换 大屏

相关推荐
indexsunny10 分钟前
互联网大厂Java面试实战:从Spring Boot到微服务的技术问答解析
java·spring boot·redis·微服务·消息队列·电商
希望永不加班1 小时前
SpringBoot 过滤器(Filter)与请求链路梳理
java·spring boot·后端·spring
jiankeljx2 小时前
Java实战:Spring Boot application.yml配置文件详解
java·网络·spring boot
Zzxy2 小时前
Spring Boot 参数校验
java·spring boot
Zzxy4 小时前
Spring Boot集成Swagger/Knife4j
spring boot·swagger
IT界的老黄牛4 小时前
Prometheus + Grafana + AlertManager 监控体系搭建:Docker 一把梭
运维·grafana·prometheus
用户8307196840826 小时前
Spring Boot 中Servlet、Filter、Listener 四种注册方式全解析
java·spring boot
xixingzhe26 小时前
spring boot druid 10秒超时问题
java·数据库·spring boot
毕业设计-小慧6 小时前
计算机毕业设计springboot城市休闲垂钓园管理系统 基于Spring Boot的都市休闲垂钓基地数字化运营平台 城市智慧钓场综合服务管理平台
spring boot·后端·课程设计
csdn2015_9 小时前
springboot controller 参数可以是List吗
spring boot·后端·list