DEV Community

Lahiru Rajapakshe
Lahiru Rajapakshe

Posted on

Feign Client vs Rest Client

A Comprehensive Guide
Introduction

In modern microservice architectures, services often need to communicate with each other via HTTP. Java offers multiple ways to make REST calls between services. Two popular approaches are Feign Client and RestTemplate (often referred to as Rest Client). Understanding the differences between them is essential for choosing the right tool for the job. In this documentation, we will delve into the key aspects of Feign Client and Rest Client, their benefits, limitations, and usage scenarios.

  1. Overview

1.1 Feign Client

Feign is a declarative web service client that simplifies writing HTTP clients in Java. Developed by Netflix, it integrates seamlessly with Spring Cloud for easier microservice communication. With Feign, you define REST API clients declaratively using simple Java interfaces and annotations.

1.2 Rest Client (RestTemplate)

RestTemplate is a synchronous HTTP client provided by Spring, primarily used before Spring 5 for making REST API calls. It requires writing manual code to construct HTTP requests, making it more verbose but highly flexible. Since Spring 5, RestTemplate is being phased out in favor of more modern, non-blocking clients like WebClient, but it’s still widely used in legacy applications.

  1. Key Differences Between Feign Client and Rest Client

2.1 Ease of Use

Feign Client: Feign offers a declarative approach. You define an interface, annotate it with the HTTP request methods, and Spring automatically wires the client. This makes the code cleaner, with no need to write boilerplate HTTP request code.

@FeignClient(name = "user-service")
public interface UserClient {
@GetMapping("/users/{id}")
User getUserById(@PathVariable("id") Long id);
}
- **Rest Client (RestTemplate)**: With RestTemplate, you manually build HTTP requests. This approach offers more control but requires more code. For example, to make a GET request, you’d do:
Enter fullscreen mode Exit fullscreen mode
RestTemplate restTemplate = new RestTemplate();
User user = restTemplate.getForObject("http://user-service/users/{id}", User.class, id);
Enter fullscreen mode Exit fullscreen mode

2.2 Configuration and Customization

-Feign Client: Feign abstracts away a lot of HTTP configuration by relying on annotations. It integrates with Spring Boot’s configuration management, allowing for easy customization of clients, including retries, timeouts, and interceptors via properties or custom configuration beans.

Example of configuration via application.yml:

user-service:
url: http://localhost:8080
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
Enter fullscreen mode Exit fullscreen mode

Rest Client: With RestTemplate, configuration like timeouts, interceptors, and error handling has to be set manually. For instance, you’d create an HttpClient and configure it like so:

HttpClient client = HttpClientBuilder.create().setConnectTimeout(5000).build();
RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(client));
Enter fullscreen mode Exit fullscreen mode

2.3 Integrations

Feign Client: Feign integrates well with various Spring Cloud components, such as Eureka for service discovery, Hystrix for circuit breaker patterns, and Ribbon for client-side load balancing. This makes Feign particularly suited for microservices environments.

Rest Client: RestTemplate can also be used with service discovery, circuit breakers, and load balancing, but these integrations require manual configuration. For instance, integrating Eureka or Hystrix requires you to code the discovery or resilience logic separately.

2.4 Annotations and HTTP Methods Support

Feign Client: Feign uses a declarative style, making use of annotations to map REST calls directly in Java interfaces. It supports all major HTTP methods (GET, POST, PUT, DELETE, etc.), and Spring Cloud Feign clients can use annotations like @RequestParam, @RequestBody, @PathVariable, and @Headers.

Rest Client: RestTemplate supports all HTTP methods as well but uses imperative coding. You manually write out the details of each request, which can make the code more verbose.

2.5 Error Handling and Resilience

Feign Client: Feign’s integration with Hystrix (for circuit-breaking) or Resilience4j allows for easy error handling and resilience patterns. If a service call fails, fallback methods can be defined directly within the interface or via configuration.

@FeignClient(name = "user-service", fallback = UserClientFallback.class)
public interface UserClient {
@GetMapping("/users/{id}")
User getUserById(@PathVariable("id") Long id);
}
Enter fullscreen mode Exit fullscreen mode

Rest Client: Error handling in RestTemplate is done by catching exceptions or using ResponseErrorHandler. For example, implementing a custom ResponseErrorHandler requires significantly more code compared to Feign’s fallback mechanisms.

2.6 Asynchronous Support

Feign Client: By default, Feign operates synchronously. However, it can be configured to support asynchronous calls using CompletableFuture or by integrating it with Hystrix.

@GetMapping("/users/{id}")
CompletableFuture<User> getUserByIdAsync(@PathVariable("id") Long id);
Enter fullscreen mode Exit fullscreen mode

Rest Client: RestTemplate is a synchronous client. For non-blocking communication, Spring recommends using WebClient (introduced in Spring 5) instead of RestTemplate for asynchronous operations.

2.7 Performance and Efficiency

Feign Client: Feign adds a layer of abstraction and is built on top of other HTTP clients (like Apache HttpClient or OkHttp). While this abstraction simplifies usage, it can add some overhead in terms of performance, especially in high-throughput applications. However, in most cases, this overhead is minimal.

Rest Client: RestTemplate gives more control over HTTP requests, which can lead to better fine-tuning and optimizations for performance. That said, writing optimized RestTemplate code is more manual and can be error-prone if not handled carefully.

  1. When to Use Feign Client?

3.1 Pros of Feign Client

  • Declarative Approach: Writing less boilerplate code makes development faster and cleaner.

  • Spring Cloud Integration: Feign works out-of-the-box with Eureka, Ribbon, Hystrix, and other Spring Cloud components.

  • Built-in Retry Mechanisms: Easy to configure timeouts, retries, and circuit breakers using Hystrix or Resilience4j.

  • Service Discovery: With Eureka integration, Feign dynamically resolves service instances without needing hardcoded URLs.

3.2 Cons of Feign Client

  • Abstraction Overhead: Feign may not provide the same performance as fine-tuned RestTemplate implementations, especially in high-performance, low-latency environments.

  • Lacks Asynchronous Support Out-of-the-box: While it can be configured for async calls, this is not as intuitive as using a non-blocking client like WebClient.

  1. When to Use Rest Client?

4.1 Pros of Rest Client

  • Complete Control: RestTemplate gives the developer full control over every aspect of the HTTP request, enabling advanced optimizations and configurations.

  • Better for Legacy Systems: Many legacy Spring applications still use RestTemplate, and migrating to Feign may not be necessary for some use cases.

  • Synchronous Operations: RestTemplate’s synchronous nature may be preferable in some environments that do not require the additional complexity of asynchronous or non-blocking communication.

4.2 Cons of Rest Client

  • Verbose Code: Requires more code to manage each REST call, making the code harder to maintain compared to Feign.

  • Less Integration: While it’s possible to integrate with Eureka, Ribbon, and Hystrix, this requires more manual configuration.

  • Deprecated: RestTemplate is deprecated in favor of WebClient for non-blocking calls in Spring 5 and beyond.

  1. Conclusion

Both Feign Client and Rest Client have their strengths and limitations. The choice between the two largely depends on your project needs:

  • Use Feign Client when you need to rapidly develop microservices that communicate with each other via HTTP, especially in Spring Cloud ecosystems where you can leverage service discovery and resilience features with minimal setup.

  • Opt for Rest Client (RestTemplate) if you need full control over HTTP requests and responses, or if you’re working in a legacy codebase that already uses it extensively.

For new projects, especially in Spring 5 or higher, consider using WebClient as it provides a more modern and reactive way to handle RESTful communication.

Thank you!

Top comments (0)