当前位置: 首页 > news >正文

spring boot + Prometheus + Grafana 实现项目监控

一、引入依赖

<dependencies>
    <!-- Spring Boot Starter Actuator -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- Micrometer Registry for Prometheus -->
    <dependency>
        <groupId>io.micrometer</groupId>
        <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>
</dependencies>

二、ymal配置

# 配置actuator监视器的全部监控端点打开
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    prometheus:
      enabled: true
    health:
      show-details: always
  metrics:
    export:
      prometheus:
        enabled: true # 启用Prometheus

spring:
  application:
      name: easy-brilliant

三、配置数据格式转化

package easy.brilliant.configuration;

import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.prometheus.PrometheusConfig;
import io.micrometer.prometheus.PrometheusMeterRegistry;
import io.prometheus.client.CollectorRegistry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.actuate.autoconfigure.metrics.MeterRegistryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PrometheusConfiguration {

    @Bean
    public CollectorRegistry collectorRegistry() {
        return new CollectorRegistry();
    }

    @Bean
    public PrometheusMeterRegistry prometheusMeterRegistry(PrometheusConfig config, CollectorRegistry collectorRegistry) {
        return new PrometheusMeterRegistry(config, collectorRegistry, Clock.SYSTEM);
    }

    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurePrometheus(@Value("${spring.application.name}") String name){
        return (registry -> registry.config().commonTags("application",name));
    }

}

四、安装并配置Prometheus

下载地址:https://prometheus.io/ (下载最新版) 我的csdn地址:https://download.csdn.net/download/weixin_42172472/90579623

prometheus 配置:

# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  - job_name: 'easy-brilliant'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8047'] # 使用你的Spring Boot应用程序的实际主机和端口替换

测试是否配置成功,请求 http://127.0.0.1:9090/targets,出现如下界面就算请求成功

五、安装并配置Grafana

下载Grafana : Download Grafana | Grafana Labs  ,我的csdn地址:https://download.csdn.net/download/weixin_42172472/90579615

安装并启动grafana-server.exe ,访问 http://127.0.0.1:3000/

1.Grafana 数据源配置

配置 Connection,点击Save & test 测试是否配置成功。

2.面板配置

点击Import 

输入ID 21319,选择数据源Prometheus,点击load,监控成功

相关文章:

  • 2️⃣ Coze创建智能体教学(2025年全新版本)
  • 探索轻量高性能的 Rust HTTP 服务器框架 —— Hyperlane
  • 内存管理及内建函数
  • AI芯片混战:GPU vs TPU vs NPU的算力与能效博弈
  • 嵌入式软硬件开发,常见通信总线
  • Spring启示录、概述、入门程序以及Spring对IoC的实现
  • Spring Boot 框架注解:@ConfigurationProperties
  • Java文件流操作 - 【Guava】IO工具
  • React 列表与 Keys 的深入探讨
  • 聊聊Spring AI的PgVectorStore
  • OpenCV 图形API(17)计算输入矩阵 src 中每个元素的平方根函数sqrt()
  • oklink js逆向(入口定位)
  • 1.2 测试设计阶段:打造高质量的测试用例
  • c++ 函数后面加const 作用
  • kaggle竞赛——房价预测
  • 轨迹预测Physical Plausibility-aware Trajectory Prediction via Locomotion Embodiment
  • 基于 Vue 3 + html2canvas 实现网页任意区域截图组件
  • 抓wifi无线空口包之Ubuntu抓包(二)
  • Linux-CentOS-7—— 安装MySQL 8
  • Kafka 中的幂等机制
  • 禅城网站建设公司/网络推广服务