Spring Boot Actuator+Prometheus + Grafana 监控JVM数据

背景

在开发中,使用 spring boot + netty 开发了一套tcp长连接的服务。在使用过程中,需要对这个服务进行指标监控。

  • grafana 是一个跨平台的开源的度量分析和可视化工具
  • prometheus 是 SoundCloud 开源监控警告解决方案,存储的是时序数据
  • spring boot actuator 可以监控和度量spring boot 应用程序
  • micrometer 是java平台上性能数据收集提供的一个通用api,系统多种度量指标类型

maven 引用


    org.springframework.boot
    spring-boot-starter-actuator




    io.micrometer
    micrometer-registry-prometheus
    1.7.1

修改 application.yaml 文件,配置如下内容, 开放所有的监控指标

management:
  endpoints:
    web:
      exposure:
        include: "*"

验证 prometheus 格式生效

运行程序,浏览器打开http://localhost:8080/actuator/prometheus, 显示如下内容,表示 acuator 结合 prometheus生效

image.png

配置 Prometheus

Prometheus 是 Cloud Native Computing Foundation 项目之一,是一个系统和服务监控系统。它按给定的时间间隔从配置的目标收集指标。
通过Prometheus 来抓取

编辑 prometheus.yml

scrape_configs:
  - job_name: 'actuator-springboot'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8080']

这里的localhost:8080就是本地启动的服务地址,也是prometheus要监控的服务地址

docker 运行 Prometheus + Grafana

docker-compose.yaml 内容如下

version: '3'
services:
  grafana:
    container_name: grafana
    image: grafana/grafana
    environment:
      - TZ=Asia/Shanghai
    ports:
      - 3000:3000
    volumes:
      - ./grafanaplugin:/var/lib/grafana/plugins/grafanaplugin
    privileged: true
    restart: always
  prom:
    image: quay.io/prometheus/prometheus:latest
    volumes:
      # 将prometheus.yml 文件放在与docker-compose 同级目录
      - ./monitor/prometheus.yml:/etc/prometheus/prometheus.yml
    command: "--config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus"
    ports:
     - "23333:9090" 
    depends_on:
     - exporter
  exporter:
    image: prom/node-exporter:latest
    ports:
     - "19100:9100"

运行docker 容器 docker-compose up -d

验证 prometheus 采集

浏览器里打开 http://localhost:23333, 通过菜单,将页面切换到 Targets, 在targets 里能看到我们的监控任务

image.png

image.png

异常信息正常后,Prometheus现在已经可以正常监控到应用的JVM信息了

配置Grafana

添加prometheus 数据源

这里我们要添加的就是上面的Prometheus数据源


image.png
image.png
image.png
image.png

添加监控页面

导入监控JVM的Dashboard模板,编号是 4701

image.png

image.png

监控界面效果

image.png
image.png

你可能感兴趣的:(Spring Boot Actuator+Prometheus + Grafana 监控JVM数据)