The value of the linddotnetcore~polly component to the micro-service scenario

Source: Internet
Author: User

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?
    1. Provides a retry mechanism for HTTP requests
    2. Special handling of specified exceptions
    3. 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

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.