What is Feign Client?
Feign client, developed by Netflix, is a declarative web service client. It is useful for the communication or data transfer between two different micro-services via Rest API.
Why should we use Feign Client?
Feign aims at simplifying HTTP API clients. It is an abstraction over REST-based calls, which works on the declarative principle, which means, before using Feign Client, developers do not need to know about its REST internal details instead they only need to concentrate on the business logic. To use Feign Client, developers only need to declare an interface and annotate it and Spring will create the original implementation by itself.
By changing only the Feign configuration in Java or using properties you can add logging, encoding or decoding, and change the REST call implementation library. All this is done only through the configuration, while the business logic that calls the service remains unchanged. Since Feign uses standard Java interfaces, it's also easy to mock them during unit tests.
Feign Client Implementation
Let's understand how to implement Feign Client in an application step by step:
Step 1: Add the feign dependency in your pom.xml file
Step 2: In the next step, we will create an interface with the declaration of the services we want to communicate with. Here, we have to pass the name of the service in the annotation parameter.
Step 3: In this step, we will auto wire our ServiceCall interface in the controller. During runtime, Spring will inject the original implementation for data transfer.
Step 4: This is a final step, where we tell our project that we are using Feign Client so scan its annotation, we need to put an annotation @EnableFeignClient at the top of our class in which our main method is implemented.
That's it! Our Feign Client is implemented.