Polly micro-blown strategy of service

Polly NET Core micro-blown strategy of service

Then the one that we continue to introduce Polly this library

 

Fusing strategy (Circuit-breaker)

 

If the call is a target of service too much overtime, and abnormal appears, you can take the call within a certain period of time fusing the service requests during the fuse will not continue to invoke the target service, but directly returned, conserve resources, improve the stability of services after the end of the fusing cycle if the goal is to restore the situation improved service calls.

 

Note: For stability services, execution requires multiple Retry the retry strategy (retry strategy, little interested partners can see my last, or self-search), fusing the best combination of strategies to prevent possible the risks.

 

Blown state

 

 

  1. Open (Open)

    Fuse open, then a call to the target service directly returns an error, do not go inside network requests fuse cycle, enter the semi-open state when the fuse at the end of the cycle;

  2. Close (Closed)

    Normal network requests occur in a closed state, but the number of execution times continuously blown meet the conditions will be recorded, if the number of errors reaches a set threshold (if returned to normal before the threshold is not reached, the cumulative number of times before will be zero), into the blown state to an open state;

  3. Half-open (Half-Open)

    Quantitative allowed under the half-open state of the service request, if the calls are successful (or a certain percentage) is considered restored, turn off the fuse, otherwise considered not good, I went back to the fuse open state;

 

Fuse instructions

 

// After 3 consecutive abnormal blown, blown state and holding for 1 minute, the caller will receive the abnormality information of the circuit breaker 
the Policy 
    .handle <SomeExceptionType> () 
    .CircuitBreaker (3, TimeSpan.FromMinutes (. 1));

 

Fuse code testing

 

Copy the code
static int = 0 Times Private; 
public static void TestPolicy () 
{ 
    var = circuitBreakerPolicy the Policy 
            .handle <Exception> () 
            .CircuitBreaker ( 
                exceptionsAllowedBeforeBreaking:. 4,. 4 times // exception 
                durationOfBreak: TimeSpan.FromMinutes (1), // off 1 minute apart 
                onBreak: (exception, breakDelay) = > // open the circuit breaker 
                    Console.WriteLine ($ "blown: {breakDelay.TotalMilliseconds} ms, exception:" + Exception.Message), 
                onreset: () => // fuse off 
                    Console.WriteLine ( "fuses shut down"), 
                onHalfOpen: () => // fusing time at the end, into the half-open state from the oFF state
                    Console.WriteLine ( "fusing time to enter the half-open state") 
            ); 

    for (int I = 0; I <12 is; I ++) // simulate multiple calls, to trigger the fuse 
    { 
        the try 
        { 
            var circuitBreakerPolicy.Execute Result = (the Test ); 
            Console.WriteLine (Result); 
        } 
        the catch (Exception EX) 
        { 
            Console.WriteLine ( "the catch the try-:" + ex.Message); 
        } 
        the Thread.Sleep (500); 
    } 
} 

Private static String the Test () 
{ 
    Times ++ ; 
 
    ! IF (Times = 0. 5%) // Throws mimic certain error conditions
    { 
        the throw new new Exception ( "Exception Message"); 
    } 
    return "Success"; 
}
Copy the code

 

 

Fusing advanced configuration

 

The total number of requests in the proportion of abnormal triggering period fusing:

 

Copy the code
var advancedCircuitBreakerPolicy = Policy
    .Handle<Exception>()
    .AdvancedCircuitBreaker(
        failureThreshold: 0.5,                          // 至少50%有异常则熔断
        samplingDuration: TimeSpan.FromSeconds(10),     // 10秒内
        minimumThroughput: 8,                           // 最少共有多少次调用
        durationOfBreak: TimeSpan.FromSeconds(30),
        onBreak: (exception, breakDelay) =>             // 断路器打开时
            Console.WriteLine($"熔断: {breakDelay.TotalMilliseconds } ms, 异常: " + exception.Message),  
        onReset: () =>                                  // 熔断器关闭时
            Console.WriteLine("熔断器关闭了"),                                                            
        onHalfOpen: () => // fusing time at the end, from the half open state into the OFF state 
            Console.WriteLine ( "fusing time to enter the half-open state")                                                    
    );
Copy the code

 

 You can see it is still very convenient and simple to use, and can be combined with the project framework out different games are played, ha ha ha, interested students can self-old brother or degree of your mother and Kazakhstan. See you

Guess you like

Origin www.cnblogs.com/Leo_wl/p/11105121.html