Declarative REST client – Feign basics

According to the Feign documentation:

"Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of ReSTfulness."

Netflix has developed a declarative web service client called Feign. It is very easy to create compared to other web service clients, such as Spring's RestTemplate, DiscoveryClient, and EurekaClient. To create a Feign REST client, create an interface and annotate this interface with an annotation provided by the Netflix Feign library. You don't need to implement this interface in your cloud application to use the microservice. The Feign client provides support to use Feign annotations and JAX-RS annotations. And you can also use the Spring MVC annotations and the same HttpMessageConverters as we used in the Spring web module, the Feign client supports all annotations of the Spring MVC module for a REST application. It also provides support for pluggable encoders and decoders. Feign, by default, provides the functionality of Ribbon and Eureka to provide a load-balanced HTTP client.

In the previous chapters, we created microservices and its consumers. In the examples, account-consumer (that is, a web application or another microservice CUSTOMER-SERVICE) consumed the REST services exposed by the account-service producer using RestTemplate. Let's look at the following diagram, which illustrates the consumption of microservices without using Feign:

As you can see, microservices communicating with each other without the Feign client required a lot of boilerplate code related to Eureka, Ribbon, and load-balancing. The required code also increases the complexity in the client application when the number of microservices increases. Let's see the following requirements for which we have to write a lot of code:

  • To make a resilient system we have to create a load-balancing client using Ribbon
  • To know the Service instance and then the Base URL of a microservice using Eureka
  • To make use of RestTemplate for consuming service

The following code shows how to consume an account microservice:

package com.dineshonjava.webapplication.service; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.cloud.client.loadbalancer.LoadBalanced; 
import org.springframework.stereotype.Service; 
import org.springframework.web.client.RestTemplate; 
 
import com.dineshonjava.webapplication.domain.Account; 
import com.dineshonjava.webapplication.exception.AccountNotFoundException; 
 
@Service 
public class WebAccountsService { 
    
   @Autowired 
    @LoadBalanced 
    protected RestTemplate restTemplate; 
    
   protected String serviceUrl = "http://ACCOUNT-SERVICE";  
    
    public Account getByNumber(String accountNumber) { 
        Account account = restTemplate.getForObject(serviceUrl 
                + "/account/{accountId}", Account.class, 
accountNumber); if (account == null) throw new AccountNotFoundException(accountNumber); else return account; } ... ... }

Now, let's use the Feign declarative REST client and see how it resolves the complexity of communicating with microservices. Let's see the following diagram using the Feign client:

We don't need to write the code for Eureka, Ribbon, and load-balancing, it's automatically added if these libraries or dependencies are available on the classpath of your client application. You don't even need to write a class for the client code, just create an interface with the @FeignClient annotation with a logical service name as the annotation argument. This is a much easier and cleaner way of using Netflix Feign. If the Netflix Ribbon dependency is also in the classpath, then Feign takes care of load-balancing by default.

Let's see how to include Feign in the client application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.9.169