Skip to content

Instantly share code, notes, and snippets.

@RobWin
Created April 7, 2019 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobWin/718a9e8e85d60e2d245b4efe6b0efcf3 to your computer and use it in GitHub Desktop.
Save RobWin/718a9e8e85d60e2d245b4efe6b0efcf3 to your computer and use it in GitHub Desktop.
Functional Web Spring 5 with Resilience4j
@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