mule4-circuit-breaker-demo-app
Code
When you want to see the circuit open, refuse calls and close on a real runtime, mule4-circuit-breaker-demo-app, the application from the article Circuit Breaker in MuleSoft: When Retry Makes Things Worse, uses the mule4-circuit-breaker plugin against a simulated backend that you can make fail, slow down or recover. With a curl in a loop, the first five calls return 502, because the backend was called and failed, and the sixth returns 503: the circuit opened and the backend is no longer called.
Watch the circuit open
The circuit starts closed, opens when the fifth failing call fills the window and tests again after the resetTimeout (10 seconds by default). To see it recover, open the circuit, wait the 10 seconds and call with ok: the test call passes and the circuit closes.
for i in 1 2 3 4 5 6; do curl -i http://localhost:8081/orders/demo -H 'X-Demo-Backend-Mode: always_fail'; done
sleep 10
curl -i http://localhost:8081/orders/demo -H 'X-Demo-Backend-Mode: ok'
What it does
GET /orders/{circuitKey}calls the backend insidecircuit-breaker:execute; the key chooses the circuit.GET /backend/ordersis the simulated backend; theX-Demo-Backend-Modeheader sets its behavior, with no state on the server.- The
global-error-handlertranslates: an open circuit becomes 503, a business error 400, an infrastructure failure 502. GET /orders-bypass/{circuitKey}makes the same call without the plugin, as a latency baseline.
Limit: the circuit state lives in the plugin’s Object Store, which matters across replicas, so on a single local instance the demo shows less, and the CloudHub 2.0 deployments run on an Anypoint trial and stop when it ends.
Getting started
Needs Maven 3.9.x, an installed standalone Mule 4 runtime and the plugin, built from the tag, with no Anypoint account; the build may ask for an anypoint-exchange-v3 server entry in ~/.m2/settings.xml. mvn clean package runs MUnit, which requires MuleSoft’s Enterprise repository, hence -DskipMunitTests:
git clone --branch v2.0.0 https://github.com/brunosouzas/mule4-circuit-breaker.git
mvn -f mule4-circuit-breaker/pom.xml -DskipTests install
git clone https://github.com/brunosouzas/mule4-circuit-breaker-demo-app.git
cd mule4-circuit-breaker-demo-app
mvn clean package -DskipMunitTests
Copy the jar from target/ to <MULE_HOME>/apps/ and start the runtime; the application listens on port 8081.
curl -i http://localhost:8081/orders/demo -H 'X-Demo-Backend-Mode: ok'