Route order in Spring Cloud Gateway
Asked Answered
G

3

5

We are implementing routes programmatically using a implementation of RouteDefinitionLocator. We have two services which should register at the same route path, where one of them is meant as a fallback if the other one does not exist. The preferred route with the specific path is:

RouteDefinition{id='CompositeDiscoveryClient_ms-1400133464', predicates=[PredicateDefinition{name='Path', args={pattern=/yambas/rest/apps/*/models/ms/**}}], filters=[FilterDefinition{name='RewritePath', args={regexp=/yambas/rest/apps/(?<remaining>.*), replacement=/ms/apps/${remaining}}}], uri=lb://ms, order=0}

And the more general one, which should only fire if other route was found:

RouteDefinition{id='CompositeDiscoveryClient_yambas-1171178634', predicates=[PredicateDefinition{name='Path', args={pattern=/yambas/rest/**}}], filters=[], uri=lb://yambas, order=0}

Thus, when calling /yambas/rest/apps/bla/models/ms, the first route should be used, and when calling /yambas/rest/apps/bla/models/otherms/*, the second (fallback ) should be used.

Problem is, that even the order attribute at the route seems not to solve this; currently we find no possibility to set up this order / priority of routes. Is this intendet?

Gere answered 6/5, 2019 at 5:53 Comment(2)
They are both order 0 which means it's random. Did you set an order and it isn't reflected? What version are you using?Hanoi
Hi Spencer, i tried setting the first one to order 50 and the second to 0 - then it does not find any route for /yambas/rest/apps/bla/models/msGere
G
3

It seems that we set the order the wrong way - setting the lower priority route to order 1 and the higher to 0 makes it work.

@spencergibb that would be definitely worth to be mentioned in the documentation.

Gere answered 7/5, 2019 at 14:22 Comment(0)
L
10

The more specific routes should appear first in the routing configuration, generic ones at the end.

For example: http://gateway-url/v1/accounts/** always matches with the route-id-1 while http://gateway-url/v1/** matches with route-id-2

spring:
  cloud:
    gateway:
      routes:
      - id: route-id-1
        uri: http://www.someurl.com
        order: 0
        predicates:
        - Path=/v1/accounts/** #<--
        filters:
        - RewritePath=/v1/accounts/(?<segment>.*), /$\{segment}
      - id: route-id-2
        uri: http://www.someurl1.com
        order: 1
        predicates:
        - Path=/v1/** #<--
        filters:
        - RewritePath=/v1/(?<segment>.*), /$\{segment}
Leis answered 18/6, 2021 at 21:41 Comment(1)
G
3

It seems that we set the order the wrong way - setting the lower priority route to order 1 and the higher to 0 makes it work.

@spencergibb that would be definitely worth to be mentioned in the documentation.

Gere answered 7/5, 2019 at 14:22 Comment(0)
Y
0

About the route order, it was answered at Is there a recommended order range for Filters in Spring Cloud Gateway?.

TL;DR
The order is based on org.springframework.core.Ordered interface, which lower values have higher precedence, as also mentioned by @andreas.

Yearround answered 12/9, 2023 at 22:58 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewSherfield

© 2022 - 2024 — McMap. All rights reserved.