$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill spring-boot-resilience4jProvides fault tolerance patterns for Spring Boot 3.x using Resilience4j. Use when implementing circuit breakers, handling service failures, adding retry logic with exponential backoff, configuring rate limiters, or protecting services from cascading failures. Generates circuit b
| 1 | # Spring Boot Resilience4j Patterns |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Provides Resilience4j patterns (circuit breaker, retry, rate limiter, bulkhead, time limiter, fallback) for Spring Boot 3.x fault tolerance with configuration and testing workflows. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Implementing fault tolerance and preventing cascading failures |
| 10 | - Adding circuit breakers, retry logic, or rate limiting to service calls |
| 11 | - Handling transient failures with exponential backoff |
| 12 | - Protecting services from overload and resource exhaustion |
| 13 | - Combining multiple patterns for comprehensive resilience |
| 14 | |
| 15 | ## Instructions |
| 16 | |
| 17 | ### 1. Setup and Dependencies |
| 18 | |
| 19 | Add Resilience4j dependencies to your project. For Maven, add to `pom.xml`: |
| 20 | |
| 21 | ```xml |
| 22 | <dependency> |
| 23 | <groupId>io.github.resilience4j</groupId> |
| 24 | <artifactId>resilience4j-spring-boot3</artifactId> |
| 25 | <version>2.2.0</version> // Use latest stable version |
| 26 | </dependency> |
| 27 | <dependency> |
| 28 | <groupId>org.springframework.boot</groupId> |
| 29 | <artifactId>spring-boot-starter-aop</artifactId> |
| 30 | </dependency> |
| 31 | <dependency> |
| 32 | <groupId>org.springframework.boot</groupId> |
| 33 | <artifactId>spring-boot-starter-actuator</artifactId> |
| 34 | </dependency> |
| 35 | ``` |
| 36 | |
| 37 | For Gradle, add to `build.gradle`: |
| 38 | |
| 39 | ```gradle |
| 40 | implementation "io.github.resilience4j:resilience4j-spring-boot3:2.2.0" |
| 41 | implementation "org.springframework.boot:spring-boot-starter-aop" |
| 42 | implementation "org.springframework.boot:spring-boot-starter-actuator" |
| 43 | ``` |
| 44 | |
| 45 | Enable AOP annotation processing with `@EnableAspectJAutoProxy` (auto-configured by Spring Boot). |
| 46 | |
| 47 | ### 2. Circuit Breaker Pattern |
| 48 | |
| 49 | Apply `@CircuitBreaker` annotation to methods calling external services: |
| 50 | |
| 51 | ```java |
| 52 | @Service |
| 53 | public class PaymentService { |
| 54 | private final RestTemplate restTemplate; |
| 55 | |
| 56 | public PaymentService(RestTemplate restTemplate) { |
| 57 | this.restTemplate = restTemplate; |
| 58 | } |
| 59 | |
| 60 | @CircuitBreaker(name = "paymentService", fallbackMethod = "paymentFallback") |
| 61 | public PaymentResponse processPayment(PaymentRequest request) { |
| 62 | return restTemplate.postForObject("http://payment-api/process", |
| 63 | request, PaymentResponse.class); |
| 64 | } |
| 65 | |
| 66 | private PaymentResponse paymentFallback(PaymentRequest request, Exception ex) { |
| 67 | return PaymentResponse.builder() |
| 68 | .status("PENDING") |
| 69 | .message("Service temporarily unavailable") |
| 70 | .build(); |
| 71 | } |
| 72 | } |
| 73 | ``` |
| 74 | |
| 75 | Configure in `application.yml`: |
| 76 | |
| 77 | ```yaml |
| 78 | resilience4j: |
| 79 | circuitbreaker: |
| 80 | configs: |
| 81 | default: |
| 82 | registerHealthIndicator: true |
| 83 | slidingWindowSize: 10 |
| 84 | minimumNumberOfCalls: 5 |
| 85 | failureRateThreshold: 50 |
| 86 | waitDurationInOpenState: 10s |
| 87 | instances: |
| 88 | paymentService: |
| 89 | baseConfig: default |
| 90 | ``` |
| 91 | |
| 92 | See @references/configuration-reference.md for complete circuit breaker configuration options. |
| 93 | |
| 94 | ### 3. Retry Pattern |
| 95 | |
| 96 | Apply `@Retry` annotation for transient failure recovery: |
| 97 | |
| 98 | ```java |
| 99 | @Service |
| 100 | public class ProductService { |
| 101 | private final RestTemplate restTemplate; |
| 102 | |
| 103 | public ProductService(RestTemplate restTemplate) { |
| 104 | this.restTemplate = restTemplate; |
| 105 | } |
| 106 | |
| 107 | @Retry(name = "productService", fallbackMethod = "getProductFallback") |
| 108 | public Product getProduct(Long productId) { |
| 109 | return restTemplate.getForObject( |
| 110 | "http://product-api/products/" + productId, |
| 111 | Product.class); |
| 112 | } |
| 113 | |
| 114 | private Product getProductFallback(Long productId, Exception ex) { |
| 115 | return Product.builder() |
| 116 | .id(productId) |
| 117 | .name("Unavailable") |
| 118 | .available(false) |
| 119 | .build(); |
| 120 | } |
| 121 | } |
| 122 | ``` |
| 123 | |
| 124 | Configure retry in `application.yml`: |
| 125 | |
| 126 | ```yaml |
| 127 | resilience4j: |
| 128 | retry: |
| 129 | configs: |
| 130 | default: |
| 131 | maxAttempts: 3 |
| 132 | waitDuration: 500ms |
| 133 | enableExponentialBackoff: true |
| 134 | exponentialBackoffMultiplier: 2 |
| 135 | instances: |
| 136 | productService: |
| 137 | baseConfig: default |
| 138 | maxAttempts: 5 |
| 139 | ``` |
| 140 | |
| 141 | See @references/configuration-reference.md for retry exception configuration. |
| 142 | |
| 143 | ### 4. Rate Limiter Pattern |
| 144 | |
| 145 | Apply `@RateLimiter` to control request rates: |
| 146 | |
| 147 | ```java |
| 148 | @Service |
| 149 | public class NotificationService { |
| 150 | private final EmailClient emailClient; |
| 151 | |
| 152 | public NotificationService(EmailClient emailClient) { |
| 153 | this.emailClient = emailClient; |
| 154 | } |
| 155 | |
| 156 | @RateLimiter(name = "notificationService", |
| 157 | fallbackMethod = "rateLimitFallback") |
| 158 | public void sendEmail(EmailRequest request) { |
| 159 | emailClient.send(request); |
| 160 | } |
| 161 | |
| 162 | private void rateLimitFallback(EmailRequest request, Exception ex) { |
| 163 | throw ne |