Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Cloud Feign: Is Feign efficient enough compared with RestTemplate?

I quickly browsed the source code of Feign, I found that Feign uses JDK's HttpUrlConnection to issue HTTP request and close it when request finished without using a connection pool. I doubt the efficiency of this kind of manner. Then I read the document of Spring's RestTemplate which says RestTemplate can switch to Apache Http Client or OKHttp to send HTTP request:

Note: by default the RestTemplate relies on standard JDK facilities to establish HTTP connections. You can switch to use a different HTTP library such as Apache HttpComponents, Netty, and OkHttp through the HttpAccessor.setRequestFactory(org.springframework.http.client.ClientHttpRequestFactory) property.

Does it mean RestTemplate is better than Feign at the view of performance?

like image 607
Neo Avatar asked Aug 03 '16 08:08

Neo


People also ask

Which is better feign or RestTemplate?

One of the advantages of using Feign over RestTemplate is that, we do not need to write any implementation to call the other services. So there is no need to write any unit test as there is no code to test in the first place.

Why RestTemplate is deprecated?

RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof.

Why do we use 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. It has pluggable annotation support including Feign annotations and JAX-RS annotations.

Is feign client asynchronous?

Synchronous and Asynchronous API callsThe API that we defined via Feign is synchronous — meaning that it makes blocking calls to the server. Feign allows us to easily define async APIs as well — utilizing CompletableFutures under the hood.

What is feign client in Spring Cloud?

Each feign client is part of an ensemble of components that work together to contact a remote server on demand, and the ensemble has a name that you give it as an application developer using the @FeignClientannotation. Spring Cloud creates a new ensemble as an ApplicationContexton demand for each named client using FeignClientsConfiguration.

What is the difference between feign and resttemplate?

1) Unlike a RestTemplate, Feign client acts as a load balancer. Spring Cloud integrates Netflix/ribbon and Netflix/eureka to provide a load balanced http client when using Feign. 2) Due to is declarative nature, does not require any unit tests to be written. Create an interface and annotate it with Feign and JAX-RS annotations.

How does Spring Cloud openfeign integrate with Spring MVC?

Similar to its predecessor, Spring Cloud OpenFeign integrates the predecessor project into the Spring Cloud ecosystem. Conveniently, this integration adds support for Spring MVC annotations and provides the same HttpMessageConverters. Let's compare the Feign implementation found in the Spring Cloud OpenFeign to one using Spring Cloud Netflix Feign.

What is Spring Cloud feign library?

Feign is a Spring Cloud Netflix library for providing a higher level of abstraction over REST-based service calls. Spring Cloud Feign works on a declarative principle. When using Feign, we write declarative REST service interfaces at the client, and use those interfaces to program the client.


2 Answers

Old question, but probably worth mentioning here that as of Spring 5 the RestTemplate is deprecated in favor of the WebClient.

like image 135
James Gawron Avatar answered Sep 28 '22 04:09

James Gawron


In comparison with Feign, RestTemplate takes advantage on the default client performance (although it is mapped that this client has its issues with Java 11 regarding connections resets), but it loses on the ease of integrating logging libs and the more verbose and harder to test programmatic approach.

Another good point in favor of Feign is the easiness to implement fallback strategies combined with Hystrix, implementing custom ErrorDecoder.

If you want to go deeper about how Feign implementation, take a look at this article.

Talking about performance, another attention point of RestTemplate is that it uses the Java Servlet API, which is based on the thread-per-request model. This means that the thread will block until the web client receives the response, which can lead to degraded performance and to waste resources such as memory and CPU cycles, specially when communicating with slow services. On the other hand, this is not an issue for Feign because it can be used with async clients, which are not thread blocking.

like image 30
Diego Rojas Avatar answered Sep 28 '22 06:09

Diego Rojas