> 文章列表 > Prometheus监控Spring Cloud Gateway

Prometheus监控Spring Cloud Gateway

Prometheus监控Spring Cloud Gateway

📚概述

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: /actuatorexposure:# 包含端点,全用直接使用'*'即可,多个场景['prometheus','health']include: [ 'prometheus','health' ]# 排除端点exclude: [ 'shutdown' ]# JMX 端点配置属性jmx:exposure:include: [ 'prometheus' ]exclude: [ 'shutdown' ]metrics:tags:application: ${spring.application.name}export:prometheus:descriptions: trueenabled: 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官方的面板进行自定义。
自定义面板:

Prometheus监控Spring Cloud Gateway

🗞️指标选取

🧩监控指标选取

指标 PromQL
运行状态 up
近5分钟QPS sum 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的博客-CSDN博客_gateway_requests_seconds_count

ZAN8艺术网