Unable to use Weight Route Predicate Factory in spring cloud gateway when using java config class

28 Views Asked by At

Cannot resolve method 'predicate' in 'UriSpec'

I am trying to split incoming traffic to my gateway to two services based on split weight defined, so say 80:20, 80% of my requests are going to service1 and 20% to service2

Both service1 and service2 are just different versions of same service( for the purpose of a/b testing) and have same endpoints

I am able to get the expected behaviour through application.yaml approach but when i define a gatewayConfig in java i am getting errors

Working: Application.yaml

server:
  port: 9090
eureka:
  instance:
    hostname: localhost
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        - id: service1_route
          uri: lb://service1
          predicates:
            - Weight=my_group,80
          metadata:
            cluster: my_group
        - id: service2_route
          uri: lb://service2
          predicates:
            - Weight=my_group, 20
          metadata:
            cluster: my_group

Not Working:

GatewayConfig.java

@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
    return builder.routes()
            .route("service1_route", r -> r.path("/service1")
                    .metadata("cluster", "my_group")
                    .predicate(p -> p.name("Weight").args(a -> a.add("my_group", 80))))
            .uri("lb://service1")
            .route("service2_route", r -> r.path("/service2")
                    .metadata("cluster", "my_group")
                    .predicate(p -> p.name("Weight").args(a -> a.add("my_group", 20))))
            .uri("lb://service2")
            .build();
}
    }

I tried setting Weight Route Predicate Factory, to split my incoming traffic into 2 services, each having a specific weight so 80% of my traffic goes to service1 and 20% to service2 But in java config its not working

Please provide a java config approach so i can set the percentages dynamically and not hardcode it

0

There are 0 best solutions below