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?
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.
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.
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.
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.
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.
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.
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.
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.
Old question, but probably worth mentioning here that as of Spring 5 the RestTemplate is deprecated in favor of the WebClient.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With