Reader small image

You're reading from  Mastering Spring Cloud

Product typeBook
Published inApr 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788475433
Edition1st Edition
Languages
Right arrow
Author (1)
Piotr Mińkowski
Piotr Mińkowski
author image
Piotr Mińkowski

Piotr works as a Solution Architect at Red Hat. He has several years of experience in software architecture and development. During this time, he was working in large organizations, where he was responsible for IT transformation to the modern cloud-native development approach. He is interested in technologies related to programming, containerization, and microservices. He writes about it in his blog https://piotrminkowski.com.
Read more about Piotr Mińkowski

Right arrow

Chapter 6. Communication Between Microservices

In the last two chapters, we discussed details related to very important elements in microservice architecture—service discovery and the configuration server. However, it is worth remembering that the main reason for their existence in the system is just to help in the management of the whole set of independent, standalone applications. One aspect of this management is communication between microservices. Here, a particularly important role is played by service discovery, which is responsible for storing and serving the network locations of all available applications. Of course, we may imagine our system architecture without a service discovery server. Such an example will also be presented in this chapter.

However, the most important components taking part in an inter-service communication are HTTP clients and client-side load balancers. In this chapter, we are going to focus just on them. 

The topics we will cover in this chapter include:

  • Using...

Different styles of communication 


We can identify different styles of communication between microservices. It is possible to classify them into two dimensions. The first of them is a division into synchronous and asynchronous communication protocols. The key point of asynchronous communication is that the client should not have blocked a thread while waiting for a response. The most popular protocol for that type of communication is AMQP, and we already had the opportunity to run an example of that protocol usage at the end of the previous chapter. However, the main way of communication between services is still synchronous HTTP protocol. We will be only talking about it in this chapter.

The second division is into different communication types based on whether there is a single message receiver or multiple receivers. In one-to-one communication, each request is processed by exactly one service instance. In one-to-many communication, each request may be processed by many different services...

Synchronous communication with Spring Cloud


Spring Cloud provides a set of components to help you in implementing communication between microservices. The first of them is RestTemplate, which is always used for consuming RESTful web services by a client. It is included in a Spring Web project. To use it effectively in a microservices environment, it should be annotated with the @LoadBalanced qualifier. Thanks to that, it will be automatically configured to use Netflix Ribbon and it will be able to take an advantage of service discovery by using service names instead of IP addresses. Ribbon is a client-side load balancer, which provides a simple interface allowing control over the behavior of HTTP and TCP clients. It can be easily integrated with other Spring Cloud components, such as service discovery or circuit breaker, and, furthermore, it is fully transparent to a developer. The next available component is Feign, a declarative REST client also from the Netflix OSS stack. Feign already...

Load balancing with Ribbon


The main concept around Ribbon is a named client. That's why we may call other services using their names instead of the full address with hostname and port, without connecting to a service discovery. In that case, the list of addresses should be provided in the Ribbon configuration settings inside the application.yml file.

Enabling communication between microservices using the Ribbon client

Let's proceed with the example. It consists of four independent microservices. Some of them may call endpoints exposed by the others. The application source code is available here:

https://github.com/piomin/sample-spring-cloud-comm.git.

In this example, we will try to develop a simple order system where customers may buy products. If a customer decides to confirm a selected list of products to buy, the POST request is sent to the order-service. It is processed by the Order prepare(@RequestBody Order order) {...} method inside REST controller. This method is responsible for order...

Using RestTemplate together with service discovery


In fact, an integration with service discovery is the default behavior of the Ribbon client. As you probably remember, we disabled Eureka for the client-side balancer by setting the ribbon.eureka.enabled property to false. The existence of service discovery simplifies a configuration of Spring Cloud components during inter-service communication, examples in this section.

Building example application

The system architecture is the same as for the previous example. To view the source code for the current exercise, you have to switch to the ribbon_with_discovery branch (https://github.com/piomin/shown here-spring-cloud-comm/tree/ribbon_with_discovery). The first thing you will see there is a new module, discovery-service. We have discussed in detail almost all aspects related to Eureka inChapter 4,Service Discovery, so you should not have any problems with launching it. We run a singlestandaloneEureka server with really basic settings. It is...

Using Feign client


RestTemplate is a Spring component specially adapted to interact with Spring Cloud and microservices. However, Netflix has developed their own tool that acts as a web service client for providing out-of-the-box communication between independent REST services. Feign client, which is in it, generally does the same as RestTemplate with the @LoadBalanced annotation, but in a more elegant way. It is a Java to HTTP client binder that works by processing annotations into a templatized request. When using Open Feign client, you only have to create an interface and annotate it. It integrates with Ribbon and Eureka to provide a load balanced HTTP client, fetching all the necessary network addresses from service discovery. Spring Cloud adds support for Spring MVC annotations and for using the same HTTP message converters as in Spring Web.

Support for different zones

Let me back up for a moment to the last example. I'm going to propose some changes to complicate our system architecture...

Summary


In this chapter, we have launched a couple of microservices that communicate with one another. We discussed such topics as different implementations of REST clients, load balancing between multiple instances, and integration with service discovery. In my opinion, these aspects are so important that I decided to describe them in two chapters. This chapter should be treated as an introduction to the subject of inter-service communication and a discussion of integration with other important components of microservice architecture. The next chapter will show more advanced use of load balancers and REST clients, with particular attention on network and communication problems. After reading this chapter, you should be able to use Ribbon, Feign, and evenRestTemplateproperly in your applications and connect them to other Spring Cloud components. 

In most cases, this knowledge is enough. However, sometimes you will need to customize client-side load balancer configuration or enable more advanced...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Spring Cloud
Published in: Apr 2018Publisher: PacktISBN-13: 9781788475433
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 €14.99/month. Cancel anytime

Author (1)

author image
Piotr Mińkowski

Piotr works as a Solution Architect at Red Hat. He has several years of experience in software architecture and development. During this time, he was working in large organizations, where he was responsible for IT transformation to the modern cloud-native development approach. He is interested in technologies related to programming, containerization, and microservices. He writes about it in his blog https://piotrminkowski.com.
Read more about Piotr Mińkowski