avatarMohammed Taoufik Lahmidi

Summary

The provided content discusses methods for sending custom HTTP headers using Feign clients in a microservices architecture, specifically through the @RequestHeader annotation and Request Interceptors.

Abstract

In the context of microservices, the article delves into the intricacies of inter-service communication facilitated by Feign, a declarative web service client. It outlines two principal techniques for incorporating custom HTTP headers into requests: the @RequestHeader annotation, which allows for header specification on a per-method basis within a Feign client interface, and Request Interceptors, which are used to apply headers consistently across all requests from a client. The article illustrates practical code examples for both approaches, demonstrating their implementation in a Spring Cloud environment, and concludes by offering guidance on when to use each method to enhance the robustness and maintainability of microservices communication.

Opinions

  • The use of the @RequestHeader annotation is deemed straightforward and convenient for adding headers to specific Feign client methods.
  • Employing a Map to send multiple headers via the @RequestHeader annotation is highlighted as a practical feature.
  • Request Interceptors are presented as a superior choice for ensuring header consistency across all requests made by a Feign client, emphasizing their utility when global header application is required.
  • The article suggests that understanding and appropriately applying both the @RequestHeader annotation and Request Interceptors can lead to more effective and secure microservices communication.
  • It is implied that the choice between using the @RequestHeader annotation and a Request Interceptor should be made based on the specific needs of the application, with a preference for Request Interceptors when dealing with global headers.

Sending Headers in Feign Client

In the modern world of microservices, communication between different services is crucial. Feign, a web service client that simplifies the communication between microservices . One of its powerful features is the ability to send custom headers with requests, which can be achieved using the @RequestHeader annotation or a Request Interceptor.

Sending Headers with @RequestHeader Annotation

The @RequestHeader annotation is straightforward and convenient for adding headers to specific methods in your Feign client. Here's how you can use it:

First, create an interface and annotate it with @FeignClient. This tells Spring to create a bean for this interface and handle the communication.

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

@FeignClient(name = "example-client", url = "http://example.com")
public interface ExampleClient {

    @GetMapping("/api/v1/resource")
    String getResourceV1(@RequestHeader("Custom-Header") String headerValue);

    @GetMapping("/api/v2/resource")
    String getResourceV2(@RequestHeader Map<String,String> headerValues);
}

By the way you can send also a multiple headers using a Map .

You can now autowire and use this Feign client in your Spring service or controller .

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExampleService {

    @Autowired
    private ExampleClient exampleClient;

    public String fetchResourceV1() {
        return exampleClient.getResource("HeaderValue");
    }
    
    public String fetchResourceV2() {
        Map<String,String> headerValues = new HashMap<>();
        headerValues.put("Custom-Header-1","HeaderValue1");
        headerValues.put("Custom-Header-2","HeaderValue2");
        return exampleClient.getResource(headerValues);
    }
}

Sending Headers with a Request Interceptor

While the @RequestHeader annotation is great for specific cases, sometimes you need to add headers to all requests made by a Feign client. For this, you can use a Request Interceptor.

Create a class that implements RequestInterceptor and override the apply method to add headers .

import feign.RequestInterceptor;
import feign.RequestTemplate;

@Configuration
public class MyRequestInterceptor implements RequestInterceptor{
    @Override
    public void apply(RequestTemplate requestTemplate) {
       requestTemplate.header("Custom-Header", "HeaderValue");
    }
}

Update your Feign client to not use the RequestHeader annotation .

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;

@FeignClient(name = "example-client", url = "http://example.com")
public interface ExampleClient {

    @GetMapping("/api/v1/resource")
    String getResourceV1();
}

You can now autowire and use this Feign client in your Spring service or controller, and the custom header will be included in every request.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExampleService {

    @Autowired
    private ExampleClient exampleClient;

    public String fetchResource() {
        return exampleClient.getResource();
    }
}

Choosing the Right Approach

  • Use @RequestHeader Annotation: When you need to add headers for specific methods only. This approach is simple and localized to the method where the header is needed.
  • Use Request Interceptor: When you need to add headers to all requests made by a Feign client. This approach is more centralized and ensures consistency across all requests.

Conclusion

Feign clients offer a powerful and flexible way to communicate between microservices. By understanding and utilizing the @RequestHeader annotation and Request Interceptors, you can ensure that your requests are properly authenticated and customized to meet your application's needs.

Both methods have their use cases, and knowing when to use each will help you build more robust and maintainable microservices. Happy coding!

Feign
Java
Spring Boot
Spring
Recommended from ReadMedium