实时洞察应用健康:使用Spring Boot集成Prometheus和Grafana

1. 添加Prometheus和Actuator依赖

pom.xml中添加Spring Boot Actuator和Micrometer Prometheus依赖:

xml 复制代码
<dependencies>
<!--监控功能Actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    <!-- Micrometer Prometheus Registry -->
   <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>

    <!-- Micrometer核心库 -->
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-core</artifactId>
    </dependency>
</dependencies>

2. 配置Actuator端点

application.yml文件中配置Actuator以启用Prometheus的端点:

yml 复制代码
spring:
  application:
    name: kkxx-poi
  profiles:
    active: local
#Prometheus springboot监控配置
management:
  endpoints:
    web:
      exposure:
        include: "*"
# 指标
  metrics:
    export:
      prometheus:
        enabled: true
    tags:
      application: ${spring.application.name} # 暴露的数据中添加application label

3. 启动应用程序

启动Spring Boot应用程序。默认情况下,Prometheus指标将通过/actuator/prometheus端点暴露。

http://localhost:9091/actuator/prometheus

4. 配置Prometheus

在Prometheus的配置文件prometheus.yml中,添加Scrape配置以从Spring Boot应用程序中抓取指标:

yaml 复制代码
# 监控SpringBoot应用:kkxx-poi
  - job_name: 'kkxx-poi'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:9091']# 修改为Spring Boot应用程序的实际地址

确保Prometheus已启动并正在运行。

访问:http://localhost:9090/targets?search=

可以看到注册进来的 Springboot项目指标

5. 启动Grafana并配置Prometheus数据源

  1. 启动Grafana,并登录到Grafana的Web界面。
  2. 添加一个新的数据源:
    • 数据源类型选择Prometheus
    • URL字段中输入Prometheus的地址,通常为http://localhost:9090
  3. 保存并测试数据源,确保连接成功。

6. 创建Grafana仪表板

  1. 在Grafana中创建一个新的仪表板。
  2. 打开模板商店:https://grafana.com/grafana/dashboards/?search=Java
  3. 添加一个新面板,在查询编辑器中选择Prometheus数据源并编写PromQL查询来显示你感兴趣的指标。
  4. 下载对应json 并导入 使用方法

7. 验证和监控

访问你的Spring Boot应用程序并触发一些操作,返回到Grafana仪表板,查看实时的监控数据。

相关推荐
杨DaB6 小时前
【SpringBoot】Swagger 接口工具
java·spring boot·后端·restful·swagger
昵称为空C8 小时前
SpringBoot接口限流的常用方案
服务器·spring boot
hrrrrb8 小时前
【Java Web 快速入门】十一、Spring Boot 原理
java·前端·spring boot
创码小奇客10 小时前
架构师私藏:SpringBoot 集成 Hera,让日志查看从 “找罪证” 变 “查答案”
spring boot·spring cloud·trae
Olrookie11 小时前
XXL-JOB GLUE模式动态数据源实践:Spring AOP + MyBatis 解耦多库查询
java·数据库·spring boot
waynaqua11 小时前
SpringBoot:听说你还不知道时区设置
spring boot
又是努力搬砖的一年11 小时前
SpringBoot中,接口加解密
java·spring boot·后端
风象南12 小时前
SpringBoot 自研运行时 SQL 调用树,3 分钟定位慢 SQL!
spring boot·后端
Q_Q51100828515 小时前
python的软件工程与项目管理课程组学习系统
spring boot·python·django·flask·node.js·php·软件工程
2301_7930868716 小时前
SpringCloud 02 服务治理 Nacos
java·spring boot·spring cloud