Polly:弹性和瞬态故障处理库

简介

Polly是一种.NET弹性和瞬态故障处理库,允许我们以非常顺畅和线程安全的方式来执诸如行重试,断路,超时,故障恢复等策略。 Polly针对对.NET 4.0,.NET 4.5和.NET Standard 1.1以及.NET Core实现

可以实现熔断与降级机制

主要参考以下两个链接

参考链接

https://www.cnblogs.com/edisonchou/p/9159644.html

https://www.cnblogs.com/CreateMyself/p/7589397.html

例子

 private async Task<T> getData<T>(string url, Jwt jwt, string xApiKey)
        {
            var headers = new List<KeyValuePair<string, string>>()
            {
                new KeyValuePair<string, string>( "authorizationtoken", "bearer " + jwt.access_token ),
                new KeyValuePair<string, string>( "x-api-key", xApiKey ),
            };

            try
            {
                var policy = Policy<T>.Handle<Exception>().RetryAsync<T>(10);

                return await policy.ExecuteAsync(async () =>
                {
                    return await _httpService.RequestObjAsync<T>(url, "GET", headers);
                });

            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR!!!!:" + ex.Message);
                throw ex;
            }


        }
发布了105 篇原创文章 · 获赞 46 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/wucong60/article/details/89403069