Back to Catalog
Polly is an open-source framework that can be found on GitHub, and is also a good friend of the Big Brother. A member of APP Vnext!
APP Vnext:https://github.com/app-vnext
Github:https://github.com/app-vnext/polly
Nanofabric is an open source micro-service architecture, also recommended by good friend Brother: Https://github.com/geffzhang/NanoFabric
For Nanofabric, it integrates a number of. NET core open source projects, including Consul +. NET core + Polly + ocelot + exceptionless + identityserver, do you smell a certain flavor Way!
What did Polly bring to us?
- Provides a retry mechanism for HTTP requests
- Special handling of specified exceptions
- offers a variety of strategies
Use in the program
Encapsulate a method, provide a delegate's parameters externally, the need to Polly code snippet input, for HTTP, database, network communication, etc. is very necessary, because these scenarios may have unstable factors!polly just can help us very
A friendly solution to it, the following code mainly implements the tracking of all exceptions, and then executes every 1 seconds, can be retried 5 times!
/// <summary> ///Polly Retry Mechanism/// </summary> Private StaticHttpresponsemessage Retrytwotimespolicy (funcaction) { varPolicy =Policy. Handle<Exception>() . Waitandretry (5, Retryattempt= Timespan.fromseconds (Math.pow (1, Retryattempt)), (ex, timer, C, i)={logger. Logger_info ($"execution failed! Retry count {c}"); Logger. Logger_info ($"exception from {ex. GetType (). Name}"); }); returnpolicy. Execute (action); }
Our previous Httphelper Request object, can also introduce the Polly mechanism, the global control!
///<summary> ///GET Request/// </summary> /// <param name= "RequestUri" >Service Address</param> /// <param Name= "NV" >parameter Key value</param> /// <returns></returns> Public Statichttpresponsemessage Get (stringRequestUri, NameValueCollection nv) { Try { returnRetrytwotimespolicy (() = { varresult =Httpclient.getasync (Generatoruri (RequestUri, NV)). Result; Ungzip (result); returnresult; }); } Catch(Exception ex) {Throwex; } }
Self-open two API process, one is to provide services, not one as the primary server, by other access, when it hangs, in fact, can be retried through Polly!
Thank you for reading!
Micro-services come, but we need to pay more attention to the point!
Fight It!
Back to Catalog
The value of the linddotnetcore~polly component to the micro-service scenario