I am writing a circuitbreaker wrapper which should encapsulate http calls (using Spring WebClient ). All the integration services would call the invokeService() method which in turn calls excecute().
Now, if execute() is successful, fine. If not, circuitbreaker should trigger after n failures( based on configurations). This is what I have done. Not sure if this would work( because of Mono) and the right way to do it.
@Service
class CircuitBreakerWrapper @Inject constructor(cbService: CircuitBreakerService?)
: BaseHttpExcecutor() {
private var cbService = cbService
fun <R> invokeService(
service: String,
webClient: WebClient,
HttpRequestSpec httpRequestSpec,
cbProperties: CircuitBreakerProperties?,
type: Class<R>?): Mono<R> {
val circuitBreaker = cbService
.registerCircuitBreaker(serviceName, properties)
val supplier: Supplier<Mono<R>> = Supplier {
execute(webClient, httpRequestSpec, type)
}
val decoratedSupplier = CircuitBreaker
.decorateSupplier(circuitBreaker, supplier)
return Try.ofSupplier(decoratedSupplier).get()
}
}
This is what execute() looks like:
webClient.method(HttpMethod.GET)
.uri(url, params)
.retrieve()
.bodyToMono(response)