Andreas
Andreas

Reputation: 2309

Route order in Spring Cloud Gateway

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?

Upvotes: 5

Views: 10373

Answers (3)

Eduardo Cabral
Eduardo Cabral

Reputation: 1

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.

Upvotes: 0

Purushotham CK
Purushotham CK

Reputation: 474

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}

Upvotes: 10

Andreas
Andreas

Reputation: 2309

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.

Upvotes: 3

Related Questions