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;
            }


        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章