使用Prometheus和Grafana搭建SpringBoot应用监控系统
最近公司项目介绍的时候看到了类似监控系统的展示页面,比如资源利用、GC次数、Kafka生产消费等,清晰明了,页面十分酷炫。"这是怎么实现的呢??"
原来是Grafana!!这么好的东西,竟然现在才看到。
简介:Grafana是一款Go语言开发的开源数据可视化工具,可以做数据监控和数据统计,还提供了很多"仪表板",可以实现很多炫酷且实用的可视化指标。
技术点:Spring boot actuator/micrometer(收集)、Prometheus(存储聚合)、Grafana(可视化)
个人工作中目前应该不涉及到这方面的开发,所以暂时不做深究,先只是满足下好奇心,简单记录下基本的使用过程,更多细节原理,大家可自行了解。
推荐这篇参考文章:https://www.cnblogs.com/throwable/p/13257557.html,有关于度量指标框架Micrometer的详细介绍。核心思路
以spring boot项目为例,Spring Boot Actuator提供了很多监控指标,可以通过RestFul的形式访问查看,但原始的为json数据,而非上图展示的页面。并且只是瞬时值,不能提供段时间内数据的的聚合分析等。当然这些也可以自行通过数据持久化,并开发前端页面的方式实现。而通过了解,目前已有这样的轮子,就是Prometheus,内部实现时序数据库,可以将收集到的监控数据存储,并且可以结合Grafana进行数据可视化,目前Prometheus已经成为热门且通用的监控解决方案。
关键组件之Exporter:作用类似转换器,各种被监控的对象可以基于共同的Prometheus提供的规范进行实现,从而让自己都能够接入到Prometheus。详细原理可参考文章https://zhuanlan.zhihu.com/p/273229856新建spring boot 项目
添加依赖
添加actuator是支持输出监控信息,micrometer-registry-prometheus是能够把actuator监控信息,转化为prometheus能够处理的格式。 io.micrometer micrometer-registry-prometheus org.springframework.boot spring-boot-starter-actuator 修改application.yml配置文件
在配置文件中,配置 endpoint暴露Prometheus,并允许将指标 metrics导入到Prometheus中。 server: port: 10010 spring: application: # 应用名,后续会以此展示 name: Spring Boot Monitor # 监控相关配置 # 开启监控 management: endpoints: web: exposure: include: "*" # 端点 endpoint: prometheus: enabled: true health: show-details: always # 指标 metrics: export: prometheus: enabled: true添加指定的应用名
可以直接通过配置文件引用metrics: tags: application: ${spring.application.name}
也可通过启动类,注入package com.panda00hi.springbootmonitor; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication public class SpringBootMonitorApplication { public static void main(String[] args) { SpringApplication.run(SpringBootMonitorApplication.class, args); } /** * 注册应用名,后续监控中展示该应用名 * * @param applicationName 取配置文件中的应用名 */ @Bean MeterRegistryCustomizer configure(@Value("${spring.application.name}") String applicationName) { return (registry -> registry.config().commonTags("application", applicationName)); } }启动项目
访问http://localhost:10010/actuator,已经可以得到监控信息,并且包含prometheus的可以访问
访问http://localhost:10010/actuator/prometheus,也可以得到监控信息(prometheus格式的)
配置Prometheus
利用docker搭建。
参考官方文档:https://prometheus.io/docs/prometheus/latest/getting_started/下载镜像docker pull prom/prometheus配置文件
创建本地用于挂载的数据卷目录/mydata/prometheus,并新建配置文件。
参考官方的配置。最后配置自己的应用配置。global: scrape_interval: 15s # By default, scrape targets every 15 seconds. # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: "codelab-monitor" # 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=` to any timeseries scraped from this config. - job_name: "prometheus" # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ["localhost:9090"] # 额外添加的任务配置,每隔五秒钟抓取数据 - job_name: "springboot-prometheus" scrape_interval: 5s metrics_path: "/actuator/prometheus" static_configs: # spring boot服务运行的服务器地址。我是物理机运行的,填写的是本机地址 - targets: ["192.168.3.47:10010"] 启动容器docker run -d --name=prometheus -p 9090:9090 -v /mydata/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml -d prom/prometheus
浏览器访问prometheus默认地址http://172.16.224.100:9090,可以正常访问首页,http://172.16.224.100:9090/metrics可以获取到数据。
选择【status】-》【targets】,即可看到之前配置文件中配置的springboot信息。
配置Grafana下载镜像docker pull grafana/grafana启动容器docker run -d --name=grafana -p 3000:3000 grafana/grafana访问页面
默认端口是3000,默认用户名密码都是admin
172.16.224.100:3000
登陆成功,有那种感觉了,不得不说页面UI颜值真的很高。
添加prometheus数据源
添加数据源,选择prometheus,并配置URL
点击保存按钮,成功会提示
选择合适的仪表盘
官方提供了很多https://grafana.com/grafana/dashboards/
如,Grafana提供的JVM面板,记住右侧的编码。在自己的Grafana页面导入该编码即可。
导入编码
选择数据源prometheus,导入完成后即可看到效果。
艾菲尔丁安东尼奥教练让全队拧成绳,让国青全队无形中变默契直播吧3月16日讯3月15日晚间,结束U20亚洲杯的U20国青回到了国内。在接受天天体育采访时,队长艾菲尔丁谈到了这届赛事的感受。艾菲尔丁说其实大家都知道,看过我踢球的人都知道,我
刘国梁高升,12岁女儿学高尔夫,入选国家队,没有女承父业!最近国家队征战各类比赛的同时,刘国梁有个非常多的事情要忙碌,就在最近他获得重大的认可高升成为全国政协委员,不得不说,这次的任命对于刘国梁的重大肯定。还有一个让的大家欣喜的消息是刘国
王元丰ESG不是骗局,应得到重视来源环球时报ESG是环境社会和公司治理(Environmental,SocialandGovernance)的简称,是本世纪初联合国前秘书长科菲安南牵头发起并推动的企业更可持续框架
V观财报涉信披违规及违规减持ST奇信两股东被警示中新经纬3月15日电因信披违规及违规减持行为,ST奇信两持股5以上股东被江西证监局警示。15日盘后,ST奇信公告称,公司持股5以上股东新余高新区智大投资有限公司(以下简称新余智大投
新乡市外贸实现开门红前2个月进出口总值同比增长15。1来源新乡日报据新乡海关统计,今年前2个月,我市外贸进出口总值22。6亿元人民币,比去年同期增长15。1,比全国同期外贸进出口高出15。9个百分点,对外贸易实现良好开局。据新乡海关负
独家产品延期一年多未清算,长安信托被认定存两大问题界面新闻记者张晓云界面新闻编辑界面新闻获悉,其购买的长安信托发布稳健260号集合资金信托计划(下称长安信托稳健260号)在到期一年多后迟迟无法清算,而长安信托作为稳健260号受托方
为实体经济加油鼓劲采访金融界的代表委员,感受到他们对经济政策的关心对经济形势的关注,还有对实体经济的关爱。更好服务实体经济,越来越成为金融人的共识。经济是肌体,金融是血液。过去一年,我们继续引导金融
商务部前2个月中国外贸开局平稳展现出较强发展韧性光明网讯(记者赵艳艳)今年前两个月,我国货物进出口总额6。18万亿元人民币,与去年同期基本持平。3月16日,商务部召开例行新闻发布会,商务部新闻发言人束珏婷表示,2023年中国外贸
日本2月贸易逆差8977亿日元,连续19个月逆差(观察者网讯)共同社3月16日报道,日本财务省16日公布的2月贸易统计初值(以通关为准)显示,出口减去进口的贸易收支为逆差8977亿日元(约合人民币467亿元)。资源价格上涨和日元
当你啥都不是时,只能低着头,让别人说了算当你啥都不是时,只能低着头,让别人说了算。这是一种无奈的感觉,一种被束缚的感觉。在这个世界上,每个人都有自己的角色和位置,但有时候我们会发现自己似乎没有什么存在感,没有什么价值。或
人到中年,学会微笑前行作者子墨春日里,风景清亮透明,就如人生中平常的日子,无需奢华,更无需繁花似锦,只要简单舒适,明净温暖就好。忙忙碌碌的生活,我们总是忘记了自己该怎么样活着,猛然,看见花开,才知道春天