Understanding Spring Cloud Hoxton M2 LoadBalancer: From Setup to Source Code
This article introduces Spring Cloud Hoxton.M2's new load balancer implementation that replaces Ribbon, explains how to configure it with Maven, provides sample code for RestTemplate integration, delves into the source code of BlockingLoadBalancerClient and RoundRobinLoadBalancer, and compares its features with Ribbon's default strategies.
Background
In the morning I saw the announcement of Spring Cloud Hoxton.M2 release, shared it on my knowledge community, and a friend later asked about it. After spending half a day studying the spring-cloud-loadbalancer source code, I wrote this summary.
Spring Cloud Hoxton.M2 is the first version that integrates a new load balancer implementation to replace Ribbon.
How to Use
You must use the latest Hoxton.M2 version, so configure Spring's Maven repository proxy.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.M2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>Add the Nacos client (version 2.1.0) and be sure to exclude the Ribbon dependency, otherwise the load balancer will be ineffective.
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>Add the spring-cloud-loadbalancer dependency coordinates.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>Configuration is similar to Ribbon.
@Configuration
public class LbConfiguration {
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
@GetMapping("/demo")
public String doOtherStuff() {
return restTemplate.getForObject("http://big-provider-server/demo", String.class);
}
}Source Code Analysis
LoadBalancerClient Implementation
Currently only the BlockingLoadBalancerClient implementation is provided. (See Chinese comments in the source.)
public class BlockingLoadBalancerClient implements LoadBalancerClient {
@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
ServiceInstance serviceInstance = choose(serviceId);
return execute(serviceId, serviceInstance, request);
}
@Override
public ServiceInstance choose(String serviceId) {
ReactiveLoadBalancer<ServiceInstance> loadBalancer = loadBalancerClientFactory.getInstance(serviceId);
Response<ServiceInstance> loadBalancerResponse = Mono.from(loadBalancer.choose()).block();
return loadBalancerResponse.getServer();
}
}LoadBalancer Strategy Implementation
At present there is only one strategy: RoundRobinLoadBalancer, which selects servers in a round‑robin fashion.
public class RoundRobinLoadBalancer implements ReactorServiceInstanceLoadBalancer {
public Mono<Response<ServiceInstance>> choose(Request request) {
ServiceInstanceSupplier supplier = this.serviceInstanceSupplier.getIfAvailable();
return supplier.get().collectList().map(instances -> {
if (instances.isEmpty()) {
log.warn("No servers available for service: " + this.serviceId);
return new EmptyResponse();
}
int pos = Math.abs(this.position.incrementAndGet());
ServiceInstance instance = instances.get(pos % instances.size());
return new DefaultResponse(instance);
});
}
}Comparison with Ribbon
Default Load Balancing Comparison
Ribbon provides seven default load‑balancing strategies; the most commonly used is ZoneAvoidanceRule, which considers both the server's zone performance and its availability.
Configuration Richness
Currently spring-cloud-loadbalancer only supports retry‑related configuration.
Ribbon supports timeout, lazy loading, retry, and advanced properties such as Hystrix integration.
Conclusion
Honestly, just keep using Ribbon.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
