SpringBoot+Prometheus+Grafana 打造一款高逼格的可视化监控系统

本文主要是介绍SpringBoot+Prometheus+Grafana 打造一款高逼格的可视化监控系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

点击关注公众号,利用碎片时间学习

一、背景

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

ab559a01907480dec142459e7d3ebf1c.png
关系图

二、开发SpringBoot应用

首先,创建一个SpringBoot项目,pom文件如下:

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-actuator</artifactId>  
</dependency>  
<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-web</artifactId>  
</dependency>  <dependency>  <groupId>org.projectlombok</groupId>  <artifactId>lombok</artifactId>  <optional>true</optional>  
</dependency>  <!-- https://mvnrepository.com/artifact/io.prometheus/simpleclient_spring_boot -->  
<dependency>  <groupId>io.prometheus</groupId>  <artifactId>simpleclient_spring_boot</artifactId>  <version>0.8.1</version>  
</dependency>  <dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-security</artifactId>  
</dependency>

注意: 这里的SpringBoot版本是1.5.7.RELEASE,之所以不用最新的2.X是因为最新的simpleclient_spring_boot只支持1.5.X,不确定2.X版本的能否支持。

MonitorDemoApplication启动类增加注解

package cn.sp;   import io.prometheus.client.spring.boot.EnablePrometheusEndpoint;   
import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector;   
import org.springframework.boot.SpringApplication;   
import org.springframework.boot.autoconfigure.SpringBootApplication;   
@EnablePrometheusEndpoint   
@EnableSpringBootMetricsCollector   
@SpringBootApplication   
public class MonitorDemoApplication {   public static void main(String[] args) {   SpringApplication.run(MonitorDemoApplication.class, args);   }   }

配置文件application.yml

server:   port: 8848   
spring:   application:   name: monitor-demo   security:   user:   name: admin   password: 1234   basic:   enabled: true   # 安全路径列表,逗号分隔,此处只针对/admin路径进行认证   path: /admin   # actuator暴露接口的前缀   
management:   context-path: /admin   # actuator暴露接口使用的端口,为了和api接口使用的端口进行分离   port: 8888   security:   enabled: true   roles: SUPERUSER

测试代码TestController

@RequestMapping("/heap/test")  
@RestController  
public class TestController {  public static final Map<String, Object> map = new ConcurrentHashMap<>();  @RequestMapping("")  public String testHeapUsed() {  for (int i = 0; i < 10000000; i++) {  map.put(i + "", new Object());  }  return "ok";  }  
}

这里的逻辑就是在请求这个接口后,创建大量对象保存到map中增加堆内存使用量,方便后面测试邮件报警。

启动项目后,可以在IDEA中看到有很多Endpoints,如图:

126ac68492743a178c979d4ece675384.png
enter description here

开始我的IDEA是不显示这个Endpoints,后来发现是我使用的idea版本太老了,还是2017.1的,  而这个需要 idea2017.2版本以上才能看到。

后来只好重新下载安装,弄了好久。。。。

启动完毕,访问http://localhost:8888/admin/prometheus就可以看到服务暴露的那些监控指标了。

b7985f25e13a259954130552e78abd57.png
监控指标

注意:

由于开启了安全认证,所以访问这个URL的需要提示输入账号/密码,如果提示404请检查下你的请求地址是否正确,如果不设置management.context-path则默认地址是http://ip:port/prometheus

三、安装Prometheus

下载地址

https://prometheus.io/download/

本文下载的是Windows版本prometheus-2.17.2.windows-amd64.tar.gz。

解压后修改prometheus.yml文件,配置数据采集的目标信息。

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: ['localhost:9090']   - job_name: 'monitor-demo'   scrape_interval: 5s # 刮取的时间间隔   scrape_timeout: 5s    metrics_path: /admin/prometheus   scheme: http    basic_auth: #认证信息   username: admin   password: 1234   static_configs:   - targets:   - 127.0.0.1:8888  #此处填写 Spring Boot 应用的 IP + 端口号

更多配置信息请查看官方文档。

现在可以启动Prometheus了,命令行输入:prometheus.exe --config.file=prometheus.yml

访问http://localhost:9090/targets,查看Spring Boot采集状态是否正常。

b320ed797001043e819ffb98360e88c1.png
采集目标信息

四、安装Grafana

下载地址

https://grafana.com/grafana/download

本文用到的是Windows版本grafana-6.3.3.windows-amd64.zip。

解压后运行bin目录下的grafana-server.exe启动,游览器访问http://localhost:3000即可看到登录页面,默认账号密码是admin/admin。

现在开始创建自己的可视化监控面板。

1.设置数据源

c9a8ed370a8de2943449cd2b2cd557dd.png

2. 创建一个Dashboard

21e9c5177cecb0d0e465d1a7c299a091.png 7a3dd37f9973ffb1238efafb762b85c9.png

3. 填写采集的指标点

7f8e8bcf4cd5ebccad3282bb28ee3f1a.png

注意: 这里的指标点不能随便填,必须是已有的可以在 Prometheus看到。

8c711d268601b060c7599c91b3f8a7e6.png
采集指标

4.选择图表样式

89fefd9211ab7293c4998b791275c7a2.png

5.填写标题描述

d786cc97cdb8bde2d4748b7488d9a0aa.png

最后点击右上角的保存,输入Dashboad的名称即可。

3b2e32d028b8348024367663a2c93ba9.png
结果展示

Tips: 这里的图表布局是可以用鼠标拖动的

五、添加邮件报警

在实际项目中当监控的某的个指标超过阈值(比如CPU使用率过高),希望监控系统自动通过短信、钉钉和邮件等方式报警及时通知运维人员,Grafana就支持该功能。

第一步: 点击[Alerting]——>[Notification channels]添加通知通道

5ee3fd1bf9df1f730b8aeb0bdd9a8c3e.png
创建通道
d273096712e2cb35ce60f280dd21557c.png
编辑

这里的Type有很多选项,包括webhook、钉钉等,这里以邮件为例。

第二步: 邮箱配置

Grafana默认使用conf目录下defaults.ini作为配置文件运行,根据官方的建议我们不要更改defaults.ini而是在同级目录下新建一个配置文件custom.ini。

以腾讯企业邮箱为例,配置如下:

#################################### SMTP / Emailing #####################  
[smtp]  
enabled = true  
host = smtp.exmail.qq.com:465  
user = xxxx@ininin.com  
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""  
password = XXX  
cert_file =  
key_file =  
skip_verify = true  
from_address = xxxx@ininin.com  
from_name = Grafana  
ehlo_identity = ininin.com

然后需要重启Grafana,命令grafana-server.exe -config=E:\file\grafana-6.3.3\conf\custom.ini

第三步: 为指标添加alert

dd47abe62a1483cdd6a51ca563ae4aa3.png
配置预警规则
a7a0e9fab558c6744f04c45a962d1d37.png
配置通知方式和信息

Evaluate every

表示检测评率,这里为了测试效果,改为1秒

For

如果警报规则配置了For,并且查询违反了配置的阈值,那么它将首先从OK变为Pending。从OK到Pending Grafana不会发送任何通知。一旦警报规则的触发时间超过持续时间,它将更改为Alerting并发送警报通知。

Conditions

when 表示什么时间,of 表示条件,is above 表示触发值
同时,设置了is above后会有一条红线。

If no data or all values are null

如果没有数据或所有值都为空,这里选择触发报警

If execution error or timeout

如果执行错误或超时,这里选择触发报警

注意: 下一次触发,比如10秒后,它不会再次触发,防止报警风暴产生!

第四步: 测试

请求http://localhost:8848/heap/test接口后,内存升高大于设置的阈值,然后就收到报警邮件。

e17138b4a2103edbf7e27db1017bba0c.png
报警邮件

这里图片没有显示出来,搞不懂为什么。

六、总结

这套监控功能还是挺强大的,就是Prometheus的表达式有点多。

附上几个链接:

  • https://prometheus.io/docs/introduction/first_steps/

  • https://grafana.com/docs/grafana/latest/

  • https://github.com/2YSP/monitor-demo

来源:cnblogs.com/2YSP/p/12827487.html 

推荐:

主流Java进阶技术(学习资料分享)

1bf35b7ee1e9ae0808cb2c8acc558e5c.png

PS:因为公众号平台更改了推送规则,如果不想错过内容,记得读完点一下“在看”,加个“星标”,这样每次新文章推送才会第一时间出现在你的订阅列表里。点“在看”支持我们吧!

这篇关于SpringBoot+Prometheus+Grafana 打造一款高逼格的可视化监控系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/194001

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

流媒体平台/视频监控/安防视频汇聚EasyCVR播放暂停后视频画面黑屏是什么原因?

视频智能分析/视频监控/安防监控综合管理系统EasyCVR视频汇聚融合平台,是TSINGSEE青犀视频垂直深耕音视频流媒体技术、AI智能技术领域的杰出成果。该平台以其强大的视频处理、汇聚与融合能力,在构建全栈视频监控系统中展现出了独特的优势。视频监控管理系统EasyCVR平台内置了强大的视频解码、转码、压缩等技术,能够处理多种视频流格式,并以多种格式(RTMP、RTSP、HTTP-FLV、WebS

不懂推荐算法也能设计推荐系统

本文以商业化应用推荐为例,告诉我们不懂推荐算法的产品,也能从产品侧出发, 设计出一款不错的推荐系统。 相信很多新手产品,看到算法二字,多是懵圈的。 什么排序算法、最短路径等都是相对传统的算法(注:传统是指科班出身的产品都会接触过)。但对于推荐算法,多数产品对着网上搜到的资源,都会无从下手。特别当某些推荐算法 和 “AI”扯上关系后,更是加大了理解的难度。 但,不了解推荐算法,就无法做推荐系

基于人工智能的图像分类系统

目录 引言项目背景环境准备 硬件要求软件安装与配置系统设计 系统架构关键技术代码示例 数据预处理模型训练模型预测应用场景结论 1. 引言 图像分类是计算机视觉中的一个重要任务,目标是自动识别图像中的对象类别。通过卷积神经网络(CNN)等深度学习技术,我们可以构建高效的图像分类系统,广泛应用于自动驾驶、医疗影像诊断、监控分析等领域。本文将介绍如何构建一个基于人工智能的图像分类系统,包括环境