spring - Spring gateway - 如何只为所有路由去除前缀一次
问题描述
我正在使用配置模式。多 (20) 条路线在哪里。但是我的服务器可以在 URL PATH 前缀 http://prefixHere/method:port?property=value 由于防火墙后面访问,并且无法更改。
所以当我有 20 种不同的方法(每个都以其他服务结束)时,我必须定义 20 次。
我只想定义一次 StripPrefix。这在以前的 Zuul 网关中有效。在云网关怎么做?
这是我的配置:
spring:
cloud:
gateway:
discovery:
locator:
lower-case-service-id: true
enabled: true
routes:
- id: auth-service
uri: lb://server-auth
predicates:
- Path=/prefixHere/auth/**
filters:
**- StripPrefix=1**
- id: operation-service
uri: lb://operation-service
predicates:
- Path=/prefixHere/operation/**
filters:
**- StripPrefix=1**
解决方案
是的,使用“default-filters:”,它将立即应用于所有路由。
spring:
application:
name: gateway
cloud:
gateway:
default-filters:
- StripPrefix=1
例如:
spring:
application:
name: GATEWAY-SERVICE
cloud:
gateway:
default-filters:
- StripPrefix=1
discovery:
locator:
enabled: true
lowerCaseServiceId: true
routes:
- id: department-service
uri: lb://department-service
predicates:
- Path=/api/departments/**
- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/api/users/**