当前位置: 首页 > article >正文

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 依赖:

<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 端点可以被访问:

指定暴露部分端点:

# 暴露 Prometheus 端点
management.endpoints.web.exposure.include=prometheus,health,info

指定暴露全部端点:

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

或者直接暴露所有端点:

# 暴露 所有Prometheus 端点
management.endpoints.web.exposure.include=*

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

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

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

{
    "_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,看到类似这样的输出:

# 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 来定期抓取和分析这些数据,普罗米修斯我们后续再深入讲解,今天就到这,先消化一下。

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


http://www.kler.cn/news/303532.html

相关文章:

  • 在 macOS 上安装 Python 3 并设置本地编程环境的方法
  • Reactive 编程-Project Reactor
  • Web3 详解
  • vue vueUse利用useInfiniteScroll API 实现虚拟滚动
  • Spring Boot实战-文章管理系统(1.用户相关接口)
  • 【动态规划】回文串问题
  • PHP省时省力海报在线制作系统小程序源码
  • day-55 从字符串中移除星号
  • Nature|纪念斯隆凯·特琳癌症中心发布有效率达50%的个性化RNA疫苗,强力阻断胰腺癌复发|顶刊速递·24-09-14
  • 掌握ChatGPT:高效利用AI助手
  • SciPy 插值
  • 机器学习--卷积神经网络(包括python实现)
  • spring揭秘20-spring事务02-编程式事务与声明式事务管理
  • 如何在Oracle中实现数据的加密
  • 深度学习-12-多模态Multimodality人工智能
  • React与Vue的对比
  • 秋招突击——算法练习——9/4——73-矩阵置零、54-螺旋矩阵、48-旋转图像、240-搜索二维矩阵II
  • vue原理分析(十四)研究new Vue()中的 initProvide
  • 局域网windows下使用Git
  • c#如何读取Modbus中Slave和Poll的值
  • vue之 package.json和package-lock.json
  • 【机器学习】线性动态系统的基本概念以及卡尔曼滤波器的概念和应用方式
  • c#引用同一命名空间下的其他类
  • 提权——Linux
  • Sequential的使用和搭建实战
  • js 深入理解生成器
  • 实时分析都靠它→揭秘YashanDB列式存储引擎的技术实现
  • 力扣第560题 和为k的子数组
  • 解锁编程潜力,从掌握GitHub开始
  • 突发!OpenAI发布最强模型o1:博士物理92.8分,IOI金牌水平