Spring Cloud Gateway match multiple path elements
Asked Answered
L

2

6

Spring Cloud Greenwich puts spring-cloud-netflix-zuul in maintenance mode, so I am trying to migrate from Zuul to Spring Cloud Gateway.

With Zuul I have a route like

zuul:
  routes:
    masterplan_match:
      path: /**/masterplans/**
      serviceId: api-masterplan

What I am doing there is basically ignoring everything that comes before or after masterplan in the received path and redirect to api-masterplan. For example, both /some/path/to/masterplans and /masterplans goes to the same api (the reason being masterplans are a subresource of a bigger entity, which is responsible for creating new masterplans, but then masterplans can be treated as full blown resources for purposes like GETting details, updating, deleting).

Can I map this configuration to Spring Cloud Gateway? Looking at predicates, the feasible one seems to be path predicate, but then it looks like all matchers work on individual path elements (except the WildcardTheRestPathElement, which however can be used only as last element - I think), ie: I would need to write something like

spring
    cloud:
      gateway:
        routes:
        - id: masterplan_match
          uri: lb://api-masterplan # Coming from discovery client
          predicates:
          - Path=/some/path/to/masterplans/**, /masterplans/**

Am I missing something and can the two paths be condensed in one?

Loosejointed answered 25/1, 2019 at 7:55 Comment(4)
Does this solved ur problem ? - Path=/some/path/to/masterplans/**, /masterplans/**Avulsion
@Avulsion sorry for the late reply, afair it did not help. in the meantime I solved the problem somehow but also changed company and honestly don't remember how. plus there has probably been a lot of development on Spring Cloud Gateway, but unfortunately did not have any chance to use that project againLoosejointed
No Problem buddy. I resolved it anyway. Thanks for your reply. Cheers !Avulsion
Confirm this works now...couldn't find it anywhere in the docs tho....Dre
V
16

Spring Cloud Gateway support multiple patterns since version 2.1.0

spring
    cloud:
      gateway:
        routes:
        - id: masterplan_match
          uri: lb://api-masterplan # Coming from discovery client
          predicates:
          - Path=/some/path/to/masterplans/**, /masterplans/**

See Also:

Add support for multiple paths in path predicate #256

Multiple patterns in Host Route Predicate #589

Voltammeter answered 2/1, 2021 at 13:7 Comment(0)
P
0
   #Route for MAIN-SERVICE (running on port 8081)
        spring.cloud.gateway.routes[0].id= main_service
        spring.cloud.gateway.routes[0].uri=lb://main-service
        spring.cloud.gateway.routes[0].predicates[0]=Path=/v1/campaign/** , /v1/dashboard/** , \
          /v1/image/** , /v1/institute/**, \
          /v1/number/**, /v1/obd/** , \
          /v1/optout/** , /v1/report/** ,\
           /v1/sender/** , /v1/user/** , \
          /v1/validation/** , /v1/whatsapp/**
Pastore answered 27/9 at 5:24 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Lazurite

© 2022 - 2024 — McMap. All rights reserved.