本文将详细介绍Prometheus和Grafana的快速搭建,并实现JVM、Mysql等实时监控。
本文将在Windows环境搭建Demo。
版权说明:本文由作者原创,转载请注明出处。
原文地址:https://blog.csdn.net/qq_38688267/article/details/108101708
文章目录
资源下载
Prometheus-2.20 下载
- 官网:https://prometheus.io/
- windows版:https://download.csdn.net/download/qq_38688267/12683264
- Linux版:https://download.csdn.net/download/qq_38688267/12728855
Grafana-7.1下载
- 官网:https://grafana.com/
- windows版:https://download.csdn.net/download/qq_38688267/12683258
- Linux版:https://download.csdn.net/download/qq_38688267/12728849
安装启动
Grafana安装启动
-
Windows:
Windows版安装启动很简单,下载完成双击安装即可,安装完成后会生成后台服务。 -
Linux:
1、解压:tar -xvzf grafana-7.1.3.linux-amd64.tar.gz
2、运行:/grafana-7.1.3/bin/grafana-server
默认端口:
3000
访问地址:localhost:3000
默认管理员账号密码:admin / admin
Prometheus安装启动
-
Windows:
下载完成后解压,根目录下双击运行prometheus.exe
即可 -
Linux:
1、解压:tar -xvzf grafana-7.1.3.linux-amd64.tar.gz
2、运行:/grafana-7.1.3/bin/grafana-server
默认端口:
9090
访问地址:localhost:9090
监控SpringBoot项目JVM
实现JVM监控非常简单。5步即可:
1、引入依赖
增加MAVEN依赖:
<!-- actuator健康监控依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<!-- JVM信息收集 -->
<dependency>
<groupId>io.github.mweirauch</groupId>
<artifactId>micrometer-jvm-extras</artifactId>
<version>0.2.0</version>
</dependency>
2、增加配置
在项目application.yml/application.properties中增加配置如下:
management:
endpoints:
web:
exposure:
# 监控所有信息,也可以根据需要选择
include: "*"
# 访问监控父路径,默认"/actuator",
base-path: /actuator
3、Prometheus配置并重启
修改Prometheus根目录下的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']
########## 以上为默认配置
########## 只需要增加下面的配置即可
########## 注意缩进和空格,如果修改配置文件后启动秒退,表示配置文件有问题!
# 任务名称
- job_name: 'sys-service'
# 注意这个指标路径,不要填错了
metrics_path: '/actuator/prometheus'
static_configs:
# 数据对象地址,就我们的JAVA应用地址
- targets: ['localhost:8080']
### ======修改完配置后记得重启哦~======= ###
4、检查是否生效
- 检查应用健康监控是否生效
访问 localhost:8080/actuator,查看是否正确返回数据。
如果应用有设置访问拦截之类的,记得要将/actuator/**
设置为白名单哦。
- 检查Prometheus监控是否生效
修改prometheus配置并重启完成后,进入普罗米修斯界面选择status
=>targets
,就可以看到他监控的对象,如果对象中出现了我们之前配置好的对象并且state=UP
表示监控成功。
如果这里我们配置的对象
state=DOWN
且ERROR="INVALID" is not a valid start token
的错误,这个错误是由于监控的数据格式不是Prometheus要求的,需要检查Prometheus的配置:- job_name下的metrice_path
属性是否正确,因为他默认值为/metrics
,但如果是用Actuator组件的话需要配置的路径为/actuator/prometheus
。
5、配置Grafana可视化
- 添加数据源
如果之前没有添加过数据源的话,在进入grafana后就会显示添加数据源的按钮。
如果已经添加过数据源则按下图添加我们之前配置的Prometheus的数据源。
- 增加图表
我们选择比较流行的可视化界面模板,如下图所示操作即可。
此处的4701
在后文的补充中有说明。
等待几秒就会出现如下图所示的表单,输入完信息后,点击Import
即可。
6、大功告成
炫酷的暗黑风格可视化图表就完成了~
还能利用Grafana的告警功能设置监控告警哦~
监控MySQL
TODO。。。
补充
Grafana可视化模板
为了减少不必要的工作,Grafana提供可视化配置模板导入的功能,也维护了官方模块库。
传送门:https://grafana.com/grafana/dashboards
我们在导入时,只需要输入模板的唯一ID即可,ID在详情页中有显示,根据详情页地址也能看出来。
同理,在主页我们根据浏览器的跳转路径提示也可以知道模板的ID。