Retrofit vs FeignClient on Server Side with Spring Cloud

Carvia Tech | August 03, 2019 | 3 min read | 20 views


The focus of this article is to convince you to use FeignClient as the declarative web service client for inter-service communication in server side spring cloud environment.

Many of us from Java/Android background makes a mistake to use Retrofit on server side for inter-service communication in Spring projects.

Let us first get introduced to Retrofit and Feign:

Retrofit

A type-safe HTTP client for Android and Java. It is a declarative web service client that turns HTTP API into a Java interface.

For more information, check it out:

https://square.github.io/retrofit/

Feign Client

Feign is a declarative web service client. It makes writing web service clients easier. To use Feign create an interface and annotate it.

Similarities between the two:
  1. Both are type-safe HTTP clients for android and Java.

  2. Both makes it easy to consume JSON or XML data by parsing them into POJO.

Both are super easy to configure and use, but there are subtle differences between the two specially from server side point of view.

Why choose FeignClint over Retrofit on server side

  1. It is super easy to create Feign Client in Spring Boot application. We just need to add the below dependencies in build.gradle

    build.gradle
    compile('org.springframework.cloud:spring-cloud-starter-openfeign')
    
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-ribbon')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-hystrix')
    Creating a Feign Client for stores-service
    @FeignClient("stores")
    public interface StoreClient {
    
        @RequestMapping(method = RequestMethod.GET, value = "/stores")
        List<Store> getStores();
    }
  2. Eureka discovery client integration: If your spring boot application is a Eureka client, then spring cloud will make Feign Client eureka aware i.e. Feign will resolve the service in the Eureka service registry. So no need to specify/hardcode host/port/protocol for target service. All you need to have this working is add spring-cloud-starter-netflix-eureka-client dependency in build.gradle and apply the below two annotations to your main class.

    @EnableDiscoveryClient
    @EnableFeignClients
    public class App {
    
        public static void main(String[] args) {
            SpringApplication.run(ShunyaApp.class, args);
        }
    }
  3. Ribbon integration: Spring cloud integrates client side load balancer with feign if spring-cloud-starter-netflix-ribbon is on the classpath. Now Feign enjoys all the benefits of client side load balancing without any extra plumbing. Ribbon will take care of distributing the load on multiple instances of remote service.

  4. Circuit breaker pattern: If Hystrix is on the classpath and feign.hystrix.enabled=true, Feign will wrap all methods with a circuit breaker.

  5. Spring MVC annotations support is added to Feign client by Spring Cloud. So we need not learn any new syntax for using Feign.

  6. The default Spring Web HttpMessageConverters are configured to use with Feign out of the box when used with Spring Cloud.

Verdict

Retrofit is best suited for Android development, while Feign is better suited for server side inter service communication in Spring Cloud environment.


Top articles in this category:
  1. Integrating non-JVM apps into Spring Cloud using Sidecar approach
  2. Feign exception propagation in Spring Cloud
  3. Creating AWS Lambda in Java using Spring Cloud Function
  4. Creating AWS Lambda in Kotlin using Spring Cloud Function
  5. Setting a Random Port in Spring Boot Application at startup
  6. Spring Boot 2.0 Reactive Web Performance Metrics
  7. Custom banner in spring boot application



Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info