@LoadBalanced RestTemplate Call Internal Microservice

In this tutorial, you will learn how to create an instance of @LoadBalanced RestTemplate and make it communicate with an internal microservice.

For a step by step series of video lessons, please check this page: Spring Boot Microservices and Spring Cloud.

Create RestTemplate Bean

To create an instance of RestTemplate, you will need to first make sure your Spring Boot project has the following dependency spring-boot-starter-web.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Once you have the spring-boot-starter-web dependency in the pom.xml of your Spring Boot project, you can create an instance of RestTemplate the following way.

@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

Please note the @LoadBalanced annotation. The @LoadBalanced annotation will make an instance of created RestTemplate load-balanced. There is no code you need to write to make RestTemplate load-balance HTTP request it sends to an internal microservice. The RestTemplate bean will be intercepted and auto-configured by Spring Cloud.

Inject Load-Balanced RestTemplate

You will inject RestTemplate the same way you are injecting any other objects in your Spring Boot application. Although if you have more than one RestTemplate bean, you will need to specify which one you are injecting.

When autowiring the RestTemplate, use @LoadBalanced annotation.

@Autowired
@LoadBalanced
RestTemplate restTemplate;

Start More Than One Instance of Internal Microservice

To see how load-balancing works with internal Microservice, you will need to start more than one instance of an internal Microservice that is being consumed by RestTemplate. If you have only one internal microservice running, you will not really see how load-balancing works.

To be able to start more than one instance of internal microservice you will need to do the following two changes in application.properties file of your internal microservice.

Set the server port to zero

To start more than one instance of internal microservice, you will need to set the port number to zero. Setting the server port to zero will help you start each instance of internal microservice on different random ports. Open application.properties file of a microservice project and set port number the following way:

server.port=0

Set Unique Instance Id

To help Eureka distinguish multiple instances of your internal microservices, you will need to make your microservice register with Eureka using a unique client instance-id. Open application.properties file of your internal microservice and add the following.

eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}

RestTemplate Call Internal Microservice

To make your RestTemplate instance communicate with internal microservice you will need to use the application name of a destination microservice in the request URL.

For example, in the below code snippet I am using RestTemplate to send HTTP GET request to an internal Microservice that is registered with Eureka discovery service with the name ALBUMS-WS. 

ResponseEntity<String> albumsResponse = restTemplate.exchange("http://ALBUMS-WS/albums", HttpMethod.GET, null, String.class);

You can see the name under which your microservice has registered with Eureka, by opening the Eureka dashboard.

Microservice application name in Eureka

You specify the application name in the application.properties file using the spring.application.name property.

spring.application.name=albums-ws

I hope this tutorial was of some value to you. For a step by step series of video lessons, please check this page: Spring Boot Microservices and Spring Cloud.

Happy coding! 🙋🏻‍♂️

Leave a Reply

Your email address will not be published. Required fields are marked *