How to correctly handle “FeignException$ServiceUnavailableException” with fallback and without deprecated @EnableCircuitbreaker annotation

Ended up using resilience4j for spring cloud. Next to feign.circuitbreaker.enabled=true in application.properties which sets up a circuitbreaker with a default config.

One can add a custom configuration like this:

@Configuration
public class FeignConfiguration {
    @Bean
    public Customizer<Resilience4JCircuitBreakerFactory> circuitBreakerFactoryCustomizer() {
        CircuitBreakerConfig circuitBreakerConfig =
                CircuitBreakerConfig.custom()
                        .ignoreException(FeignException.ServiceUnavailable.class::isInstance)
                        .build();
        return circuitBreakerFactory -> circuitBreakerFactory
                .configure(builder -> builder.circuitBreakerConfig(circuitBreakerConfig),
                        ObjectsClient.class.getSimpleName());
    }
}

Also, the resilience4j dependency needs to be in place, here a maven pom.xml is used.

<project> 
    <!-- .. --> 
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
        </dependency>
   </dependencies>

That way you can replace the deprecated hystrix implementation and remove the @EnableCircuitBreaker annotation. In case you want a more fine-grained setup, here are is a discussion how to implement fallback with resilience4j via default methods.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top