springcloud gateway routes 路由规则

文章详细解释了SpringCloudGateway中如何通过predicates配置域名和路径规则,以及filters如何处理请求,如StripPrefix用于去除请求前缀,PrefixPath添加前缀。展示了保密效果的实现方式。
摘要由CSDN通过智能技术生成
  • uri:请求将被转发到的地址
  • predicates:匹配请求条件,决定哪些请求应该被路由
  • filters:对请求进行处理和转换
  1. 所有 test.com 的请求都被路由到 uri 指定的目的地
spring:
  cloud:
    gateway:
      routes:
        - id: test-route # 唯一标识符
          uri: http://localhost:10001 # 路由目的地
          predicates: # 路由规则配置
            - Host=test.com** # 域名规则配置,所有 test.com 的请求都被路由到 uri 指定的目的地
  1. 所有以 /brand 开始的请求都被路由到 uri 指定的目的地
---
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
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
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

predicates:
  - Path=/**
filters:
  - Path=/api/brand/**
  - PrefixPath=/brand # 添加前缀
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快,把我桶也提着

如果对您有帮助欢迎支持哦~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值