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

      切换 大屏

相关推荐
jerryxiaosa14 分钟前
能源管理系统多设备对接时,业务层如何做到无感调用?ems4j 的实现思路
spring boot
数据知道16 分钟前
MongoDB性能监控仪表板:Grafana+Prometheus集成实战
mongodb·grafana·prometheus
李白的粉38 分钟前
基于springboot的论坛网站
java·spring boot·毕业设计·课程设计·论坛网站
Hvitur44 分钟前
eclipse新建SpringBoot项目
java·spring boot·eclipse
最初的↘那颗心1 小时前
Spring AI Alibaba 多模态全家桶:图片理解、图片生成与语音合成实战
spring boot·大模型·多模态·通义千问·spring ai
**蓝桉**1 小时前
Prometheus时间出现误差
linux·运维·prometheus
码喽7号1 小时前
Springboot学习五:MybatisPlus的快速上手
spring boot·学习·spring
Knight_AL1 小时前
为什么要用 ApplicationReadyEvent 来初始化 RabbitTemplate 回调?
spring boot
熙胤2 小时前
springboot与springcloud对应版本
java·spring boot·spring cloud
J2虾虾2 小时前
SpringBoot 中给 @Autowired 搭配 @Lazy
java·spring boot·后端