背景

Polly是一个.NET弹性和瞬态故障处理库,允许开发人员以流畅和线程安全的方式表达诸如重试,断路器,超时,隔离头和回退之类的策略。

Polly面向.NET Standard 1.1(覆盖范围:.NET Core 1.0,Mono,Xamarin,UWP,WP8.1 +)和.NET Standard 2.0+(覆盖范围:.NET Core 2.0 + 、. NET Core 3.0和更高版本的Mono,Xamarin和UWP目标)。nuget软件包还包括.NET Framework 4.6.1和4.7.2的直接目标。

使用方式

通过NuGet安装

Install-Package Polly

代码实现

故障处理,响应策略

故障处理策略处理您通过策略执行的委托引发的特定异常或          返回的结果。

步骤1:指定您希望策略处理的异常/错误

// Single exception type
Policy.Handle<HttpRequestException>()// Single exception type with condition
Policy.Handle<SqlException>(ex => ex.Number == 1205)// Multiple exception types
Policy.Handle<HttpRequestException>().Or<OperationCanceledException>()// Multiple exception types with condition
Policy.Handle<SqlException>(ex => ex.Number == 1205).Or<ArgumentException>(ex => ex.ParamName == "example")// Inner exceptions of ordinary exceptions or AggregateException, with or without conditions
// (HandleInner matches exceptions at both the top-level and inner exceptions)
Policy.HandleInner<HttpRequestException>().OrInner<OperationCanceledException>(ex => ex.CancellationToken != myToken)

步骤2:指定策略应如何处理这些故障

2.1、重试

// Retry once
Policy.Handle<SomeExceptionType>().Retry()// Retry multiple times
Policy.Handle<SomeExceptionType>().Retry(3)// Retry multiple times, calling an action on each retry
// with the current exception and retry count
Policy.Handle<SomeExceptionType>().Retry(3, onRetry: (exception, retryCount) =>{// Add logic to be executed before each retry, such as logging});// Retry multiple times, calling an action on each retry
// with the current exception, retry count and context
// provided to Execute()
Policy.Handle<SomeExceptionType>().Retry(3, onRetry: (exception, retryCount, context) =>{// Add logic to be executed before each retry, such as logging });

2.2、永远重试(直到成功)

// Retry forever
Policy.Handle<SomeExceptionType>().RetryForever()// Retry forever, calling an action on each retry with the
// current exception
Policy.Handle<SomeExceptionType>().RetryForever(onRetry: exception =>{// Add logic to be executed before each retry, such as logging       });// Retry forever, calling an action on each retry with the
// current exception and context provided to Execute()
Policy.Handle<SomeExceptionType>().RetryForever(onRetry: (exception, context) =>{// Add logic to be executed before each retry, such as logging       });

2.3、等待并重试

// Retry, waiting a specified duration between each retry.
// (The wait is imposed on catching the failure, before making the next try.)
Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)});// Retry, waiting a specified duration between each retry,
// calling an action on each retry with the current exception
// and duration
Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan) => {// Add logic to be executed before each retry, such as logging    }); // Retry, waiting a specified duration between each retry,
// calling an action on each retry with the current exception,
// duration and context provided to Execute()
Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan, context) => {// Add logic to be executed before each retry, such as logging    });// Retry, waiting a specified duration between each retry,
// calling an action on each retry with the current exception,
// duration, retry count, and context provided to Execute()
Policy.Handle<SomeExceptionType>().WaitAndRetry(new[]{TimeSpan.FromSeconds(1),TimeSpan.FromSeconds(2),TimeSpan.FromSeconds(3)}, (exception, timeSpan, retryCount, context) => {// Add logic to be executed before each retry, such as logging    });// Retry a specified number of times, using a function to
// calculate the duration to wait between retries based on
// the current retry attempt (allows for exponential backoff)
// In this case will wait for
//  2 ^ 1 = 2 seconds then
//  2 ^ 2 = 4 seconds then
//  2 ^ 3 = 8 seconds then
//  2 ^ 4 = 16 seconds then
//  2 ^ 5 = 32 seconds
Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) );// Retry a specified number of times, using a function to
// calculate the duration to wait between retries based on
// the current retry attempt, calling an action on each retry
// with the current exception, duration and context provided
// to Execute()
Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, context) => {// Add logic to be executed before each retry, such as logging});// Retry a specified number of times, using a function to
// calculate the duration to wait between retries based on
// the current retry attempt, calling an action on each retry
// with the current exception, duration, retry count, and context
// provided to Execute()
Policy.Handle<SomeExceptionType>().WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)), (exception, timeSpan, retryCount, context) => {// Add logic to be executed before each retry, such as logging});

开源地址

https://github.com/App-vNext/Polly

ASP VNext 开源服务容错处理库Polly相关推荐

  1. 开源服务容错处理库Polly使用文档

    在进入SOA之后,我们的代码从本地方法调用变成了跨机器的通信.任何一个新技术的引入都会为我们解决特定的问题,都会带来一些新的问题.比如网络故障.依赖服务崩溃.超时.服务器内存与CPU等其它问题.正是因 ...

  2. 在 ASP.NET Core Web API中使用 Polly 构建弹性容错的微服务

    在 ASP.NET Core Web API中使用 Polly 构建弹性容错的微服务 https://procodeguide.com/programming/polly-in-aspnet-core ...

  3. 最全的ASP.NET开源CMS汇总

    国内: 1.SiteServer CMS SiteServer CMS 网站内容管理系统(著作权登记号2008SR15710)是定位于中高端市场的CMS内容管理系统,能 最近汇总了一些asp.net开 ...

  4. 白话SpringCloud | 第五章:服务容错保护(Hystrix)

    前言 前一章节,我们知道了如何利用RestTemplate+Ribbon和Feign的方式进行服务的调用.在微服务架构中,一个服务可能会调用很多的其他微服务应用,虽然做了多集群部署,但可能还会存在诸如 ...

  5. Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)【Dalston版】

    前言 在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服 ...

  6. Spring Cloud构建微服务架构:服务容错保护(Hystrix服务降级)【Dalston版】 1

    前言 在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服 ...

  7. Spring Cloud Hystrix 服务容错保护

    在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身间 ...

  8. 断路器Hystrix实现服务容错

    一.服务容错 1.服务存在的问题 微服务架构中,生产过程中我们或多或少的会遇到一些问题.如果服务提供者对服务消费者的响应非常的缓慢,那么服务消费对服务提供者的请求就会被迫等待,直到提供者响应或者超时. ...

  9. C# asp.net 开源资源大汇总

    一.AOP框架       Encase 是C#编写开发的为.NET平台提供的AOP框架.Encase 独特的提供了把方面(aspects)部署到运行时代码,而其它AOP框架依赖配置文件的方式.这种部 ...

最新文章

  1. 【独家】深入浅出话AI:定义和主要研究方法
  2. 深入javascript——构造函数和原型对象
  3. 反函数的导数:理论与应用
  4. 谁偷走了程序员的时间??
  5. C#设计模式之十二代理模式(Proxy Pattern)【结构型】
  6. CozyRSS开发记录8-解析一份RSS
  7. java http超时重连_httpclient 重连机制
  8. BP算法和RNN_模型CNN-RNN-LSTM和GRU简介
  9. 来及Java空间的传送门2
  10. 【Mockplus教程】界面闪烁花屏怎么办?
  11. IE被哪个T吗D修改了,卧槽!
  12. js new Date()
  13. shiro框架的使用
  14. linux系统下 java 环境的安装
  15. 图片版坦克大战其他相关的 类(三)
  16. c语言规定棋盘大小的,求数据结构C语言大神们解释下马踏棋盘程序
  17. php 遍历文件夹并压成zip_将文件夹压缩成zip文件的php代码
  18. 三国志2017服务器维护时间,《三国志2017》版本更新公告
  19. windows7微软官方_Microsoft Windows 7 | 第1部分
  20. Linux中使用shell脚本生成随机数

热门文章

  1. 写一个简单的 django_post demo
  2. Map 遍历取值及jstl的取值
  3. CF374 Maxim and Array
  4. Echart..js插件渲染报错 data.length1?
  5. 媒体层图形技术之AssetsLibrary 学习笔记
  6. objective-c中的static
  7. HDOJ 1875 HDU 1875 畅通工程再续 ACM 1875 IN HDU
  8. linux 下eclipse调试程序,文章2 Linux安装Eclipse阅读及调试程序
  9. Android FrameWork学习(一)Android 7 0系统源码下载 编译
  10. 梦回编程- 由LD_LIBRARY_PATH引发JNI的理解