SpringBoot集成Prometheus实现监控

news/2024/5/18 21:27:21 标签: spring boot, prometheus, 后端
SpringBoot配置Prometheus
	 <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  • 自定义指标

引入上面两个依赖之后,SpringBoot的/actuator/prometheus路径会默认暴露一些指标。

在这里插入图片描述

prometheus通过Http协议拉取的指标数据格式为
指标名 {标签} 值
如 jvm_memory_max_bytes{application=“blog”,area=“heap”,id=“Eden Space”,} 7.1630848E7
这个指标的指标名是jvm_memory_max_bytes,标签是 {application=“blog”,area=“heap”,id=“Eden Space”,},而指标值是 7.1630848E7,将来使用PromQL查询时,标签可以起到筛选条件的作用。

除了引入依赖所提供的指标外,还可以自定义指标。

@SuppressWarnings("all")
@Component
public class MetricsCounter {

    private static Counter loginCounter = null;
    private static Counter registerCounter = null;

    private static AtomicInteger atomicInteger;

    public MetricsCounter(MeterRegistry registry) {
        loginCounter = registry.counter("login_nums");
        registerCounter = registry.counter("register_nums");
        atomicInteger = registry.gauge("ssl_expire_days", new AtomicInteger(10));
    }

    /**
     * 此方法可能会被多线程执行,需要考虑线程安全问题
     */
    public synchronized static void incrLogin() {
        loginCounter.increment();
    }

    public synchronized static void incrRegister() {
        registerCounter.increment();
    }

    public static void updateSslExpireDays(){
        atomicInteger.set(new Random().nextInt(100));
    }

}

通过拿到MeterRegistry 自定义指标,这里定义了两种类型的指标,一种是Counter 计数器,值只增不减,一种是gauge,gauge类型可以随意修改。

  1. 编写一个接口,改变指标
@RestController
public class TestRest {

    @GetMapping("t1")
    public String t1(){
        MetricsCounter.incrLogin();
        MetricsCounter.incrRegister();
        MetricsCounter.updateSslExpireDays();
        return "t1";
    }

}
  1. 访问路径

在这里插入图片描述
可以看到最新的指标值。

二 、 Prometheus 端配置

SpringBoot将指标暴露出去后,还需要配置Prometheus 的配置文件,让Prometheus 定时去访问路径拉取到指标。

# 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: "nodeExporter"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["192.168.240.130:9100"] #监控自己主机上的端口
  - job_name: "springboot"
    scrape_interval: 3s                                                # 多久采集一次数据
    scrape_timeout: 3s                                                 # 采集时的超时时间
    metrics_path: '/actuator/prometheus'                # 采集的路径
    static_configs:                                     # 采集服务的地址,设置成Springboot应用所在服务器的具体地址
      - targets: ["192.168.1.103:8188"]

alerting : 配置告警管理器地址
rule_files : 配置告警 规则
scrape_configs : 配置指标抓取规则,在这个配置项下配置SpringBoot的指标路径。

  • 启动promteus
nohup ./prometheus --config.file=./prometheus.yml &

启动后的端口默认是 9090

在这里插入图片描述

可以在上述的input输入框中输入PromQL进行对指标的查询。

具体的算术运算符、关系运算符以及内置函数等 可参考 Prometheus官网。

到此SpringBoot已完成与Prometheus的整合。


http://www.niftyadmin.cn/n/5041206.html

相关文章

如何在Ubuntu系统部署RabbitMQ服务器并公网访问【内网穿透】

文章目录 前言1.安装erlang 语言2.安装rabbitMQ3. 内网穿透3.1 安装cpolar内网穿透(支持一键自动安装脚本)3.2 创建HTTP隧道 4. 公网远程连接5.固定公网TCP地址5.1 保留一个固定的公网TCP端口地址5.2 配置固定公网TCP端口地址 前言 RabbitMQ是一个在 AMQP(高级消息队列协议)基…

rufus报错:设备正在被另一个使用中。请关闭可能访问设备的任意其它进程。[014332] C:\windows\Explorer.EXE (r)

可能原因 1.镜像在被刷机U盘上面 2.镜像在移动硬盘盒上&#xff0c;但是上传后链接断开 3.参考这个答案&#xff0c;但是参考意义不大。链接&#xff1a;Rufus几乎每次都被explorer.exe打断 | 码农俱乐部 - Golang中国 - Go语言中文社区 (mlog.club)

【车载开发系列】AutoSar软件组件接口

【车载开发系列】AutoSar软件组件接口 【车载开发系列】AutoSar软件组件接口 【车载开发系列】AutoSar软件组件接口一. 端口概念二. 三种端口方向三. 五种端口属性1&#xff09;S/R Port2&#xff09;C/S Port3&#xff09;Mode Switch interface4&#xff09;其他Port 四. Por…

Haproxy负载均衡群集

HAproxy搭建Web群集一、Web集群调度器1、常见的Web集群调度器2、常用集群调度器的优缺点&#xff08;LVS ,Nginx,Haproxy)2.1 Nginx2.2 LVS2.3 Haproxy 3、LVS、Nginx、HAproxy的区别 二、Haproxy1、简介2、Haproxy应用分析3、HAProxy的主要特性4、Haproxy调度算法&#xff08;…

NSS [NISACTF 2022]middlerce

NSS [NISACTF 2022]middlerce 开题&#xff0c;直接给了源码。 由语句$command json_decode($txw4ever,true)[cmd];可得&#xff0c;$txw4ever一定是json格式的数据&#xff0c;但是&#xff0c;preg_match()函数却过滤了{&#xff0c;同时.*贪婪匹配后又匹配括号里的字符&am…

springboot01

目录 新建Maven工程&#xff0c;什么都不选 ​pom.xml加上 新建包top.cjz.controller 新建类HelloController ​新建类HelloApplication ​运行浏览器访问 新建Maven工程&#xff0c;什么都不选 pom.xml加上 <!--springboot工程需要继承的父工程--> <parent…

玩玩“小藤”开发者套件 Atlas 200I DK A2 之VSCode远程连接

玩玩“小藤”开发者套件 Atlas 200I DK A2 之VSCode远程连接 0. 背景1. VSCode 安装 Remote - SSH 插件2. 安装 OpenSSH 组件3. VSCode SSH 连接 Atlas 200I DK A24. 打开远程文件夹 0. 背景 总所周知&#xff0c;英伟达的GPU供不应求&#xff0c;还各种限制。华为推出了升腾A…

UEFI 安装 Debian12 Linux 物理机虚拟机VMware通用

文章目录 前言⭐前置虚拟机物理机 安装流程选择安装方式语言及键盘选择网络选择创建用户系统磁盘分区新旧磁盘分区方式BOOT分区SWAP分区根分区 安装过程中其他选项选择软件包安装流程末 前言⭐ 物理机和虚拟机安装仅有设置UFFI引导的差别、这里前置为设置UEFI引导。安装步骤大…