8

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 **/
}
Vinod Kumar Rai
  • 304
  • 1
  • 5
  • 23
  • Have you tried this: https://stackoverflow.com/questions/55200113/how-to-disable-hystrix-in-one-of-multiple-feign-clients?noredirect=1&lq=1 –  Jul 07 '20 at 04:58
  • I have exclusively mentioned that question in my post here as well. The suggested solutions there did not work for me. – Vinod Kumar Rai Jul 08 '20 at 14:11
  • is your MyFeignClientConfiguration annotated with @Configuration? – stacker Jul 08 '20 at 18:00
  • @stacker I don't. Both my configuration classes are not annotated. I've updated the question with my Spring boot config just in case it helps. – Vinod Kumar Rai Jul 08 '20 at 20:27
  • @VinodKumarRai add your pom file – stacker Jul 08 '20 at 21:41
  • @stacker For security reasons, I may not be able to provide the entire pom.xml. However I can provide you dependency versions if you are looking for something specific. I'm currently on Spring Boot v1.5.13.RELEASE, Spring Cloud vDalston.SR5 and have spring-cloud-starter-hystrix added as a dependency. – Vinod Kumar Rai Jul 09 '20 at 13:55

3 Answers3

2

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

Conrad
  • 536
  • 5
  • 13
  • This did not work for me. The behavior is the same as before and my Hystrix fallback simply does not work if I introduce the Feign.Builder bean. Also adding the @Configuration is not suggested in the official docs as shown here https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html#spring-cloud-feign-overriding-defaults – Vinod Kumar Rai Jul 14 '20 at 03:52
  • I have updated my answer @VinodKumarRai Check it out – Conrad Jul 14 '20 at 19:04
0

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();
  }
}
0

feign-hystrix was a part of spring-cloud-starter-openfeign with version less than 3.0.0.

Starting from 3.0.0 , feign-hystrix no longer included

ayman.mostafa
  • 451
  • 6
  • 5