Spring Boot Metrics监控之Prometheus&Grafana(转)
欢迎来到Spring Boot Actuator教程系列的第二部分。在第一部分中,你学习到了spring-boot-actuator
模块做了什么,如何配置spring boot应用以及如何与各样的actuator endpoints交互。
在这篇文章中,你将学习sprint boot如何整合外部监控系统Prometheus和图表解决方案Grafana。
在这篇文章的末尾,你将在自己本地电脑上建立一个Prometheus和Grafana仪表盘,用来可视化监控Spring Boot应用产生的所有metrics。
Prometheus
Prometheus是一个开源的监控系统,起源于SoundCloud。它由以下几个核心组件构成:
- 数据爬虫:根据配置的时间定期的通过HTTP抓去metrics数据。
- time-series 数据库:存储所有的metrics数据。
- 简单的用户交互接口:可视化、查询和监控所有的metrics。
Grafana
Grafana使你能够把来自不同数据源比如Elasticsearch, Prometheus, Graphite, influxDB等多样的数据以绚丽的图标展示出来。
它也能基于你的metrics数据发出告警。当一个告警状态改变时,它能通知你通过email,slack或者其他途径。
值得注意的是,Prometheus仪表盘也有简单的图标。但是Grafana的图表表现的更好。这也是为什么,在这篇文章中,我们将整合Grafana和Pormetheus来可视化metrics数据。
增加Micrometer Prometheus Registry到你的Spring Boot应用
Spring Boot使用Micrometer,一个应用metrics组件,将actuator metrics整合到外部监控系统中。
它支持很多种监控系统,比如Netflix Atalas, AWS Cloudwatch, Datadog, InfluxData, SignalFx, Graphite, Wavefront和Prometheus等。
为了整合Prometheus,你需要增加micrometer-registry-prometheus
依赖:
<!-- Micrometer Prometheus registry -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
一旦你增加上述的依赖,Spring Boot会自动配置一个PrometheusMeterRegistry
和CollectorRegistry
来收集和输出格式化的metrics数据,使得Prometheus服务器可以爬取。
所有应用的metrics数据是根据一个叫/prometheus
的endpoint来设置是否可用。Prometheus服务器可以周期性的爬取这个endpoint来获取metrics数据。
解析Spring Boot Actuator的/prometheus Endpoint
首先,你可以通过actuator endpoint-discovery页面(http://localhost:8080/actuator)来看一下prometheus
endpoint。
"prometheus": {
"href": "http://127.0.0.1:8080/actuator/prometheus",
"templated": false
}
prometheus
endpoint暴露了格式化的metrics数据给Prometheus服务器。你可以通过prometheus
endpoint(http://localhost:8080/actuator/prometheus)看到被暴露的metrics数据:
# HELP jvm_memory_committed_bytes The amount of memory in bytes that is committed for the Java virtual machine to use
# TYPE jvm_memory_committed_bytes gauge
jvm_memory_committed_bytes{area="nonheap",id="Code Cache",} 9830400.0
jvm_memory_committed_bytes{area="nonheap",id="Metaspace",} 4.3032576E7
jvm_memory_committed_bytes{area="nonheap",id="Compressed Class Space",} 6070272.0
jvm_memory_committed_bytes{area="heap",id="PS Eden Space",} 2.63192576E8
jvm_memory_committed_bytes{area="heap",id="PS Survivor Space",} 1.2058624E7
jvm_memory_committed_bytes{area="heap",id="PS Old Gen",} 1.96608E8
# HELP logback_events_total Number of error level events that made it to the logs
# TYPE logback_events_total counter
logback_events_total{level="error",} 0.0
logback_events_total{level="warn",} 0.0
logback_events_total{level="info",} 42.0
logback_events_total{level="debug",} 0.0
logback_events_total{level="trace",} 0.0
...
使用Docker下载和运行Prometheus
下载Prometheus
你可以使用docker pull
命令来下载Prometheus docker image。
$ docker pull prom/prometheus
一旦这个image被下载下来,你可以使用docker image ls
命令来查看本地的image列表:
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest b82ef1f3aa07 5 days ago 119MB
Prometheus配置(prometheus.yml)
接下来,我们需要配置Prometheus来抓取Spring Boot Actuator的/prometheus
endpoint中的metrics数据。
创建一个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).
# 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: ['127.0.0.1:9090']
- job_name: 'spring-actuator'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['HOST_IP:8080']
在Prometheus文档中,上面的配置文件是basic configuration file的扩展。
上面中比较重要的配置项是spring-actuator
job中的scrape_configs
选项。
metrics_path
是Actuator中prometheus
endpoint中的路径。targes
包含了Spring Boot应用的HOST
和PORT
。
请确保替换HOST_IP
为你Spring Boot应用运行的电脑的IP地址。值得注意的是,localhost
将不起作用,因为我们将从docker container中连接HOST机器。你必须设置网络IP地址。
使用Docker运行Prometheus
最后,让我们在Docker中运行Prometheus。使用以下命令来启动一个Prometheus服务器。
$ docker run -d --name=prometheus -p 9090:9090 -v <PATH_TO_prometheus.yml_FILE>:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
请确保替换<PATH_TO_prometheus.yml_FILE>为你在上面创建的Prometheus配置文件的保存的路径。
在运行上述命令之后,docker将在container中启动一个Prometheus服务器。你可以通过以下命令看到所有的container:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e036eb20b8ad prom/prometheus "/bin/prometheus --c…" 4 minutes ago Up 4 minutes 0.0.0.0:9090->9090/tcp prometheus
在Prometheus仪表盘中可视化Spring Boot Metrics
你可以通过访问http://localhost:9090访问Prometheus仪表盘。你可以通过Prometheus查询表达式来查询metrics。
下面是一些例子:
- 系统CPU使用

- API的延迟响应

你可以从Prometheus官方文档中学习更多的 Prometheus Query Expressions
。
使用Docker下载和运行Grafana
使用以下命令可以使Docker下载和运行Grafana:
$ docker run -d --name=grafana -p 3000:3000 grafana/grafana
上述命令将在Docker Container中开启一个Grafana,并且使用3000端口在主机上提供服务。
你可以使用docker container ls
来查看Docker container列表:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
939dd22a7179 quay.io/prometheus/prometheus "/bin/prometheus --c…" 14 minutes ago Up 14 minutes 0.0.0.0:9090->9090/tcp vigilant_neumann
1f94c46bcf5c grafana/grafana "/run.sh" 22 hours ago Up 22 hours 0.0.0.0:3000->3000/tcp grafana
你可以访问http://localhost:3000,并且使用默认的账户名(admin)密码(admin)来登录Grafana。
配置Grafana导入Prometheus中的metrics数据
通过以下几步导入Prometheus中的metrics数据并且在Grafana上可视化。
在Grafana上增加Prometheus数据源

建立一个仪表盘图表

添加一个Prometheus查询
默认的可视化

你可以在Github上看到完整的Actutator demo应用。
阅读第一部分:Spring Boot Actuator:健康检查、审计、统计和监控。
更多阅读资源
翻译源
作者:alvin_wang
链接:https://www.jianshu.com/p/afc3759e75b9
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Spring Boot Metrics监控之Prometheus&Grafana(转)的更多相关文章
- 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控
由于最近在做监控方面的工作,因此也读了不少相关的经验分享.其中有这样一篇文章总结了一些基于Spring Boot的监控方案,因此翻译了一下,希望可以对大家有所帮助. 原文:Near real-time ...
- 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控--转
原文地址:http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247483789&idx=1&sn=ae11f04780 ...
- Spring Boot Actuator监控使用详解
在企业级应用中,学习了如何进行SpringBoot应用的功能开发,以及如何写单元测试.集成测试等还是不够的.在实际的软件开发中还需要:应用程序的监控和管理.SpringBoot的Actuator模块实 ...
- spring Boot(十九):使用Spring Boot Actuator监控应用
spring Boot(十九):使用Spring Boot Actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台 ...
- 转载-Spring Boot应用监控实战
概述 之前讲过Docker容器的可视化监控,即监控容器的运行情况,包括 CPU使用率.内存占用.网络状况以及磁盘空间等等一系列信息.同样利用SpringBoot作为微服务单元的实例化技术选型时,我们不 ...
- 使用 Spring Boot Admin 监控应用状态
程序员优雅哥 SpringBoot 2.7 实战基础 - 11 - 使用 Spring Boot Admin 监控应用状态 1 Spring Boot Actuator Spring Boot Act ...
- java spring boot 开启监控信息
效果: 配置 // pom <dependency> <groupId>org.springframework.boot</groupId> <artifac ...
- Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用
Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用 1. 引言 在上一篇文章<Spring Boot (九): 微服务应用监控 Spring ...
- Prometheus + Spring Boot 应用监控
1. Prometheus是什么 Prometheus是一个具有活跃生态系统的开源系统监控和告警工具包.一言以蔽之,它是一套开源监控解决方案. Prometheus主要特性: 多维数据模型,其中包含 ...
随机推荐
- Surface 系统恢复
Surface Pro 6超详细教程之下载Surface 系统恢复镜像并制作系统恢复U盘 https://www.jianshu.com/p/d1b41d913f91 怎样创建Windows 10系统 ...
- ServicePointManager 类
地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.servicepointmanager?redirectedfrom=MSDN&am ...
- 在springboot项目中使用swaggerui
在pom.xml文件中配置(用的2.6.1版本,2.9.2有点丑) <properties> <!--<spring.swagger2.version>2.9.2< ...
- HtmlAgilityPack解析html文档
一.概述 HtmlAgilityPack(以下简称HAP)是一个基于.Net的.第三方免费开源的微型类库,主要用于在服务器端解析html文档. HtmlAgilityPack为网页提供了标准的DOM ...
- Django上传文件和修改date格式
上传大文件的时候: 修改date数据:
- 题解 UVa11388
题目大意 \(T\) 组数据,每组数据给定两个整数 \(G,L\),输出数对 \(x,y\) 满足 \(GCD(x,y)=G,LCM(x,y)=L\) 且 \(x\) 最小.若无解则输出 \(-1\) ...
- Java设计模式的6大原则
Java设计模式的6大原则 1.开闭原则(Open Close Principle) 开闭原则就是说对扩展开放,对修改关闭.在程序需要进行拓展的时候,不能去修改原有的代码,实现一个热插拔的效果.简单来 ...
- H3CNE学习1 课程简介
一.认证对比 二.企业网架构
- S1_搭建分布式OpenStack集群_01 准备虚拟机
Openstack版本:openstack-queen 版本 一.环境准备 网络规划: Management + API Network:10.10.11.0/24 eth1 网桥:br1 VM ...
- 开始编写Makefile
1.Makefile 的编写规则一 目标列表:关联性列表 命令列表 目标列表:可以是多个以空格隔开多个目标文件 关联列表页称为先决条件:同样是用个或多个空格分开的目标文件 命令列表:用<tab& ...