SpringBoot+Prometheus采集Metrics指标数据

这篇具有很好参考价值的文章主要介绍了SpringBoot+Prometheus采集Metrics指标数据。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

简介

本文介绍在springboot3.x里配置启用系统指标监控功能,来监视系统各项Metrics指标,从而实时了解应用的运行健康状态,通过promtheus服务提供数据收集与指标可视化方案;

Metrics指标

metrics指标表示应用程序代码运行中提供的多维度指标数据,每一条指标数据通常由时间序列及一组标签键值对结构数据组成,常见的运行维度指标有:服务响应时间,HTTP请求量,CPU利用率,内存使用大小,磁盘读写大小,JVM内存使用率等等;

Spring Boot Actuator为Micrometer提供依赖管理和自动配置,Micrometer 是一个支持众多监控系统,并提供应用程序的可观察性度量指标数据组件;

Micrometer官方描述为计时器、计量器、计数器、分布摘要和长任务计时器提供供应商中立的接口,并具有维度数据模型,当与维度监控系统配合使用时,可以有效访问特定的命名指标,并具有向下钻取的能力跨越它的维度。

SpringBoot启用metrics

在springboot生态体系spring-boot-starter-actuator组件中已集成相关Metrics指标配置与服务,可以方便支持从 Spring Boot 应用程序中获取应用程序可观察性指标。

相关的配置与使用方式,在springboot官方文档中都有进行描述;详情参见:Production-ready Features

以一个springboot3.x项目做为示例,演示如何使用metrics指标服务,并对接到prometheus中做可观察性指标展示;

项目信息:springboot.3.1.0 + jdk17

pom.xml

创建任意一个springboot项目,引入以下pom配置项

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <java.version>17</java.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.1.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.projectreactor</groupId>
        <artifactId>reactor-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- prometheus依赖 -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
        <version>1.11.0</version>
    </dependency>
</dependencies>

application.yml

# 本地服务访问
server:
  # 服务端口
  port: 8080
  # 服务IP
  address: 0.0.0.0

spring:
  application:
    # 应用服务名
    name: testDemo

# 是否启用springboot的debug调试模式,会打印详细日志信息
debug: false

# 开启健康检查
management:
  # 自定义管理服务地址和端口,默认和应用程序地址端口一致
#  server:
#    port: 8081
#    address: 0.0.0.0
  endpoint:
    shutdown:
      enabled: true
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        # 开启端点,*表示开启所有
        include:
          - '*'
        # 某些端点除外,不做开启
        exclude:
          - env
          - beans
      # 跨域配置
      cors:
        # 开放所有域访问
        allowed-origins:
          - '*'
  metrics:
    # 指标采集标签名
    tags:
      application: ${spring.application.name}
  # 启用对接prometheus服务采集指标数据
  prometheus:
    metrics:
      export:
        enabled: true

ApplicationStart.java

@SpringBootApplication
public class ApplicationStart {
    public static void main(String[] args) {
        SpringApplication.run(ApplicationStart.class, args);
    }
}

浏览器访问

启动springboot程序后,通过以下URL在浏览器中打开

http://127.0.0.1:8080/actuator

查看已开启的端点与端点数据展示链接

{
    "_links": {
        "self": {
            "href": "http://127.0.0.1:8080/actuator",
            "templated": false
        },
        "caches-cache": {
            "href": "http://127.0.0.1:8080/actuator/caches/{cache}",
            "templated": true
        },
        "caches": {
            "href": "http://127.0.0.1:8080/actuator/caches",
            "templated": false
        },
        "health": {
            "href": "http://127.0.0.1:8080/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://127.0.0.1:8080/actuator/health/{*path}",
            "templated": true
        },
        "info": {
            "href": "http://127.0.0.1:8080/actuator/info",
            "templated": false
        },
        "conditions": {
            "href": "http://127.0.0.1:8080/actuator/conditions",
            "templated": false
        },
        "shutdown": {
            "href": "http://127.0.0.1:8080/actuator/shutdown",
            "templated": false
        },
        "configprops": {
            "href": "http://127.0.0.1:8080/actuator/configprops",
            "templated": false
        },
        "configprops-prefix": {
            "href": "http://127.0.0.1:8080/actuator/configprops/{prefix}",
            "templated": true
        },
        "loggers": {
            "href": "http://127.0.0.1:8080/actuator/loggers",
            "templated": false
        },
        "loggers-name": {
            "href": "http://127.0.0.1:8080/actuator/loggers/{name}",
            "templated": true
        },
        "heapdump": {
            "href": "http://127.0.0.1:8080/actuator/heapdump",
            "templated": false
        },
        "threaddump": {
            "href": "http://127.0.0.1:8080/actuator/threaddump",
            "templated": false
        },
        "prometheus": {
            "href": "http://127.0.0.1:8080/actuator/prometheus",
            "templated": false
        },
        "metrics-requiredMetricName": {
            "href": "http://127.0.0.1:8080/actuator/metrics/{requiredMetricName}",
            "templated": true
        },
        "metrics": {
            "href": "http://127.0.0.1:8080/actuator/metrics",
            "templated": false
        },
        "scheduledtasks": {
            "href": "http://127.0.0.1:8080/actuator/scheduledtasks",
            "templated": false
        },
        "mappings": {
            "href": "http://127.0.0.1:8080/actuator/mappings",
            "templated": false
        }
    }
}

安装prometheus

prometheus是一个开源监控系统,用于收集和聚合指标作为时间序列数据,它前身是SoundCloud的警告工具包。

prometheus通过抓取或轮询获取各个应用程序实例的指标。Spring Boot 提供了一个执行器端点/actuator/prometheus输出数据格式,以提供prometheus服务抓取。

下载

Download | Prometheus

prometheus支持windows环境安装与使用,本文采用prometheus-2.37.0(windows-amd64)版本做为演示使用;

prometheus.yml

解压下载包后,进入prometheus解压根目录,编辑prometheus.yml配置,添加拉取的springboot应用服务配置;

# 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).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# 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: ["localhost:9090"]
  # 增加spring任务节点,从/actuator/prometheus拉取目标指标数据
  - job_name: "spring"
    metrics_path: "/actuator/prometheus"
    static_configs:
      - targets: ["localhost:8080"]

浏览器访问

完成prometheus.yml配置后,直接双击prometheus.exe程序运行服务;服务启动完毕后,通过以下URL在浏览器中访问:

http://127.0.0.1:9090/

点击 “菜单栏》Status》Targets” 查看已配置并启用的采集端点应用

prometheus采集数据,SpringBoot,java,spring boot,prometheus,指标监控

Metrics指标跟踪

点击菜单栏的“Graph”项,在搜索框中输入jvm,则会自动联想多个带有jvm内容的可观察性指标;

prometheus采集数据,SpringBoot,java,spring boot,prometheus,指标监控

我们以常见的java应用程序内存使用为例,选择jvm_memory_used_bytes,观察应用服务运行过程中的jvm内存使用情况,进行实时监控以便及时了解应用服务运行状况,为应用服务提供有效的健康跟踪方案;

prometheus采集数据,SpringBoot,java,spring boot,prometheus,指标监控

Grafana大盘展示

由于metrics维度指标众多,通过prometheus服务采集后,通过数据指标转化成可观察性图形,为应用程序运维提供了合适的监控技术与管理平台,同时也可以将prometheus采集的指标输出到第三方各类平台上进行展示,比如运维中常见的Grafana,提供了许多丰富的图标插件,高大上的的漂亮UI,以及Grafana官网第三方上传的展示模板,可以非常便捷的套用开放模板后立即呈现出可视化监控图形界面;

  • 注:此处以一个已有的Grafana服务为例,不额外描述Grafana安装过程,安装请自行百度;

默认在explore界面查找指标展示;

prometheus采集数据,SpringBoot,java,spring boot,prometheus,指标监控

添加SpringBoot监控大盘,官方有很多共享的仪表盘资源可下载,从而减少自已不熟悉仪表盘的配置与脚本,避免无法操作出想要的效果,通过获得第三方共享资源可以快速进行展示。

Dashboards | Grafana Labs

以下为共享ID:12900 (SpringBoot APM Dashboard)

prometheus采集数据,SpringBoot,java,spring boot,prometheus,指标监控

在共享大盘页面复制共享ID后,在Grafana》Dashboards浏览界面,点击import按钮进入导入共享资源界面;

prometheus采集数据,SpringBoot,java,spring boot,prometheus,指标监控

输入共享大盘ID:12900,点击load后,进入大盘配置盘面,底部的Prometheus选择已安装的Prometheus数据源选择(如没有,需要先在插件库中提前安装);

prometheus采集数据,SpringBoot,java,spring boot,prometheus,指标监控

导入共享大盘后,进入到大盘展示界面,开始加载Prometheus监控的维度指标数据,通过各种仪表盘进行详细展示;

prometheus采集数据,SpringBoot,java,spring boot,prometheus,指标监控

到此,一个完整的springboot项目从集成metrics指标组件,到Prometheus监控服务搭建,到最后Grafana大盘仪表展示流程结束;文章来源地址https://www.toymoban.com/news/detail-760318.html

到了这里,关于SpringBoot+Prometheus采集Metrics指标数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • K8S篇之实现利用Prometheus监控pod的实时数据指标

    一、监控部署 1、将k8s集群中kube-state-metrics指标进行收集,服务进行部署 1.1 pod性能指标(k8s集群组件自动集成) k8s组件本身提供组件自身运行的监控指标以及容器相关的监控指标。通过cAdvisor 是一个开源的分析容器资源使用率和性能特性的代理工具,集成到 Kubelet中,当Ku

    2024年02月05日
    浏览(47)
  • prometheus采集服务的jmx数据,grafana通过dashboard展示jmx数据

    重命名目录 http://ip:9090 http://ip:9090/metrics 启动grafana 查看grafana状态 账号和密码都是:admin prometheus的URL是http://ip:9090 添加成功后保存数据源,成功如下所示 修改配置文件prometheus.yml 重启prometheus 发现成功采集debezium服务的jmx数据 grafana官网搜索你想监控的服务的dashboard: https

    2024年02月16日
    浏览(40)
  • Kubernetes 笔记(17)— 系统监控、使用Metrics Server、hpa 自动伸缩 Pod 数量、Prometheus 的使用

    如果你对 Linux 系统有所了解的话,也许知道有一个命令 top 能够实时显示当前系统的 CPU 和内存利用率,它是性能分析和调优的基本工具,非常有用。 Kubernetes 也提供了类似的命令,就是 kubectl top ,不过默认情况下这个命令不会生效,必须要安装一个插件 Metrics Server 才可以。

    2024年02月01日
    浏览(28)
  • 利用Prometheus做指标统计

    背景:目前公司需要统计一些数据, 而这些数据有些量非常大(千万量级的数据)。 如果在mysql中做统计, 可能会引发慢查询或者造成mysql集群负载过高的问题。 目前业务没有将这些数据同步到ES等数据库, 如果为了统计而调整业务, 这个改造成本比较高。 目前这些指标,

    2024年02月12日
    浏览(27)
  • Prometheus监控Elasticsearch指标

    Prometheus 可以很方便的监控 Elasticsearch 的指标。 方式一: 通过启动ES自带的监控模块暴露指标数据,主要步骤如下: 在 Elasticsearch 中启用监控模块修改 Elasticsearch 的配置文件,加入监控相关配置: 重启 Elasticsearch 实例后,监控相关 API 会自动启用。 配置 Prometheus 监控 Elasticsearch 在

    2024年02月09日
    浏览(26)
  • Prometheus相关的主机监控指标

    CPU负载指标 node_load1 node_load5 node_load15 以上三个指标为主机CPU平均负载,分别对应一分钟、五分钟和十五分钟的时间间隔。CPU负载是指某段时间内占用CPU时间的进程和等待CPU时间的进程数之和。一般来说,cpu负载数/cpu核数如果超过0.7,应该开始关注机器性能情况 ,如果超过

    2023年04月17日
    浏览(27)
  • Prometheus实现自定义指标监控

    前面我们已经通过 Prometheus+Grafana 实现了监控,可以在 Grafana 上看到对应的 SpringBoot 应用信息了, 通过这些信息我们可以对 SpringBoot 应用有更全面的监控。 但是如果我们需要对一些业务指标做监控,我们应该怎么做呢?这篇文章就带你一步步实现一个模拟的订单业务指 标监

    2024年02月12日
    浏览(33)
  • Prometheus之rabbitmq监控指标详解

    rabbitmq_channels 用于显示RabbitMQ服务器上当前打开的通道数量。 通过监控这个指标,您可以了解到RabbitMQ服务器打开的通道数随时间变化的情况,以及通道数量是否很高或者非常低。 rabbitmq_connections 用于显示与RabbitMQ服务器的连接总数。 该指标可以帮助您跟踪RabbitMQ服务器的连

    2024年02月14日
    浏览(27)
  • Prometheus监控指标查询性能调优

    一、背景 在《SRE: Google运维解密》一书中作者指出,监控系统需要能够有效的支持白盒监控和黑盒监控。黑盒监控只在某个问题目前正在发生,并且造成了某个现象时才会发出紧急警报。“白盒监控则大量依赖对系统内部信息的检测,如系统日志、抓取提供指标信息的 HTTP 节

    2024年02月13日
    浏览(30)
  • k8s v1.27.4 部署metrics-serverv:0.6.4,kube-prometheus

    只有一个问题,原来的httpGet存活、就绪检测一直不通过,于是改为tcpSocket后pod正常。 修改后的yaml文件,镜像修改为阿里云 部署kube-prometheus 兼容1.27的为main分支 只克隆main分支 处理: 修改prometheus-clusterRole.yaml 使用ServiceMonitor添加监控: 以ingress-nginx为例 修改ingress-nginx.yaml的

    2024年02月12日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包