11 Star 26 Fork 9

無色眼镜 / dawdler-series

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 2.41 KB
一键复制 编辑 原始数据 按行查看 历史
jackson.song 提交于 2022-08-19 11:12 . update doc & add schedule

dawdler-circuit-breaker

模块介绍

基于时间滑动窗口方式实现的熔断器,支持熔断配置,降级.

1. web端的pom中引入依赖

 <groupId>dawdler</groupId>
 <artifactId>dawdler-circuit-breaker</artifactId>

2. 在service接口层的方法上定义熔断器

使用@CircuitBreaker标注的方法会被开启熔断器

例:

@RemoteService(group="user-service")
public interface UserService {
 
 @CircuitBreaker
 Map<String, Object> selectUserList(int pageOn)throws Exception;
 
}

3. 服务降级

@CircuitBreaker 中有fallbackMethod的属性,fallbackMethod是接口中方法的实现.

例:

@CircuitBreaker(fallbackMethod = "selectUserListFallback")
Map<String, Object> selectUserList(int pageOn)throws Exception;
 
default Map<String, Object> selectUserListFallback(int pageOn)throws Exception{
  Map<String, Object> result = new HashMap<>();
  return result;
}

4. CircuitBreaker属性说明

public @interface CircuitBreaker {

 /**
  * @return String
  * @Description 标识key,默认为"" 则为servicePath+serviceName+serviceMethod组合
  * @date 2018年3月10日
  */
 String breakerKey() default "";

 /**
  * @return int
  * @Description 统计时长 intervalInMs/windowsCount 建议为整数,默认3000,单位为毫秒.
  * @date 2018年3月10日
  */
 int intervalInMs() default 3000;

 /**
  * @return int
  * @Description 窗口大小
  * @date 2018年3月10日
  */
 int windowsCount() default 2;

 /**
  * @return int
  * @Title sleepWindowInMilliseconds
  * @Description 熔断器打开后,所有的请求都会直接失败,熔断器打开时会在经过一段时间后就放行一条请求成功则关闭熔断器,此配置就为指定的这段时间,默认值是
  *              5000,单位为毫秒.
  * @date 2018年3月10日
  */
 int sleepWindowInMilliseconds() default 5000;

 /**
  * @return int
  * @Description 启用熔断器功能窗口时间内的最小请求数,默认为5.
  * @date 2018年3月10日
  */

 int requestVolumeThreshold() default 5;

 /**
  * @return double
  * @Description 错误百分比,默认为40% 达到40%的错误率会触发熔断(大于requestVolumeThreshold)
  * @date 2018年3月10日
  */
 double errorThresholdPercentage() default 0.4;

 /**
  * @return String
  * @Description 熔断后执行的方法 参数与返回值与执行的方法相同
  * @date 2018年3月10日
  */
 String fallbackMethod() default "";

}
Java
1
https://gitee.com/srchen1987/dawdler-series.git
git@gitee.com:srchen1987/dawdler-series.git
srchen1987
dawdler-series
dawdler-series
master

搜索帮助