How to Achieve Client‑Side Load Balancing with Spring Cloud Ribbon and Feign
This tutorial walks through configuring Spring Cloud Ribbon and Spring Cloud Feign to perform client‑side load balancing for a compute-service, covering prerequisite setup, Maven dependencies, application code, REST client implementation, and verification of balanced requests across multiple service instances.
Spring Cloud Ribbon Overview
Spring Cloud Ribbon is a client‑side load‑balancing library built on Netflix Ribbon that works with HTTP and TCP, automatically integrating with Eureka or Consul to obtain service instance lists.
Prerequisites
Start the service registry (Eureka or Consul) and launch multiple instances of compute-service on different ports:
java -jar compute-service.jar --server.port=8081
java -jar compute-service.jar --server.port=8082Ribbon Consumer Application
Add the required Maven dependencies (spring‑cloud‑starter‑ribbon, spring‑cloud‑starter‑eureka, spring‑boot‑starter‑web, etc.) to pom.xml:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
... (other dependencies omitted for brevity) ...Create the main application class with discovery and a
@LoadBalanced RestTemplatebean:
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonApplication {
@Bean
@LoadBalanced
RestTemplate restTemplate() { return new RestTemplate(); }
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}Implement a controller that calls the add endpoint of compute-service via the load‑balanced RestTemplate:
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add() {
return restTemplate.getForEntity(
"http://COMPUTE-SERVICE/add?a=10&b=20", String.class).getBody();
}
}Configure the client in application.properties to point to the Eureka server:
spring.application.name=ribbon-consumer
server.port=3333
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/Running the consumer and invoking http://localhost:3333/add shows balanced calls to both compute‑service instances, as confirmed by the service logs.
Spring Cloud Feign Overview
Spring Cloud Feign provides a declarative REST client that automatically integrates with Ribbon and Eureka for load‑balanced HTTP calls.
Feign Setup
Add Maven dependencies for Feign and discovery (replace Eureka with Consul if needed):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
... (other dependencies omitted for brevity) ...Enable Feign in the main class:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}Define a Feign client interface that maps to the
compute-service /addendpoint:
@FeignClient("compute-service")
public interface ComputeClient {
@RequestMapping(method = RequestMethod.GET, value = "/add")
Integer add(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
}Inject the Feign client into a controller and delegate the call:
@RestController
public class ConsumerController {
@Autowired
ComputeClient computeClient;
@RequestMapping(value = "/add", method = RequestMethod.GET)
public Integer add() {
return computeClient.add(10, 20);
}
}Use the same application.properties configuration as the Ribbon example:
spring.application.name=feign-consumer
server.port=3333
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/After starting the Feign consumer, invoking http://localhost:3333/add yields the same balanced responses as the Ribbon approach, confirming that Feign also performs client‑side load balancing.
Both Ribbon (imperative RestTemplate) and Feign (declarative interface) enable seamless client‑side load balancing in Spring Cloud microservices, allowing developers to choose the style that best fits their project.
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.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
