Sunday, August 18, 2019

Declarative REST Client: Feign


We already know that how microservices communicate with each other using RestTemplate. In this blog, we will see that how this can happen using Feign Client as well. 

What is a Feign Client?

Feign is an abstraction over REST based call. It makes writing web service clients easier. It Is as easy as creating interface and then annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. It also supports pluggable encoders and decoders and has supports for Spring MVC annotation.

Using Feign, microservices can easily communicate with each other and developers don't have to bother about REST internal details and can only concentrate on business logic.
Implementation.

I am going to use my previous applications to demonstrate the working of Feign Client. So before starting feign-client application. Make sure all the below three applications are already up and running. 

Feign Client Implementation

  • Create a spring boot application and add Feign starter dependency to our pom.xml file.

  • Add @EnableFeignClients annotation. With this annotation, we enable component scanning for interfaces that declare they are Feign clients. 

  • Declare a Feign client using the @FeignClient annotation.

  • Either name or url must be passed under @FeignClient annotation. If name attribute present then it will fetch the service location from service registry and then hit product-service and in case of url, it will directly hit the application to get the data from product-service. In case of both present then it will always first check for the name attribute. 
  • Use Spring Web annotations to declare the APIs that we want to reach out to.
Note: If your application is not registered with service registry then directly use url attribute to get the data but I would recommend to use name attribute and to use name attribute, make sure your application is connecting to Eureka server to get the service location. 

Read my blog to understand on how to add support for Eureka Client to your application.

Now start your application and you should be able to fetch product-service from feign-client. 


You can download the source code from here. 

Custom Configuration

Feign Client also support for Custom configuration changes like telling Feign to use OkHttpClient instead of the default one in order to support HTTP/2. 

Let’s go deep down to check how can we customize Feign Client.

There are two ways to configure it i.e. using properties file or override them using a @Configuration class, which we then need to add to the FeignClient annotation. 

Properties file:



Using @Configuration





Logging:

By default, a logger gets created for each Feign Client and to enable it, we have to declare it in the application.properties file using the package name of the client interface.

logging.level.com.com.waheedtechblog.feignclient=DEBUG

There are four logging levels to choose from:

NONE – no logging, which is the default
BASIC – log only the request method, URL, and response status
HEADERS – log the basic information together with request and response headers
FULL – log the body, headers, and metadata for both request and response

Handling Errors with Feign

By default, Feign’s default error handler, ErrorDecoder.default, always throws a FeignException.

To customize the Exception thrown, we can override it by writing our own Custom Error class and implements ErrorDecoder and declare this bean under @Configuration as we did earlier.



Spring-boot microservices can be downloaded from GITHUB

Happy Coding...!!!

No comments:

Post a Comment

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...