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是 ...
随机推荐
- .NET Emit 入门教程:第二部分:构建动态程序集(追加构建静态程序集教程)
前言: 在本部分中,我们将深入探讨如何使用C# Emit 来创建动态程序集. 动态程序集是在运行时生成的,为我们提供了一种灵活.动态地构建和加载程序集的能力. 1. 程序集的概念 程序集是.NET中的 ...
- Oracle查询表空间信息
记录一下 SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)", D.TOT_ ...
- 【环境配置】vscode配置C C++开发和调试环境
按照本教程配置好之后,不再需要写任何tasks.json和launch.json文件,减轻记忆负担,即使是调试程序,也不用再写这些文件了,跟着做就能得到一个很好的集成开发环境 目录 msys2的安装与 ...
- Oracle 触发器迁移至KingbaseES常见的问题
oracle数据库的触发器迁移到KingbaseES的时候经常会出现一下两类错误: 1.SQL 错误 [42809]: 错误: "xxxxxxxx" 是一个视图.Detail: 视 ...
- KingbaseES特殊权限介绍
用户需求:新建一个用户B,需要能够查询A用户的所有表,并且对以后新建的表也要有select权限. 对于现有的表可以通过动态sql批量进行授权,但是未来新建的表要如何进行授权呢? 查询了帮助文档发现通过 ...
- jsonb操作符
json类型以文本方式存储json对象,把输入的数据原封不动的存放到数据库中,会保留多余的空格,保留重复的Key,保留Key的顺序. jsonb类型转换文本格式json对象为二进制格式,不保留多余的空 ...
- Fastjson反序列化分析
依赖 先研究1.2.24版本的,版本高了就有waf了,不过也能绕,高版本以后再说 <dependency> <groupId>com.alibaba</groupId&g ...
- [网络/HTTPS/Java] PKI公钥基础设施体系、CA证书与认证工具(jre keytool / openssl)
0 序 1 CA证书概述 说起 HTTP 的那些事,则不得不提 HTTPS ,而说起 HTTPS ,则不得不提数字证书. 本文将从 Java 的角度,学习 HTTPS 和数字证书技术. 1.1 访问 ...
- #线性筛,质数#LOJ 6165 一道水题
题目 \((lcm_{i=1}^ni)\bmod 10^8+7,n\leq 10^8\) 分析 考虑对于某个质数\(p\),在\(n\)范围内做出的最大贡献为\(p^k(p^k\leq n)\), 线 ...
- Node 项目通过 .npmrc 文件指定依赖安装源
背景 npm 命令运行时,往往通过命令行指定相关配置,最常用的便是使用 --registry 来指定依赖的安装源. npm install --registry=https://registry.np ...