美文网首页
Prometheus+Grafana监控Springboot应用

Prometheus+Grafana监控Springboot应用

作者: 李小二的倔强 | 来源:发表于2021-07-19 16:18 被阅读0次

红尘浊浪两茫茫,忍辱柔和是妙方,从来硬弩弦先断,自古钢刀口易伤。人为贪财身先死,鸟为夺食命早亡。任你奸猾多取巧,难免荒郊土内藏。

1、简介

项目越做越发觉得,任何一个系统上线,运维监控都太重要了。关于Springboot微服务的监控,之前使用Springboot Admin监控你的微服务应用,这个方案可以实时监控并提供告警提醒功能,但不能记录历史数据,无法查看过去1小时或过去1天等运维情况。本文介绍Prometheus + Grafana的方法监控Springboot 2.X,实现美观漂亮的数据可视化。

2、配置SpringBoot应用

将Prometheus引入依赖如下:

<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

对于Springboot,要开启Actuator,并打开对应的Endpoint:

management.endpoints.web.exposure.include=*
# 或者
management.endpoints.web.exposure.include=prometheus

启动Springboot后,可以通过下面URL看能不能正确获取到监控数据:

localhost:8080/actuator/prometheus

3、Prometheus

Docker安装可参考链接
使用Docker启动Prometheus

# 拉取docker镜像
docker pull prom/prometheus

准备配置文件prometheus.yml:

scrape_configs:
# 可随意指定
- job_name: 'spring'
  # 多久采集一次数据
  scrape_interval: 15s
  # 采集时的超时时间
  scrape_timeout: 10s
  # 采集的路径
  metrics_path: '/actuator/prometheus'
  # 采集服务的地址,设置成Springboot应用所在服务器的具体地址
  static_configs:
  - targets: ['hostname:9000','hostname:8080']

启动docker实例:
下面这种可以设置参数

// --log.level=debug:日志以debug输出
// --storage.tsdb.retention.time=3d,数据保留3天
// --config.file=/etc/prometheus/prometheus.yml:这个其实是容器里面的默认路径,我也不知道为啥需要这个,反正我加了这个就解决了
docker run -d -p 9090:9090 -v /root/katy/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml --name prometheus  prom/prometheus:latest --log.level=debug --storage.tsdb.retention.time=3d --config.file=/etc/prometheus/prometheus.yml

作者用的如下这种方式:

# 端口为9090,指定配置文件
docker run -d -p 9090:9090 -v ~/temp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml

成功启动后,就可以打开网页查看了,并且能图形化展示,URL为http://localhost:9090/

image.png

如上图所示,打开网页后,随便选取一个对应的监控指标与参数,点击Execute就可以查看了。

4、Grafana

Grafana是一个开源的度量分析与可视化套件,纯JavaScript开发的前端工具,通过访问库(如InfluxDB),展示自定义报表、显示图表等。它的UI十分灵活,有丰富的插件和模板,功能强大。一般用在时序数据的监控方面。

4.1 Docker安装与启动
# 拉取镜像
docker pull grafana/grafana
# 运行实例
docker run -d -p 3000:3000 grafana/grafana

启动成功后,访问http://localhost:3000 检查是否成功,初始管理员账号密码为admin/admin

4.2配置 Prometheus 数据源

1).Date Sources--> 选择 Prometheus,配置 Name 和 URL
\color{red}{注意}\color{red}{Name}必须为首字母大写的 \color{red}{Prometheus},不能写成全小写的 \color{red}{prometheus}

image.png
image.png
image.png
image.png
2)、保存并测试 Prometheus 数据源
点击“Save & Test”,页面弹出“Data source is working”信息,表明 Prometheus 数据源是可用的。
image.png
3)、新建 Dashboard
左侧加号,Create Dashboard。 Add new empty panel
image.png
配置 Panel tile、Description。 选择 Prometheus 数据源
image.png
选择指标
image.png

5、Grafana 模板套用

能够获取数据后,就可以自定义数据可视化展示了。但如果自己一条指标一条指标的加,就会很麻烦。实际上,Grafana提供了许多优秀的模板,可以网页https://grafana.com/grafana/dashboards 查找。
本文使用Spring Boot 2.1 Statistics模板,导入方法如下:
点击+号 --> Import --> 输入模板链接或ID --> 点击Load

image.png

成功导入后,就能监控数据了,而且,界面真的很好看:


image.png

end.

相关文章

网友评论

      本文标题:Prometheus+Grafana监控Springboot应用

      本文链接:https://www.haomeiwen.com/subject/lcnvpltx.html