Search code examples
springspring-bootspring-cloudspring-cloud-gateway

Did properties-based (from configuration server) override/replace java-based routes config?


I use server with defining of some routes in yml configuration, which stored in Consul Key/Value. When I'm trying to define route using Fluent API (Java based config), gateway doesn't work properly and doens't process this routes.

Example of server based config:

cloud:
    gateway:
      discovery:
        locator:
          enabled: false
      routes:
        - id: foo
          predicates:
            - Path=/foo/**
          uri: lb:https://bar

And defining routes in Fluent style:

    @Bean
    public RouteLocator routes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(p -> p
                        .path("/testing_route")
                        .filters(f -> f.addRequestHeader("Hello", "World"))
                        .uri("http://httpbin.org:80"))
                .build();
    }

As result gateway return 404 status code for all requests to the /testing_route path, which mean this route is not working.

In case of my problem i want to modify request body using ModifyRequestBodyFilter which based on DSL configuration, that means - I need to use both ways to configure context. In reality this code does nothing.

Can we combine RouteLocatorBuilder with property-based config in yml?

Spring Boot 2.2.5 RELEASE Spring Cloud Hoxton.SR3


Solution

  • Answered in issue thread https://github.com/spring-cloud/spring-cloud-gateway/issues/1953#issuecomment-705081934

    TL;DR Need to enable CachingRouteLocator

     @Bean 
     @Primary 
     @ConditionalOnMissingBean(name = "cachedCompositeRouteLocator") 
     // TODO: property to disable composite? 
     public RouteLocator cachedCompositeRouteLocator(List<RouteLocator> routeLocators) { 
        return new CachingRouteLocator(new CompositeRouteLocator(Flux.fromIterable(routeLocators))); 
     }