本篇文章小编给大家分享一下Springboot添加jvm监控实现数据可视化代码示例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

1.简介

任何一个系统上线,运维监控都太重要了。利用Prometheus + Grafana的方法监控Springboot 2.X,实现美观漂亮的数据可视化。

2.添加监控

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

org.springframework.boot

spring-boot-starter-actuator

io.micrometer

micrometer-registry-prometheus

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

#prometheus配置

info:alen:alenmanagement:endpoints:web:base-path:/actuatorexposure:include:"*"

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

获取数据成功,说明Springboot能正常提供监控数据。

d1d88ee1df95e95549c85b2162f63b15.png

主要的端点

8dd6ea37e6f75ae11e410bc8fb4c8c50.png

3.配置Prometheus

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

特性

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

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

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

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

• 推送时间序列

通过中间网关得到支持

• 通过 服务发现 或 静态配置 来发现目标

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

• 支持分级和水平

federation

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

- job_name: 'actuator-ruoyi'

# metrics_path defaults to '/metrics'

# scheme defaults to 'http'.

metrics_path: '/actuator/prometheus'

static_configs:

- targets: ['10.42.95.4']

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

530018ab7b245e285c72ea6d7f1e8995.png

4.Grafana添加监控模版

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

dad939f628d031be0888fa33afbb9a77.png

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

6efa75b1298dfe9c587ba7723a68a76e.png

5.备注

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

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

问题二:

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

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

237c08784a4bfa11e5ee77044e3901dd.png

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐