springcloud gateway routes 路由规则

  • uri:请求将被转发到的地址
  • predicates:匹配请求条件,决定哪些请求应该被路由
  • filters:对请求进行处理和转换
  1. 所有 test.com 的请求都被路由到 uri 指定的目的地
yaml 复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: test-route # 唯一标识符
          uri: http://localhost:10001 # 路由目的地
          predicates: # 路由规则配置
            - Host=test.com** # 域名规则配置,所有 test.com 的请求都被路由到 uri 指定的目的地
  1. 所有以 /brand 开始的请求都被路由到 uri 指定的目的地
yml 复制代码
---
spring:
  cloud:
    gateway:
      routes:
        - id: test-route # 唯一标识符
          uri: http://localhost:10001 # 路由目的地
          predicates: # 路由规则配置
            - Path=/brand/** # 所有以 /brand 开始的请求都被路由到 uri 指定的目的地
  1. 发送请求为:/api/brand/abc,满足 predicates 的匹配规则,然后 filters 通过 StripPrefix 去掉第一个前缀,转换为 /brand/abc,转发到 http://localhost:10001
yml 复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: test-route
          uri: http://localhost:10001
          predicates:
            - Path=/api/brand/**
          filters:
            - StripPrefix=1 # 去掉请求的第一个前缀
  1. 发送请求为:/abc,满足 predicates 的匹配规则,然后 filters 通过 PrefixPath 添加前缀,转换为 /brand/abc,转发到 http://localhost:10001
yaml 复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: test-route
          uri: http://localhost:10001
          predicates:
            - Path=/**
          filters:
            - PrefixPath=/brand # 添加前缀

StripPrefix 和 PrefixPath 一般不一起使用(一起使用也没问题)

StripPrefix 在前,PrefixPath 在后可以实现用户实际输入的路径无效,达到一定的保密效果

以下面配置为例,不管用户输入的是什么:/acs/ddd;/dsa/ddd...都会被转为 /brand/ddd

yaml 复制代码
predicates:
  - Path=/**
filters:
  - Path=/api/brand/**
  - PrefixPath=/brand # 添加前缀
相关推荐
SUPER526616 小时前
FastApi项目启动失败 got an unexpected keyword argument ‘loop_factory‘
java·服务器·前端
sanx1816 小时前
专业电竞体育数据与系统解决方案
前端·数据库·apache·数据库开发·时序数据库
你的人类朋友19 小时前
【Node】认识一下Node.js 中的 VM 模块
前端·后端·node.js
Cosolar19 小时前
FunASR 前端语音识别代码解析
前端·面试·github
@大迁世界21 小时前
Vue 设计模式 实战指南
前端·javascript·vue.js·设计模式·ecmascript
芭拉拉小魔仙21 小时前
Vue项目中如何实现表格选中数据的 Excel 导出
前端·vue.js·excel
jump_jump1 天前
妙用 localeCompare 获取汉字拼音首字母
前端·javascript·浏览器
U.2 SSD1 天前
Echarts单轴坐标系散点图
前端·javascript·echarts
德育处主任Pro1 天前
前端玩转大模型,DeepSeek-R1 蒸馏 Llama 模型的 Bedrock 部署
前端·llama
Jedi Hongbin1 天前
Three.js NodeMaterial 节点材质系统文档
前端·javascript·three.js·nodematerial