LindDotNetCore ~ The value of Polly components in microservice scenarios,
Back to directory
Polly is an open-source framework that can be found on github. It has been indexed by hoyou and is also a member of. App vNext!
App vNext: https://github.com/App-vNext
GitHub: https://github.com/App-vNext/Polly
NanoFabric is an open source microservice architecture, is also good friend recommended: https://github.com/geffzhang/NanoFabric
For NanoFabric, it integrates many. net core open-source projects, including Consul +. NET Core + Polly + Ocelot + Exceptionless + IdentityServer. Do you smell something!
What programs Does Polly bring to us?
Encapsulate a method to provide a delegate parameter to the outside, and enter the code segment that requires polly. It is necessary for http, database, and network communication, these scenarios may be unstable! Polly can help us very well.
The following code is used to track all exceptions and re-execute every 1 second. You can try again five times!
/// <Summary> /// polly Retry Mechanism /// </summary> private static HttpResponseMessage retryTwoTimesPolicy (Func <HttpResponseMessage> action) {var policy = 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} ") ;}); return policy. Execute (action );}
Our previous httpHelper request object can also introduce the polly mechanism for global control!
/// <Summary> /// GET request /// </summary> /// <param name = "requestUri"> service address </param> /// <param name = "nv"> parameter key value </param> // <returns> </returns> public static HttpResponseMessage Get (string requestUri, nameValueCollection nv) {try {return retryTwoTimesPolicy () =>{ var result = httpClient. getAsync (GeneratorUri (requestUri, nv )). result; UnGZip (result); return result;}) ;}catch (Exception ex) {throw ex ;}}
Start two api processes on your own. One is to provide external services, and the other is to be accessed by others as the master server. When it crashes, you can try again through polly!
Thank you for reading this article!
Microservices are coming, but we need more attention!
Struggle!
Back to directory