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

She is self-motivated and dedicated person in Development team. She is working on Java Technology.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch


Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Chat With Us