{"id":3762,"date":"2025-12-24T10:00:07","date_gmt":"2025-12-24T15:00:07","guid":{"rendered":"https:\/\/www.mymiller.name\/wordpress\/?p=3762"},"modified":"2025-12-24T10:00:07","modified_gmt":"2025-12-24T15:00:07","slug":"spring-cloud-circuit-breaker-with-feign-client-a-fallback-strategy","status":"publish","type":"post","link":"https:\/\/www.mymiller.name\/wordpress\/spring_circuit_breaker\/spring-cloud-circuit-breaker-with-feign-client-a-fallback-strategy\/","title":{"rendered":"Spring Cloud Circuit Breaker with Feign Client: A Fallback Strategy"},"content":{"rendered":"\n<div class=\"wp-block-jetpack-markdown\"><p>Microservices often rely on each other, but what happens when a service goes down? Enter Spring Cloud Circuit Breaker, a pattern that prevents cascading failures by providing fallback mechanisms when a service is unavailable. This article focuses on integrating Spring Cloud Circuit Breaker with Feign client, a declarative HTTP client, to handle such scenarios gracefully.<\/p>\n<h3>1. Project Setup<\/h3>\n<p><strong>Dependencies:<\/strong><\/p>\n<p>Start by adding the necessary dependencies to your <code>build.gradle<\/code> (Gradle) or <code>pom.xml<\/code> (Maven) file.<\/p>\n<p><strong>Gradle:<\/strong><\/p>\n<pre><code class=\"language-gradle\">dependencies {\n    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'\n    implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j'\n}\n<\/code><\/pre>\n<p><strong>Maven:<\/strong><\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-openfeign&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.cloud&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-cloud-starter-circuitbreaker-resilience4j&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n<p><strong>Note:<\/strong> This example uses Resilience4j as the circuit breaker implementation. You can choose another implementation like Hystrix or Sentinel by replacing the <code>spring-cloud-starter-circuitbreaker-resilience4j<\/code> dependency.<\/p>\n<h3>2. Enabling Circuit Breaker for Feign Clients<\/h3>\n<p>Enable circuit breaker functionality for your Feign clients by adding the following property to your <code>application.properties<\/code> or <code>application.yml<\/code> file:<\/p>\n<pre><code class=\"language-properties\">feign.circuitbreaker.enabled=true\n<\/code><\/pre>\n<h3>3. Defining a Feign Client<\/h3>\n<p>Define a Feign client interface with the <code>@FeignClient<\/code> annotation. Specify the name of the service and the fallback class:<\/p>\n<pre><code class=\"language-java\">@FeignClient(name = &quot;inventory-service&quot;, fallback = InventoryServiceFallback.class)\npublic interface InventoryServiceClient {\n\n    @GetMapping(&quot;\/inventory\/{productId}&quot;)\n    Inventory getInventory(@PathVariable Long productId);\n}\n<\/code><\/pre>\n<h3>4. Implementing the Fallback Class<\/h3>\n<p>Create a fallback class that implements the Feign client interface and provides alternative logic for when the service is unavailable:<\/p>\n<pre><code class=\"language-java\">@Component\npublic class InventoryServiceFallback implements InventoryServiceClient {\n\n    @Override\n    public Inventory getInventory(Long productId) {\n        \/\/ Fallback logic, e.g., return a default value or cached data\n        return new Inventory(productId, 0);\n    }\n}\n<\/code><\/pre>\n<h3>5. Configuring the Circuit Breaker<\/h3>\n<p>Customize the circuit breaker behavior using Resilience4j configuration properties in your <code>application.properties<\/code> or <code>application.yml<\/code> file:<\/p>\n<pre><code class=\"language-yaml\">resilience4j.circuitbreaker:\n  instances:\n    inventory-service:\n      registerHealthIndicator: true\n      slidingWindowSize: 10\n      failureRateThreshold: 50\n      slowCallRateThreshold: 50\n      slowCallDurationThreshold: 2s\n      permittedNumberOfCallsInHalfOpenState: 5\n      waitDurationInOpenState: 5s\n<\/code><\/pre>\n<p>This configuration sets parameters like the failure rate threshold, wait duration in open state, and the permitted number of calls in half-open state for the \u201cinventory-service\u201d circuit breaker.<\/p>\n<h3>6. Using the Feign Client<\/h3>\n<p>Inject the Feign client into your service and use it to make requests:<\/p>\n<pre><code class=\"language-java\">@Service\npublic class ProductService {\n\n    @Autowired\n    private InventoryServiceClient inventoryServiceClient;\n\n    public Product getProduct(Long productId) {\n        \/\/ ... other logic ...\n        Inventory inventory = inventoryServiceClient.getInventory(productId);\n        \/\/ ... use the inventory data ...\n    }\n}\n<\/code><\/pre>\n<p>When the \u201cinventory-service\u201d is unavailable or experiencing failures, the circuit breaker will open, and the <code>getInventory()<\/code> method in the <code>InventoryServiceFallback<\/code> class will be executed, providing a fallback response.<\/p>\n<h3>Conclusion<\/h3>\n<p>By integrating Spring Cloud Circuit Breaker with Feign client, you can build resilient microservices that gracefully handle service failures. This approach helps prevent cascading failures and ensures a better user experience even when dependencies are down. Remember to configure the circuit breaker with appropriate thresholds and fallback logic based on your specific needs and service requirements.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":3445,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[434],"tags":[69,319],"series":[],"class_list":["post-3762","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring_circuit_breaker","tag-java-2","tag-spring"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2023\/11\/manhattan-3866140_640.jpg?fit=640%2C427&ssl=1","jetpack-related-posts":[{"id":3764,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_circuit_breaker\/mastering-fallback-methods-in-spring-cloud-circuit-breaker\/","url_meta":{"origin":3762,"position":0},"title":"Mastering Fallback Methods in Spring Cloud Circuit Breaker","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Spring Cloud Circuit Breaker provides an elegant way to handle failures in distributed systems by employing the circuit breaker pattern. A crucial aspect of this pattern is the fallback mechanism, which allows your application to gracefully handle situations where a service dependency is unavailable or experiencing issues. This article dives\u2026","rel":"","context":"In &quot;Spring Circuit Breaker&quot;","block_context":{"text":"Spring Circuit Breaker","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_circuit_breaker\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/sphere-5551752_1280-jpg.avif 3x"},"classes":[]},{"id":3767,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_circuit_breaker\/leveraging-the-circuitbreakerfactory-in-spring-cloud-circuit-breaker\/","url_meta":{"origin":3762,"position":1},"title":"Leveraging the CircuitBreakerFactory in Spring Cloud Circuit Breaker","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Spring Cloud Circuit Breaker provides a powerful abstraction for implementing the circuit breaker pattern in your microservices. At the heart of this abstraction lies the CircuitBreakerFactory, a key component that simplifies the creation and management of circuit breakers. This article provides a comprehensive guide to using CircuitBreakerFactory, including setup, configuration,\u2026","rel":"","context":"In &quot;Spring Circuit Breaker&quot;","block_context":{"text":"Spring Circuit Breaker","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_circuit_breaker\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8221377_1280-png.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8221377_1280-png.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8221377_1280-png.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8221377_1280-png.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8221377_1280-png.avif 3x"},"classes":[]},{"id":3771,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_circuit_breaker\/mastering-circuitbreaker-in-spring-cloud-a-comprehensive-guide\/","url_meta":{"origin":3762,"position":2},"title":"Mastering CircuitBreaker in Spring Cloud: A Comprehensive Guide","author":"Jeffery Miller","date":"December 23, 2025","format":false,"excerpt":"Spring Cloud Circuit Breaker provides an elegant way to handle failures in distributed systems by implementing the circuit breaker pattern. This pattern prevents cascading failures and improves application resilience by isolating failing services and providing fallback mechanisms. This article offers a deep dive into using CircuitBreaker in Spring Cloud, covering\u2026","rel":"","context":"In &quot;Spring Circuit Breaker&quot;","block_context":{"text":"Spring Circuit Breaker","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_circuit_breaker\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/network-5987786_1280-jpg.avif 3x"},"classes":[]},{"id":3826,"url":"https:\/\/www.mymiller.name\/wordpress\/spring-gateway\/resilient-gateways-implementing-circuit-breakers-for-spring-data-rest-services-with-spring-cloud-gateway\/","url_meta":{"origin":3762,"position":3},"title":"Resilient Gateways: Implementing Circuit Breakers for Spring Data REST Services with Spring Cloud Gateway","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"In a microservice architecture, services inevitably encounter transient failures \u2013 network hiccups, temporary overload, or slow responses from dependencies. Without proper handling, these failures can cascade, leading to a degraded user experience and even system-wide outages. This is where the circuit breaker pattern comes into play, providing a mechanism to\u2026","rel":"","context":"In &quot;Spring Gateway&quot;","block_context":{"text":"Spring Gateway","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring-gateway\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/ai-generated-8314612_640.jpg?fit=640%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/ai-generated-8314612_640.jpg?fit=640%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/04\/ai-generated-8314612_640.jpg?fit=640%2C480&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":3773,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_circuit_breaker\/choosing-the-right-circuit-breaker-a-comparison-of-implementations\/","url_meta":{"origin":3762,"position":4},"title":"Choosing the Right Circuit Breaker: A Comparison of Implementations","author":"Jeffery Miller","date":"December 24, 2025","format":false,"excerpt":"Spring Cloud Circuit Breaker provides a facade over popular circuit breaker implementations, giving developers flexibility and a consistent API. However, the level of support for Spring Cloud Circuit Breaker interfaces varies across implementations. Let\u2019s explore their differences, assess their compatibility, and highlight their unique features, paying close attention to any\u2026","rel":"","context":"In &quot;Spring Circuit Breaker&quot;","block_context":{"text":"Spring Circuit Breaker","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_circuit_breaker\/"},"img":{"alt_text":"","src":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8656640_1280-jpg.avif","width":350,"height":200,"srcset":"https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8656640_1280-jpg.avif 1x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8656640_1280-jpg.avif 1.5x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8656640_1280-jpg.avif 2x, https:\/\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/10\/circuit-board-8656640_1280-jpg.avif 3x"},"classes":[]},{"id":3542,"url":"https:\/\/www.mymiller.name\/wordpress\/spring_open_feign\/simplified-rest-clients-with-openfeign-in-java\/","url_meta":{"origin":3762,"position":5},"title":"Simplified REST Clients with OpenFeign in Java","author":"Jeffery Miller","date":"April 20, 2026","format":false,"excerpt":"OpenFeign, a declarative HTTP client, simplifies REST API interactions in Java. Let\u2019s see how: Dependency: Add OpenFeign to your project (Maven example): <dependency> <groupId>org.springframework.cloud<\/groupId> <artifactId>spring-cloud-starter-openfeign<\/artifactId> <\/dependency> Interface: Define an interface mirroring your REST endpoints: @FeignClient(name = \"myService\", url = \"${myService.url}\") public interface MyServiceClient { @GetMapping(\"\/users\/{id}\") User getUserById(@PathVariable(\"id\") Long id, @RequestHeader(\"Authorization\")\u2026","rel":"","context":"In &quot;Spring Open Feign&quot;","block_context":{"text":"Spring Open Feign","link":"https:\/\/www.mymiller.name\/wordpress\/category\/spring_open_feign\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/technology-7173626_1280.jpg?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/technology-7173626_1280.jpg?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/technology-7173626_1280.jpg?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/technology-7173626_1280.jpg?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.mymiller.name\/wordpress\/wp-content\/uploads\/2024\/06\/technology-7173626_1280.jpg?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3762","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/comments?post=3762"}],"version-history":[{"count":1,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3762\/revisions"}],"predecessor-version":[{"id":3763,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/posts\/3762\/revisions\/3763"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media\/3445"}],"wp:attachment":[{"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/media?parent=3762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/categories?post=3762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/tags?post=3762"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/www.mymiller.name\/wordpress\/wp-json\/wp\/v2\/series?post=3762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}