Spring Cloud Circuit Breaker - SirajChaudhary/comprehensive-example-on-microservices-using-spring-boot-with-spring-cloud GitHub Wiki
Spring Cloud Circuit breaker provides an abstraction across different circuit breaker implementations. It provides a consistent API to use in your applications allowing you the developer to choose the circuit breaker implementation that best fits your needs for your app. Supported Implementations are,
Example: writing a circuit breaker for downstream airport-pilot-service so if the airport-pilot-service is down we send a dummy response as its result
Step1: Add spring cloud circuit breaker resilience4j dependency in pom.xml in a upstream microservice (airport-flight-service)
<!-- spring cloud starter circuit breaker (resilience4j implementation) -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
<version>2.0.2</version>
</dependency>
<!-- spring cloud starter aop (Its needed for spring-cloud-starter-circuitbreaker-resilience4j) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Step2: Add spring cloud circuit breaker resilience4j properties in application.yml in a upstream microservice (airport-flight-service)
# SPRING CLOUD CIRCUIT BREAKER WITH RESILIENCE4J IMPLEMENTATION
# Spring Cloud Circuit breaker provides an abstraction across different circuit breaker implementations
# i.e Hystrix, Resilience4j, Sentinel, Spring Retry
# circuit breaker switch is indicated with following terms
# i.e. UP means switch is closed and so allowed all the calls closed - UP, open - DOWN, half-open - UNKNOWN
resilience4j:
circuitbreaker:
instances:
airportPilotService:
# slidingWindowSize indicate (e.g. 5) after how many calls the switch will be open or close
slidingWindowSize: '5'
# we are now setting threshold value here like failure rate is greater then 60% make the switch open(DOWN)
failureRateThreshold: '60'
# switch will wait 50 seconds in open(DOWN) state so the down service airport-pilot-service get 50 seconds to recover
waitDurationInOpenState: '50000'
# after 50 seconds making switch as half open
automaticTransitionFromOpenToHalfOpenEnabled: 'true'
# allow 4 calls in half-open state of switch
permittedNumberOfCallsInHalfOpenState: '4'
allowHealthIndicatorToFail: 'true'
registerHealthIndicator: 'true'
Step3: Create a circuit breaker in a upstream microservice (airport-flight-service) for a downstream microservice (airport-pilot-service)
Step4: Using a created circuit breaker in a upstream microservice (airport-flight-service) for a downstream microservice (airport-pilot-service)