spring-cloud / spring-cloud/spring-cloud-gateway

Spring Cloud Gateway MVC does not honor the route "order" attribute

Open
#3,495 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

waiting-for-triage
Dominant language
Java
Stars
4.9k
Forks
3.5k
Avg merge
20h 57m
Merged PRs (30d)
8

Description

Version: org.springframework.cloud:spring-cloud-starter-gateway-mvc -> 4.1.5

org.springframework.cloud.gateway.server.mvc.config.RouteProperties defines an attribute order that presumably would control the order that routes are evaluated. I would like to maintain the routes in configuration such that the route ids are in alphabetical, and in the case of overlapping predicates where order is important, use the order attribute to control evaluation order. When I tried this though, it did not work, and I had to change the physical order of the route definitions to get the correct behavior.
It appears that either the order attribute should be deprecated and it should be documented that the physical order of routes is the only way to control evaluation order, or some sorting of the routes by order should be implemented such that when the order is specified, it is honored.

This is what I would like my configuration to look like:

        - id: route-1
          order: 2
          uri: lb://service1
          predicates:
            - Path=/api/v1/resource/**
          filters:
            - RewritePath=/api/v1/resource/(?<segment>.*),/api/service1/v1/resource/$\{segment}
        - id: route-2
          order: 1
          uri: lb://service2
          predicates:
            - Path=/api/v1/resource/sub-resource/**
          filters:
            - RewritePath=/api/v1/resource/sub-resource/(?<segment>.*),/api/service2/v2/sub-resource/$\{segment}

but using this configuration all requests to /api/v1/resource/** were routed using "route-1", nothing used "route-2".

I needed to revise my configuration as follows to get the desired behavior:

        - id: route-2
          uri: lb://service2
          predicates:
            - Path=/api/v1/resource/sub-resource/**
          filters:
            - RewritePath=/api/v1/resource/sub-resource/(?<segment>.*),/api/service2/v2/sub-resource/$\{segment}
        - id: route-1
          uri: lb://service1
          predicates:
            - Path=/api/v1/resource/**
          filters:
            - RewritePath=/api/v1/resource/(?<segment>.*),/api/service1/v1/resource/$\{segment}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with org.springframework.cloud.gateway.server.mvc.config.RouteProperties and reproduce the two overlapping Path predicates from the issue configuration. Trace how the MVC gateway evaluates those configured routes and determine whether the order attribute is intended to control evaluation or should be deprecated and documented. Done means the behavior is resolved consistently for the supplied route pair.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.