Springboot+Prometheus+grafana 制作自己的业务监控
//@desc:Springboot+Prometheus+grafana 制作自己的业务监控
//@desc:码字不宜,转载请注明出处
//@author:张慧源 <turing_zhy@163.com>
//@date:2022/09/09
知识储备
Prometheus 中文文档 : https://prometheus.fuckcloudnative.io/
系统架构

开始实现
生成业务指标
经典实现方法
经典的实现方法网上有很多案例,即利用切面在接口发生调用的时候写入指标数据
详见:https://www.jb51.net/article/202726.htm
自己的实现方案
由于自己的业务是为服务架构,第一期打算先实现从无到有,于是采用了定时任务跑指标的方案
引入依赖
<!--prometheus-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.8.4</version>
</dependency>
<!--定时任务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-quartz</artifactId>
<version>2.6.6</version>
</dependency>
<!--actuator 监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.7.2</version>
</dependency>
配置文件
#监控路径
management:
endpoints:
web:
exposure:
include: "*"
注册自己的指标[我这里用的是guaua格式数据]
package cn.hexcloud.m82.monitoring.service.monitor;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.Gauge;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
/**
* 优惠相关监控
*
* @author abner<huiyuan.zhang @ hex-tech.net>
* @date 2022-09-08 17:09:14
*/
@Service
public class PromotionMonitor {
/**
* 核券数量
*/
public static Gauge VERIFY_COUPON_COUNT;
/**
* 核券金额
*/
public static Gauge VERIFY_COUPON_AMOUNT_SUM;
/**
* 核活动数量
*/
public static Gauge VERIFY_ACTIVITY_COUNT;
/**
* 核活动金额
*/
public static Gauge VERIFY_ACTIVITY_AMOUNT_SUM;
/**
* 构造函数
*
* @author abner<huiyuan.zhang @ hex-tech.net>
* @date 2022-09-08 11:33:56
*/
@PostConstruct
private void init() {
VERIFY_COUPON_COUNT = initVerifyCouponCount();
VERIFY_COUPON_AMOUNT_SUM = initVerifyCouponAmountSum();
VERIFY_ACTIVITY_COUNT = initVerifyActivityCount();
VERIFY_ACTIVITY_AMOUNT_SUM = initVerifyActivityAmountSum();
}
@Resource
private PrometheusMeterRegistry prometheusMeterRegistry;
/**
* 获取 verify coupon count 句柄
*
* @author abner<huiyuan.zhang @ hex-tech.net>
* @date 2022-09-08 11:47:45
*/
public Gauge getVerifyCouponCount() {
return VERIFY_COUPON_COUNT;
}
/**
* 获取 verify coupon amount sum 句柄
*
* @author abner<huiyuan.zhang @ hex-tech.net>
* @date 2022-09-08 11:47:45
*/
public Gauge getVerifyCouponAmountSum() {
return VERIFY_COUPON_AMOUNT_SUM;
}
/**
* 获取 experience count 句柄
*
* @author abner<huiyuan.zhang @ hex-tech.net>
* @date 2022-09-08 11:47:45
*/
public Gauge getVerifyActivityCount() {
return VERIFY_ACTIVITY_COUNT;
}
/**
* 获取 verify activity amount sum 句柄
*
* @author abner<huiyuan.zhang @ hex-tech.net>
* @date 2022-09-08 11:47:45
*/
public Gauge getVerifyActivityAmountSum() {
return VERIFY_ACTIVITY_AMOUNT_SUM;
}
/**
* 核券数量
*/
public Gauge initVerifyCouponCount() {
Gauge verifyCouponCount = Gauge.build().name("verify_coupon_count").labelNames("partner_id").help("verify coupon count").register();
prometheusMeterRegistry.getPrometheusRegistry().register(verifyCouponCount);
return verifyCouponCount;
}
/**
* 核券金额
*/
public Gauge initVerifyCouponAmountSum() {
Gauge verifyCouponAmountSum = Gauge.build().name("verify_coupon_amount_sum").labelNames("partner_id").help("verify coupon amount sum").register();
prometheusMeterRegistry.getPrometheusRegistry().register(verifyCouponAmountSum);
return verifyCouponAmountSum;
}
/**
* 核销活动数量
*/
public Gauge initVerifyActivityCount() {
Gauge verifyActivityCount = Gauge.build().name("verify_activity_count").labelNames("partner_id").help("verify activity count").register();
prometheusMeterRegistry.getPrometheusRegistry().register(verifyActivityCount);
return verifyActivityCount;
}
/**
* 核活动金额
*/
public Gauge initVerifyActivityAmountSum() {
Gauge verifyActivityAmountSum = Gauge.build().name("verify_activity_amount_sum").labelNames("partner_id").help("verify activity amount sum").register();
prometheusMeterRegistry.getPrometheusRegistry().register(verifyActivityAmountSum);
return verifyActivityAmountSum;
}
}
写入指标
log.info("VerifyActivityCount-partnerId:{}-count:{}",partnerCountAmountActivityDto.getPartnerId(),partnerCountAmountActivityDto.getCount());
promotionMonitor.getVerifyActivityCount().labels(partnerCountAmountActivityDto.getPartnerId()).set(partnerCountAmountActivityDto.getCount());
float verifyCouponAmount = partnerCountAmountActivityDto.getAmount()
.divide(BigDecimal.TEN.multiply(BigDecimal.TEN), 2, RoundingMode.HALF_UP)
.floatValue();
log.info("VerifyActivityAmountSum-partnerId:{}-amount:{}",partnerCountAmountActivityDto.getPartnerId(),verifyCouponAmount);
promotionMonitor.getVerifyActivityAmountSum().labels(partnerCountAmountActivityDto.getPartnerId()).set(verifyCouponAmount);
效果展示
路径:http://localhost:8080//actuator/prometheus

配置prometheus 抓取上面的指标
** 可以搭建自己的prometheus服务也可以使用阿里云的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=` to any timeseries scraped from this config.
- job_name: 'prometheus'
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ['localhost:9090']
配置自己的图表
我的业务需要分租户展示,kennel跟大家的不太一样,稍微展示下吧

最终效果展示

Springboot+Prometheus+grafana 制作自己的业务监控的更多相关文章
- SpringBoot+Prometheus+Grafana实现应用监控和报警
一.背景 SpringBoot的应用监控方案比较多,SpringBoot+Prometheus+Grafana是目前比较常用的方案之一.它们三者之间的关系大概如下图: 关系图 二.开发SpringBo ...
- Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统
prometheus安装和配置 prometheus组件介绍 1.Prometheus Server: 用于收集和存储时间序列数据. 2.Client Library: 客户端库,检测应用程序代码,当 ...
- 使用 Prometheus + Grafana 对 Kubernetes 进行性能监控的实践
1 什么是 Kubernetes? Kubernetes 是 Google 开源的容器集群管理系统,其管理操作包括部署,调度和节点集群间扩展等. 如下图所示为目前 Kubernetes 的架构图,由 ...
- Kubernetes使用prometheus+grafana做一个简单的监控方案
前言 本文介绍在k8s集群中使用node-exporter.prometheus.grafana对集群进行监控.其实现原理有点类似ELK.EFK组合.node-exporter组件负责收集节点上的me ...
- 微服务监控之三:Prometheus + Grafana Spring Boot 应用可视化监控
一.Springboot增加Prometheus 1.Spring Boot 应用暴露监控指标,添加如下依赖 <dependency> <groupId>org.springf ...
- 基于Prometheus和Grafana打造业务监控看板
前言 业务监控对许许多多的场景都是十分有意义,业务监控看板可以让我们比较直观的看到当前业务的实时情况,然后运营人员可以根据这些情况及时对业务进行调整操作,避免业务出现大问题. 老黄曾经遇到过一次比较尴 ...
- prometheus+grafana监控mysql最佳实践
导航 前言 环境准备 安装Docker 安装prometheus 安装mysqld_exporter prometheus采集数据 安装grafana grafana配置数据源 感谢您的阅读,预计阅读 ...
- Prometheus(一):Prometheus+Grafana 安装配置
一.基础环境 系统 IP 监控主机 CentOS 7 192.168.56.200 被监控主机 CentOS 7 192.168.56.201 二.Prometheus服务端安装 以下操作皆在监控主机 ...
- Prometheus+Grafana监控SpringBoot
Prometheus+Grafana监控SpringBoot 一.Prometheus监控SpringBoot 1.1 pom.xml添加依赖 1.2 修改application.yml配置文件 1. ...
- 基于Docker+Prometheus+Grafana监控SpringBoot健康信息
在微服务体系当中,监控是必不可少的.当系统环境超过指定的阀值以后,需要提醒指定的运维人员或开发人员进行有效的防范,从而降低系统宕机的风险.在CNCF云计算平台中,Prometheus+Grafana是 ...
随机推荐
- RowHammer 攻击:内存的隐形威胁
今天看了一篇 IT 之家关于 AMD 处理器受 RowHammer 内存攻击影响的报道,心血来潮了解了一下 RowHammer 攻击的原理,把了解到的知识记录下来. RowHammer 攻击是一种相对 ...
- 网络设备性能指标之pps
基本概念: Bps:Byte per second 每秒传输多少字节 bps: bits per second 每秒传输多少位 ,这个也叫做端口速率 pps:Packet Per Second(包每秒 ...
- #LCT,树状数组#CF1137F Matches Are Not a Child's Play
题目 分析 考虑从删除序列末尾来看,最大值一定在末尾, 然后与次大值之间夹了整条路径的点,降序以此类推 实际上从小到大是每个点到最大点的路径被打通的过程,由此分成若干条实链. 删除序列的位置实际上是到 ...
- #计数#CF10C Digital Root
题目 定义\(d(x)\)为\(x\)的数位和嵌套,直至\(0\leq d(x)<10\) 询问在\([1\sim n]\)中有多少个三元组\((a,b,c)\)满足 \[ab\neq c,d( ...
- #树链剖分,zkw线段树#nssl 1489 大冰隙2
题目 有一个长度为\(n\)的01数列\(a\)和一个长度为\(n\)的数列\(b\)表示权值 支持单点修改以及区间查询,\(0\)和\(1\)可以看作左括号和右括号, 将一段区间所有可匹配的的括号去 ...
- #树状数组,哈希#洛谷 6687 论如何玩转 Excel 表格
题目 分析 首先一列的数不会发生变化,只是交换列, 并且交换列的时候奇数列变成偶数列取反, 偶数列变成奇数列取反,考虑直接将偶数列全部取反, 那只需要交换列就可以了,奇数列交换到偶数列会取反, 奇数列 ...
- 如何实现OpenHarmony的OTA升级
OTA简介 随着设备系统日新月异,用户如何及时获取系统的更新,体验新版本带来的新的体验,以及提升系统的稳定性和安全性成为了每个厂商都面临的严峻问题.OTA(Over the Air)提供对设备远程升级 ...
- 深入理解 C++ 语法:从基础知识到高级应用
C++ 语法 让我们将以下代码分解以更好地理解它: 示例 #include <iostream> using namespace std; int main() { cout <&l ...
- openGauss数据与PostgreSQL的差异对比
openGauss 数据与 PostgreSQL 的差异对比 前言 openGauss 数据库已经发布 2.0.1 版本了,中启乘数科技是一家专业的专注于极致性能的数据库服务提供商,所以也关注 ope ...
- openGauss数据库将磁盘表转换为MOT
openGauss 数据库将磁盘表转换为 MOT 一.将磁盘表转换为 MOT 方法 磁盘表直接转换为 MOT 尚不能实现,这意味着尚不存在将基于磁盘的表转换为 MOT 的 ALTER TABLE 语句 ...