springcloud gateway routes 路由规则

  • 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 # 添加前缀

相关推荐

  1. springcloud gateway routes 规则

    2023-12-22 11:44:03       37 阅读
  2. Elasticsearch索引数据的规则与自定义分发

    2023-12-22 11:44:03       11 阅读
  3. Elasticsearch搜索优化-自定义规划(routing)

    2023-12-22 11:44:03       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-22 11:44:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-22 11:44:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-22 11:44:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-22 11:44:03       18 阅读

热门阅读

  1. .sync修饰符

    2023-12-22 11:44:03       39 阅读
  2. 决策树和随机森林算法 简介

    2023-12-22 11:44:03       39 阅读
  3. react:useContent

    2023-12-22 11:44:03       38 阅读
  4. 软件设计模式:单例模式

    2023-12-22 11:44:03       40 阅读
  5. 【leetcode刷题之算法】

    2023-12-22 11:44:03       52 阅读
  6. ffprobe工具

    2023-12-22 11:44:03       38 阅读
  7. Milvus实战:构建Q&A系统及推荐系统

    2023-12-22 11:44:03       53 阅读
  8. ros2/ros1中的cmakelists.txt文件解释

    2023-12-22 11:44:03       40 阅读