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

相关推荐
时空无限2 小时前
grafana 切换 org 导致的页面跳转不可访问处理
grafana
wniuniu_2 小时前
testground local-grafana 环境排查
grafana
炸裂狸花猫2 小时前
开源监控体系Prometheus & Thanos & Grafana & Alertmanager
云原生·开源·prometheus·监控·thanos
披着羊皮不是狼3 小时前
多用户跨学科交流系统(4)参数校验+分页搜索全流程的实现
java·spring boot
q***23924 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
q***78784 小时前
Spring Boot的项目结构
java·spring boot·后端
百***17074 小时前
Spring Boot spring.factories文件详细说明
spring boot·后端·spring
Q_Q5110082854 小时前
python+django/flask的宠物用品系统vue
spring boot·python·django·flask·node.js·php
whltaoin5 小时前
【Java 微服务中间件】RabbitMQ 全方位解析:同步异步对比、SpringAMQT基础入门、实战、交换机类型及消息处理详解
spring boot·微服务·中间件·rabbitmq·spring amqt