NetCore Ocelot 之 Qos

发布时间 2023-10-08 19:06:07作者: 云霄宇霁

Qos quality of service

Ocelot supports one Qos capability at the current time.You can set on a per Route basis if you want to use a circuit breaker when making requests to a downstream service. This uses an awesome .NET library called Polly.

The first thing you need to do fi you want to use the administration API is bring in the relevant NuGet package.

 Then config in your configure service.

builder.Services.AddOcelot()
    //.AddCacheManager(c => c.WithDictionaryHandle())// cache manager
    .AddSingletonDefinedAggregator<CustomAdvancedAggregator>()
    .AddCustomLoadBalancer((serviceProvider, route, serviceDiscoveryProvider) => new CustomRandomLoadBalancer(serviceDiscoveryProvider.Get))
    .AddConsul()
    .AddPolly();

The Qos configuration in ocelot.json

 "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 10000, //ms
        "TimeoutValue": 10000 //ms,default 90s
      }

     ExceptionAllowedBeforeBreaking - This value must set a numebr greater than 0 against ExceptionAllowedBeforeBreaking for this rule to be impletemented.

     DurationOfBreak - This value specify the circuit breaker will stay open for 10 seconds after it is tripped.

      TimeoutValue  - This value specify if a request takes more than 10 seconds it will automatically be timed out.

You can set the TimeoutValue in isolation of the ExceptionAllowedBeforeBreaking and DurationOfBreak options.

"QoSOptions": {
    "TimeoutValue":5000
}

If you do not add a Qos section Qos will not be used however Ocelot will default to a 90 seconds timeout on all downstream requests. If someone needs this to be configurabel open an issue.

Stop all api services e.g.