Disable Hystrix for a single Feign client

天涯浪子 提交于 2020-08-06 12:45:30

问题


My SpringBoot app has Hystrix enabled with fallback defined for some of the Feign clients and undefined for the rest them.

Now, I wanted to disable Hystrix for the ones that did not have a fallback defined as yet. So I followed the steps listed in [paragraph 7.4] https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html which is to create a separate Feign configuration with a vanilla Feign.Builder. However adding the new @Bean Feign.Builder disables my Hystrix functionality across all Feign clients which I don't want. If I remove the @Bean Feign.Builder, Hystrix fallback kicks in like usual in myhystrixclient. A similar SO question here How to disable hystrix in one of multiple feign clients is still open. What am I doing wrong?

public class MyFeignClientConfiguration {

@Bean
public FeignErrorDecoder feignErrorDecoder() {
    return new FeignErrorDecoder();
}

@Bean
@Scope("prototype")
public Feign.Builder feignBuilder() {
    return Feign.builder();
}
}

My Feign Client looks like below:

@FeignClient(name = "myregularclient", configuration = MyFeignClientConfiguration.class)
public interface MyRegularClient {
//my APIs here
}

My Hystrix Feign Configuration looks like the below:

public class MyFeignClientHystrixConfiguration {

@Bean
public FeignErrorDecoder feignErrorDecoder() {
    return new FeignErrorDecoder();
}
}

And here is my Feign client where Hystrix fallback is implemented

@FeignClient(name = "myhystrixclient", configuration = MyFeignClientHystrixConfiguration.class, fallback = MyFallbackService.class)
public interface MyHystrixClient {
//my APIs here
}

UPDATE

Adding my Application.java for further review of the component scan aspects.

@ComponentScan(basePackages ="com.demo.xyz")
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, 
MetricFilterAutoConfiguration.class,         
MetricRepositoryAutoConfiguration.class})
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class MyApplication {

/** Start the app **/
}

回答1:


I managed to reproduce problem on Spring Cloud vDalston.SR5 and it seems I found a solution. It doesn't brake other feign clients which use hystrix. Tested it manually and with integration tests.

Create feign client without hystrix. Notice that configuration class is annotated with @Configuration annotation.

@FeignClient(name = "withoutHystrix",
      url = "conrad.fake",
      fallback = FeignClientWithoutHystrix.FallbackThatShouldNotOccur.class,
      configuration = FeignClientWithoutHystrixConfig.class)
public interface FeignClientWithoutHystrix {

   @RequestMapping(method = RequestMethod.GET, value = "/fake/url")
   String getFromFakeUrl();

   @Component
   class FallbackThatShouldNotOccur implements FeignClientWithoutHystrix {

      private final Logger log = LoggerFactory.getLogger(this.getClass());

      @Override
      public String getFromFakeUrl() {
         log.error("This fallback shouldn't occur");
         return "Fallback";
      }
   }
}

@Configuration
public class FeignClientWithoutHystrixConfig {

   @Bean
   public Feign.Builder feignBuilder() {
      return Feign.builder();
   }

}

Exclude feign client configuration from @ComponentScan with excludeFilters:

@EnableFeignClients
@SpringBootApplication
@ComponentScan(basePackages = "konrad",
      excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = FeignClientWithoutHystrixConfig.class)})
public class CloudClient {

   public static void main(String[] args) {
      SpringApplication.run(CloudClient.class, args);
   }

}

Enable hystrix

feign:
  hystrix:
    enabled: true

If you want to check anything or run tests, this is my repository https://github.com/MrJavaNoHome/spring-cloud-client




回答2:


please try this configuration:

import feign.Feign;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class MyFeignClientConfiguration {

  @Bean
  @Scope("prototype")
  public Feign.Builder feignBuilder() {
    return Feign.builder();
  }
}

And:

import feign.hystrix.HystrixFeign;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
public class MyFeignClientHystrixConfiguration {  

  @Bean
  @Scope("prototype")
  public HystrixFeign.Builder feignBuilder() {
    return HystrixFeign.builder();
  }
}


来源:https://stackoverflow.com/questions/62669138/disable-hystrix-for-a-single-feign-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!