Prometheus+Grafana 监控搭建

Prometheus+Grafana 监控搭建

Prometheus 是使用 Golang 开发的,安装和运行都非常简单,只需直接运行可执行文件即可。个人认为,只要理解了 Prometheus 的架构图,整体概念就会变得非常清晰。

Prometheus 做的挺成熟,使用非常简单,主要是想自己记录一下,方便后续记忆。

1、Prometheus 架构图

image

  • Prometheus server : Prometheus 的核心组件,负责收集监控数据并提供 HTTP 接口用于查询和访问这些数据。
  • Alertmanager : 告警模块。
  • Pushgateway : Prometheus 采用 PULL 模式收集数据,这种模式需要在配置文件中预先配置数据源的地址信息。对于某些任务,其地址可能无法提前预知,需要任务主动上报数据。Pushgateway 作为一个中转组件,可以接收这些任务上报的数据,只需在 Prometheus 的配置中添加对 Pushgateway 的配置即可完成数据的抓取。
  • Prometheus web UI : 使用 PromQL 查询提供可视化服务。

2、安装配置 Prometheus

2.1、安装

下载地址 https://prometheus.io/download/

wget --no-check-certificate https://github.com/prometheus/prometheus/releases/download/v3.1.0/prometheus-3.1.0.linux-amd64.tar.gz

解压

tar -zxvf prometheus-3.1.0.linux-amd64.tar.gz

运行

./prometheus-3.1.0.linux-amd64/prometheus

2.2、配置

配置文件在解压目录中:prometheus.yml。内容如下:

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label job=<job_name> to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

这里先不做任何配置,默认 Prometheus 对自己的数据进行了采集,后续涉及到再进行配置。

2、安装 Grafana

基本上也不用说,下载下来运行就可以了。运行后端口为:3000,首次访问用户名密码均为:admin 。添加数据源为 Prometheus 输入地址信息保存即可。

3、监控 Spring Boot 应用

关于这一块文档说明:https://docs.spring.io/spring-boot/docs/2.7.18/reference/html/actuator.html

3.1、添加依赖

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

3.2、配置暴露 endpoints

编辑 application.yml

management:
  endpoints:
    web:
      exposure:
        include: "prometheus"
  metrics:
    tags:
      application: ${spring.application.name}

3.3、对于某些重点接口单独监控

有一些重要交易接口想单独监控指标,那么可以使用 io.micrometer.core.annotation.Timed 注解进行修饰,例如:@Timed("order.create") 那么则会生成对应的指标信息,如下:

# HELP order_create_seconds Duration of HTTP server request handling
# TYPE order_create_seconds summary
order_create_seconds_count{application="learn-springboot",exception="None",method="GET",outcome="SUCCESS",status="200",uri="/order/create",} 1.0
order_create_seconds_sum{application="learn-springboot",exception="None",method="GET",outcome="SUCCESS",status="200",uri="/order/create",} 0.0320229
# HELP order_create_seconds_max Duration of HTTP server request handling
# TYPE order_create_seconds_max gauge
order_create_seconds_max{application="learn-springboot",exception="None",method="GET",outcome="SUCCESS",status="200",uri="/order/create",} 0.0320229

这样就可以在 Grafana 中配置指标进行检测了。例如:

每分钟QPS
rate(http_server_requests_seconds_count[1m])

每分钟平均响应时长
rate(http_server_requests_seconds_sum[1m])/rate(http_server_requests_seconds_count[1m])

3.4、配置 Prometheus PULL 数据

编辑 prometheus.yml 配置文件,加入一下内容:

scrape_configs:
  - job_name: "learn-springboot"
    scrape_interval: 10s
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["127.0.0.1:8081"]
  • scrape_interval 10s 表示 10s 秒拉取一次数据,如果不配置就会使用 global 中配置项。
posted @   半夜起来撸代码  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 2 本地部署DeepSeek模型构建本地知识库+联网搜索详细步骤
点击右上角即可分享
微信分享提示