Reader small image

You're reading from  Mastering Microservices with Java - Third Edition

Product typeBook
Published inFeb 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781789530728
Edition3rd Edition
Languages
Right arrow
Author (1)
Sourabh Sharma
Sourabh Sharma
author image
Sourabh Sharma

Sourabh Sharma works at Oracle as a lead technical member where he is responsible for developing and designing the key components of the blueprint solutions that are used by various Oracle products. He has over 18 years of experience delivering enterprise products and applications for leading companies. His expertise lies in conceptualizing, modeling, designing, and developing N-tier and cloud-based web applications as well as leading teams. He has vast experience in developing microservice-based solutions and implementing various types of workflow and orchestration engines. Sourabh believes in continuous learning and sharing knowledge through his books and training.
Read more about Sourabh Sharma

Right arrow

Inter-Process Communication Using REST

In this chapter, we'll learn how REST is used for inter-process communication. In the process of REST-based inter-process communication, we will explore various REST clients—RestTemplate, the OpenFeign client, and the newly revamped HTTPClient from Java 11, for implementing the inter-process, also known as inter-service communication. This chapter will also elaborate on the use of load balancing for inter-process communication. It is very handy when more than one instance of a service is deployed in the environment.

This chapter is divided into the following sections:

  • REST and inter-process communication
  • Load balanced calls and RestTemplate implementation
  • OpenFeign client implementation
  • Java 11 HTTPClient

We'll use the existing code base of Chapter 5, Microservice Patterns – Part 1, and add new code in booking-service...

REST and inter-process communication

Microservices represents the domain-driven design (known as DDD )-based domain service that runs as a process. Each microservice is independent. These independent services need communication with each other to implement the domain functionalities. No service directly accesses the database of other services. Instead, they use the APIs that are exposed by the service (microservice). These APIs could be implemented in various ways—using REST or events or gRPC. In this section, you'll learn how a service can consume the APIs of another exposed service using REST implementation.

Sample OTRS application services are registered and discoverable on eureka-server. Eureka Server allows the load balancing of calls using the Netflix Ribbon library. Spring Cloud also provides the discovery client. Remember that the @EnableEurekaClient or @EnableDiscoveryClient...

Load balanced calls and RestTemplate implementation

Spring Cloud uses Netflix Ribbon, a client-side load balancer that plays a critical role and can handle both HTTP and TCP protocols. Ribbon is cloud-enabled and provides built-in failure resiliency. Ribbon also allows you to use multiple and pluggable load balancing rules. It integrates clients with load balancers.

Ribbon is integrated with the Eureka Server for client-side load balancing and with the Zuul server for server-side load balancing in Spring Cloud by default. This integration provides the following features:

  • There is no need to hard code remote server URLs for discovery when Eureka Server is used. This is a prominent advantage, although you can still use the configured server list (listOfServers) in application.yml if required.
  • The server list gets populated from Eureka Server. Eureka Server overrides ribbonServerList...

OpenFeign client implementation

OpenFeign client is another alternative that helps with executing the REST API calls. Its main advantage is that it removes the boilerplate code. Have a look at the code mentioned in step 3. It is sleek, readable, and contains less code compared to others.

It just needs a Java interface that has the REST API signatures—that's it. Then, you can use it. For each service, you can define a separate Feign interface. It works very well with Eureka. Use of OpenFeign requires the following steps:

  1. First, you need to add a new dependency in the pom.xml file on booking-service, as shown in the following example:
<!-- OpenFeign client dependency -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  1. Next, you need to add the...

Java 11 HTTPClient

HttpClient was officially introduced with Java 11. It was first introduced in Java 9 as an incubator. You could say it is a new version of java.net.HttpUrlConnection.

It offers many new features:

  • Supports both HTTP 1.1 and HTTP 2 (default)
  • Supports both synchronous and asynchronous calls
  • Provides reactive data and streams to both request and response with non-blocking back pressure

It works very well in asynchronous mode and with streams. However, here, we'll only cover the synchronous calls to align with other REST clients.

First, we'll add a provider class that will build the HttpClient and provide methods to build and send HTTP requests, as shown in the following example:

public class RestClient {
HttpClient httpClient = HttpClient
.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL...

Wrapping it up

Each of the REST clients have their own advantages and limitations. So far, RestTemplate has proved very popular, but looking at the Spring future plans and introduction of WebClient makes it less demanding. A migration to WebClient seems like a better choice.

OpenFeign is very sleek and intuitive. However, in the past, lots of Common Vulnerabilities and Exposures (CVEs) make it vulnerable and so the least preferable choice. It also depends on lots of third-party libraries. This is where most of the CVEs were reported.

Java 11's HttpClient looks very attractive and provides lots of advanced features. If you can grab it and write an intuitive API on top of it, then it looks like the best suited choice.

We have discussed the pros and cons of each of these REST client options. You need to have a hard look and adopt one of these, or many other available REST clients...

Summary

In this chapter, you have learned how to use REST APIs for inter-process communication. We have also found the way to find out about the registered and available services on service registration and the discovery server. We have explored the different libraries to consume REST APIs—RestTemplate, the OpenFeign, client and the newly introduced Java 11 HttpClient. At the end, trade-offs of different clients were explained.

In the next chapter, we'll learn how to write the gRPC-based services and establish the inter-process communication using a gRPC client.

Further reading

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Microservices with Java - Third Edition
Published in: Feb 2019Publisher: PacktISBN-13: 9781789530728
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Sourabh Sharma

Sourabh Sharma works at Oracle as a lead technical member where he is responsible for developing and designing the key components of the blueprint solutions that are used by various Oracle products. He has over 18 years of experience delivering enterprise products and applications for leading companies. His expertise lies in conceptualizing, modeling, designing, and developing N-tier and cloud-based web applications as well as leading teams. He has vast experience in developing microservice-based solutions and implementing various types of workflow and orchestration engines. Sourabh believes in continuous learning and sharing knowledge through his books and training.
Read more about Sourabh Sharma