Spring Boot Microservices Communication

In this tutorial, we will learn how to make REST API calls from one microservice to another microservice using RestTemplate, WebClient, and OpenFiegn library.

Prerequisites

Before continuing this tutorial, create two microservices: department-service and user-service

Use this tutorial to create department-service and user-service microservices: Spring Boot Microservices REST API Example.

In this tutorial, we will make a REST API call from the user service to the department service to fetch a particular user department in different ways.

1. Communication between Microservices using RestTemplate

Let's use the RestTemplate class to make Synchronous communication between multiple microservices.

1.1 Configure RestTemplate as Spring Bean

@Bean
public RestTemplate restTemplate(){
    return new RestTemplate();
}

1.2 Use RestTemplate to call the REST API

Inject and use RestTemplate to make REST API calls:

ResponseEntity<DepartmentDto> responseEntity = restTemplate
    .getForEntity("http://localhost:8080/api/departments/" + user.getDepartmentId(),
    DepartmentDto.class);

DepartmentDto departmentDto = responseEntity.getBody();

System.out.println(responseEntity.getStatusCode());
Complete tutorial: Spring Boot Microservices Communication Example using RestTemplate

2. Communication between Microservices using WebClient

WebClient is a non-blocking, reactive client that performs HTTP requests, exposing a fluent, reactive API over underlying HTTP client libraries such as Reactor Netty. 

We must add Spring WebFlux dependency to the classpath to use WebClient in our Spring boot project.

2.1 Configure WebClient as Spring Bean

@Bean
public WebClient webClient(){
    return WebClient.builder().build();
}

2.2 Use WebClient to call the REST API

Inject and use WebClient to make REST API calls:

DepartmentDto departmentDto = webClient.get()
    .uri("http://localhost:8080/api/departments/" + user.getDepartmentId())
    .retrieve()
    .bodyToMono(DepartmentDto.class)
    .block();
Complete tutorial: Spring Boot Microservices Communication Example using WebClient

3. Communication between Microservices using Spring Cloud OpenFeign

Feign makes writing web service clients easier with pluggable annotation support, which includes Feign annotations and JAX-RS annotations. Also, Spring Cloud adds support for Spring MVC annotations and for using the same HttpMessageConverters as used in Spring Web. 

One great thing about using Feign is that we don't have to write any code to call the service other than an interface definition.

3.1 Add Spring cloud open feign Maven dependency to User-Service

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

3.2 Enable Feign Client using @EnableFeignClients

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }
}

3.3 Create feign API client

@FeignClient(value = "DEPARTMENT-SERVICE")
public interface APIClient {
    @GetMapping(value = "/api/departments/{id}")
    DepartmentDto getDepartmentById(@PathVariable("id") Long departmentId);
}

3.4 Change the getUser method to call APIClient

DepartmentDto departmentDto = apiClient.getDepartmentById(user.getDepartmentId());
Complete Tutorial: Spring Boot + Spring Cloud Open Feign Microservices Communication Example

Reference Links


Comments