解锁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等)来展示数据。

      切换 大屏

相关推荐
java水泥工3 分钟前
旅游管理系统|基于SpringBoot和Vue的旅游管理系统(源码+数据库+文档)
spring boot·vue·计算机毕业设计·java毕业设计·旅游管理系统
计算机学长felix32 分钟前
基于SpringBoot的“基于数据安全的旅游民宿租赁系统”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·旅游
Q_Q51100828510 小时前
python+uniapp基于微信小程序的旅游信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
Q_Q51100828511 小时前
python基于web的汽车班车车票管理系统/火车票预订系统/高铁预定系统 可在线选座
spring boot·python·django·flask·node.js·汽车·php
DokiDoki之父12 小时前
MyBatis—增删查改操作
java·spring boot·mybatis
摇滚侠13 小时前
Spring Boot 项目, idea 控制台日志设置彩色
java·spring boot·intellij-idea
运维栈记15 小时前
使用Grafana监控K8S中的异常Pod
docker·kubernetes·grafana
Code blocks15 小时前
GB28181视频服务wvp部署(一)
java·spring boot·后端
我命由我1234516 小时前
Spring Boot - Spring Boot 静态资源延迟响应(使用拦截器、使用过滤器、使用 ResourceResolver)
java·spring boot·后端·spring·java-ee·intellij-idea·intellij idea