Created
April 7, 2019 14:59
-
-
Save RobWin/718a9e8e85d60e2d245b4efe6b0efcf3 to your computer and use it in GitHub Desktop.
Functional Web Spring 5 with Resilience4j
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@SpringBootApplication | |
@Configuration | |
public class DemoApplication { | |
private CircuitBreakerRegistry circuitBreakerRegistry; | |
private RetryRegistry retryRegistry; | |
private EmployeeRepository employeeRepository; | |
public DemoApplication(CircuitBreakerRegistry circuitBreakerRegistry, | |
RetryRegistry retryRegistry, | |
EmployeeRepository employeeRepository){ | |
this.circuitBreakerRegistry = circuitBreakerRegistry; | |
this.retryRegistry = retryRegistry; | |
this.employeeRepository = employeeRepository; | |
} | |
@Bean | |
RouterFunction<ServerResponse> getAllEmployeesRoute() { | |
return route(GET("/employees"), | |
req -> ok().body(protect(employeeRepository.findAllEmployees()), Employee.class)); | |
} | |
private <T> Flux<T> protect(Flux<T> flux){ | |
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("employeeBackend"); | |
Retry retry = retryRegistry.retry("employeeBackend"); | |
return flux | |
.transform(CircuitBreakerOperator.of(circuitBreaker)) | |
.transform(RetryOperator.of(retry)); | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment