REST of the future!
In today’s era of technology, where new technologies are being introduced in a developers life on a nearly daily basis, monolithic applications are being broken down to smaller and highly scalable independent services and old technologies are evolving to meet the need for high scalability and low latency of services, we face the question and challenge to improve the communication between our microservices.
With my recent curiosity to understand microservice architecture and various architectural patterns involved in designing it, I came across a basic yet important question,
Whether to move forward with RestTemplate or WebClient by Spring Boot to enable communication between my microservices?
Before we dive deeper into these two approaches provided by Spring Boot, let me introduce you to both individually.
RestTemplate, as per the official documentation, is a Synchronous client to perform HTTP requests, exposing a simple, template method API over underlying HTTP client libraries such as the JDK, HttpUrlConnection, Apache HttpComponents, and others.
on the other hand, WebClient is a reactive way of communicating with web services/microservices. In simpler words, it is a Non-blocking, Asynchronous as well as the synchronous client to perform HTTP requests, exposing a fluent, reactive API over underlying HTTP client libraries such as Reactor Netty.
It provides as very simple, straight forward approaches for calling RestAPI’s from your backend, typically used to get data from other microservices or even third party web services.
One way of implementing a RestTemplate in your application is to create an instance of RestTemplate and call getForObject() passing URL and return type class as arguments.
RestTemplate restTemplate = new RestTemplate();
User user = restTemplate.getForObject(“http://localhost:8081/api/user/1”, User.class);
The above example returns a JSON by executing a GET request to User Service endpoint and fetching the user details for user id = 1 thereafter unmarshalling it to User Object.
This is a very basic but synchronous way of calling services from within your services. The good part is that it requires a very minimum code to be written and it’s easy to implement in your code but the cost is that this keeps a thread waiting to get a response from the user service and only once the response is received and unmarshalled, the thread is gonna move forward to next line to do something else.
In a scenario where I’ll be making multiple API calls, that do not block each other to get some data, I hope to achieve better efficiency and cleaner code by using WebClient.
There is also a reactive approach to doing the same thing and allowing your thread to not wait for the response and execute further tasks by using WebClient. The WebClient client takes the URL, body(if a POST request), and return type and returns the body in form of a Mono which acts as a promise that when the response is returned it will be unmarshalled to provided Object type, the thread does not have to wait for it and can continue further executions.
Refer the following code to understand better :
WebClient.Builder webClientBuilder = new WebClient().builder();
User user = webClientBuilder.build()
The above implementation of WebClient acts asynchronously, but to make your Controller return asynchronously too, you need to return a Mono from your controller too. To make it synchronous, you just need to append a block() after calling bodyToMono(User.class).
User user = webClientBuilder.build()
The WebClient is part of the WebFlux dependency of SpringBoot and can be added to your Maven or Gradle file as any other dependency. It contains a whole new reactive way of doing things.
As we move forward, I have provided you with the starting point of the two given implementations of calling REST APIs from within your code which is up to your preference and needs. Today most of the developers use RestTemplate, which can also be seen in many application code bases, but as we move towards the future the WebClient seems to be the upcoming way of doing things. Also mentioned in Spring’s latest RestTemplate Javadoc, it is supposed to be deprecated and replaced by WebClient.
Yes, REST is the way to go into the future, but maybe on a different train!
I will soon be writing a Part 2, where I will be discussing the different ways the microservices communicate with each other and how to do it efficiently.
Hope to see you there!