Defining behavior for circuit breaker open state
Problem describe I need to know the Resilience4j implementation to define the behavior of the CircuitBreaker when it is opened. Here's why
import io.github.resilience4j.circuitbreaker.CallNotPermittedException
factory.create(name).run(block) { e ->
when(e) {
is CallNotPermittedException -> // Open
else -> // Not open
}
}
I only want to know the Spring cloud circuit breaker interface, not the resilience4j implementation, because I want to be able to easily change to a different implementation in the future.
Solution
- When the circuit breaker is open, an exception specifically defined by Spring Cloud Circuit Breaker is thrown.
- The result of the CircuitBreaker's run function is encapsulated in a custom Response model, which will indicate whether the CircuitBreaker is open.
- The run function of the Circuitbreaker is given a function that is only executed when the CircuitBreaker is open.
- There is a way to check the current status of the circuit breaker.
Alternatives I'm currently using a custom exception to handle the situation when the circuit breaker is open. For more details, you can refer to this implementation: https://github.com/kyle-log/circuit-breaker-without-annotation"
The exception that should be thrown when the circuit breaker is open is CallNotPermittedException.
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Thanks for the answer. It seems that CallNotPermittedException is a specific exception used in the Resilience4j framework.
// as is
import io.github.resilience4j.circuitbreaker.CallNotPermittedException;
factory.create(name).run(
() -> { /* block */ },
e -> {
if (e instanceof CallNotPermittedException) {
// Open
return DefaultValue()
} else {
// Not open
throw e
}
}
);
// to be, (example)
import org.springframework.cloud.client.CircuitOpenException
factory.create(name).run(
() -> { /* block */ },
e -> {
if (e instanceof CircuitOpenException) {
// Open
return DefaultValue()
} else {
// Not open
throw e
}
}
);
This is to provide flexibility in case the circuit breaker implementation Resilience4j changes to something else..
Sorry I am not following what you are saying