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')
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.
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.
https://cloud.spring.io/spring-cloud-openfeign/reference/html/
Similarities
-
Both are type-safe HTTP clients for android and Java/Kotlin.
-
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
-
It is super easy to create Feign Client in Spring Boot application. We just need to add the below dependencies in build.gradle
build.gradleCreating a Feign Client for stores-service@FeignClient("stores") public interface StoreClient { @RequestMapping(method = RequestMethod.GET, value = "/stores") List<Store> getStores(); }
-
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 inbuild.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); } }
-
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. -
Circuit breaker pattern: If Hystrix is on the classpath and
feign.hystrix.enabled=true
, Feign will wrap all methods with a circuit breaker. -
Spring MVC annotations support is added to Feign client by Spring Cloud. So we need not learn any new syntax for using Feign.
-
The default Spring Web
HttpMessageConverters
are configured to use with Feign out of the box when used with Spring Cloud. -
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.ymlfeign: 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.
References
Top articles in this category:
- Feign exception handling in Spring Cloud
- Feign Client Logging and connection timeout
- Feign RequestInterceptor in Spring Boot
- How does Session handling works in Servlet environment
- Spring Boot 2.0 Reactive Web Performance Metrics
- What is difference between Component, Repository, Service, Controller & RestController
- Spring Boot multipart file upload server