I'm using spring cloud load balancer as a client side load balancer in a spring microservice project. I know by default Spring load balancer uses round robin strategy. But for my use case Least connections
strategy is best suited. Where the instance handling least number of connections get the request routed to it. After digging through docs I have found @LoadBalancerClient
annotation lets you canfigure the routing algorithm.
So, I did this:
@Configuration
@LoadBalancerClient(configuration = BalancerConfig.class)
public class RestConfig {
@Bean
@LoadBalanced
RestTemplate restClient(RestTemplateBuilder builder) {
return builder.build();
}
}
For the class BalancerConfig.java I have:
@Configuration
public class BalancerConfig {
@Bean
ReactorLoadBalancer<ServiceInstance> randomLoadBalancer(Environment environment,
LoadBalancerClientFactory loadBalancerClientFactory) {
String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
// currently selects an instance randomly of a given service.
// but I want to switch it to use the instance that is currently having least
// traffic
return new RandomLoadBalancer(loadBalancerClientFactory
.getLazyProvider(name, ServiceInstanceListSupplier.class),
name);
}
}
Please tell me is it even possible with Spring application gateway?
If you want all clients to use a custom policy, you just only need to define a ReactorLoadBalancer bean as below
But if you want to customize a different policy for each client,you need to do like this
Configure two routes
Run GateWayApplication
In this way, the first route uses RoundRobinLoadBalancer, and the second route uses RandomLoadBalancer.