Search code examples
springspring-bootspring-cloudupgradeservice-discovery

How to add setAdditionalFilters in DiscoveryClient when DiscoveryClientOptionalArgs is removed from there in latest Spring Cloud


Thanks in Advance for you answers.

I am upgrading spring cloud, i earlier had a bean to add additional filter as below

 @Bean
  @ConditionalOnMissingBean(DiscoveryClient.DiscoveryClientOptionalArgs.class)
  public DiscoveryClient.DiscoveryClientOptionalArgs discoveryClientOptionalArgs() {
    List<ClientFilter> filters = new ArrayList<>();
    filters.add(new ClientFilterAdapter(jwtAuthRequestDecorator()));
    DiscoveryClient.DiscoveryClientOptionalArgs args =
        new DiscoveryClient.DiscoveryClientOptionalArgs();
    args.setAdditionalFilters(filters);
    return args;
  }

In the newer version of DiscoveryClient, DiscoveryClientOptionalArgs is removed/deprecated. could you please help me with another way to implement the same?

jwtAuthRequestDecorator is nothing but a bean to add additional header

public HttpHeaders getHeaders() {
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.add(TOKEN, jwtToken);
    return httpHeaders;
  }

i added implementation("com.sun.jersey:jersey-client:1.19.4") to include jersey client which is removed from spring cloud.


Solution

  • I fixed it by adding customized RestTemplateDiscoveryClientOptionalArgs:

    @Bean
    public RestTemplateDiscoveryClientOptionalArgs discoveryClientOptionalArgs(HeaderHttpRequestInterceptor headerInterceptor) throws Exception {
      RestTemplateDiscoveryClientOptionalArgs args = new RestTemplateDiscoveryClientOptionalArgs(new CustomEurekaClientHttpRequestFactorySupplier(headerInterceptor));
      return args;
    }