Retrofit vs Feign for Server Side

Upasana | December 03, 2019 | 3 min read | 2,564 views


We will compare Retrofit and OpenFeign declarative REST clients for their server side use in Spring Boot application.

Let us first get introduced to Retrofit and OpenFeign:

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

Similarities between Retrofit and OpenFeign:
  1. Both are type-safe HTTP clients for android and Java/Kotlin.

  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 their server-side usage.

Feign advantages over Retrofit for server side usage

  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-loadbalancer')
    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.

  7. OpenFeign has pluggable support for both Apache HttpClient and OkHttp as the underlying HttpClient for necessary Http communication. To enable OkHttp in feign, you can set the following property. Similarly you can choose among Jackson and Gson for Json Serialization/DeSerialization, Slf4j for logging.

    application.yml
    feign:
      okhttp:
        enabled: true

    By default Apache HttpClient is used, if no property has been set. On the other hand, Retrofit v2+ works only with OkHttp.

Final Verdict

Retrofit is best suited for Android development, while OpenFeign is better suited for server side inter-service communication in Spring Cloud based microservices.


Top articles in this category:
  1. Feign exception handling in Spring Cloud
  2. Feign Client Logging and connection timeout
  3. Feign RequestInterceptor in Spring Boot
  4. How does Session handling works in Servlet environment
  5. Spring Boot 2.0 Reactive Web Performance Metrics
  6. What is difference between Component, Repository, Service, Controller & RestController
  7. Spring Boot multipart file upload server

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.