Polly 断路器不使用 .net 核心 HTTP 客户端维护状态 [英] Polly Circuit breaker not maintaining state with .net core HTTP Client

查看:32
本文介绍了Polly 断路器不使用 .net 核心 HTTP 客户端维护状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实施了波利重试和断路器策略(已封装).当调用失败并且电路为前一个调用打开时,下一个调用再次进入重试并再次击中断路器,而不是仅仅抛出断路器异常.我认为即使使用类型化的客户端,HTTP 客户端也会以某种方式重新创建.我无法弄清楚这个问题.这是代码

I have implemented the polly retry and Circuit breaker policy (wrapped). when the call fails and the circuit is open for the previous call the next call again goes to the retry and hit the circuit breaker again instead of just throwing the circuitbreakexception. I think somehow the HTTP client is getting recreated again even though am using the typed client. I am not able to figure the issue. Here is the code

启动

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddHttpClient<IIntCall, IntCall>().WrapResilientPolicies();
    }

界面

public interface IIntCall
    {
        Task<bool> DoSomething();
    }

实施:

public class IntCall : IIntCall
    {
    private readonly HttpClient client;

    public IntCall(HttpClient httpClient)
    {
        this.client = httpClient;
    }

    public async Task<bool> DoSomething()
    {
        var response = await client.GetAsync("http://www.onegoogle.com");
        var content = await response.Content.ReadAsStringAsync();
        return false;
    }
}

Polly 实现

public static class CBExtensions
    {
        public static void WrapResilientPolicies(this IHttpClientBuilder builder)
        {
            builder.AddPolicyHandler((service, request) =>
            GetRetryPolicy().WrapAsync(GetCircuitBreakerPolicy()));
        }

    private static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
    {
        return HttpPolicyExtensions.HandleTransientHttpError()
            .CircuitBreakerAsync(3, TimeSpan.FromSeconds(30), (result, retryAttempt) =>
            {
                Debug.WriteLine("circuit broken");
            },
            () =>
            {
                Debug.WriteLine("circuit closed");
            });
    }

    private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
    {
        return HttpPolicyExtensions.HandleTransientHttpError()
            .Or<Exception>(e => !(e is BrokenCircuitException))
          .WaitAndRetryAsync(3,
              retryAttempt => TimeSpan.FromMilliseconds(500),
              onRetry: (context, attempt) =>
              {
                  Debug.WriteLine("error");
              }
          );
    }
}

推荐答案

我想到了这个问题.因为我正在获取请求详细信息,所以每次调用都会注入策略,因此状态会更新.我从

I figured the issue. because I am fetching the request details the policy is injected every call and hence the state is renewed. I moved my code from

public static void WrapResilientPolicies(this IHttpClientBuilder builder)
        {
            builder.AddPolicyHandler((service, request) =>
            GetRetryPolicy().WrapAsync(GetCircuitBreakerPolicy()));
        }

到这里

public static void WrapResilientPolicies(this IHttpClientBuilder builder)
        {
            builder.AddPolicyHandler(
                GetRetryPolicy().WrapAsync(GetCircuitBreakerPolicy()));
        }

这篇关于Polly 断路器不使用 .net 核心 HTTP 客户端维护状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆