/// <summary>
        /// Httpclient扩展方法
        /// </summary>
        /// <param name="services">ioc容器</param>
        /// <param name="name">HttpClient 名称(针对不同的服务进行熔断,降级)</param>
        /// <param name="action">熔断降级配置</param>
        /// <param name="TResult">降级处理错误的结果</param>
        /// <returns></returns>
        public static IServiceCollection AddPollyHttpClient(this IServiceCollection services, string name, Action <PollyHttpClientOptions> action)
        {
            // 1、创建选项配置类
            PollyHttpClientOptions options = new PollyHttpClientOptions();

            action(options);

            // 2、封装降级消息
            var fallbackResponse = new HttpResponseMessage
            {
                Content    = new StringContent(options.ResponseMessage), // 降级消息
                StatusCode = HttpStatusCode.GatewayTimeout               // 降级状态码
            };

            // 2、配置httpClient,熔断降级策略
            services.AddHttpClient(name)

            //1.1 降级策略
            .AddPolicyHandler(Policy <HttpResponseMessage> .HandleInner <Exception>().FallbackAsync(fallbackResponse, async b =>
            {
                // 1、降级打印异常
                Console.WriteLine($"服务{name}开始降级,异常消息:{b.Exception.Message}");
                // 2、降级后的数据
                Console.WriteLine($"服务{name}降级内容响应:{fallbackResponse.RequestMessage}");
                await Task.CompletedTask;
            }))
            // 1.2 断路器策略
            .AddPolicyHandler(Policy <HttpResponseMessage> .Handle <Exception>().CircuitBreakerAsync(options.CircuitBreakerOpenFallCount, TimeSpan.FromSeconds(options.CircuitBreakerDownTime), (ex, ts) =>
            {
                Console.WriteLine($"服务{name}断路器开启,异常消息:{ex.Exception.Message}");
                Console.WriteLine($"服务{name}断路器开启时间:{ts.TotalSeconds}s");
            }, () =>
            {
                Console.WriteLine($"服务{name}断路器关闭");
            }, () =>
            {
                Console.WriteLine($"服务{name}断路器半开启(时间控制,自动开关)");
            }))
            // 1.3 重试策略
            .AddPolicyHandler(Policy <HttpResponseMessage>
                              .Handle <Exception>()
                              .RetryAsync(options.RetryCount)
                              )
            // 1.4 超时策略
            .AddPolicyHandler(Policy.TimeoutAsync <HttpResponseMessage>(TimeSpan.FromSeconds(options.TimeoutTime)));
            return(services);
        }
        /// <summary>
        /// 注册 Polly HttpClient服务注入IOC容器扩展
        /// </summary>
        /// <param name="services">ioc容器</param>
        /// <param name="httpClientName">HttpClient 名称(针对不同的服务进行熔断,降级)</param>
        /// <param name="action">熔断降级配置</param>
        /// <param name="TResult">降级处理错误的结果</param>
        public static IServiceCollection AddPollyHttpClient(this IServiceCollection services, string httpClientName, Action <PollyHttpClientOptions> action)
        {
            // 1. 创建选项配置类
            var options = new PollyHttpClientOptions();

            action(options);

            // 2. 配置 HttpClient 各种策略
            services.AddHttpClient(httpClientName)

            // 1)降级策略
            .AddPolicyHandler(Policy <HttpResponseMessage> .HandleInner <Exception>().FallbackAsync(options.httpResponseMessage, async b =>
            {
                // 1、降级打印异常
                Console.WriteLine($"服务{httpClientName}开始降级,异常消息:{b.Exception.Message}");
                // 2、降级后的数据
                Console.WriteLine($"服务{httpClientName}降级内容响应:{await options.httpResponseMessage.Content.ReadAsStringAsync()}");
                await Task.CompletedTask;
            }))

            // 2) 熔断策略
            .AddPolicyHandler(Policy <HttpResponseMessage> .Handle <Exception>().CircuitBreakerAsync(options.CircuitBreakerOpenFallCount, TimeSpan.FromSeconds(options.CircuitBreakerDownTime), (ex, ts) =>
            {
                Console.WriteLine($"服务{httpClientName}断路器开启,异常消息:{ex.Exception.Message}");
                Console.WriteLine($"服务{httpClientName}断路器开启时间:{ts.TotalSeconds}s");
            }, () =>
            {
                Console.WriteLine($"服务{httpClientName}断路器关闭");
            }, () =>
            {
                Console.WriteLine($"服务{httpClientName}断路器半开启(时间控制,自动开关)");
            }))

            // 2) 重试策略
            .AddPolicyHandler(Policy <HttpResponseMessage>
                              .Handle <Exception>()
                              .RetryAsync(options.RetryCount)
                              )

            // 1.4 超时策略
            .AddPolicyHandler(Policy.TimeoutAsync <HttpResponseMessage>(TimeSpan.FromSeconds(options.TimeoutTime)));



            return(services);
        }
Exemplo n.º 3
0
        public static IServiceCollection AddPollyClient(this IServiceCollection services, string name, Action <PollyHttpClientOptions> action)
        {
            //创建 选项配置表
            PollyHttpClientOptions options = new PollyHttpClientOptions();

            action(options);
            var fallbackResponse = new HttpResponseMessage
            {
                //降级消息
                Content = new StringContent(options.ResponseMessage),
                //降级状态码
                StatusCode = HttpStatusCode.GatewayTimeout
            };
            //2.配置 httpclient熔断降级策略
            object addPolicyHandler = services.AddHttpClient(name)
                                      //1.降级策略
                                      .AddPolicyHandler(Policy <HttpResponseMessage> .HandleInner <Exception>().FallbackAsync(fallbackResponse, async b =>
            {
                //1.降级打印
                Console.WriteLine($"服务{name}开始降级,异常消息:{b.Exception.Message}");
                //2.降级后的数据
                Console.WriteLine($"服务{name}降级内容响应:{fallbackResponse.RequestMessage}");
                await Task.CompletedTask;
            }))
                                      //2.熔断策略
                                      .AddPolicyHandler(Policy <HttpResponseMessage> .Handle <Exception>().CircuitBreakerAsync(options.CircuitBreakerOpenFallCount, TimeSpan.FromSeconds(options.CircuitBreakerDownTime), (ex, ts) =>
            {
                Console.WriteLine($"服务{name}断路器开启,异常消息:{ex.Exception.Message}");

                Console.WriteLine($"服务{name}断路器开启时间:{ts.TotalSeconds}s");
            }, () =>
            {
                Console.WriteLine($"服务{name}断路器关闭");
            }, () =>
            {
                Console.WriteLine($"服务{name}断路器半开启(时间控制,自动开启)");
            }))
                                      //3.重试策略
                                      .AddPolicyHandler(Policy <HttpResponseMessage> .Handle <Exception>().RetryAsync(options.RetryCount))
                                      //4.超时策略
                                      .AddPolicyHandler(Policy.TimeoutAsync <HttpResponseMessage>(TimeSpan.FromSeconds(options.TimeOut)));

            return(services);
        }
        /// <summary>
        /// HttpClient扩展方法
        /// </summary>
        /// <param name="services">ioc容器</param>
        /// <param name="name">HttpClient名称(针对不同的服务进行熔断、降级)</param>
        /// <param name="action">熔断降级配置</param>
        /// <returns></returns>
        public static IServiceCollection AddPollyHttpClientService(this IServiceCollection services, string name, Action <PollyHttpClientOptions> action)
        {
            //创建选项配置类
            PollyHttpClientOptions options = new PollyHttpClientOptions();

            action(options);

            //配置httpClient,熔断降级策略
            //超时->重试->熔断->降级,从下往上,下层被上层捕获
            services.AddHttpClient(name)
            //降级策略(捕获异常,进行自定义处理)
            .AddPolicyHandler(Policy <HttpResponseMessage> .HandleInner <Exception>().FallbackAsync(options.HttpResponseMsg
                                                                                                    , async ex =>
            {
                //降级打印异常
                Console.WriteLine($"服务{name}开始降级,异常消息:{ex.Exception.Message}");
                //降级后的数据
                Console.WriteLine($"服务{name}降级内容响应:{options.HttpResponseMsg.Content.ToString()}");
                await Task.CompletedTask;
            }))
            //断路器策略,熔断机制,设置3次异常开启熔断,10s后下一个请求重新请求微服务,否则不请求微服务直接返回熔断器自定义异常信息
            .AddPolicyHandler(Policy <HttpResponseMessage> .Handle <Exception>().CircuitBreakerAsync(options.CircuitBreakerOpenFallCount, TimeSpan.FromSeconds(options.CircuitBreakerDownTime)
                                                                                                     , (ex, ts) =>
            {
                Console.WriteLine($"服务{name}断路器开启,异常消息:{ex.Exception.Message}");
                Console.WriteLine($"服务{name}断路器开启时间:{ts.TotalSeconds}s");
            }
                                                                                                     , () =>
            {
                Console.WriteLine($"服务{name}断路器关闭");
            }
                                                                                                     , () =>
            {
                //每过10s关闭熔断,下个请求微服务,之后重新打开
                Console.WriteLine($"服务{name}断路器半开启(时间控制,自动开关)");
            }))
            //重试策略,失败重试
            .AddPolicyHandler(Policy <HttpResponseMessage> .Handle <Exception>().RetryAsync(options.RetryCount))
            //超时策略
            .AddPolicyHandler(Policy.TimeoutAsync <HttpResponseMessage>(TimeSpan.FromSeconds(options.TimeoutTime)));

            return(services);
        }