polly的几种常用方法

参考资料:https://www.cnblogs.com/edisonchou/p/9159644.html

特征:可以实现一些代码的熔断和降级

代码:

 ////普通,其中 Fallback相当于降级处理
            //Policy.Handle<ArgumentException>().Fallback(()=>{
            //    MessageBox.Show("Error occured");
            //}).Execute(()=> {
            //    MessageBox.Show("Job Start");
            //    throw new ArgumentException("Hello Polly!");
            //    MessageBox.Show("Job End");
            //});

            ////重试  3次重试后结束
            //Policy.Handle<ArgumentException>().Retry(3).Execute(() => {
            //    MessageBox.Show("Job retry Start");
            //    throw new ArgumentException("Hello retry Polly!");
            //    MessageBox.Show("Job retry End");
            //});

            ////熔断   重试3次后再次失败以后十秒一次调用
            //var policy = Policy.Handle<Exception>().CircuitBreaker(3, TimeSpan.FromSeconds(10));
            //while (true)
            //{
            //    try
            //    {
            //        policy.Execute(() =>
            //        {
            //            Console.WriteLine("Job Start");
            //            throw new Exception("Special error occured");
            //            Console.WriteLine("Job End");
            //        });
            //    }
            //    catch (Exception ex)
            //    {
            //        Console.WriteLine("There's one unhandled exception : " + ex.Message);
            //    }
            //    Thread.Sleep(500);
            //}

            ////这里涉及到Polly中关于超时的两个策略:一个是悲观策略(Pessimistic),一个是乐观策略(Optimistic)。其中,悲观策略超时后会直接抛异常,而乐观策略则不会
            //// Wrap是指策略封装,可以把多个ISyncPolicy合并到一起执行。Timeout则是指超时处理,但是超时策略一般不能直接使用,而是其其他策略封装到一起使用。
            ////thread.sleep这个超时没有用走Fallback方法,但是再excute里面执行一个方法出异常的时候会走fallback方法
            //try
            //{
            //    var policyExp = Policy.Handle<Exception>().Fallback(() =>
            //    {
            //        Console.WriteLine("Fallback");
            //    });

            //    var policyTimtout = Policy.Timeout(3, TimeoutStrategy.Pessimistic);

            //    var mainPolicy = Policy.Wrap(policyTimtout, policyExp);
            //    mainPolicy.Execute(() =>
            //    {
            //        Console.WriteLine("Job Start...");
            //        Thread.Sleep(5000);
            //        //throw new Exception("error");
            //        Console.WriteLine("Job End...");
            //    });
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine($"Unhandled exception : {ex.GetType()} : {ex.Message}");
            //}

            ////一些高级方法 :WaitAndRetryAsync,等待异步完成,并且重试指定次数  ExecuteAndCaptureAsync,执行并且返回一个自己方法需要返回的类型,可以在这里查看更多 https://github.com/App-vNext/Polly

            var policy = Policy<byte[]>.Handle<Exception>()
            .FallbackAsync(async c =>
            {
                //熔断后再来个通知
                Console.WriteLine("熔断完成,通知一下");
                return new byte[0];
            }, async r =>
            {
                try
                {
                    //这里如果抛出错误不用catch捕捉的话,就不会走到通知的方法。
                    throw new Exception("error");
                    //先来个降级熔断。
                    Console.WriteLine("我降级熔断了");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("熔断异常");
                }
            });

            try
            {
                //设置一个超时时间,里面加个回调函数给个提示
                var pTimeout = Policy.TimeoutAsync(20, TimeoutStrategy.Pessimistic,
                  async (context, timespan, task) =>
                  {
                      Console.WriteLine("Timeout!");
                  });
                var excPolicy = policy.WrapAsync(pTimeout);
                var bytes = await excPolicy.ExecuteAsync(async () =>
                {
                    Console.WriteLine("start Job");
                    //throw new Exception("error");
                    HttpClient httpClient = new HttpClient();
                    var result = await httpClient.GetByteArrayAsync("https://images2018.cnblogs.com/blog/381412/201806/381412-20180606230929894-145212290.png");
                    Console.WriteLine("Finish Job");
                    return result;
                });
                Console.WriteLine($"Length of bytes : {bytes.Length}");
            }
            catch (Exception ex)
            {
                //这里如果熔断的方法抛出异常的时候,可以从这里捕捉主方法的执行异常,如果熔断方法没有异常的情况下,主方法报错也不会走到这个地方的
                Console.WriteLine($"Unhandled exception : {ex.GetType()} : {ex.Message}");
            }
原文地址:https://www.cnblogs.com/llcdbk/p/11841743.html