Resilience4j usage
1. pom
1) normal
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-circuitbreaker</artifactId>
<version>0.13.2</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-retry</artifactId>
<version>0.13.2</version>
</dependency>
2) spring boot
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot</artifactId>
<version>0.13.2</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-retry</artifactId>
<version>0.13.2</version>
</dependency>
3) spring boot2
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>0.13.2</version>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-retry</artifactId>
<version>0.13.2</version>
</dependency>
2. create CB
1) CircuitBreakerRegistry
(1)default
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.ofDefaults(); // Get a CircuitBreaker from the CircuitBreakerRegistry with the global default configuration
CircuitBreaker circuitBreaker2 = circuitBreakerRegistry.circuitBreaker("myCB2");
(2)custom
// Create a custom configuration for a CircuitBreaker
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.waitDurationInOpenState(Duration.ofMillis(1000))
.ringBufferSizeInHalfOpenState(2)
.ringBufferSizeInClosedState(2)
.build(); // Create a CircuitBreakerRegistry with a custom global configuration
CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig); // Get a CircuitBreaker from the CircuitBreakerRegistry with a custom configuration
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker("myCB1", circuitBreakerConfig);
2) directly
(1) default
CircuitBreaker defaultCircuitBreaker = CircuitBreaker.ofDefaults("myCB2"); // Create a Retry with at most 3 retries and a fixed time interval between retries of 500ms
Retry retry = Retry.ofDefaults("myCB2");
(2) custom
// Create a custom configuration for a CircuitBreaker
CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
.failureRateThreshold(50)
.waitDurationInOpenState(Duration.ofMillis(1000))
.ringBufferSizeInHalfOpenState(2)
.ringBufferSizeInClosedState(2)
.build(); CircuitBreaker customCircuitBreaker = CircuitBreaker.of("myCB1", circuitBreakerConfig); // Create a Retry with at most 3 retries and a fixed time interval between retries of 500ms
Retry retry = Retry.ofDefaults("myCB1");
3. decorate
1) backend
// Simulates a Backend Service
public interface BackendService {
String doSomething();
}
2) Attached
// Decorate your call to BackendService.doSomething() with a CircuitBreaker
Supplier<String> decoratedSupplier = CircuitBreaker
.decorateSupplier(circuitBreaker, backendService::doSomething); // Decorate your call with automatic retry
decoratedSupplier = Retry
.decorateSupplier(retry, decoratedSupplier);
4. Exec(可选)
注意:这部分主要用来测试的!实际代码中可以忽略
1) vavr
// Execute the decorated supplier and recover from any exception
String result = Try.ofSupplier(decoratedSupplier)
.recover(throwable -> "Hello from Recovery").get(); System.out.println(result);
2) lambda expression
// When you don't want to decorate your lambda expression,
// but just execute it and protect the call by a CircuitBreaker.
String result = circuitBreaker.executeSupplier(backendService::doSomething); System.out.println(result);
Reference:
1. Achieving Fault Tolerance With Resilience4j
2. resilience4j - circuit breaker
Resilience4j usage的更多相关文章
- intellij IDEA 出现“Usage of API documented as @since 1.6+”的解决办法
问题 在导入java.io.console的时候出现"Usage of API documented as @since 1.6+"
- Disk Space Usage 术语理解:unallocated, unused and reserved
通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...
- OpenCascade MeshVS Usage
OpenCascade MeshVS Usage eryar@163.com Abstract. MeshVS means Mesh Visualization Service. It can be ...
- Usage: AddDimensionedImage imageFile outputFile eclipse 运行程序出错
关于这个在eclipse中运行java程序的错,首先确认你的jdk,jre是否完整,并且与你的eclipse的位数相同,当然我相信这个错误大家应该都会去检查到. 第二个关于addDimensioned ...
- Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)
本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...
- [转]Dynamic SQL & Stored Procedure Usage in T-SQL
转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...
- 处理Linux下subversion尝试连接自建的VisualSVN server报“Key usage violation in certificate has been detected”错误的问题
在Linux下使用subversion尝试链接VisualSVN server搭建的svn库,可能会报下面错误, svn: OPTIONS of 'https://server.domain.loca ...
- 应用alter index ××× monitoring usage;语句监控索引使用与否
随着时间的累积,在没有很好的规划的情况下,数据库中也许会存在大量长期不被使用的索引,如果快速的定位这些索引以便清理便摆在案头.我们可以使用"alter index ××× monitorin ...
- hadoop 2.5 hdfs namenode –format 出错Usage: java NameNode [-backup] |
在 cd /home/hadoop/hadoop-2.5.2/bin 下 执行的./hdfs namenode -format 报错[hadoop@node1 bin]$ ./hdfs nameno ...
随机推荐
- 诡异的bug!!
在我些项目的时候,命名从来没有用过 font 元素,但是打开 Chrome 浏览器查看元素时,总是 font 元素???而且写的 <span class= "xxx"> ...
- 【转】RAID 技术发展综述
原文地址:https://blog.csdn.net/liuaigui/article/details/4581970 摘要 :现代企业信息化水平不断提高,数据已经取代计算成为了信息计算的中心.这 ...
- overload(重载)和override(覆盖)的注意点
使用overload(重载)的几个注意点: 在使用重载时只能通过不同的参数形式.例如:不同的参数类型,不同的参数个数,不同的参数顺序,当然,同一个方法内的几个参数类型必须不一样.例如可以是fun(in ...
- match和search的区别
正则表达式帮助你方便的检查一个字符串是否与某种模式匹配. re模块使Python语言拥有全部的正则表达式功能. re.match尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,mat ...
- cocos2d-x3.17 整体概述
首先,cocos引擎有三个版本:C++,Lua,Js.其底层代码是由C++编写,通过脚本文件绑定到Lua与Js,所以我们之后解析的都是cocos2d-x.其次,cocos安装等就不概述了,百度一大堆. ...
- 使用selenium爬取网站动态数据
处理页面动态加载的爬取 selenium selenium是python的一个第三方库,可以实现让浏览器完成自动化的操作,比如说点击按钮拖动滚轮等 环境搭建: 安装:pip install selen ...
- 购物车存到cookie
为什么不存session? 首先,session存在时间限制,会定期清空的,而cookie如果不主动清或者设置定期则不会清楚: session存放在服务器端,cookie存放在客户端浏览器. 购物车存 ...
- matplotlib.pyplot展示MNIST图片
import torch import torch.utils.data as Data import torchvision import torchvision.transforms as tra ...
- [cf1038E][欧拉路]
http://codeforces.com/contest/1038/problem/E E. Maximum Matching time limit per test 2 seconds memor ...
- VS2015 使用 Visual Studio Emulator For Android 调试无法命中断点的解决办法?
源解决方案是英文版的,地址:https://dzone.com/articles/fix-for-could-not-connect-to-the-debugger-while-de 问题现象: 1. ...