Prometheus监控Spring Cloud Gateway

本文介绍了如何使用Prometheus来监控SpringCloudGateway,包括添加相关依赖,配置application.yml以暴露监控端点,以及调整Prometheus的配置文件以适应Gateway的指标。此外,还提到了Grafana面板的使用,特别是对于不同版本Gateway的兼容问题,以及推荐的监控指标如QPS和请求失败次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

📚概述

API网关作为应用服务与外部交互的入口,通过对API网关的监控,可以清晰的知道应用整体的请求量,以便根据不同的并发情况进行扩容处理。
API网关的监控也是相当必要的。

通过Prometheus监控Gateway与监控普通Springboot项目几乎没有区别。基本步骤都是引入pom依赖,然后修改端点暴露metrics接口即可。

📗Gateway配置

🔖gateway服务pom配置

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>${springboot.version}</version>
  </dependency>

  <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
      <version>1.9.6</version>
  </dependency>

🔊注意:
需要注意的是micrometer-registry-prometheus的版本号需要跟spring-boot-dependencies中定义的保持一致。Springboot较高版本的定义统一在micrometer-bom中,低版本的直接在spring-boot-dependencies中定义。

💡application.yml配置文件

--- # 暴露监控端点 配置
management:
  endpoints:
    # web端点配置属性
    web:
      # 默认端点前缀为/actuator,可修改
      base-path: /actuator
      exposure:
        # 包含端点,全用直接使用'*'即可,多个场景['prometheus','health']
        include: [ 'prometheus','health' ]
        # 排除端点
        exclude: [ 'shutdown' ]
    # JMX 端点配置属性
    jmx:
      exposure:
        include: [ 'prometheus' ]
        exclude: [ 'shutdown' ]
  metrics:
    tags:
      application: ${spring.application.name}
    export:
      prometheus:
        descriptions: true
        enabled: true

🔊注意:
按照实际使用情况,开放对应监控端点即可,为了保护应用安全,不使用的不开启

📙Prometheus相关配置

📃prometheus.yml配置

# consul服务发现配置
  - job_name: 'api_gatway'
    consul_sd_configs:
      - server: '10.0.107.55:8500' #consul的服务地址
        services: ["api_gateway"]
    relabel_configs:
      - source_labels: ["__meta_consul_tags"]
        regex: .*api_gateway.*
        action: keep
      - regex: __meta_consul_service_metadata_(.+)
        action: labelmap
    # 指标标签兼容,spring cloud  gateway 3.x版本前缀加了spring_cloud_
    metric_relabel_configs:
      - source_labels: [__name__]
        regex: 'gateway(.*)'
        target_label: '__name__'
        replacement: 'spring_cloud_gateway$1'
# file_sd服务发现配置
  - job_name: 'api_gateway'
    file_sd_configs:
      - files:
        - './api_gateway_config/*.json'
        refresh_interval: 15s
    # 指标标签兼容,spring cloud  gateway 3.x版本前缀加了spring_cloud_
    metric_relabel_configs:
      - source_labels: [__name__]
        regex: 'gateway(.*)'
        target_label: '__name__'
        replacement: 'spring_cloud_gateway$1'

🔊注意:
spring cloud gateway在不同的版本中指标名称不一致,在3.X版本中指标名称加了前缀spring_cloud_,所以在prometheus配置文件中使用metric_relabel_configs对指标进行统一处理

💡Grafana面板

官方面板:https://github.com/spring-cloud/spring-cloud-gateway/blob/main/docs/src/main/asciidoc/gateway-grafana-dashboard.json
Grafana中的面板:https://grafana.com/grafana/dashboards/11506-spring-cloud-gateway/ 编号11506

grafana官方提供的仅支持2.xgateway,对于3.xgateway存在问题。因此,我们在使用面板的时候同时兼容了2.x和3.x版本,需要根据gateway官方的面板进行自定义。
自定义面板:

image.png

🗞️指标选取

🧩监控指标选取

指标PromQL
运行状态up
近5分钟QPSsum by(instance) (rate(spring_cloud_gateway_requests_seconds_count{uri!~“.actuator.”}[5m]))
近5分钟请求失败次数sum by(instance) (increase(spring_cloud_gateway_requests_seconds_count{outcome!=“SUCCESSFUL”}[5m]))

📖参考资料

  1. gateway + prometheus + grafana - _Meditation - 博客园
  2. Springcloud gateway结合grafana简单自定义监控_kingpand的博客-优快云博客_gateway_requests_seconds_count
### Spring Cloud Gateway 集成 Prometheus监控配置示例 为了实现 Spring Cloud GatewayPrometheus 的集成,可以按照以下方法完成设置和配置: #### 1. 添加依赖项 在项目的 `pom.xml` 文件中引入必要的依赖项。这些依赖项包括 Spring Boot Actuator 和 Micrometer 支持 Prometheus 的库。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-core</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> ``` 上述代码片段展示了如何通过 Maven 构建工具来添加所需的依赖项[^3]。 #### 2. 启用端点暴露 修改 `application.yml` 或 `application.properties` 文件以启用 `/actuator/prometheus` 端点并允许外部访问。 对于 YAML 格式的配置文件,如下所示: ```yaml management: endpoints: web: exposure: include: "prometheus,health" metrics: export: prometheus: enabled: true ``` 此部分配置启用了 Prometheus 数据导出功能以及健康检查接口的公开[^4]。 #### 3. 自定义指标(可选) 如果需要自定义网关特定的行为或路径统计信息,则可以通过创建 Bean 来扩展默认行为。例如,下面是一个简单的计数器例子: ```java import io.micrometer.core.instrument.Counter; import io.micrometer.core.instrument.MeterRegistry; import org.springframework.stereotype.Component; @Component public class CustomMetrics { private final Counter requestCounter; public CustomMetrics(MeterRegistry registry) { this.requestCounter = Counter.builder("custom.requests.total") .description("Total number of custom requests.") .register(registry); } public void increment() { requestCounter.increment(); } } ``` 在此 Java 类中实现了对请求次数进行跟踪的功能[^5]。 #### 4. 测试与验证 启动应用程序后,在浏览器或者命令行工具里打开地址 http://localhost:<port>/actuator/prometheus ,应该能看到类似这样的输出数据: ``` # HELP jvm_memory_used_bytes The amount of used memory # TYPE jvm_memory_used_bytes gauge jvm_memory_used_bytes{area="nonheap",id="CodeHeap 'profiled nmethods'",} 8.70912e+06 ... ``` 这表明已经成功设置了基本的度量标准收集机制[^6]。 --- ###
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值