【监控】Spring Boot+Prometheus+Grafana实现可视化监控

目录

1.概述

[2.spring actuator](#2.spring actuator)

3.Prometheus

3.1.介绍

3.2.使用

1.client端的配置

2.server端的配置

4.grafana

5.留个尾巴


1.概述

本文是博主JAVA监控技术系列的第四篇,前面已经聊过了JMX、Spring actuator等技术,本文我们就将依托于Spring actuator+监控组件prometheus+数据可视化组件grafana来实现对Spring Boot应用的可视化监控。

关于Spring actuator的相关内容,可移步:

Spring Boot 监控_springboot 监控-CSDN博客

【监控】spring actuator源码速读-CSDN博客

2.spring actuator

Spring actuator是spring体系中自带的监控组件,用来监测应用本身以及所处环境等内容。前文已经详细介绍过了,自处就不再重复和展开了。

依赖:

org.springframework.boot spring-boot-starter-actuator

配置:

management:

endpoints:

web:

exposure:

include: "*"

看一下效果:

以上这些指标都是actuator自带的,要适配Prometheus的数据采集格式的话需要引入promethus的适配层。ok,接下来我们讲一下Prometheus。

3.Prometheus

3.1.介绍

先看看整个prometheus的架构:

看起来很复杂对吧?其实并不复杂,最核心的就是Prometheus Server,其内置一个时序数据库,用来存采集上来的监控指标。然后就是Prometheus提供了很多client端,放在应用侧采集监控指标然后pull metrics(推送采集到的监控指标)个Prometheus Server。然后就是支持对接多种图形化界面如grafana来进行图形化的展示。最后就是支持各种方式的告警,如发送email等。

3.2.使用

1.client端的配置

即应用内的配置。

依赖:

复制代码
 <dependency>
   <groupId>io.micrometer</groupId>
   <artifactId>micrometer-registry-prometheus</artifactId>
   <scope>runtime</scope>
 </dependency>

配置:

management:

endpoints:

web:

exposure:

include: "*"

endpoint:

prometheus:

enabled: true

health:

show-details: always

可以看到promethus的监控指标已经出来了:

在主启动类里面,将应用的名字告诉Prometheus:

复制代码
@SpringBootApplication
public class Main {
 ?  public static void main(String[] args) {
 ? ? ?  SpringApplication.run(Main.class);
 ?  }
?
 ?  @Bean
 ?  MeterRegistryCustomizer<MeterRegistry> configure(@Value("${spring.application.name}") String applicationName){
 ? ? ?  return register -> {
 ? ? ? ? ?  register.config().commonTags("application", applicationName);
 ? ? ?  };
 ?  }
}

配置一下应用名:

spring:

application:

name: myApplicationDemo

2.server端的配置

先下载Prometheus,然后解压:

Download | Prometheus

配置Prometheus的prometheus.yml配置文件:

上面标红的要和应用对齐,否则会出现:

./prometheus启动Prometheus:

可以看到:

4.grafana

Grafana 是一个流行的开源数据可视化和监控工具,它提供了丰富的图表和仪表盘功能,用于展示和分析时间序列数据以及其他类型的数据。

官网下载地址:

Download Grafana | Grafana Labs

Linux的下载方式,官网上写的很清楚:

下载后解压即可使用。

./grafana-server启动应用,默认访问地址是IP:3000,默认账号密码都是admin。首次登录会要求修改密码。

登陆后,新建一个promethus的连接,配置好自己的promethus的地址和端口号保存即可:

在配置数据源的同时,可以选择grafana为我们准备的几套Prometheus的监控面板来使用:

我们选择其中一个,可以看到我们自己的应用了:

5.留个尾巴

ok,到这里聊完了吗?当然没有!

首先是如何自定义Prometheus的监控指标?其次grafana作为一款强大的数据可视化组件,功能当然很丰富,本文先讲解如何使用spring actuator+Prometheus+grafana实现基础的可视化监控,下文会详细介绍Prometheus和grafana的使用。

相关推荐
Voyager_424 分钟前
StringRedisTemplate 和 RedisTemplate 的区别是什么?
java·spring boot
小北方城市网1 小时前
SpringBoot 全局异常处理与接口规范实战:打造健壮可维护接口
java·spring boot·redis·后端·python·spring·缓存
Chan162 小时前
【 微服务SpringCloud | 方案设计 】
java·spring boot·微服务·云原生·架构·intellij-idea
hanqunfeng2 小时前
(三十三)Redisson 实战
java·spring boot·后端
计算机毕设指导62 小时前
基于微信小程序的运动场馆服务系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
小北方城市网3 小时前
SpringBoot 集成 MyBatis-Plus 实战(高效 CRUD 与复杂查询):简化数据库操作
java·数据库·人工智能·spring boot·后端·安全·mybatis
这儿有个昵称3 小时前
互联网大厂Java面试场景:从Spring Boot到微服务架构
java·spring boot·消息队列·微服务架构·大厂面试·数据库优化
hanqunfeng4 小时前
(四十)SpringBoot 集成 Redis
spring boot·redis·后端
小北方城市网4 小时前
SpringBoot 集成 MinIO 实战(对象存储):实现高效文件管理
java·spring boot·redis·分布式·后端·python·缓存
曹轲恒5 小时前
SpringBoot配置文件(1)
java·spring boot·后端