Microservices communication in Spring Boot with Feign client

Communicate two microservices with HTTP protocol

SABBAR El Mehdi
Geek Culture
2 min readSep 9, 2021

--

Source: https://unsplash.com

Microservices is one of those architectural patterns which has emerged from the world of domain-driven design, and its many applications communicate with each other to form a complete application. The common protocol used for that reason is Http/Https. By the end of this article, you will learn how to communicate microservices in a Spring Boot application using Feign — a declarative HTTP client developed by Netflix.

I assume that you have already two microservices(if not you will find the source code below), so I will jump directly to implementation. For this example, I two microservices(one is a UserServiceApp second is an ItemServiceApp).

I will create a service that calls a list of user items in ItemServiceApp.

To use Feign, we need to add the needed dependencies:

For the latest version, check Here.

Then add @EnableFeignClients annotation in the main Application.class

In ItemsServiceApp there is an API that fetches a list of items by user ID.

Now to consume this in UserServiceApp, we need an Interface service and add @FeignClient annotation with the name of the microservice defined in application.properties

The path value in @GetMapping is the path of the API in ItemsServiceApp.

The service will look like that:

Now, we can call the getItemByUserId() method in UserService.

That is all, not that complicated, right!

If you face any problem, please write it in the comment.

Resources:

https://spring.io/projects/spring-cloud-openfeign

Source code: here

--

--