Implementación de circuit breakers y límite de tasa en Ocelot con .NET Core

namespace WebOclot { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddOcelot()
                .AddConsul()
                .AddPolly();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseOcelot();
    }
}

}


</div>Configuración del archivo JSON de Ocelot ```
{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/sopweb/{url}",
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "UseServiceDiscovery": true,
      "ServiceName": "sopweb",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 10000,
        "TimeoutValue": 4000
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:1140",
    "ServiceDiscoveryProvider": {
      "Host": "47.92.27.244",
      "Port": 8500,
      "Type": "Consul"
    }
  }
}

Archivo Program.cs para cargar la configuración desde el arrchivo JSON ``` using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging;

namespace WebOclot { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); }

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(conf => {
                conf.AddJsonFile("OcelotSetting.json", optional: false, reloadOnChange: true);
            })
            .UseStartup<Startup>();
}

}


Configuración de límite de tasa (rate limiting) ```
{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/{url}",
      "DownstreamScheme": "http",
      "UpstreamPathTemplate": "/sopweb/{url}",
      "UpstreamHttpMethod": [ "Get", "Post" ],
      "UseServiceDiscovery": true,
      "ServiceName": "sopweb",
      "LoadBalancerOptions": {
        "Type": "RoundRobin"
      },
      "RateLimitOptions": {
        "ClientWhitelist": [ "eleven", "seven" ],
        "EnableRateLimiting": true,
        "Period": "5m",
        "PeriodTimespan": 30,
        "Limit": 5
      },
      "QoSOptions": {
        "ExceptionsAllowedBeforeBreaking": 3,
        "DurationOfBreak": 10000,
        "TimeoutValue": 4000
      }
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:1140",
    "ServiceDiscoveryProvider": {
      "Host": "47.92.27.244",
      "Port": 8500,
      "Type": "Consul"
    },
    "RateLimitOptions": {
      "QuotaExceededMessage": "Servidor sobrecargado... intente nuevamente en 30 segundos.",
      "HttpStatusCode": 666
    }
  }
}

Nota sobre el listado blanco para límite de tasa: Para que una solicitud sea excluida del control de tasa, inclluya el parámetro ClientId en la petición. Este valor debe coincidir exactamente con uno de los elementos definidos en ClientWhitelist, respetando mayúsculas y minúsculas.

Etiquetas: Ocelot .NET Core Polly Consul Circuit Breaker

Publicado el 8-12 14:38