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 dependencies spring-boot-starter-web and spring-cloud-starter-loadbalancer.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-loadbalancer --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> <version>3.1.4</version> </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 as any other object in your Spring Boot application. If you have more than one RestTemplate bean, you need to specify which one you inject.
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 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 make 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 the 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 the 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 requests to an internal Microservice 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.
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.
If you also test your Java code with JUnit, check the following tutorials that will teach you how to use TestRestTemplate.
For a step-by-step series of video lessons, please check this page: Spring Boot Microservices and Spring Cloud.
Happy coding! 🙋🏻♂️