Overview
This plugin implements the Circuit Breaker pattern described in Michael Nygard’s book
Release It!
Background
From
Release It! -
" …the circuit breaker exists to allow one subsystem (an electrical circuit) to fail (excessive current draw, possibly from a short-circuit) without destroying the entire system (the house). Furthermore, once the danger has passed, the circuit breaker can be reset to restore full function to the system.
"You can apply the same technique to software by wrapping dangerous operations with a component that can circumvent calls when the system is not healthy. This differs from retries, in that circuit breakers exist to prevent operations rather than reexecute them."
Configuration
In
resources.groovy you can configure the Circuit Breaker as follows
beans = {
xmlns aop:"http://www.springframework.org/schema/aop" breakerAspectOne(breaker.CircuitBreakerAspect)
{
id = 'breaker-one'
failureThresholdCount = 3 // trip breaker after three failures
timeoutMillis = 5000 // fail fast for 5 seconds, then try again
} aop.config("proxy-target-class":true) {
aspect(id:"serviceTestCallOneBreaker", ref:"breakerAspectOne" ) {
around method:"invoke", pointcut: "execution(* *.MyServiceInterface.testCallOne(..))"
}
}
}Environment-specific configuration
Configuration can also be conditional based on the current Grails environment:
switch(grails.util.GrailsUtil.environment)
{
case "production":
// … configure circuit breaker with prod threshold
break
case "development":
// … configure circuit breaker with dev threshold
break
}Debug Logging
Debug logging can be enabled in
Config.groovy -
log4j = {
debug 'breaker' // debug messages for all circuit breaker classes
}
Access current Circuit Breaker state via JMX
The current state of all Circuit Breakers can be accessed via JMX by using version 0.6 or above of the
JMX Plugin and by adding the following to
Config.groovy -
grails {
jmx {
exportBeans = ['myBean']
}
}
Access current Circuit Breaker state without JMX
The current state of all Circuit Breakers can also be accessed without JMX by installing the CircuitBreakerController. After installing the plugin, this can be done by:
grails install-circuit-breaker-controller
This will install a simple Grails controller and view that provide easy read-only access to the circuit breakers that have been configured in the application.
Examples
Future
- Allow configuration of timeout & failureThreshold differently by exception type
- From Release It! - 'For example, you may choose to have a lower threshold for “timeout calling remote system” failures than “connection refused” errors.'