Polly断路器无法通过.net核心HTTP客户端维护状态 [英] Polly Circuit breaker not maintaining state with .net core HTTP Client

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

问题描述

我已经实施了polly重试和断路器策略(已包装)。当呼叫失败并且上一个呼叫的电路断开时,下一个呼叫再次进入重试并再次命中断路器,而不仅仅是抛出circuitbreakexception。我认为即使正在使用类型化的客户端,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解决了这个问题。因为我正在获取请求的详细信息,所以每次调用都会插入该策略,因此状态会被更新。我从

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天全站免登陆