Introduction to Feign Client

Choosing microservice architecture and Spring Cloud means that you'll need to pick the cleanest possible way for your services to communicate between themselves. Otherways you'll have many service invocation calls in your Spring Cloud application you (or someone else ) would need to maintain later. Feign Client is one of the best solvers for this issue. It is a declarative Java web service client initially developed by Netflix. It's an abstraction over REST-based calls allowing your microservices to communicate cleanly without the need to know REST details happening underneath. Feign Client works by processing annotations into a templatized request.

The main idea behind Feign Client is to create an interface with method definitions representing your service call. Even if you need some customization on requests or responses, you can do it in a declarative way. So, now I propose that after everything I said, you understand the benefits of using Feign Client.

Get Gayle Laakmann - Cracking the Coding Interview - 4th, 5th and 6th edition

Code sample

First, you'll need Maven dependency in your project:

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

Also, on your main project class, you'll need to add annotation @EnableFeignClients .

Main keynotes:

  • this process is declarative, so you'll describe through annotations what your service should do, and that's it 
  • in FeignClient annotation add the name of service you're calling
  • when using @PathVariable, you need to provide a value, or you'll get IllegalStateException
  • parameter decode404 set to true means that instead 404 error you'll get null
  • this interface can be injected as anything else
  • next to decode404, you can also add URL parameter

This code contains two methods, both from service with name "tenants". First returns list of tenants. You'll see that I have the query parameter "withFullAddress" annotated with @RequestParam. In the second service definition, you'll see that I use the path parameter. 

package com.test.client;

import java.util.List;
import java.util.Optional;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import com.test.client.model.Tenant;
import com.test.client.model.Customer;


@FeignClient(name= "tenants", decode404= true)

public interface TenantClient {
  
@GetMapping("/api/tenants")
  public List<Tenant> getTenants(@RequestParam(name = "withFullAddress") boolean withFullAddresses);

@GetMapping("/customer/{id}")
  public Customer getCustomerById(@PathVariable(value = "id") String id);

}

 If you want to set some specifics for Feign Clients you can do this in your application.yml file.

feign:
  client:
    config:
      tenantsClient:
        connectTimeout: 6000
        readTimeout: 6000
        loggerLevel: full
        errorDecoder: com.example.SomeErrorDecoder
        retryer: com.example.SomeRetryer
        requestInterceptors:
          - com.example.TenantClientRequestInterceptor
        decode404: false
        encoder: com.example.SimpleEncoder
        decoder: com.example.SimpleDecoder
        contract: com.example.SimpleContract

Feign Client could also be created manually.

For full documentation on Feign Client visit https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html


Newsletter

Please enable the javascript to submit this form