How to Verify If Resilience4j Circuit Breaker Works

Resilience4j is a widely-used library which inspired by Hystrix, it helps with building fault tolerance distributed systems. We are using its circuit breakder module to prevent a cascade of failures when a remote service is down.

You can implement a circuit break based on the official manual, and you may want to verify if it works well or not after . Here’re some tips

Circuit Breaker Configurations

Before the testing, it’s a good idea to learn the Circuit Breaker Configurations, which are critical to a successful test. See some important configurations below:

  • failureRateThreshold: When the failure rate is equal or greater than the threshold the CircuitBreaker transitions to open and starts short-circuiting calls.
  • minimumNumberOfCalls: it means the minimum number of calls which are required (per sliding window period) before the CircuitBreaker can calculate the error rate. For example, if minimumNumberOfCalls is 10, then at least 10 calls must be recorded, before the failure rate can be calculated. If only 9 calls have been recorded the CircuitBreaker will not transition to open even if all 9 calls have failed.
  • slidingWindowSize / ringBufferSizeInClosedState (legacy): the size of the sliding window which is used to record the outcome of calls when the CircuitBreaker is closed.
  • recordExceptions: A list of exceptions that are recorded as a failure and thus increase the failure rate. If you specify a list of exceptions, all other exceptions count as a success.

Mock & Test

Mock Thresholds

The thresholds in production enviroment are usually configured as a large value and not convenient for testing. For example,

  • minimumNumberOfCalls might be 100, so you have to make 100 requests to trigger the error rate calculation.
  • slidingWindowSize / ringBufferSizeInClosedState might be 100, so you have to make another 100 requests to complete the error rate calculation.
  • failureRateThreshold might be 90, so you have to ensure 90% requests return exception so that the circuit breaker can be triggered to transit from CLOSED to OPEN.

In short, reducing those thresholds in testing environment is recommended. For example, here’s how I set them:

  • minimumNumberOfCalls = 1.
  • slidingWindowSize = 5.
  • failureRateThreshold = 10. (another option is mock a higher exception)

Mock Exceptions

Next you need to mock exception when calling the remote service so that the circuit break can recognize the call as an error, then trigger the state transition after the error rate increased and reached the threshold.

The first thing to notice is, not all exceptions will be counted in the error rate. So it is a good idea to figure out what kind of exception you need to mock first. If you’ve set recordExceptions on CircuitBreakerConfig, those exceptions are what you need to mock!

For example, below snippet means only ApiServerException will be counted in the error rate.

private static final CircuitBreakerConfig DEFAULT_CONFIG = CircuitBreakerConfig.custom()
		.slidingWindow(5, 1, SlidingWindowType.COUNT_BASED) 

So we need to mock ApiServerException during the remote call.

private Object invoke(Object adapter, Object fluentRequest) {
		if (count.incrementAndGet() % 2 == 0) {
			throw new ApiServerException("MOCK-EXCEPTION");

Note: this example mocks the exception on api client side, you can also mock on the server side.


Now you can request the target API, if the circuit breaker works well, you can find the state transit from CLOSED to OPEN once the error rate reachs the theshold. You may find a log like:

CircuitBreaker 'xx' changed state from CLOSED to OPEN

Note: assumed you’ve attached onStateTransition event.

circuitBreaker.getEventPublisher().onStateTransition(event ->
			log.warn("{}, {}", circuitBreaker.getName(), event.getStateTransition()));


In short, reducing those thresholds in testing environment is recommended, and mock exception when calling the remote service, then you can verify the state transition by monitoring the logs.

One More Thing

You may found changing the thresholds is a tedious job which need to change the code and deploy, I recommend store the thresholds in a Configuration Center, so that you can config a lower value for testing environment without touching the code while not affect production environment.

Another benefit of integrating it with Configuration Center is that you can easily adjust the thresholds to fit the real world at any time.

<div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;"> </span> <div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;"> </span> <div style="color:rgba(0,0,0,.75);"> <div style="color:rgba(0,0,0,.75);"> <span style="color:#4d4d4d;">当前课程中商城项目的实战源码是我发布在 GitHub 上的开源项目 newbee-mall (新蜂商城),目前已有 6300 多个 star,</span><span style="color:#4d4d4d;">本课程是一个 Spring Boot 技术栈的实战类课程,课程共分为 3 大部分,前面两个部分为基础环境准备和相关概念介绍,第三个部分是 Spring Boot 商城项目功能的讲解,让大家实际操作并实践上手一个大型的线上商城项目,并学习到一定的开发经验以及其中的开发技巧。<br /> 商城项目所涉及的功能结构图整理如下:<br /> </span> </div> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <p style="color:#4d4d4d;"> <img alt="modules" src=",png" /> </p> </div> <p style="color:rgba(0,0,0,.75);"> <strong><span style="color:#e53333;">课程特色</span></strong> </p> <p style="color:rgba(0,0,0,.75);">   </p> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <ul> <li> 对新手开发者十分友好,无需复杂的操作步骤,仅需 2 秒就可以启动这个完整的商城项目 </li> <li> 最终的实战项目是一个企业级别的 Spring Boot 大型项目,对于各个阶段的 Java 开发者都是极佳的选择 </li> <li> 实践项目页面美观且实用,交互效果完美 </li> <li> 教程详细开发教程详细完整、文档资源齐全 </li> <li> 代码+讲解+演示网站全方位保证,向 Hello World 教程说拜拜 </li> <li> 技术栈新颖且知识点丰富,学习后可以提升大家对于知识的理解和掌握,可以进一步提升你的市场竞争力 </li> </ul> </div> <p style="color:rgba(0,0,0,.75);">   </p> <p style="color:rgba(0,0,0,.75);"> <span style="color:#e53333;">课程预览</span> </p> <p style="color:rgba(0,0,0,.75);">   </p> <div style="color:rgba(0,0,0,.75);">   </div> <div style="color:rgba(0,0,0,.75);"> <p style="color:#4d4d4d;"> 以下为商城项目的页面和功能展示,分别为: </p> </div> <div style="color:rgba(0,0,0,.75);"> <ul> <li> 商城首页 1<br /> <img alt="" src="" /> </li> <li> 商城首页 2<br /> <img alt="" src="" /> </li> <li>   </li> <li> 购物车<br /> <img alt="cart" src=",png" /> </li> <li> 订单结算<br /> <img alt="settle" src=",png" /> </li> <li> 订单列表<br /> <img alt="orders" src=",png" /> </li> <li> 支付页面<br /> <img alt="" src="" /> </li> <li> 后台管理系统登录页<br /> <img alt="login" src=",png" /> </li> <li> 商品管理<br /> <img alt="goods" src=",png" /> </li> <li> 商品编辑<br /> <img alt="" src="" /> </li> </ul> </div> </div> </div> </div>
<p> 欢迎参加英特尔® OpenVINO™工具套件初级课程 !本课程面向零基础学员,将从AI的基本概念开始,介绍人工智能与视觉应用的相关知识,并且帮助您快速理解英特尔® OpenVINO™工具套件的基本概念以及应用场景。整个课程包含了视频的处理,深度学习的相关知识,人工智能应用的推理加速,以及英特尔® OpenVINO™工具套件的Demo演示。通过本课程的学习,将帮助您快速上手计算机视觉的基本知识和英特尔® OpenVINO™ 工具套件的相关概念。 </p> <p> 为保证您顺利收听课程参与测试获取证书,还请您于<strong>电脑端</strong>进行课程收听学习! </p> <p> 为了便于您更好的学习本次课程,推荐您免费<strong>下载英特尔® OpenVINO™工具套件</strong>,下载地址: </p> <p> 收听课程并完成章节测试,可获得本课程<strong>专属定制证书</strong>,还可参与<strong>福利抽奖</strong>,活动详情: </p> <p> 8月1日-9月30日,学习完成【初级课程】的小伙伴,可以<span style="color:#FF0000;"><strong>免费学习【中级课程】</strong></span>,中级课程免费学习优惠券将在学完初级课程后的7个工作日内发送至您的账户,您可以在:<a href=""></a>查询优惠券情况,请大家报名初级课程后尽快学习哦~ </p> <p> <span style="font-size:12px;">请注意:点击报名即表示您确认您已年满18周岁,并且同意CSDN基于商务需求收集并使用您的个人信息,用于注册OpenVINO™工具套件及其课程。CSDN和英特尔会为您定制最新的科学技术和行业信息,将通过邮件或者短信的形式推送给您,您也可以随时取消订阅不再从CSDN或Intel接收此类信息。 查看更多详细信息请点击CSDN“<a href="">用户服务协议</a>”,英特尔“<a href="">隐私声明</a>”和“<a href="">使用条款</a>”。</span> </p> <p> <br /> </p>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页