spring-cloud-gateway - 如何为路由设置默认前缀?
问题描述
我正在尝试配置 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
解决方案
是的 它是必需的。
StripPrefixGatewayFilterFactory
在将请求路径发送到下游之前剥离部分请求路径。请检查什么StringPrefixGatewayFilterFactory
。
https://cloud.spring.io/spring-cloud-gateway/reference/html/#_stripprefix_gatewayfilter_factory
如果您的请求路径为/some-api/swagger-ui.html
且配置为StripPrefix=1
,StripPrefixGatewayFilterFactory 将剥离请求路径的一部分。在这种情况下,剥离的部分是/some-api
。
要使其按您想要的方式工作,您必须剥离请求路径的一部分。
推荐阅读
- odoo - 如何在 odoo 9 中覆盖 session_gc() 方法?
- typescript - 角度 5 数组无法读取未定义的属性“”
- python - 熊猫数据框在多列上快速应用功能
- java - 最近屏幕后如何返回上一个活动
- icons - 如何在 react-styleguidist 中加载类似 Material-ui 的图标
- java - 考虑在你的配置中定义一个 'package' 类型的 bean
- julia - 为什么 Julia 1.0 无法写入 STDOUT 但 Julia 0.6 有效
- java - 使用 JUnit 5 如何测试特殊的标记测试方法
- xamarin - 我可以在 Xamarin.Forms 中结合创建元素和添加新元素吗?
- mysql - 如何使用 Bookshelf.js 编写条件更新查询?