简体   繁体   English

Spring Cloud Gateway - 重写路径语法

[英]Spring Cloud Gateway - Rewrite path syntax

The following example is given in the documenation:文档中给出了以下示例:

spring:
  cloud:
    gateway:
      routes:
      - id: rewritepath_route
        uri: http://example.org
        predicates:
        - Path=/foo/**
        filters:
        - RewritePath=/foo/(?<segment>.*), /$\{segment}

I have a lot of services, so instead of hard-coding foo in the RewritePath filter, I'd simply like to drop that part dynamically.我有很多服务,所以我不想在RewritePath过滤器中对foo进行硬编码,而是想动态删除该部分。

I came up with this but it's not working我想出了这个,但它不起作用

spring:
  cloud:
    gateway:
      default-filters:
      - RewritePath=/(?<base>.*)/(?<segment>.*), /$\{segment}
      routes:
      - id: rewritepath_route
        uri: http://example.org
        predicates:
        - Path=/foo/**

How does the correct reg exp syntax look like?正确的 reg exp 语法是什么样的?

I added a ?我加了一个? operator after <base>.* to match only the first part of the path. <base>.*之后的运算符仅匹配路径的第一部分。 Got it from here这里得到

spring:
  cloud:
    gateway:
      default-filters:
      - RewritePath=/(?<base>.*?)/(?<segment>.*), /$\{segment}
      routes:
      - id: rewritepath_route
        uri: http://example.org
        predicates:
        - Path=/foo/**

Also I didn't realize that it's just plain java regex, thanks @spencergibb.另外我没有意识到它只是普通的 Java 正则表达式,谢谢@spencergibb。

(Looking at the source code it's not magic at all, if someone is stuck with a similar problem I encourage to do the same. It's easily readable code which does what it promises to do.) (查看源代码一点也不神奇,如果有人遇到类似的问题,我鼓励他们做同样的事情。它是易于阅读的代码,可以完成它所承诺的事情。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM