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 # 添加前缀
相关推荐
小小小小宇几秒前
大模型失忆问题探讨
前端
wordbaby3 分钟前
rn-cross-calendar:一个兼容 React 18/19、RN/RNOH 的跨平台日历组件
前端·react native·harmonyos
weixin_523185325 分钟前
Collections.unmodifiableMap详解:真的不可修改吗?
java·linux·前端
江米小枣tonylua6 分钟前
关掉 VSCode:在 NeoVim12 上配置 Claude Code
前端·程序员
2301_7736436216 分钟前
ceph镜像
前端·javascript·ceph
程序员黑豆37 分钟前
AI全栈开发之Java:什么是JDK
前端·后端·ai编程
To_OC38 分钟前
万字解析《JS语言精粹》之第四章:函数15大核心精髓(JS灵魂核心)
前端·javascript·代码规范
mqcode1 小时前
Vue3 + Element Plus + Vite 企业级后台框架搭建全流程
前端
SL-staff1 小时前
Web 白板技术架构深度解析:从渲染到协作的选型哲学
前端·架构
微扬嘴角1 小时前
react篇4--setState、LazyLoad和Hooks
前端·javascript·react.js