spring-cloud-gateway icon indicating copy to clipboard operation
spring-cloud-gateway copied to clipboard

RouteLocatorBuilder does not use route order property

Open nstuart-idexx opened this issue 1 year ago • 1 comments

First, I'm not sure if this is a bug, or a lack of documentation, but when you use the RouteLocatorBuilder, the routes are never sorted, so using the route.order() method has no affect. I'm actually ok with this behavior, but if it is unintended I will future proof our configuration to add the order, even if it is not used at the moment.

The relevant bit in RouteLocatorBuilder:

        public RouteLocator build() {
            return () -> {
                return Flux.fromIterable(this.routes).map((routeBuilder) -> {
                    return (Route)routeBuilder.build();
                });
            };
        }

nstuart-idexx avatar Jan 18 '24 19:01 nstuart-idexx

You are right, it seems a bug.

kimmking avatar Jan 27 '24 14:01 kimmking

They are sorted later https://github.com/spring-cloud/spring-cloud-gateway/blob/67b612d4af1ad9882a95cd7db0025ef0c7633089/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/route/CachingRouteLocator.java#L62-L68

spencergibb avatar Mar 11 '24 20:03 spencergibb

@spencergibb , it is expected that someone doing code based config manually instantiate the CachingRouteLocator? Here's my bean definition

@Bean
fun routes(routeLocatorBuilder: RouteLocatorBuilder, config: RouteConfig): RouteLocator {
    val routes = routeLocatorBuilder.routes()
    config.userContexts.forEach { routes.addPathFor(it) }
    config.serviceRoutes.forEach { routes.addPathFor(it) }

    config.thirdPartyServers.forEach { thirdParty ->
        routes.addMissingSlashPath(thirdParty.path)
        routes.route { predicate ->
            predicate.path("${thirdParty.path}/**")
                .uri(thirdParty.service)
        }
    }
    return routes.build()
}

Which, by itself, never touches the CachingRouteLocator, and from what I can see nothing automatically wraps it in one either. It looks like it's easy enough to just new up the caching version with results from build(), but the difference in behavior between the two is never called anywhere, that I'm aware of.

nstuart-idexx avatar Mar 14 '24 17:03 nstuart-idexx

No, it is instantiated here https://github.com/spring-cloud/spring-cloud-gateway/blob/43eb8ceb3925149eb376a91cbb0146899c0f2577/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java#L257-L263

spencergibb avatar Mar 14 '24 17:03 spencergibb

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-cloud-issues avatar Mar 21 '24 17:03 spring-cloud-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-cloud-issues avatar Mar 28 '24 17:03 spring-cloud-issues