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主要特性: 多维数据模型,其中包含 ...
随机推荐
- 191010 python3分数划分ABC等级
# 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,# 60-89分之间的用B表示,60分以下的用C表示. while True: score = input(" ...
- VMware Linux系统克隆
系统克隆 网卡设备无法识别 解决克隆虚拟机后网卡设备无法识别启动问题的方法 一.故障问题 从vmware workstation中克隆(clone)了一个CentOS 6的虚拟机,启动之后发现网卡没有 ...
- .NET C#利用反射获取类文件以及其中的方法&属性 并获取类及方法上的特性
了解C#特性类并声明我们自己的特性类[AttributeTest]代码如下 using System; namespace AttributeTest { /* 特性说明 特性本质是一个继承和使用了系 ...
- 《BUG创造队》作业9:【Beta】冲刺 Scrum meeting 3
项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验十三 团队作业9:Beta冲刺与团队项目验收 团队名称 BUG创造队 作业学习目标 (1)掌握软件黑盒测试技术:(2)学 ...
- bzoj1784: [Usaco2010 Jan]island
现在居然出现一道题只有\(pascal\)题解没有\(C++\)题解的情况,小蒟蒻要打破它. 思维题:分类讨论 回归正题,此题十分考验思维,首先我们要考虑如何把不会走的地方给填上,使最后只用求一遍这个 ...
- logstash-output-jdbc遇到connection is not available,request time out after 10000ms的问题解决
上一篇logstash-output-jdbc使用中提到“运行bin/logstash -f test.conf时可能提示注册插件失败”,通过分析详细的错误日志,发现其赫然写着“connection ...
- OAuth 2.0攻击
参考文章:https://www.yuque.com/pmiaowu/web_security_1/oauth 作者:PHPoop 关于OAuth2.0协议的授权流程可以参考下面的流程图: 1.Cli ...
- POJ - 3252 - Round Numbers(数位DP)
链接: https://vjudge.net/problem/POJ-3252 题意: The cows, as you know, have no fingers or thumbs and thu ...
- springcloud实践(二)之api网关:zuul
zuul是什么? front door. API Gateway.Zuul is a JVM based router and server side load balancer by Netflix ...
- 精妙的SQL语句
说明:复制表(只复制结构,源表名:a 新表名:b)select * into b from a where 1<>1 说明:拷贝表(拷贝数据,源表名:a 目标表名:b)insert i ...