Spring gateway - 如何只删除所有路由的前缀一次

时间:2021-04-13 06:31:21

标签: spring spring-cloud-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**

1 个答案:

答案 0 :(得分:0)

是的,使用 "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/**

参考:Introduction to spring cloud gateway