//@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 制作自己的业务监控的更多相关文章

  1. SpringBoot+Prometheus+Grafana实现应用监控和报警

    一.背景 SpringBoot的应用监控方案比较多,SpringBoot+Prometheus+Grafana是目前比较常用的方案之一.它们三者之间的关系大概如下图: 关系图 二.开发SpringBo ...

  2. Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统

    prometheus安装和配置 prometheus组件介绍 1.Prometheus Server: 用于收集和存储时间序列数据. 2.Client Library: 客户端库,检测应用程序代码,当 ...

  3. 使用 Prometheus + Grafana 对 Kubernetes 进行性能监控的实践

    1 什么是 Kubernetes? Kubernetes 是 Google 开源的容器集群管理系统,其管理操作包括部署,调度和节点集群间扩展等. 如下图所示为目前 Kubernetes 的架构图,由 ...

  4. Kubernetes使用prometheus+grafana做一个简单的监控方案

    前言 本文介绍在k8s集群中使用node-exporter.prometheus.grafana对集群进行监控.其实现原理有点类似ELK.EFK组合.node-exporter组件负责收集节点上的me ...

  5. 微服务监控之三:Prometheus + Grafana Spring Boot 应用可视化监控

    一.Springboot增加Prometheus 1.Spring Boot 应用暴露监控指标,添加如下依赖 <dependency> <groupId>org.springf ...

  6. 基于Prometheus和Grafana打造业务监控看板

    前言 业务监控对许许多多的场景都是十分有意义,业务监控看板可以让我们比较直观的看到当前业务的实时情况,然后运营人员可以根据这些情况及时对业务进行调整操作,避免业务出现大问题. 老黄曾经遇到过一次比较尴 ...

  7. prometheus+grafana监控mysql最佳实践

    导航 前言 环境准备 安装Docker 安装prometheus 安装mysqld_exporter prometheus采集数据 安装grafana grafana配置数据源 感谢您的阅读,预计阅读 ...

  8. Prometheus(一):Prometheus+Grafana 安装配置

    一.基础环境 系统 IP 监控主机 CentOS 7 192.168.56.200 被监控主机 CentOS 7 192.168.56.201 二.Prometheus服务端安装 以下操作皆在监控主机 ...

  9. Prometheus+Grafana监控SpringBoot

    Prometheus+Grafana监控SpringBoot 一.Prometheus监控SpringBoot 1.1 pom.xml添加依赖 1.2 修改application.yml配置文件 1. ...

  10. 基于Docker+Prometheus+Grafana监控SpringBoot健康信息

    在微服务体系当中,监控是必不可少的.当系统环境超过指定的阀值以后,需要提醒指定的运维人员或开发人员进行有效的防范,从而降低系统宕机的风险.在CNCF云计算平台中,Prometheus+Grafana是 ...

随机推荐

  1. 记录-JS 基础知识大全

    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助 1.通过javascript向文档中输出文本 document是javascript的内置对象,代表浏览器的文档部分 document.w ...

  2. 2022北航软件研究生入学考试991考试大纲-数据结构与C

    991"数据结构与C语言程序设计"考试大纲 "数据结构与C语言程序设计"考试内容包括"数据结构"与"C语言程序设计"两门 ...

  3. AI金融预测领域综述文章筛选,附论文及代码链接,2021年版

    21年的综述最近读了3篇,总结笔记如下: (2021)Systematic Literature Review: Stock Price Prediction Using Machine Learni ...

  4. Kubernetes客户端认证(三)—— Kubernetes使用CertificateSigningRequest方式签发客户端证书

    1.概述 在<Kubernetes客户端认证(一)-- 基于CA证书的双向认证方式>和<Kubernetes客户端认证(二)-- 基于ServiceAccount的JWTToken认 ...

  5. Java实现栈

    package algorithm; import java.util.Arrays; import java.util.Iterator; /** @author Administrator @da ...

  6. windows系统cmd切换盘符路径命令失效

    问题描述:比如当我在C盘想切换到D盘的某个文件夹路径下时 只是输出了那个路径 但是并没有真的切换 这时候需要再多操作一步就会成功了

  7. yml和properties打印SQL日志信息

    1.配置文件里面配置 第一种是properties类型如下 logging.level.com.datayes.mdi.dao.rdb.mommp.**=debug其中 com.datayes.mdi ...

  8. js实现多列排序-存在问题

    js实现多列排序 根据业务逻辑调整 sortData 的数据. 排序的规则是按照第一列排序,第一列相同按照第二列排序,依次类推 // 要排序的数据 const array = [{ name: '甲' ...

  9. 使用Python插入100万条数据到MySQL数据库并将数据逐步写出到多个Excel

    Python插入100万条数据到MySQL数据库 步骤一:导入所需模块和库 首先,我们需要导入 MySQL 连接器模块和 Faker 模块.MySQL 连接器模块用于连接到 MySQL 数据库,而 F ...

  10. openstack-train-ovs-ceph 部署

    第一章 Openstack简介 https://baike.baidu.com/item/OpenStack/342467?fr=aladdin Openstack框架图![img](file:/// ...