LindDotNetCore ~ The value of Polly components in microservice scenarios,

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.