ProLesson 4.4Intermediate3 min readUpdated 31 October 2026
Client-Side Load Balancing & Eureka vs Kubernetes
Implement client-side load balancing with Spring Cloud LoadBalancer and compare Eureka with Kubernetes DNS discovery.
Once multiple instances of a microservice register with Eureka (e.g. PAYMENT-SERVICE on 10.0.1.5:8082 and 10.0.1.6:8082), how does the caller choose which instance to send requests to?
In Spring Cloud, the answer is Spring Cloud LoadBalancer.
01Configuring Spring Cloud LoadBalancer
Spring Cloud LoadBalancer replaces legacy Netflix Ribbon in Spring Boot 3.
Add the starter to pom.xml:
xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>The @LoadBalanced RestClient / RestTemplate Bean:
java
package com.veltoryx.order.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;
@Configuration
public class ClientConfig {
@Bean
@LoadBalanced // Tells Spring to intercept calls and resolve logical service names via Eureka!
public RestClient.Builder loadBalancedRestClientBuilder() {
return RestClient.builder();
}
}Using Logical Service Names:
java
@Service
public class PaymentClient {
private final RestClient restClient;
public PaymentClient(RestClient.Builder builder) {
// Notice the hostname is the Eureka service ID, NOT an IP or DNS domain!
this.restClient = builder.baseUrl("http://PAYMENT-SERVICE").build();
}
public PaymentResponse processPayment(PaymentRequest request) {
return restClient.post()
.uri("/api/v1/payments")
.body(request)
.retrieve()
.body(PaymentResponse.class);
}
}What Happens Behind the Scenes:
http://PAYMENT-SERVICE/api/v1/paymentsis intercepted byLoadBalancerInterceptor.- The load balancer queries Eureka's local cache for
PAYMENT-SERVICE. - It uses a Round-Robin algorithm to select
10.0.1.6:8082. - It rewrites the URI to
http://10.0.1.6:8082/api/v1/paymentsand executes the direct HTTP call.
Loading the rest of the lesson…