SpringBoot依赖之Spring Boot Admin(二)

本文核心:集成Prometheus 指标实现应用指标图表监控

作者语录:

我们一直在追逐需求和迭代的路上,却不曾回头看自己曾经的作品是否给自己留下经验和遗憾。技术永远没有边界,但个人必须对自己过往的行为买单,无论对与错,终究是个做个复盘。我们一直在被降本增效,我们自己也要考虑增效降本,技术人如何增效,简言之,就是释放自己成就他人。 释放自己,就需要"别人"替你去做事,无论是监控亦或是AI 还有可能是替身,最终的目的都是为了释放自己。何为成就他人?你把事做好了,他人就舒服了。那么现在作为开发者,你也可以去着手自己做独立的事情了-创业or do sth。

前文回顾

通过前文我们已经完成了,springboot整合Spring Boot Admin的应用监控功能。

SpringBoot依赖之Spring Boot Admin(一)

接下来我们逐步完成高阶功能,实现应用性能指标监控功能,这次我们先从Spring Boot 的 Micrometer 集成,Prometheus 可以采集、存储并可视化展示应用的性能指标(如 JVM 内存使用、HTTP 请求延迟、数据库连接池信息等)。结合 Spring Boot Admin的UI控制台,我们可以更直观地看到这些监控指标,并做出快速响应。

一、Prometheus 与 Spring Boot 的集成

为了让 Spring Boot 应用的性能指标能够被 Prometheus 采集,我们首先需要通过 Micrometer 启用 Prometheus 的 metrics 导出。

1.1 添加 Micrometer 和 Prometheus 依赖

在你的 Admin Client 项目中引入 Micrometer 和 Prometheus 依赖:

xml 复制代码
<dependencies>
    <!-- Micrometer Core 依赖 -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-core</artifactId>
    </dependency>
    
    <!-- Prometheus 依赖,用于导出数据 -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
</dependencies>
1.2 暴露 Prometheus 端点

在 Spring Boot 项目中,需要在 application.properties 中配置管理端点,确保 Prometheus 的 metrics 端点可以被访问:

指定暴露部分端点:

properties 复制代码
# 暴露 Prometheus 端点
management.endpoints.web.exposure.include=prometheus,health,info

指定暴露全部端点:

properties 复制代码
# 暴露 Prometheus 端点
management.endpoints.web.exposure.include=actuator,beans,caches,health,info,conditions,configprops,env,loggers,heapdump,threaddump,prometheus,metrics,sbom,scheduledtasks,mappings

或者直接暴露所有端点:

properties 复制代码
# 暴露 所有Prometheus 端点
management.endpoints.web.exposure.include=*

此配置将允许外部系统(如 Prometheus、Grafana)通过 /actuator/prometheus 端点访问应用的性能指标。这里我我们配置了所有,

1.3 验证 /actuator/prometheus 端点 Prometheus 指标

访问:
http://localhost:8083/actuator

json 复制代码
{
    "_links": {
        "self": {
            "href": "http://localhost:8083/actuator",
            "templated": false
        },
        "beans": {
            "href": "http://localhost:8083/actuator/beans",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8083/actuator/health/{*path}",
            "templated": true
        },
        "health": {
            "href": "http://localhost:8083/actuator/health",
            "templated": false
        },
        "info": {
            "href": "http://localhost:8083/actuator/info",
            "templated": false
        },
        "conditions": {
            "href": "http://localhost:8083/actuator/conditions",
            "templated": false
        },
        "configprops-prefix": {
            "href": "http://localhost:8083/actuator/configprops/{prefix}",
            "templated": true
        },
        "configprops": {
            "href": "http://localhost:8083/actuator/configprops",
            "templated": false
        },
        "env": {
            "href": "http://localhost:8083/actuator/env",
            "templated": false
        },
        "env-toMatch": {
            "href": "http://localhost:8083/actuator/env/{toMatch}",
            "templated": true
        },
        "loggers": {
            "href": "http://localhost:8083/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://localhost:8083/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://localhost:8083/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://localhost:8083/actuator/threaddump",
            "templated": false
        },
        "prometheus": {
            "href": "http://localhost:8083/actuator/prometheus",
            "templated": false
        },
        "metrics": {
            "href": "http://localhost:8083/actuator/metrics",
            "templated": false
        },
        "metrics-requiredMetricName": {
            "href": "http://localhost:8083/actuator/metrics/{requiredMetricName}",
            "templated": true
        }
    }
}

启动应用后,你可以在浏览器中访问 http://localhost:8080/actuator/prometheus,看到类似这样的输出:

plaintext 复制代码
# HELP jvm_memory_max_bytes The maximum amount of memory in bytes that can be used for memory management
# TYPE jvm_memory_max_bytes gauge
jvm_memory_max_bytes{area="heap",id="G1 Eden Space"} -1.0
jvm_memory_max_bytes{area="heap",id="G1 Old Gen"} 8.589934592E9
jvm_memory_max_bytes{area="heap",id="G1 Survivor Space"} -1.0
jvm_memory_max_bytes{area="nonheap",id="CodeCache"} 5.0331648E7
jvm_memory_max_bytes{area="nonheap",id="Compressed Class Space"} 1.073741824E9
jvm_memory_max_bytes{area="nonheap",id="Metaspace"} -1.0
....

这些指标数据可以被 Prometheus 抓取并存储。

Spring Boot Admin 提供了一个用户友好的监控界面,结合 Prometheus 的强大数据采集和分析能力,能够帮助你更加直观和高效地监控 Spring Boot 应用的健康状况和性能。我们可以直接使用SpringBoot AdminUI进行性能指标监测。



Prometheus 监控参数详解

在 Spring Boot 集成 Micrometer Prometheus 后,Prometheus 可以抓取和显示以下常见的监控指标:

  1. http_server_requests_seconds

    • 该指标记录了 HTTP 请求的处理时间。它包括以下标签:
      • method:HTTP 请求的方法(GET、POST 等)。
      • status:HTTP 响应状态码。
      • uri:被请求的路径。
    • 例如:请求处理时间的平均值可以通过 http_server_requests_seconds_sum / http_server_requests_seconds_count 计算。
  2. jvm_memory_used_bytes

    • 显示 JVM 中的内存使用情况,分为堆内存和非堆内存两部分:
      • area=heap:堆内存的使用。
      • area=nonheap:非堆内存的使用。
  3. jvm_gc_pause_seconds

    • 记录垃圾回收(GC)事件的暂停时间,帮助你监控 GC 是否对应用性能造成了影响。
      • action=major:指代 Major GC。
      • action=minor:指代 Minor GC。
  4. process_cpu_usage

    • 显示当前应用占用 CPU 的百分比,帮助监控应用的 CPU 负载。
  5. jvm_threads_live

    • 记录当前 JVM 中活动线程的数量。通过监控线程数的变化,你可以了解应用的线程消耗情况。
  6. logback_events_total

    • 记录应用的日志事件数量,分为不同的日志级别(INFO、WARN、ERROR)。
  7. system_cpu_usage

    • 显示系统整体的 CPU 使用率,帮助监控系统资源的消耗情况。
  8. process_uptime_seconds

    • 显示应用自启动以来的运行时长(以秒为单位)。

这些 Prometheus 指标能够帮助你全面监控 Spring Boot 应用的健康状况和性能表现。当与 Spring Boot Admin 集成后,不仅能够在 Spring Boot Admin UI 中查看这些指标,还可以通过 Prometheus 来定期抓取和分析这些数据,普罗米修斯我们后续再深入讲解,今天就到这,先消化一下。

欢迎一起交流讨论。关注我,为程序员职业生涯储能!!

相关推荐
JOJO___15 分钟前
Spring IoC 配置类 总结
java·后端·spring·java-ee
蜗牛学苑_武汉18 分钟前
设计模式之代理模式
java·网络·java-ee·代理模式
极客先躯28 分钟前
java和kotlin 可以同时运行吗
android·java·开发语言·kotlin·同时运行
白总Server1 小时前
MySQL在大数据场景应用
大数据·开发语言·数据库·后端·mysql·golang·php
luoluoal1 小时前
java项目之企业级工位管理系统源码(springboot)
java·开发语言·spring boot
ch_s_t1 小时前
新峰商城之购物车(一)
java·开发语言
蜜桃小阿雯1 小时前
JAVA开源项目 校园美食分享平台 计算机毕业设计
java·jvm·spring boot·spring cloud·intellij-idea·美食
黄昏_1 小时前
苍穹外卖Day01-2
java·spring
努力的八爪鱼2 小时前
记录工作中遇到的问题(持续更新~)
java
求学小火龙2 小时前
ElasticSearch介绍+使用
java·大数据·elasticsearch