在本文中,您将学习如何将 Spring Boot 执行器与名为 Prometheus 的监控系统和名为 Grafana 的图形解决方案集成。
在本文结束时,您将在本地计算机上设置 Prometheus 和 Grafana 仪表板,您将能够在其中可视化和监控从 Spring Boot 应用程序生成的所有指标。
###Prometheus
Prometheus 是一个开源监控系统,最初由 SoundCloud 构建。它由以下核心组件组成 -
一个数据抓取器,它以配置的时间间隔通过 HTTP 定期拉度量数据。
*
一个 time-series 数据库,用于存储所有指标数据。
*
一个简单的用户界面,您可以在其中可视化、查询和监控所有指标。
Grafana 允许您从各种数据源(如 Elasticsearch、Prometheus、Graphite、InfluxDB 等)中获取数据,并用漂亮的图表将它们可视化。
它还允许您根据指标数据设置警报规则。当警报更改状态时,它可以通过电子邮件、slack 或各种其他渠道通知您。
请注意,Prometheus 仪表板也有简单的图表。但是 Grafana 的图表要好得多。这就是为什么在这篇文章中,我们将 Grafana 与 Prometheus 集成以导入和可视化我们的指标数据。
Spring Boot 使用 Micrometer,一个应用程序指标外观将执行器指标与外部监控系统集成。
它支持多种监控系统,如 Netflix Atlas、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
以收集和导出 Prometheus 服务器可以抓取的格式的指标数据。
所有应用程序指标数据都可以在名为 /prometheus
的执行器端点上使用。 Prometheus 服务器可以定期抓取此端点以获取指标数据。
当 micrometer-registry-prometheus
依赖项在类路径上可用时,让我们探索 Spring Boot 公开的 prometheus
端点。
首先,您将开始在执行器端点发现页面 (http://localhost:8080/actuator
) 上看到 prometheus
端点 -
prometheus
端点以 Prometheus 服务器可以抓取的格式公开指标数据。您可以通过导航到 prometheus
端点 (http://localhost:8080/actuator/prometheus
) 来查看公开的指标数据 -
# HELP jvm_buffer_memory_used_bytes An estimate of the memory that the Java virtual machine is using for this buffer pool
# TYPE jvm_buffer_memory_used_bytes gauge
jvm_buffer_memory_used_bytes{id="direct",} 81920.0
jvm_buffer_memory_used_bytes{id="mapped",} 0.0
# HELP jvm_threads_live The current number of live threads including both daemon and non-daemon threads
# TYPE jvm_threads_live gauge
jvm_threads_live 23.0
# HELP tomcat_global_received_bytes_total
# TYPE tomcat_global_received_bytes_total counter
tomcat_global_received_bytes_total{name="http-nio-8080",} 0.0
# HELP jvm_gc_pause_seconds Time spent in GC pause
# TYPE jvm_gc_pause_seconds summary
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Allocation Failure",} 7.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Allocation Failure",} 0.232
jvm_gc_pause_seconds_count{action="end of minor GC",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of minor GC",cause="Metadata GC Threshold",} 0.01
jvm_gc_pause_seconds_count{action="end of major GC",cause="Metadata GC Threshold",} 1.0
jvm_gc_pause_seconds_sum{action="end of major GC",cause="Metadata GC Threshold",} 0.302
# HELP jvm_gc_pause_seconds_max Time spent in GC pause
# TYPE jvm_gc_pause_seconds_max gauge
jvm_gc_pause_seconds_max{action="end of minor GC",cause="Allocation Failure",} 0.0
jvm_gc_pause_seconds_max{action="end of minor GC",cause="Metadata GC Threshold",} 0.0
jvm_gc_pause_seconds_max{action="end of major GC",cause="Metadata GC Threshold",} 0.0
# HELP jvm_gc_live_data_size_bytes Size of old generation memory pool after a full GC
# TYPE jvm_gc_live_data_size_bytes gauge
jvm_gc_live_data_size_bytes 5.0657472E7
## More data ...... (Omitted for brevity)
您可以像这样使用 docker pull
命令下载 Prometheus docker 镜像 -
$ docker pull prom/prometheus
下载图像后,您可以键入 docker image ls
命令以查看本地存在的图像列表 -
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest b82ef1f3aa07 5 days ago 119MB
接下来,我们需要配置 Prometheus 从 Spring Boot Actuator 的 /prometheus
端点抓取指标数据。
使用以下配置创建一个名为 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 文档中提供的基本配置文件的扩展。
在上面的配置文件中需要注意的最重要的事情是 scrape_configs
部分中的 spring-actuator
作业。
metrics_path
是 Actuator 的 prometheus
端点的路径。 targets
部分包含 Spring Boot 应用程序的 HOST 和 PORT。
请确保将 HOST_IP
替换为运行 Spring Boot 应用程序的机器的 IP 地址。请注意, localhost
在这里不起作用,因为我们将从 docker 容器连接到 HOST 机器。您必须指定网络 IP 地址。
最后,让我们使用 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 将在容器内启动 Prometheus 服务器。您可以使用以下命令查看所有容器的列表 -
$ 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
就是这样!您现在可以导航到 http://localhost:9090
以探索 Prometheus 仪表板。
您可以在 Expression
文本字段中输入 Prometheus 查询表达式,并可视化该查询的所有指标。
以下是我们 Spring Boot 应用程序指标的一些 Prometheus 图 -
你可以查看官方的 Prometheus 文档以了解有关 Prometheus Query Expressions
的更多信息。
键入以下命令以使用 Docker 下载和运行 Grafana -
$ docker run -d --name=grafana -p 3000:3000 grafana/grafana
上述命令将在 Docker 容器内启动 Grafana,并使其在主机上的端口 3000
上可用。
您可以输入 docker container ls
来查看 Docker 容器列表 -
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
cf9196b30d0d grafana/grafana "/run.sh" Less than a second ago Up 5 seconds 0.0.0.0:3000->3000/tcp grafana
e036eb20b8ad prom/prometheus "/bin/prometheus --c…" 16 minutes ago Up 16 minutes 0.0.0.0:9090->9090/tcp prometheus
就是这样!您现在可以导航到 http://localhost:3000
并使用默认用户名 admin
和密码 admin
登录 Grafana。
按照以下步骤从 Prometheus 导入指标并在 Grafana 上可视化它们:
您可以在 Github 上找到 Actuator Demo 应用程序的完整源代码。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.callicoder.com/spring-boot-actuator-metrics-monitoring-dashboard-prometheus-grafana/
内容来源于网络,如有侵权,请联系作者删除!