百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

使用Prometheus和Grafana搭建SpringBoot应用监控系统

wptr33 2024-12-11 17:30 13 浏览

最近公司项目介绍的时候看到了类似监控系统的展示页面,比如资源利用、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,导入完成后即可看到效果。

相关推荐

每天一个编程技巧!掌握这7个神技,代码效率飙升200%

“同事6点下班,你却为改BUG加班到凌晨?不是你不努力,而是没掌握‘偷懒’的艺术!本文揭秘谷歌工程师私藏的7个编程神技,每天1分钟,让你的代码从‘能用’变‘逆天’。文末附《Python高效代码模板》,...

Git重置到某个历史节点(Sourcetree工具)

前言Sourcetree回滚提交和重置当前分支到此次提交的区别?回滚提交是指将改动的代码提交到本地仓库,但未推送到远端仓库的时候。...

git工作区、暂存区、本地仓库、远程仓库的区别和联系

很多程序员天天写代码,提交代码,拉取代码,对git操作非常熟练,但是对git的原理并不甚了解,借助豆包AI,写个文章总结一下。Git的四个核心区域(工作区、暂存区、本地仓库、远程仓库)是版本控制的核...

解锁人生新剧本的密钥:学会让往事退场

开篇:敦煌莫高窟的千年启示在莫高窟321窟的《降魔变》壁画前,讲解员指着斑驳色彩说:"画师刻意保留了历代修补痕迹,因为真正的传承不是定格,而是流动。"就像我们的人生剧本,精彩章节永远...

Reset local repository branch to be just like remote repository HEAD

技术背景在使用Git进行版本控制时,有时会遇到本地分支与远程分支不一致的情况。可能是因为误操作、多人协作时远程分支被更新等原因。这时就需要将本地分支重置为与远程分支的...

Git恢复至之前版本(git恢复到pull之前的版本)

让程序回到提交前的样子:两种解决方法:回退(reset)、反做(revert)方法一:gitreset...

如何将文件重置或回退到特定版本(怎么让文件回到初始状态)

技术背景在使用Git进行版本控制时,经常会遇到需要将文件回退到特定版本的情况。可能是因为当前版本出现了错误,或者想要恢复到之前某个稳定的版本。Git提供了多种方式来实现这一需求。...

git如何正确回滚代码(git命令回滚代码)

方法一,删除远程分支再提交①首先两步保证当前工作区是干净的,并且和远程分支代码一致$gitcocurrentBranch$gitpullorigincurrentBranch$gi...

[git]撤销的相关命令:reset、revert、checkout

基本概念如果不清晰上面的四个概念,请查看廖老师的git教程这里我多说几句:最开始我使用git的时候,我并不明白我为什么写完代码要用git的一些列指令把我的修改存起来。后来用多了,也就明白了为什么。gi...

利用shell脚本将Mysql错误日志保存到数据库中

说明:利用shell脚本将MYSQL的错误日志提取并保存到数据库中步骤:1)创建数据库,创建表CreatedatabaseMysqlCenter;UseMysqlCenter;CREATET...

MySQL 9.3 引入增强的JavaScript支持

MySQL,这一广泛采用的开源关系型数据库管理系统(RDBMS),发布了其9.x系列的第三个更新版本——9.3版,带来了多项新功能。...

python 连接 mysql 数据库(python连接MySQL数据库案例)

用PyMySQL包来连接Python和MySQL。在使用前需要先通过pip来安装PyMySQL包:在windows系统中打开cmd,输入pipinstallPyMySQL ...

mysql导入导出命令(mysql 导入命令)

mysql导入导出命令mysqldump命令的输入是在bin目录下.1.导出整个数据库  mysqldump-u用户名-p数据库名>导出的文件名  mysqldump-uw...

MySQL-SQL介绍(mysql sqlyog)

介绍结构化查询语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同数据库系统,可以使用相同...

MySQL 误删除数据恢复全攻略:基于 Binlog 的实战指南

在MySQL的世界里,二进制日志(Binlog)就是我们的"时光机"。它默默记录着数据库的每一个重要变更,就像一位忠实的史官,为我们在数据灾难中提供最后的救命稻草。本文将带您深入掌握如...