Introduction to Feign Client

Posted By :Rozi Ali |29th November 2019

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

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

 

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.

@FeignClient(name="dataProduction")
public interface ServiceCall {
   @RequestMapping(method = RequestMethod.GET, value = "/data/products")
   public List<Products> getProducts();
}

 


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.

@RestController
public class ConsumptionController {
   @Autowired
   private ServiceCall serviceCall;
   @RequestMapping("/homepage/products")
   public List<Products> allProducts(){
      return List<Products> products = serviceCall.getProducts();
   }
}

 


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.

@EnableDiscoveryClient
@EnableFneignClients
@SpringBootApplication
public class ProductionConsumptioServiceApplication {
   public static void main(String[] args) {
      SpringApplication.run(ProductionConsumptioServiceApplication.class, args);
   }
}

 

That's it! Our Feign Client is implemented.

Thank you.


About Author

Rozi Ali

Rozi Ali is an accomplished software developer with extensive experience in the field of JAVA. She possesses a solid grasp of programming languages such as Java/Spring-boot, Python, and Typescript/Nodejs/GraphQL. Rozi has a strong background in Object-oriented programming (OOP) and is skilled in working with both relational databases like MySql, PostgreSQL and non-relational databases like MongoDb. She is proficient in REST APIs, Microservices, and code deployment, along with the development tools such as Jira, Git, and Bash. Additionally, Rozi has experience working with Cloud providers such as AWS and Azure. She has contributed significantly to a number of projects, including Konfer, VNS, Influsoft, VN Platform, QuickDialog, and Oodles-Dashboard.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us