简体   繁体   English

Spring云网关路由URL模式配置

[英]Spring Cloud Gateway Route URL pattern configuration

I'm have implemented Spring Cloud Gateway with Eureka discovery service, everything works fine but I have seen something that I don't know how to deal with when I write the URL and if I don't put a / at the end of the URL the gateway redirects to the app directly using its actual URL (registered in Eureka).我已经使用 Eureka 发现服务实现了 Spring 云网关,一切正常,但我看到了一些我在编写 URL 时不知道如何处理的东西,如果我没有在末尾添加 / URL 网关直接使用其实际的 URL(在 Eureka 中注册)重定向到应用程序。

For example:例如:

Is there a configuration to avoid the first situation?有没有可以避免第一种情况的配置?

My configuration is the following:我的配置如下:

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: bar-service
          uri: lb://BAR-SERVICE/
          predicates:
            - Path=/bar/**
        - id: other-service
          uri: lb://OTHER-SERVICE/
          predicates:
            - Path=/OTHER/**

Additional information:附加信息:

  • I have a controller in every app that has '/' as an entry point (home page)我在每个以“/”作为入口点的应用程序中都有一个 controller(主页)
  • I can use java configuration instead if necessary如有必要,我可以改用 java 配置

Any advice will be appreciated!任何建议将被认真考虑! Cheers!干杯!

You should use RewritePath in gateway configuration.您应该在网关配置中使用 RewritePath。 The below is sample and hope it helpful to you.以下是示例,希望对您有所帮助。

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      routes:
        - id: bar-service
          uri: lb://BAR-SERVICE/
          predicates:
            - Path=/bar/**
          filters:
            - RewritePath=/bar(?<segment>.*), /$\{segment}

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

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