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

1. 添加Prometheus和Actuator依赖

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

<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的端点:

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应用程序中抓取指标:

# 监控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仪表板,查看实时的监控数据。
在这里插入图片描述

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveCode.

永远年轻,永远热泪盈眶

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值