欢迎来到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使用
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. centos7小命令

    修改时区:timedate [root@centos2 ~]# timedatectl set-timezone Asia/Shanghai 修改语言:localectl [root@centos2 ...

  2. ISCC之msc1(签到题)

    V2VsbCBkb25lIQoKIEZsYWc6IElTQ0N7TjBfMG5lX2Nhbl9zdDBwX3kwdX0K Flag: ISCC{N0_0ne_can_st0p_y0u} 签到题 八进制 ...

  3. String/StringBuffer

    1. 将String中的空格替换成 %20 public class ReplaceBlank { public static void main(String[] args) { String st ...

  4. 用java刷剑指offer(平衡二叉树)

    题目描述 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 牛客网链接 java代码 import java.lang.Math; public class Solution { public bool ...

  5. Union-Find(并查集): Union-Find Application

    Union-find 可以应用在很多方面 之前我们看到了union-find在dynamic connectivity上的应用,接下来介绍它在percolation上的应用. union-find在K ...

  6. KVM虚拟机,如何设置虚拟机的CPU型号与物理机是一样的

    1.在kvm主机上修改配置文件 [root@node160 ~]# virsh edit CentOS-7.3-X86_64 将xml配置文件中的: <cpu mode='custom' mat ...

  7. mysql跨表删除多条记录

    Mysql可以在一个sql语句中同时删除多表记录,也可以根据多个表之间的关系来删除某一个表中的记录. 假定我们有两张表:Product表和ProductPrice表.前者存在Product的基本信息, ...

  8. Stone Game

    Description There is a stone game.At the beginning of the game the player picks n piles of stones in ...

  9. Tensorflow细节-P62-完整的神经网络样例程序

    这样是比较好的一个summary命名 (1)'networks'.'layer_%d' % n_layer.'weights'三个命名空间相互叠加 (2) if i % 50 == 0: result ...

  10. WSAStartup() - 使用方法

    当一个应用程序调用WSAStartup函数时, 操作系统根据请求的Socket版本来搜索相应的Socket库,然后绑定找到的Socket库到该应用程序中. 以后应用程序就可以调用所请求的Socket库 ...