翻译自:https://github.com/App-vNext/Polly/wiki/PolicyRegistry
点此跳转到系列目录
策略注册
v5.2起, IReadOnlyPolicyRegistry接口 v5.3起可用, IConcurrentPolicyRegistry v7.2起可用
目标
PolicyRegistry
提供了一个注册表,用于存储已配置的策略实例,并在以后检索它们以供使用。
PolicyRegistry
降低了策略的定义和使用的耦合度,是的Polly支持这种常见的模式:例如在启动时集中定义策略,并在使用时作为注入的依赖项访问它们。 这种模式对也方便了Polly单元测试.
PolicyRegistry
对于动态读/写操作也是线程安全的。例如, 您可以使用“PolicyRegistry”来管理动态分布式系统中每个下游节点的断路器,在动态分布式系统中,可能会动态地添加/删除下游节点(以及针对它们的策略)。
语法
PolicyRegistry
具有类似 Dictionary<TKey, Policy>
的语法.
创建注册表
PolicyRegistry registry = new PolicyRegistry();
// or if using Polly with HttpClientFactory, see also https://github.com/App-vNext/Polly/wiki/Polly-and-HttpClientFactory
var registry = services.AddPolicyRegistry();
用策略填充注册表
registry.Add("StandardHttpResilience", myStandardHttpResiliencePolicy);
// Or:
registry["StandardHttpResilience"] = myStandardHttpResiliencePolicy;
// Or (from Polly v7) with collection initialisation syntax:
PolicyRegistry registry = new PolicyRegistry()
{
{ "StandardHttpResilience", myStandardHttpResiliencePolicy }
};
向调用处提供注册表
// Pass the registry instance to usage sites by DI, perhaps
public class MyServiceGateway
{
public void MyServiceGateway(..., IReadOnlyPolicyRegistry<string> registry, ...)
{
...
}
}
:要强制注册表填充(通常在启动时)与注册表使用分离,可以向调用处传递IReadOnlyPolicyRegistry<TKey>
接口。
从注册表中取出策略使用
var policy = registry.Get<IAsyncPolicy<HttpResponseMessage>>("StandardHttpResilience");
HttpResponseMessage response = await policy.ExecuteAsync<HttpResponseMessage>(...);
// Or:
HttpResponseMessage response = await
registry.Get<IAsyncPolicy<HttpResponseMessage>>("StandardHttpResilience")
.ExecuteAsync<HttpResponseMessage>(...);
// Or:
HttpResponseMessage response = await
((IAsyncPolicy<HttpResponseMessage>)registry["StandardHttpResilience"])
.ExecuteAsync<HttpResponseMessage>(...);
接口和进一步的语法
PolicyRegistry
暴露了类似字典的语义和行为。
Interface | Method |
---|---|
IReadOnlyPolicyRegistry<TKey> | bool ContainsKey(TKey key) |
IReadOnlyPolicyRegistry<TKey> | TPolicy Get<TPolicy>(TKey key) |
IReadOnlyPolicyRegistry<TKey> | bool TryGet<TPolicy>(TKey key, out TPolicy policy) |
IReadOnlyPolicyRegistry<TKey> | int Count |
IPolicyRegistry<TKey> | void Clear() |
IPolicyRegistry<TKey> | void Add<TPolicy>(TKey key, TPolicy policy) |
IPolicyRegistry<TKey> | bool Remove(TKey key) |
IConcurrentPolicyRegistry<TKey> | bool TryAdd<TPolicy>(TKey key, TPolicy policy) |
IConcurrentPolicyRegistry<TKey> | bool TryRemove<TPolicy>(TKey key, out TPolicy policy) |
IConcurrentPolicyRegistry<TKey> | bool TryUpdate<TPolicy>(TKey key, TPolicy newPolicy, TPolicy comparisonPolicy) |
IConcurrentPolicyRegistry<TKey> | TPolicy GetOrAdd<TPolicy>(TKey key, Func<TKey, TPolicy> policyFactory) |
IConcurrentPolicyRegistry<TKey> | TPolicy GetOrAdd<TPolicy>(TKey key, TPolicy policy) |
IConcurrentPolicyRegistry<TKey> | TPolicy AddOrUpdate<TPolicy>(TKey key, Func<TKey, TPolicy> addPolicyFactory, Func<TKey, TPolicy, TPolicy> updatePolicyFactory) |
IConcurrentPolicyRegistry<TKey> | TPolicy AddOrUpdate<TPolicy>(TKey key, TPolicy addPolicy, Func<TKey, TPolicy, TPolicy> updatePolicyFactory) |
使用
默认实现是存储在在内存中的ConcurrentDictionary
。
这个键代表什么?
默认实现的字符串key
就是设计成没有特定的含义,以最大限度地提高灵活性。用户可以为TKey键定义自己的模式。
使用IPolicyRegistry<TKey>
实现另一个注册表
:Polly还公开了一个IPolicyRegistry<TKey>
接口。您可以使用自己来实现这个接口。IPolicyRegistry<TKey>
可以在如下示例情形使用:
- 提供由替代存储支持的实现
- 以提供某种复合“TKey”的实现。
翻译自:https://github.com/App-vNext/Polly/wiki/PolicyRegistry
点此跳转到系列目录