Circuit Breaker
Dependency :
compile ":circuit-breaker:0.2"
Summary
Installation
grails install-plugin circuit-breaker
Description
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
Inresources.groovy you can configure the Circuit Breaker as followsbeans = {
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 inConfig.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 toConfig.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
Examples
- Tests: CircuitBreakerAspectTests
- An example application
- With tests
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.'