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 **/
}
If you want, you can disable Hystrix as a whole by adding hystrix. enabled = false to your application. properties . This code is actually ready to be put into Spring Boot's autoconfigure module :).
Yes you can use Feign without Ribbon, All you need to do is specify the base url in your Feign Java interface class.
Feign is a declarative web service client, which comes with Hystrix built in when you use it with Spring Cloud. Hystrix is a Netflix OSS library that implements the circuit breaker pattern.
Synchronous and Asynchronous API calls The API that we defined via Feign is synchronous — meaning that it makes blocking calls to the server. Feign allows us to easily define async APIs as well — utilizing CompletableFutures under the hood.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With