title: Spring Cloud Netflix : Passing host request parameter via RequestInterceptor to FeignClient
body: <p>I am building a Spring Cloud project (Brixton.M4 with Spring Boot 1.3.1) with Eureka, Zuul and FeignClient where I am trying to add multi tenancy support (Tenants are identified by subdomain : tenant1.myservice.com). To do so, I would like to somehow pass the original subdomain along requests that are forwarded from a service to the other via Feign but I can't seem to be able to find the right way to do it.</p>
  
  <p>What I have is a client that exposes a @RestController which calls a @FeignClient to communicate with my backend which exposes server operations to the client through its own @RestController.</p>
  
  <p>The @FeignClient using same interface as my @RestController on the server :</p>
  
  <pre><code>@FeignClient(name = "product")
  public interface ProductService extends IProductService {
  
  }
  </code></pre>
  
  <p>What I am currently trying to do is set a header in a RequestInterceptor :</p>
  
  <pre><code>@Component
  public class MultiTenancyRequestInterceptor implements RequestInterceptor {
  
      private CurrentTenantProvider currentTenantProvider;
  
      @Autowired
      public MultiTenancyRequestInterceptor(CurrentTenantProvider currentTenantProvider) {
          this.currentTenantProvider = currentTenantProvider;
      }
  
      @Override
      public void apply(RequestTemplate template) {
          try {
              template.header("TENANT", currentTenantProvider.getTenant());
          } catch (Exception e) {
              // "oops"
          }
      }
  }
  </code></pre>
  
  <p>My provider class is a simple component where I'm trying to inject a request / session scope bean :</p>
  
  <pre><code>@Component
  public class CurrentTenantProvider {
  
      @Autowired
      private CurrentTenant currentTenant;
      //...
  }
  </code></pre>
  
  <p>The bean (I tried both session and request scope) :</p>
  
  <pre><code>@Bean
  @Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
  public CurrentTenant currentTenant() {
      return new CurrentTenant();
  }
  </code></pre>
  
  <p>On the server, I use Hibernate multitenant provider that is supposed to catch the header value and use it to define which DB to connect to :</p>
  
  <pre><code>@Autowired
  private HttpServletRequest httpRequest;
  
  @Override
  public String resolveCurrentTenantIdentifier() {
      return httpRequest.getHeader("TENANT");
  }
  </code></pre>
  
  <p>It seems the Feign call to the server is done in another thread and out of the incoming request scope, so i'm not sure how to pass that value along.</p>
  
  <p>It all works fine when I hardcode the tenant value in the RequestInterceptor so I know the rest is working properly.</p>
  
  <p>I have also looked at many other posts about Zuul "X-Forwaded-For" header and cannot find it in the request received on the server. I have also tried adding a ZuulFilter to pass host name to next request but what I see is that original request to the Client is picked up by the ZuulFilter and I can add but not when the Feign request is sent to the backend service even if I map it in zuul (i guess that is intended ?).</p>
  
  <p>I am not really sure what's the next step and would appreciate some suggestions. </p>