Grafana+Prometheus监控篇-SpringBoot服务

本文详细介绍了如何在SpringBoot应用中配置Prometheus监控,并通过Grafana展示监控数据。首先,通过引入相关依赖并配置Actuator以启用监控接口。接着,配置Prometheus的`prometheus.yml`文件,添加新的监控目标。然后,启动Grafana并展示监控面板。此外,文章还深入解析了HikariCP数据库连接池的监控指标,包括连接数、活跃连接、空闲连接等,帮助理解数据库连接池的运行状态。

一、SpringBoot配置.

   ①、引入依赖

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

   ②、开启监控

management:
  endpoints:
    web:
      exposure:
        include: prometheus

   ③、启动服务,查看监控采集:

http://test.code.com/skywalk/actuator/prometheus

二、定义Prometheus监控任务.

   ①、prometheus.yml

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]
  - job_name: 'Windows'
    static_configs:
    - targets: ['192.168.1.6:9182']
      labels:
        instance: Windows
  # 新添加的采集目标
  - job_name: "skywalk-boot"
    metrics_path: '/skywalk/actuator/prometheus'
    static_configs:
      - targets: ["192.168.1.6:80"] 

   ②、启动prometheus

  ③、 查看界面

 三、启动Grafana

  ①、直接启动即可.

springboot监控面板,推荐第一个非常不错.

Dashboards | Grafana Labs

### 依赖引入 在Spring Boot 3项目的`pom.xml`文件中添加相关依赖,以支持OpenTelemetry、Prometheus和Actuator等功能: ```xml <dependencies> <!-- Spring Boot Actuator for metrics --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- OpenTelemetry for tracing and metrics --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-tracing-bridge-opentelemetry</artifactId> </dependency> <dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-exporter-prometheus</artifactId> </dependency> <!-- Prometheus support --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> </dependencies> ``` ### 配置OpenTelemetry和Prometheus 在`application.properties`或`application.yml`中进行配置,启用OpenTelemetry和Prometheus相关功能: ```properties # Enable OpenTelemetry management.tracing.enabled=true # Configure OpenTelemetry exporter for Prometheus management.metrics.export.prometheus.enabled=true ``` ### 配置Prometheus服务 创建`prometheus.yml`文件,用于配置Prometheus如何采集Spring Boot应用的指标: ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'spring-boot-app' metrics_path: '/actuator/prometheus' static_configs: - targets: ['localhost:8080'] ``` 启动Prometheus服务,使用以下命令: ```sh prometheus --config.file=prometheus.yml ``` ### 配置Grafana 启动Grafana服务,登录Grafana控制台。在Grafana中添加Prometheus作为数据源,之后创建仪表盘,利用Grafana的可视化功能展示Prometheus采集的指标数据和OpenTelemetry的链路追踪数据。 ### 可选:集成Jaeger进行链路追踪可视化 若需要更强大的链路追踪可视化和分析功能,可集成Jaeger。添加Jaeger依赖: ```xml <dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-exporter-jaeger</artifactId> </dependency> ``` 配置Jaeger exporter: ```properties otel.traces.exporter=jaeger otel.exporter.jaeger.endpoint=http://localhost:14250 ``` ### 代码详解 Spring Boot 3.x官方推荐集成OpenTelemetry实现分布式追踪,替代Spring Cloud Sleuth,具有零侵入、轻量级、高性能的特点 [^2]。OpenTelemetry的Java Agent可自动插桩,降低接入成本,采集应用的Traces、Metrics和Logs数据。Prometheus采集这些指标数据,Grafana进行可视化展示。当Prometheus的指标告警时,可在Grafana面板上关联到Jaeger的链路追踪数据,方便定位问题 [^1]。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

大道之简

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值