背景
在開發(fā)中,使用 spring boot + netty 開發(fā)了一套tcp長連接的服務。在使用過程中驹吮,需要對這個服務進行指標監(jiān)控。
- grafana 是一個跨平臺的開源的度量分析和可視化工具
- prometheus 是 SoundCloud 開源監(jiān)控警告解決方案,存儲的是時序數(shù)據(jù)
- spring boot actuator 可以監(jiān)控和度量spring boot 應用程序
- micrometer 是java平臺上性能數(shù)據(jù)收集提供的一個通用api而芥,系統(tǒng)多種度量指標類型
maven 引用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--將actuator監(jiān)控的指標轉(zhuǎn)為prometheus格式-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.7.1</version>
</dependency>
修改 application.yaml 文件律罢,配置如下內(nèi)容, 開放所有的監(jiān)控指標
management:
endpoints:
web:
exposure:
include: "*"
驗證 prometheus 格式生效
運行程序,瀏覽器打開http://localhost:8080/actuator/prometheus
, 顯示如下內(nèi)容棍丐,表示 acuator 結(jié)合 prometheus生效
配置 Prometheus
Prometheus 是 Cloud Native Computing Foundation 項目之一误辑,是一個系統(tǒng)和服務監(jiān)控系統(tǒng)。它按給定的時間間隔從配置的目標收集指標歌逢。
通過Prometheus 來抓取
編輯 prometheus.yml
scrape_configs:
- job_name: 'actuator-springboot'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080']
這里的localhost:8080就是本地啟動的服務地址巾钉,也是prometheus要監(jiān)控的服務地址
docker 運行 Prometheus + Grafana
docker-compose.yaml 內(nèi)容如下
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 里能看到我們的監(jiān)控任務
異常信息正常后秘案,Prometheus現(xiàn)在已經(jīng)可以正常監(jiān)控到應用的JVM信息了
配置Grafana
添加prometheus 數(shù)據(jù)源
這里我們要添加的就是上面的Prometheus數(shù)據(jù)源
添加監(jiān)控頁面
導入監(jiān)控JVM的Dashboard模板砰苍,編號是 4701