当前位置: 首页 > news >正文

GateWay路由规则

Spring Cloud GateWay 帮我们内置了很多 Predicates功能,实现了各种路由匹配规
则(通过 Header、请求参数等作为条件)匹配到对应的路由

1 时间点后匹配 

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- After=2023-03-18T17:42:47.789-07:00[America/Denver]

2 时间点前匹配 

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- Before=2025-03-18T17:42:47.789+08:00[Asia/Shanghai]

3 时间区间匹配

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- Between=2022-03-18T17:42:47.789+08:00[Asia/Shanghai],2025-03-18T17:42:47.789+08:00[Asia/Shanghai]

4 指定Cookie正则匹配指定值

这个配置的含义是当接收到的请求中包含名为 sessionId 的 Cookie,且其值匹配正则表达式 \d+(表示任意数字)时,该请求将被路由到 http://example.org。换句话说,这个规则会筛选出带有特定 Cookie 值的请求,并将其转发到指定的 URI。

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- Cookie=sessionId, \d+

5 指定Header正则匹配指定值

这个配置指定了一个名为 consumer 的路由,它将请求转发到 https://consumer-service 服务上。该路由的谓词条件是请求头中的 requestinfo 的值必须是一个数字。

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- Header=requestinfo, \d+

6 请求Host匹配指定值

这个配置定义了一个名为 consumer 的路由,它将请求转发到 https://consumer-service 服务上。该路由的谓词条件是请求的 Host 头必须匹配 **.consumer-service.** 的正则表达式模式。

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- Host=**.consumer-service.**

7 请求Method匹配指定请求方式

指定请求类型是get 或者让post请求

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- Method=GET,POST

8 请求路径正则匹配

这个配置中的 Path=/consumer-service/** 表示当请求的路径以 /consumer-service/ 开头时,将请求转发到 https://consumer-service 服务上

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- Path=/consumer-service/**

9 请求包含某参数

在这个配置中,使用了 Query 谓词来匹配请求中的查询参数。具体来说,Query=name 表示只有请求中带有名为 name 的查询参数时,该路由规则才会被匹配。这意味着只有符合该条件的请求会被转发到 https://consumer-service 服务。

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- Query=name

10 远程地址匹配

在这个配置中,使用了 RemoteAddr 谓词来匹配请求的远程地址。具体来说,RemoteAddr=http://192.168.201.116 表示只有来自远程地址为 192.168.201.116 的请求才会被匹配到该路由规则。这意味着只有符合该条件的请求会被转发到 https://consumer-service 服务。

server:port: 8888
spring:application:name: gateway-servicecloud:nacos:discovery:server-addr: localhost:8848gateway:discovery:locator:enabled: trueroutes:- id: consumeruri: https://consumer-servicepredicates:- RemoteAddr=192.168.201.116

http://www.lryc.cn/news/323002.html

相关文章:

  • shell脚本基础改造
  • 静态综合实验
  • Spring Web MVC入门(6)
  • muduo异步日志
  • 在智慧能源的发展历程中,哪些技术的出现起到了关键性的作用?
  • SQLiteC/C++接口详细介绍sqlite3_stmt类(十三)
  • 扫雷(蓝桥杯,acwing)
  • macOS 通过 MacPorts 正确安装 MySQL 同时解决无法连接问题
  • Semi-supervised Open-World Object Detection
  • C语言实现射击小游戏
  • c++11 标准模板(STL)本地化库 - std::islower(std::locale) 检查字符是否被本地环境分类为小写
  • 粘度指数改进剂市场需求增长 为润滑油添加剂细分产品
  • LabVIEW柴油机安保监控系统
  • 实测国内AI大模型问答效果
  • 不得不等待的无奈 -《葡萄成熟时》
  • 【Python】Python中装饰器和魔法方法的区别
  • 【React】创建你的第一个React组件
  • 五分钟搞懂MySQL索引下推
  • 【数据库】SQL如何添加数据
  • ClickHouse01-什么是ClickHouse
  • 使用Docker搭建Nascab
  • Elasticsearch8.x版本Java客户端Elasticsearch Java API 如何并发修改
  • Docker 安装 Skywalking以及UI界面
  • mysql 空间查询 多边形内的点
  • 实际开发中,git版本切换操作
  • 线程池实现“线程复用”的原理
  • [Linux开发工具]——make/Makefile的使用
  • C++中的动态数组vector的基本操作
  • vsc ctrl+. 无效的问题
  • 科大讯飞开放平台-python语音转文字教程