使用Prometheus和Grafana搭建SpringBoot应用监控系统
wptr33 2024-12-11 17:30 15 浏览
最近公司项目介绍的时候看到了类似监控系统的展示页面,比如资源利用、GC次数、Kafka生产消费等,清晰明了,页面十分酷炫。“这是怎么实现的呢??”
原来是Grafana!!这么好的东西,竟然现在才看到。
简介:Grafana是一款Go语言开发的开源数据可视化工具,可以做数据监控和数据统计,还提供了很多“仪表板”,可以实现很多炫酷且实用的可视化指标。
技术点:Spring boot actuator/micrometer(收集)、Prometheus(存储聚合)、Grafana(可视化)
个人工作中目前应该不涉及到这方面的开发,所以暂时不做深究,先只是满足下好奇心,简单记录下基本的使用过程,更多细节原理,大家可自行了解。
推荐这篇参考文章:https://www.cnblogs.com/throwable/p/13257557.html,有关于度量指标框架Micrometer的详细介绍。
核心思路
以spring boot项目为例,Spring Boot Actuator提供了很多监控指标,可以通过RestFul的形式访问查看,但原始的为json数据,而非上图展示的页面。并且只是瞬时值,不能提供段时间内数据的的聚合分析等。当然这些也可以自行通过数据持久化,并开发前端页面的方式实现。而通过了解,目前已有这样的轮子,就是Prometheus,内部实现时序数据库,可以将收集到的监控数据存储,并且可以结合Grafana进行数据可视化,目前Prometheus已经成为热门且通用的监控解决方案。
关键组件之Exporter:作用类似转换器,各种被监控的对象可以基于共同的Prometheus提供的规范进行实现,从而让自己都能够接入到Prometheus。详细原理可参考文章https://zhuanlan.zhihu.com/p/273229856
新建spring boot 项目
添加依赖
添加actuator是支持输出监控信息,micrometer-registry-prometheus是能够把actuator监控信息,转化为prometheus能够处理的格式。
<!--添加prometheus和actuator依赖-->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
修改application.yml配置文件
在配置文件中,配置endpoint暴露Prometheus,并允许将指标metrics导入到Prometheus中。
server:
port: 10010
spring:
application:
# 应用名,后续会以此展示
name: Spring Boot Monitor
# 监控相关配置
# 开启监控
management:
endpoints:
web:
exposure:
include: "*"
# 端点
endpoint:
prometheus:
enabled: true
health:
show-details: always
# 指标
metrics:
export:
prometheus:
enabled: true
添加指定的应用名
可以直接通过配置文件引用
metrics:
tags:
application: ${spring.application.name}
也可通过启动类,注入
package com.panda00hi.springbootmonitor;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringBootMonitorApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMonitorApplication.class, args);
}
/**
* 注册应用名,后续监控中展示该应用名
*
* @param applicationName 取配置文件中的应用名
*/
@Bean
MeterRegistryCustomizer<MeterRegistry> configure(@Value("${spring.application.name}") String applicationName) {
return (registry -> registry.config().commonTags("application", applicationName));
}
}
启动项目
访问http://localhost:10010/actuator,已经可以得到监控信息,并且包含prometheus的可以访问
访问http://localhost:10010/actuator/prometheus,也可以得到监控信息(prometheus格式的)
配置Prometheus
利用docker搭建。
参考官方文档:https://prometheus.io/docs/prometheus/latest/getting_started/
下载镜像
docker pull prom/prometheus
配置文件
创建本地用于挂载的数据卷目录/mydata/prometheus,并新建配置文件。
参考官方的配置。最后配置自己的应用配置。
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
# 额外添加的任务配置,每隔五秒钟抓取数据
- job_name: 'springboot-prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
# spring boot服务运行的服务器地址。我是物理机运行的,填写的是本机地址
- targets: ['192.168.3.47:10010']
启动容器
docker run -d --name=prometheus \
-p 9090:9090 \
-v /mydata/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
-d prom/prometheus
浏览器访问prometheus默认地址http://172.16.224.100:9090,可以正常访问首页,http://172.16.224.100:9090/metrics可以获取到数据。
选择【status】-》【targets】,即可看到之前配置文件中配置的springboot信息。
配置Grafana
下载镜像
docker pull grafana/grafana
启动容器
docker run -d --name=grafana -p 3000:3000 grafana/grafana
访问页面
默认端口是3000,默认用户名密码都是admin
172.16.224.100:3000
登陆成功,有那种感觉了,不得不说页面UI颜值真的很高。
添加prometheus数据源
添加数据源,选择prometheus,并配置URL
点击保存按钮,成功会提示
选择合适的仪表盘
官方提供了很多https://grafana.com/grafana/dashboards/
如,Grafana提供的JVM面板,记住右侧的编码。在自己的Grafana页面导入该编码即可。
导入编码
选择数据源prometheus,导入完成后即可看到效果。
- 上一篇:线上应用诊断与调试利器——Arthas
- 下一篇:PHP 字符串函数详解
相关推荐
- 「网络安全」JAVA代码审计——XXE外部实体注入
-
一、WEB安全部分想要了解XXE,在那之前需要了解XML的相关基础二、XML基础...
- Web前端面试题目及答案汇总(web前端面试题最新)
-
Web前端面试题目及答案汇总来源:极客头条以下是收集一些面试中经常会遇到的经典面试题以及自己面试过程中无法解决的问题,通过对知识的整理以及经验的总结,重新巩固自身的前端基础知识,如有错误或更好的答案,...
- 什么是脚本文件?与可执行文件有什么不同?
-
今天的内容是脚本文件和可执行文件是两种不同类型的计算机文件,它们在结构和执行方式上有显著区别。脚本文件:定义与特性...
- 20个实用Python运维脚本(收藏级)(python 运维工具)
-
系统环境:支持Linux(Ubuntu/CentOS/Debian)和Windows...
- 2026年前每个开发者都应该学习的技能
-
优秀开发者...
- Linux 如何每 5、10、15 或 30 分钟运行一次 Cron 作业?
-
在Linux系统中,Cron是一个强大的工具,用于自动化重复性任务。通过合理配置...
- Shell脚本编程进阶实战:从入门到高效自动化
-
Shell脚本编程进阶实战:从入门到高效自动化一、参数处理进阶:打造专业级CLI工具1.高级参数解析示例...
- 在Bash中按分隔符拆分字符串的方法
-
技术背景在Bash脚本编程中,经常会遇到需要按特定分隔符拆分字符串的需求,例如处理CSV文件、解析日志等。掌握字符串拆分的方法对于数据处理和脚本自动化非常重要。...
- 程序员用5分钟,把一个400多MB的苹果安装包削掉了187MB
-
丰色发自凹非寺量子位|公众号QbitAI前些日子,一个...
- 如何在 Windows 上编写批处理脚本
-
你知道如何使用命令提示符吗?如果这样做,您可以编写一个批处理文件。在最简单的形式中,批处理文件(或批处理脚本)是双击文件时执行的几个命令的列表。批处理文件一直回到DOS,但仍然适用于现代版本的Win...
- 一文搞懂shell脚本(shell脚本应用实战)
-
一文搞懂shell脚本1、shell脚本介绍什么是shell脚本...
- 一文讲清ShellScript脚本编程知识
-
摘要:本文详尽地讲述了ShellScript的基础内容,还有它在Linux系统里的运用情况,涵盖了它的基本语法、常用的命令以及高级的功能。ShellScript可是一种简单又非常实用的编...
- 在Bash脚本中获取自身所在目录的方法
-
技术背景在使用Bash脚本时,有时需要获取脚本自身所在的目录。比如,当脚本作为另一个应用程序的启动器时,需要将工作目录更改为脚本所在的目录,以便对该目录中的文件进行操作。然而,由于脚本的调用方式多样(...
- shell中如何确定脚本的位置?这篇文章告诉你
-
我想从同一个位置读取一些配置文件,如何确定脚本的位置?。这个问题的出现主要是由两个原因引发的:一是您希望将脚本的数据或配置进行外部化,因此需要一种方式来寻找这些外部资源;二是您的脚本需要对某些捆绑资源...
- bash shell 语法(bash命令用法)
-
下面是**Shell(Bash)语法的常用知识点总结**,适合初学者和日常脚本编写参考。内容涵盖变量、判断、循环、函数、重定向、正则、数组等常见用法。---#Shell(Bash)语法速查总结...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)