Cloud Native 6 min read

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.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Understanding Spring Cloud Hoxton M2 LoadBalancer: From Setup to Source Code

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaSpring CloudLoad Balancer
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.