Spring Cloud - Using RestTemplate as a Load Balancer Client with NetFlix Ribbon

[Updated: Jul 6, 2019, Created: Sep 29, 2018]

Spring RestTemplate can be used for client side load balancing.

Client side vs server side load balancing

The multiple instances of the same microservice is run on different computers for high reliability and availability.

Server side load balancing is distributing the incoming requests towards multiple instances of the service.

Client side load balancing is distributing the outgoing request from the client itself.

Spring client side load balancing

Spring Netflix Eureka has a built-in client side load balancer called Ribbon.

Ribbon can automatically be configured by registering RestTemplate as a bean and annotating it with @LoadBalanced.

In this example we will focus on how to access a microservice instance transparently using RestTemplate and @LoadBalance.


Eureka Server Instance


  port: 7777
    hostname: localhost
    registerWithEureka: false
    fetchRegistry: false
public class HelloEurekaServerMain {
  public static void main(String[] args) {, args);

Run above class from IDE.

Creating hello microservice

public class HelloController {
  private AtomicLong counter = new AtomicLong();

  public HelloObject getHelloWordObject() {
      HelloObject hello = new HelloObject();
      hello.setMessage("Hi there! you are number " + counter.incrementAndGet());
      return hello;



public class HelloServiceMain{

	public static void main(String[] args) {, args);

Run above main class from IDE

Creating hello web client service



package com.logicbig.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

public class HelloWebClientServiceMain {

  RestTemplate restTemplate() {
      return new RestTemplate();

  public static void main(String[] args) {, args);
public class HelloWebClientController {
  private RestTemplate restTemplate;

  public String handleRequest(Model model) {
      //accessing hello-service
      HelloObject helloObject = restTemplate.getForObject("http://hello-service/hello", HelloObject.class);
      model.addAttribute("msg", helloObject.getMessage());
      return "hello-page";


<!DOCTYPE html>
<html xmlns=""

<h2>Hello Page</h2>
<div th:text="${msg}"/>
<div>Time: <span th:text="${time}"/></div>

Run above main class from IDE.

Accessing Eureka at http://localhost:7777/

Accessing web client service at http://localhost:9080/

The next example shows how to run multiple instances of our hello-service and access them via RestTemplate load balancer.

Example Project

Dependencies and Technologies Used:

  • Spring Boot 2.0.4.RELEASE
    Corresponding Spring Version 5.0.8.RELEASE
  • Spring Cloud Finchley.SR1
  • spring-cloud-starter-netflix-eureka-server 2.0.1.RELEASE: Spring Cloud Starter Netflix Eureka Server.
  • spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
  • spring-cloud-starter-netflix-eureka-client 2.0.1.RELEASE: Spring Cloud Starter Netflix Eureka Client.
  • spring-boot-starter-thymeleaf : Starter for building MVC web applications using Thymeleaf views.
    Uses org.thymeleaf:thymeleaf-spring5 version 3.0.9.RELEASE
  • JDK 1.8
  • Maven 3.5.4

Use RestTemplate for load balancing Select All Download
  • spring-cloud-rest-template-load-balancing
    • hello-eureka-server
      • pom.xml
      • hello-service
      • hello-web-client-service

    See Also