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 # 添加前缀
相关推荐
大怪v9 小时前
AI抢饭?前端佬:我要验牌!
前端·人工智能·程序员
新酱爱学习9 小时前
字节外包一年,我的技术成长之路
前端·程序员·年终总结
小兵张健9 小时前
开源 playwright-pool 会话池来了
前端·javascript·github
IT_陈寒12 小时前
Python开发者必知的5大性能陷阱:90%的人都踩过的坑!
前端·人工智能·后端
codingWhat13 小时前
介绍一个手势识别库——AlloyFinger
前端·javascript·vue.js
代码老中医13 小时前
2026年CSS彻底疯了:这6个新特性让我删掉了三分之一JS代码
前端
不会敲代码113 小时前
Zustand:轻量级状态管理,从入门到实践
前端·typescript
踩着两条虫13 小时前
VTJ.PRO 双向代码转换原理揭秘
前端·vue.js·人工智能
扉川川13 小时前
OpenClaw 架构解析:一个生产级 AI Agent 是如何设计的
前端·人工智能
远山枫谷13 小时前
一文理清页面/组件通信与 Store 全局状态管理
前端·微信小程序