欢迎来到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会自动配置一个PrometheusMeterRegistryCollectorRegistry来收集和输出格式化的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应用的HOSTPORT

请确保替换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使用
system-cpu-usage.png
  • API的延迟响应
response-latency.jpg

你可以从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数据源

data-source.png

建立一个仪表盘图表

![add-cpu-usage.png](https://upload-images.jianshu.io/upload_images/793918-2e46ebd23646f1f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

添加一个Prometheus查询

默认的可视化

dashboard.png

你可以在Github上看到完整的Actutator demo应用。

阅读第一部分:Spring Boot Actuator:健康检查、审计、统计和监控

更多阅读资源

翻译源

作者:alvin_wang
链接:https://www.jianshu.com/p/afc3759e75b9
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

Spring Boot Metrics监控之Prometheus&Grafana(转)的更多相关文章

  1. 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控

    由于最近在做监控方面的工作,因此也读了不少相关的经验分享.其中有这样一篇文章总结了一些基于Spring Boot的监控方案,因此翻译了一下,希望可以对大家有所帮助. 原文:Near real-time ...

  2. 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控--转

    原文地址:http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247483789&idx=1&sn=ae11f04780 ...

  3. Spring Boot Actuator监控使用详解

    在企业级应用中,学习了如何进行SpringBoot应用的功能开发,以及如何写单元测试.集成测试等还是不够的.在实际的软件开发中还需要:应用程序的监控和管理.SpringBoot的Actuator模块实 ...

  4. spring Boot(十九):使用Spring Boot Actuator监控应用

    spring Boot(十九):使用Spring Boot Actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台 ...

  5. 转载-Spring Boot应用监控实战

    概述 之前讲过Docker容器的可视化监控,即监控容器的运行情况,包括 CPU使用率.内存占用.网络状况以及磁盘空间等等一系列信息.同样利用SpringBoot作为微服务单元的实例化技术选型时,我们不 ...

  6. 使用 Spring Boot Admin 监控应用状态

    程序员优雅哥 SpringBoot 2.7 实战基础 - 11 - 使用 Spring Boot Admin 监控应用状态 1 Spring Boot Actuator Spring Boot Act ...

  7. java spring boot 开启监控信息

    效果: 配置 // pom <dependency> <groupId>org.springframework.boot</groupId> <artifac ...

  8. Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用

    Spring Boot (十): Spring Boot Admin 监控 Spring Boot 应用 1. 引言 在上一篇文章<Spring Boot (九): 微服务应用监控 Spring ...

  9. Prometheus + Spring Boot 应用监控

    1.  Prometheus是什么 Prometheus是一个具有活跃生态系统的开源系统监控和告警工具包.一言以蔽之,它是一套开源监控解决方案. Prometheus主要特性: 多维数据模型,其中包含 ...

随机推荐

  1. Alternative method of unlocking the bootloader - without code from HUAWEI

    原文:https://forum.xda-developers.com/p8lite/general/alternative-method-unlocking-bootloader-t3799294 ...

  2. Python itchat模块的使用,利用图灵机器人进行微信消息自动回复

    一.下载安装itchat模块 二.小实验:获取微信好友头像信息 这需要用itchat模块中的一个方法 itchat.get_friends()#获取微信所有微信好友信息 现在我们导入itchat,打印 ...

  3. Maven简易笔记

    Maven笔记 Maven笔记 Maven组成 安装配置 基本概念 Maven目录的典型结构 POM文件格式 GAV 依赖 依赖管理与父项目 关于父项目的一点主意事项 repository Maven ...

  4. 【实战1】记一次提至administrator权限历程

    本文首发于先知社区 https://xz.aliyun.com/t/5080 前言:这是一次挖掘cms通用漏洞时发现的网站,技术含量虽然不是很高,但是也拿出来和大家分享一下吧,希望能给一部分人带来收获 ...

  5. MPU-6050

    MPU-6000(6050)为全球首例整合性6轴运动处理组件,相较于多组件方案,免除了组合陀螺仪与加速器时间轴之差的问题,减少了大量的封装空间.当连接到三轴磁强计时,MPU-60X0提供完整的9轴运动 ...

  6. 《基于 Java EE 的高校重修管理系统设计与实现》论文笔记(九)

    标题:基于 Java EE 的高校重修管理系统设计与实现 一.基本信息 时间:2015 来源:河海大学文天学院 关键词::Java EE 架构: B/S 模式: 重修管理系统 二.研究内容 1.需求分 ...

  7. 《少年先疯队》第八次团队作业:Alpha冲刺1-5

    博文简要信息表: 项目 内容 软件工程 https://www.cnblogs.com/nwnu-daizh/ 本次实验链接地址 https://www.cnblogs.com/nwnu-daizh/ ...

  8. keras模块学习之层(layer)的使用-笔记

    本笔记由博客园-圆柱模板 博主整理笔记发布,转载需注明,谢谢合作! keras的层主要包括: 常用层(Core).卷积层(Convolutional).池化层(Pooling).局部连接层.递归层(R ...

  9. C# 7.0 中的新特性((.NET Framework 4.7 与 Visual Studio 2017 ))

    C#7.0 于 2017年3月 随 .NET 4.7 和 VS2017 发布. 一. out 变量(out variables) 以前我们使用out变量必须在使用前进行声明,C# 7.0 给我们提供了 ...

  10. 洛谷 P1966 火柴排队 题解

    归并排序 很玄学的一道题目,用另类的方法求出逆序对的数量就可以AC 我的思路是这样的: 按照题目,输入数据用两个数组a,b储存, 同时,用另外两个数组c,d分别对应前面两个a,b储存, 就是前面两个的 ...