首页 > 解决方案 > 如何为路由设置默认前缀?

问题描述

我正在尝试配置 Spring-Cloud-Gateway。

基本目标是为其他服务添加路线,其中包括

/some-api/**       -> http://some-api/**
/some-other-api/** -> http://some-other-api.com/**

现在唯一的工作配置是这个。

spring:
  cloud:
    gateway:
      routes:
        - id: some-api
          uri: https://some-api.com
          predicates:
            - Path=/some-api/**
          filters:
            - StripPrefix=1 # REQUIRED?
        - id: some-other-api
          uri: https://some-other-api.com
          predicates:
            - Path=/some-other-api/**./g
          filters:
            - StripPrefix=1 # REQUIRED?

我的问题是这些- StripPrefix=1线是必需的吗?

当我注释掉它们时,路线不起作用。

源服务获取前缀请求,例如。

/some-api/swagger-ui.html -> http://some-api.com/some-api/swagger-ui.html

应该是

/some-api/swagger-ui.html -> http://some-api.com/swagger-ui.html

标签: spring-cloud-gateway

解决方案


是的 它是必需的。

StripPrefixGatewayFilterFactory在将请求路径发送到下游之前剥离部分请求路径。请检查什么StringPrefixGatewayFilterFactory
https://cloud.spring.io/spring-cloud-gateway/reference/html/#_stripprefix_gatewayfilter_factory

如果您的请求路径为/some-api/swagger-ui.html且配置为StripPrefix=1,StripPrefixGatewayFilterFactory 将剥离请求路径的一部分。在这种情况下,剥离的部分是/some-api

要使其按您想要的方式工作,您必须剥离请求路径的一部分。


推荐阅读