SpringBoot+Prometheus+Grafana 实现自定义监控

news2024/9/30 7:25:07

1.Spring Boot 工程集成 Micrometer

1.1引入依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
  <groupId>io.micrometer</groupId>
  <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

1.2配置

management.server.port=9003
management.endpoints.web.exposure.include=*
management.endpoint.metrics.enabled=true
management.endpoint.health.show-details=always
management.endpoint.health.probes.enabled=true
management.endpoint.prometheus.enabled=true
management.metrics.export.prometheus.enabled=true
management.metrics.tags.application=voice-qc-backend

这里management.endpoints.web.exposure.include=*配置为开启 Actuator 服务,因为Spring Boot Actuator 会自动配置一个 URL 为/actuator/Prometheus的 HTTP 服务来供 Prometheus 抓取数据,不过默认该服务是关闭的,该配置将打开所有的 Actuator 服务。

management.metrics.tags.application配置会将该工程应用名称添加到计量器注册表的 tag 中去,方便后边 Prometheus 根据应用名称来区分不同的服务。

1.3监控jvm信息

然后在工程启动主类中添加 Bean 如下来监控 JVM 性能指标信息:

@SpringBootApplication
public class GatewayDatumApplication {

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

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(
            @Value("${spring.application.name}") String applicationName) {
        return (registry) -> registry.config().commonTags("application", applicationName);
    }

}

1.4创建自定义监控

监控请求次数与响应时间

package com.lianxin.gobot.api.monitor;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Author: GZ
 * @CreateTime: 2022-08-30  10:50
 * @Description: 自定义监控服务
 * @Version: 1.0
 */
@Component
public class PrometheusCustomMonitor {
    /**
     * 上报拨打请求次数
     */
    @Getter
    private Counter reportDialRequestCount;
    /**
     * 上报拨打URL
     */
    @Value("${lx.call-result-report.url}")
    private String callReportUrl;

    /**
     * 上报拨打响应时间
     */
    @Getter
    private Timer reportDialResponseTime;
    @Getter
    private final MeterRegistry registry;


    @Autowired
    public PrometheusCustomMonitor(MeterRegistry registry) {
        this.registry = registry;
    }

    @PostConstruct
    private void init() {
        reportDialRequestCount = registry.counter("go_api_report_dial_request_count", "url",callReportUrl);
        reportDialResponseTime=  registry.timer("go_api_report_dial_response_time", "url",callReportUrl);
    }
}

1.5添加具体业务代码监控

//统计请求次数
prometheusCustomMonitor.getReportDialRequestCount().increment();
long startTime = System.currentTimeMillis();
String company = HttpUtils.post(companyUrl,"");
//统计响应时间
long endTime = System.currentTimeMillis();
prometheusCustomMonitor.getReportDialResponseTime().record(endTime-startTime, TimeUnit.MILLISECONDS);

在浏览器访问http://127.0.0.1:9001/actuator/prometheus,就可以看到服务的一系列不同类型 metrics 信息,例如jvm_memory_used_bytes gaugejvm_gc_memory_promoted_bytes_total countergo_api_report_dial_request_count

到此,Spring Boot 工程集成 Micrometer 就已经完成,接下里就要与 Prometheus 进行集成了。

2.集成 Prometheus

2.1安装

docker pull prom/prometheus
mdkir /usr/local/prometheus
vi 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).

# 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: ['192.168.136.129:9090']
docker run -d --name prometheus -p 9090:9090 -v/usr/local/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus

2.2集成配置

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
    - targets: ["localhost:9090"]
  - job_name: "metricsLocalTest"
    metrics_path: "/actuator/prometheus"
    static_configs:
    - targets: ["localhost:9003"]

这里localhost:9001就是上边本地启动的服务地址,也就是 Prometheus 要监控的服务地址。同时可以添加一些与应用相关的标签,方便后期执行 PromSQL 查询语句区分。最后重启 Prometheus 服务

3.使用 Grafana Dashboard 展示监控项

3.1安装grafana

 docker pull grafana/grafana
docker run -d --name grafana -p 3000:3000 -v /usr/local/grafana:/var/lib/grafana grafana/grafana

默认用户名/密码 admin/admin

3.2配置prometheus数据源

3.3增加jvm面板

模板编号为4701

3.4配置业务接口监控面板

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/96266.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

E1. Erase and Extend (Easy Version)(纯暴力+string)

Problem - 1537E1 - Codeforces 这是该问题的简单版本。唯一的区别是对n和k的约束。只有当所有版本的问题都解决了&#xff0c;你才能进行黑客攻击。 你有一个字符串s&#xff0c;你可以对它进行两种类型的操作。 删除字符串的最后一个字符。 复制字符串&#xff1a;s:ss&…

数据结构---堆排序

堆排序JAVA实现和快速排序区别二叉堆的构建&#xff0c;删除&#xff0c;调整是实现堆排序的基础之前博客写了二叉堆&#xff1a; 二叉堆最大堆的堆顶是整个堆中的最大元素。最小堆的堆顶是整个堆中的最小元素。 堆排序步骤&#xff1a; 把无序数组构建成二叉堆。(需要从小到…

ArcGIS基础:等高线数据生成栅格DEM数据

以下操作为生成栅格DEM数据的方法。 一般方法是先创建TIN&#xff0c;然后在转为栅格DEM数据。 原始数据如下&#xff1a;为等高线数据&#xff0c;创建TIN数据需要用到等高线数据的【高程】字段。 声明&#xff1a;数据来源于网络。 工具位于【3D分析工具】下的【TIN】下…

BeanDefinition

1. 前言 Spring最重要的一个概念当属Bean了&#xff0c;我们写的Controller、Service、Dao凡是加了对应注解交给Spring管理的&#xff0c;都是Spring容器中的一个Bean。把我们自己写的类变成一个Bean交给Spring管理有很多的好处&#xff0c;比如我们不用自己去new对象了&#…

ssh+mysql实现的Java web企业人事人力资源管理系统源码+运行教程+参考论文+开题报告

今天给大家演示的是一款由sshmysql实现的Java web企业人事人力资源管理系统&#xff0c;其中struts版本是struts2&#xff0c;本系统功能非常完善&#xff0c;已经达到了可以商用的地步&#xff0c;基本全部实现了整个人力资源管理的所有功能&#xff0c;包括员工档案信息、部门…

jekins集成部署

jekins集成部署1.jekins简介2.Jenkins部署环境3. jekins安装4.配置jekins启动和停止脚本5.插件安装5.1.安装maven插件安装5.2 安装gitee插件5.3 安装Publish Over SSH插件5.4 安装 事件机制插件6.任务构建6.1 构建任务6.2 配置giteeApi令牌6.3 配置gitee源码地址6.4 在build中配…

3D激光里程计其二:NDT

3D激光里程计其二&#xff1a;NDT1. 经典NDT2. 计算方式2.1 2D场景求解:2.2 3D场景求解&#xff1a;3. 其他 NDTReference:深蓝学院-多传感器融合 1. 经典NDT NDT 核心思想&#xff1a;基于概率的匹配。目标是将点集 Y 匹配到固定的点集 X 中。这里的联合概率说的是将 X 划分成…

计算机毕业设计springboot+vue社区疫情防控系统

项目介绍 本系统运用最新的技术springboot框架,此框架是现在社会公司生产中所用的必需框架,非常实用,相比于以前的ssm框架,简单很多。前端框架运用vue框架,vue框架是最近几年非常流行的前端技术,适合很多开发语言。主要可以是系统的前端和后端进行解耦,分离,有利于开发者分别注…

设计模式——中介者模式

中介者模式一、基本思想二、应用场景三、结构图四、代码五、优缺点优点缺点一、基本思想 定义一个中介对象来封装一系列对象之间的交互&#xff0c;使原有对象之间的耦合松散&#xff0c;且可以独立地改变它们之间的交互。中介者模式又叫调停模式&#xff0c;它是迪米特法则的…

Proteus8仿真:51单片机LCD1602显示

51单片机LCD1602显示元器件原理图部分代码main.c工程文件元器件 元器件名称排阻RESPACK-851单片机AT89C51LCD1602LM016L按键BUTTON 原理图部分 LCD1602驱动: HD44780显示主要有8位操作8位两行显示&#xff0c;4位操作8位一行显示&#xff0c;8位操作8位一行显示。 LCD1602主要…

【Python】循环语句

目录 1.while 循环 2. for 循环 3. continue 4. break 1.while 循环 基本语法格式 while 条件&#xff1a; 循环体 条件为真&#xff0c;则执行循环体代码 条件为假&#xff0c;则结束循环 例1&#xff1a;打印 1 - 10 的整数 num 1 while num < 10:print(num)num 1 …

【AI with ML】第 10 章 :创建 ML 模型以预测序列

&#x1f50e;大家好&#xff0c;我是Sonhhxg_柒&#xff0c;希望你看完之后&#xff0c;能对你有所帮助&#xff0c;不足请指正&#xff01;共同学习交流&#x1f50e; &#x1f4dd;个人主页&#xff0d;Sonhhxg_柒的博客_CSDN博客 &#x1f4c3; &#x1f381;欢迎各位→点赞…

MySQL底层索引

目录 一、什么是索引&#xff1f; 二、MySQL索引结构分析【MySQL底层采用的是BTree】 1、为什么不使用二叉树&#xff1f; 2、为什么不使用红黑树&#xff1f; 3、为什么不使用Hash&#xff1f; 4、BTree与B-Tree的区别&#xff1f; 三、MySQL数据库的表结构、索引、数据 1、M…

基于Sharfetter-Gummel和改进的Sharfetter-Gummel计算对流扩散方程的通量(Matlab代码实现)

&#x1f468;‍&#x1f393;个人主页&#xff1a;研学社的博客 &#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜…

1. Arthas的命令

万恶淫为首&#xff0c;百善孝为先 Arthas命令 可以查看相应的文档: https://arthas.aliyun.com/doc/命令.html 如: https://arthas.aliyun.com/doc/grep.html https://arthas.aliyun.com/doc/cat.html 基础命令 help 查看命令帮助信息 cat 查看当前arthas 系统中的任意文件…

【算法】八月算法打卡

2022-08-01 低配版 promise class MyPromise {constructor(executor) {// 成功回调队列this._resolveQueue [];// 失败回调队列this._rejectQueue [];let resolve (val) > {while (this._resolveQueue.length) {const callback this._resolveQueue.shift();callback(v…

【iOS】对象,消息,运行期

文章目录对象&#xff0c;消息&#xff0c;运行期属性属性特质原子性方法名在对象内部尽量直接访问实例变量对象等同性特定类所具有的等同性判断等同性判定的执行深度容器中可变类的等同性以“类族模式”隐藏实现细节创建类族Cocoa里的类族在既有类中使用关联对象存放自定义数据…

原型,原型链,原型的继承

原型的作用? 1.节省内存空间 2.实现数据共享(继承) 什么是原型? 任何一个函数都有propotype属性,它本身是一个对象,我们称之为原型 构造函数,实例化对象与原型之间的关系? 1.任何一个函数都有prototype属性,它本身是一个对象,我们称之为原型 2.构造函数也是函数,也都…

C语言中头文件(.h)编写,头文件的包含<>和““很大不同

头文件书写技巧 1、头文件主要作用&#xff0c;提前声明函数&#xff0c;因为在c语言中必须先声明或定义才能使用 2、在使用到.c文件中需要把这个.h文件进行包含格式是#include <h文件>或#include "h文件名"&#xff0c;但**强烈建议用引号**&#xff0c;尖括…

【码极客精讲】do while语句

上次讲了while语句&#xff0c;这次讲一下do while语句。 do...while 循环是 while 循环的变体。在检查while()条件是否为真之前&#xff0c;该循环首先会执行一次do{}之内的语句&#xff0c;然后在while()内检查条件是否为真&#xff0c;如果条件为真的话&#xff0c;就会重复…