指标监控:Prometheus 结合 Grafana,监控redis、mysql、springboot程序等等

发布于:2025-05-01 ⋅ 阅读:(28) ⋅ 点赞:(0)

软件作用说明

  • Prometheus‌:采集各种指标数据(如CPU、内存、请求数),并存储到时序数据库中。
  • Grafana‌:数据可视化,生成监控仪表盘。
     

架构说明

被监控服务(如Redis/MySQL/SpringBoot)
    │
    ▼
[Exporter](将服务数据转为Prometheus格式,暴露HTTP端点)
    │
    ▼
Prometheus(定时拉取Exporter/应用端点数据,存储到TSDB时序数据库)
    │
    ▼
Grafana(连接Prometheus,通过SQL类查询语言PromQL获取数据,导入或自己建仪表盘,即可用图表展示数据)
Prometheus‌ 官网下载对应的Exporterspringboot需要开发Exporter
https://prometheus.io/download/
Grafana 社区提供了许多现成的仪表盘模板,地址如下
https://grafana.com/grafana/dashboards


示例,从redis、mysql、springboot中拉取指标数据,到Grafana 展示


环境说明
redis、mysql、springboot 运行在win11上,IP为 192.168.1.102

Prometheus、Grafana 运行在 linux上,,IP为 192.168.83.16

1-a: redis、mysql 下载对应的 Exporter, 并运行

my.cnf内容如下:
[client]
user=root
password=123456
host=192.168.1.102
port=3306

运行以下命令
.\mysqld_exporter.exe --config.my-cnf="C:\my.cnf"

redis_exporter.exe --redis.addr=redis://localhost:6379 --redis.password=123456 --web.listen-address=:9121

1-b springboot 开发Exporter 如下

<!-- Spring Boot Actuator -->
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<!-- Micrometer Prometheus Registry -->
<dependency>
     <groupId>io.micrometer</groupId>
     <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

management:
  endpoints:
    web:
      exposure:
        include: '*' # 暴露所有端点
  endpoint:
    health:
      show-details: always
  metrics:
    tags:
      application: ${spring.application.name} # 自定义标签

2  Prometheus、Grafana用docker安装及指标数据抓取配置

目录结构如下
[root@node prometheus_grafana]# ls
docker-compose.yml
grafana_data
prometheus_data
prometheus.yml

[root@node prometheus_grafana]# pwd
/opt/prometheus_grafana
docker-compose.yml 内容如下
version: "3"
services:
  prometheus:
    image: prom/prometheus:latest
    container_name: "prometheus"
    ports:
      - "9090:9090"
    restart: always
    volumes:
      - "./prometheus.yml:/etc/prometheus/prometheus.yml"
      - "./prometheus_data:/prometheus"
    networks:
      - monitoring
      
  grafana:
    image: grafana/grafana:latest
    container_name: "grafana"
    ports:
      - "3000:3000"
    restart: always
    volumes:
      - "./grafana_data:/var/lib/grafana"
    networks:
      - monitoring      
         
networks:
  monitoring:
    driver: bridge
prometheus.yml 内容如下,指标抓取配置
global:
  scrape_interval:     15s # 默认抓取周期
  external_labels:
    monitor: 'codelab-monitor'
scrape_configs:
  - job_name: 'prometheus' # 抓取 Prometheus 自身的指标
    static_configs:
      - targets: ['192.168.83.16:9090']

  - job_name: 'redis'
    static_configs:
      - targets: ['192.168.1.102:9121'] # redis_exporter 的地址,抓取redis指标
      
  - job_name: 'mysql'
    static_configs:
      - targets: ['192.168.1.102:9104'] # mysqld_exporter 的地址
      
  - job_name: 'springboot-app'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['192.168.1.102:8080'] # Spring Boot 应用的地址
安装
创建网络
docker network create monitoring

docker network ls

chmod -R 777 /opt/prometheus_grafana/prometheus_data
chmod -R 777 /opt/prometheus_grafana/grafana_data

安装
docker compose up -d

3 检查Prometheus、Grafana,指标是否可正常收集

Prometheus 可以打开,且都是up状态,说明Prometheus 已经可以接收到指标数据
http://192.168.83.16:9090/targets

Grafana,默认用户名和密码都是 admin
http://192.168.83.16:3000/


4 Grafana 添加数据源,导入仪表盘ID,就可以使用了

添加数据源
登录后,点击左侧菜单中的 Configuration > Data Sources。点击 Add data source,选择 Prometheus。

导入仪表盘
点击左侧菜单中的 Dashboards > Import。输入仪表盘 ID(例如:763 监控redis),然后点击 Load。选择刚刚配置的 Prometheus 数据源,完成导入。


网站公告

今日签到

点亮在社区的每一天
去签到