弹性和瞬态故障处理库Polly之重试策略
来源:互联网 发布:中国电信云计算基地 编辑:程序博客网 时间:2024/05/22 00:50
弹性和瞬态故障处理库Polly
源码及参考见文章底部
Nuget中Polly库的引用:
Install-Package Polly
以下策略调用的方法:
详细代码见
public static int random = 10;public static int DivideByZero(int random){ //int a = 0; if (random != 25) random = 0; return 100 / random;}
Retry策略
public static void Retry(){ var retry = Policy .Handle<DivideByZeroException>() .Retry(3, (exception, retryCount) => { Console.WriteLine($"执行异常,重试次数:{retryCount},【异常来自:{exception.GetType().Name}】."); }); retry.Execute(() => { DivideByZero(random); });}
结果:
WaitAndRetry策略
public static void WaitAndRetry(){ var retry = Policy .Handle<DivideByZeroException>() .WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(1, retryAttempt)), (exception, timespan, retryCount, context) => { Console.WriteLine($"重试,[当前随机数为{random}] [retrycount{retryCount}] [异常来自:{exception.GetType().Name}] [{timespan}]"); random = new Random().Next(10, 50); if (random == 25) Console.WriteLine($"成功,随机数为{random}"); }); retry.Execute(() => DivideByZero(random));}
结果:
WaitandRetryForeverUntilSuccess策略
public static void WaitandRetryForeverUntilSuccess(){ var retryForever = Policy .Handle<DivideByZeroException>() .WaitAndRetryForever(retryAttempt => TimeSpan.FromSeconds(Math.Pow(1, retryAttempt)), (exception, timespan) => { Console.WriteLine($"重试,[当前随机数为{random}][异常来自:{exception.GetType().Name}] [{timespan}]"); random = new Random().Next(10, 50); if (random == 25) Console.WriteLine($"成功,随机数为{random}"); }); retryForever.Execute(() => { DivideByZero(random); });}
结果:
参考:Github-Polly以及Jeffcky的博客
源码: Github
阅读全文
1 0
- 弹性和瞬态故障处理库Polly之重试策略
- Polly一种.NET弹性和瞬态故障处理库(重试策略、断路器、超时、隔板隔离、缓存、回退、策略包装)
- 弹性和瞬态故障处理库Polly介绍
- Polly 弹性和瞬态故障处理库 介绍
- 【小白的CFD之旅】24 稳态和瞬态
- SaberRD之瞬态分析
- Android中的volley_12_请求重试策略RetryPolicy和DefaultRetryPolicy
- node js- 路由转发和失败重试策略
- 失败重试策略
- guava-retrying重试工具库: 阻塞策略BlockStrategy
- 工作笔记之故障处理
- Volley超时重试策略_RetryPolicy
- 系统技巧和故障处理
- 系统技巧和故障处理
- API网关Ocelot 使用Polly 处理部分失败问题
- API网关Ocelot 使用Polly 处理部分失败问题
- API网关Ocelot 使用Polly 处理部分失败问题
- API网关Ocelot 使用Polly 处理部分失败问题
- 关于theano 听说theano不维护了,早知道学tensflow了,肾疼。
- <C++ Primer_5th>习题_1.10
- java异常checked和check
- python爬虫由浅入深11---selenium的基础与使用
- js中嵌入jstl标签的实战 : 第四卷
- 弹性和瞬态故障处理库Polly之重试策略
- JAVA多线程与并发学习总结
- CentOS7环境下搭建storm集群
- 搜索引擎选择: Elasticsearch与Solr
- 冒泡排序
- 最优二叉搜索树 (dp)
- Qt多线程学习:创建多线程
- sql 语句实现根据日期字段计算总营业额的查询
- Android8-Oreo奥利奥 SDK下载与各文件解读-1、android_m2repository_r47.zip