Certainly! Here's a concise example of how to use Spring Cloud Starter Reactor Circuit Breaker with Retry functionality. This example demonstrates a simple service that makes HTTP calls and uses a circuit breaker with retry logic.
Dependencies
First, add the necessary dependencies to your pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j-retry</artifactId>
</dependency>
</dependencies>
Configuration
Create a configuration class to define the circuit breaker and retry settings:
import org.springframework.cloud.circuitbreaker.resilience4j.ReactiveResilience4JCircuitBreakerFactory;
import org.springframework.cloud.circuitbreaker.resilience4j.Resilience4JConfigBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.retry.RetryConfig;
import java.time.Duration;
@Configuration
public class Resilience4JConfig {
@Bean
public ReactiveResilience4JCircuitBreakerFactory circuitBreakerFactory() {
ReactiveResilience4JCircuitBreakerFactory factory = new ReactiveResilience4JCircuitBreakerFactory();
factory.configure(builder -> builder
.circuitBreakerConfig(CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.waitDurationInOpenState(Duration.ofMillis(1000))
.slidingWindowSize(2)
.build())
.retryConfig(RetryConfig.custom()
.maxAttempts(3)
.waitDuration(Duration.ofMillis(500))
.build()), "myCircuitBreaker");
return factory;
}
}
Service
Create a service that uses the circuit breaker and retry logic:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
@Service
public class MyService {
private final WebClient webClient;
private final ReactiveCircuitBreakerFactory circuitBreakerFactory;
@Autowired
public MyService(WebClient.Builder webClientBuilder, ReactiveCircuitBreakerFactory circuitBreakerFactory) {
this.webClient = webClientBuilder.baseUrl("http://example.com").build();
this.circuitBreakerFactory = circuitBreakerFactory;
}
public Mono<String> makeRequest() {
return webClient.get()
.uri("/api/resource")
.retrieve()
.bodyToMono(String.class)
.transform(it -> circuitBreakerFactory.create("myCircuitBreaker").run(it, throwable -> Mono.just("Fallback response")));
}
}
Controller
Finally, create a controller to expose an endpoint that uses the service:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
@RestController
public class MyController {
private final MyService myService;
@Autowired
public MyController(MyService myService) {
this.myService = myService;
}
@GetMapping("/request")
public Mono<String> request() {
return myService.makeRequest();
}
}
Summary
This example demonstrates how to set up a Spring Boot application with Spring Cloud Starter Reactor Circuit Breaker and Retry functionality using Resilience4J. The configuration class defines the circuit breaker and retry settings, the service makes HTTP calls with the circuit breaker and retry logic, and the controller exposes an endpoint to trigger the service.
Copied
Perform Analysis
Time complexity
Space complexity
Change Language
Select language
Python
C++
Java
C#
JavaScript
Testing Tools
Generate test data
Integrate test cases
More Actions
Perform code review
Explain the code
Add error handling
Make code compilable