15

Spring Boot 添加应用 JVM 监控,实现数据可视化

 4 years ago
source link: https://mp.weixin.qq.com/s/ZVlT2j8ndB6d6U2mpXE2Aw
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

1.简介

最近越发觉得,任何一个系统上线,运维监控都太重要了。本文介绍Prometheus + Grafana的方法监控Springboot 2.X,实现美观漂亮的数据可视化。

2.添加监控

Spring-boot-actuator module 可帮助您在将应用程序投入生产时监视和管理应用程序。您可以选择使用 HTTP 端点或 JMX 来管理和监控您的应用程序。Auditing, health, and metrics gathering 也可以自动应用于您的应用程序。引入依赖如下:

  1. <!--监控报警-->

  2. <dependency>

  3. <groupId>org.springframework.boot</groupId>

  4. <artifactId>spring-boot-starter-actuator</artifactId>

  5. </dependency>

  6. <dependency>

  7. <groupId>io.micrometer</groupId>

  8. <artifactId>micrometer-registry-prometheus</artifactId>

  9. </dependency>

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

  1. #prometheus配置

  2. info:

  3. alen:alen

  4. management:

  5. endpoints:

  6. web:

  7. base-path:/actuator

  8. exposure:

  9. include:"*"

启动Springboot后,可以通过下面URL看能不能正确获取到监控数据:localhost/actuator/prometheus 获取数据成功,说明Springboot能正常提供监控数据。

7ZziUn2.png!web

主要的端点

YVZ3YzJ.png!web

3.配置Prometheus

Prometheus 是 Cloud Native Computing Foundation 项目之一,是一个系统和服务监控系统。它按给定的时间间隔从配置的目标收集指标,评估规则表达式,显示结果,并且如果观察到某些条件为真,则可触发警报。

特性

  1. 多维度数据模型(由度量名称和键/值维度集定义的时间序列)

  2. 灵活的查询语言来利用这种维度

  3. 不依赖分布式存储;单个服务器节点是自治的

  4. 时间序列采集通过HTTP上的 pull model 发生

  5. 推送时间序列通过中间网关得到支持

  6. 通过服务发现静态配置来发现目标

  7. 多种模式的图形和仪表盘支持

  8. 支持分级和水平 federation

通过 Prometheus 来抓取数据 Prometheus 会按照配置的时间周期去 pull 暴露的端点(/actuator/prometheus)中的指标数据 prometheus.yml 配置

  1. - job_name:'actuator-ruoyi'

  2. # metrics_path defaults to '/metrics'

  3. # scheme defaults to 'http'.

  4. metrics_path:'/actuator/prometheus'

  5. static_configs:

  6. - targets:['10.42.95.4']

重然后再启动prometheus,访问prometheus网址,查看targets启动是否正常如下图

NZzaAzj.png!web

4.Grafana添加监控模版

默认启动后Grafana地址为:http://localhost:3000/,登录用户名和密码是admin/admin,import配置grafana id = 4701

R7JRJn3.png!web

最终的效果是这样子的,如下图

Y3uIB3J.png!web

5.备注

问题:添加上访问不了Endpoint?解决方案:原因是项目用的shiro,需要添加过滤

  1. filterChainDefinitionMap.put("/actuator/**","anon");

问题二:

  1. /O exception (java.io.IOException) caught when processing request to {}->unix://localhost:80:Broken pipe

解决方法:导致这个错误的原因是 project.artifactId 可能包含了大写。改成小写就行

u2uAfqe.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK