【监控】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的使用。

相关推荐
ะัี潪ิื4 小时前
springboot加载本地application.yml和加载Consul中的application.yml配置反序列化LocalDate类型差异
spring boot·consul·java-consul
m0_740043735 小时前
SpringBoot05-配置文件-热加载/日志框架slf4j/接口文档工具Swagger/Knife4j
java·spring boot·后端·log4j
招风的黑耳6 小时前
我用SpringBoot撸了一个智慧水务监控平台
java·spring boot·后端
大佐不会说日语~6 小时前
Spring AI Alibaba 的 ChatClient 工具注册与 Function Calling 实践
人工智能·spring boot·python·spring·封装·spring ai
Miss_Chenzr6 小时前
Springboot优卖电商系统s7zmj(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
程序员游老板6 小时前
基于SpringBoot3+vue3的爱心陪诊平台
java·spring boot·毕业设计·软件工程·课程设计·信息与通信
期待のcode6 小时前
Springboot核心构建插件
java·spring boot·后端
lvbinemail6 小时前
Grafana模板自动复制图表
数据库·mysql·zabbix·grafana·监控
Miss_Chenzr6 小时前
Springboot旅游景区管理系统9fu3n(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·旅游
五阿哥永琪7 小时前
Spring Boot 中自定义线程池的正确使用姿势:定义、注入与最佳实践
spring boot·后端·python