Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

c# - 使用Polly断路器处理多种服务的正确方法(Proper way to handle multiple services with polly circuit breaker)

I have an application where we communicate with hundreds of HTTPs endpoints.

(我有一个与数百个HTTP端点进行通信的应用程序。)

The application is a proxy of sorts.

(该应用程序是各种代理。)

When testing with polly, I've noticed that if one endpoint, say "api.endpoint1.com" fails, the calls to "api.endpoint2.com" and "api.endpoint3.com" will also be in an open/blocked state.

(在使用polly进行测试时,我注意到如果一个端点(例如“ api.endpoint1.com”)失败,则对“ api.endpoint2.com”和“ api.endpoint3.com”的调用也将处于打开/阻止状态州。)

This makes sense as I've only defined one policy, but what is the recommended approach to handling this scenario so that calls to unrelated endpoints are not blocked due to another having performance issues?

(这很有意义,因为我仅定义了一个策略,但是建议使用什么方法来处理这种情况,以防止由于另一个存在性能问题而阻止对不相关端点的调用?)

Do I create a collection of Policy's, one for each endpoint or is there a way to supply a context key of sorts(ie the hostname) to scope the failures to a given host endpoint?

(我是否创建策略集合,每个端点一个,还是有办法提供某种上下文密钥(即主机名)来将故障范围限定在给定的主机端点上?)

I've reviewed Polly's docs regarding context keys and it appears these are a way to exchange data back and forth and not what I'm looking for here.

(我查看了Polly关于上下文密钥的文档 ,看来这些是来回交换数据的方法,而不是我在这里寻找的内容。)

var policy = Policy
            .Handle<TimeoutException>()
            .CircuitBreaker(1, TimeSpan.FromSeconds(1));

//dynamic, large list of endpoints. 
var m = new HttpRequestMessage(HttpMethod.Post, "https://api.endpoint1.com")
{
    Content = new StringContent("some JSON data here", Encoding.UTF8,"application/json")
};


policy.Execute(() => HTTPClientWrapper.PostAsync(message));
  ask by user3783214 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, your best bet is to create a separate policy per endpoint.

(是的,最好的选择是为每个端点创建一个单独的策略。)

This is better than doing it per host because an endpoint may be slow responding for a reason that's specific to that endpoint (eg, stored procedure is slow).

(这比对每个主机执行此操作更好,因为端点可能由于特定于该端点的原因(例如,存储过程缓慢)而响应缓慢。)

I've used a Dictionary<string, Policy> with the endpoint URL as the key.

(我使用了Dictionary<string, Policy>和端点URL作为键。)

if (!_circuitBreakerPolices.ContainsKey(url))
{
    CircuitBreakerPolicy policy = Policy.Handle<Exception>().AdvancedCircuitBreakerAsync(
        onBreak: ...
    );
    _circuitBreakerPolicies.Add(url, policy);
}
await _circuitBreakerPolicies[url].ExecuteAsync(async () => ... );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...