{"id":363391,"date":"2024-05-21T01:54:54","date_gmt":"2024-05-21T01:54:54","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=363391"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=363391","title":{"rendered":"<span>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432 .NET. \u041a\u044d\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 Rate Limiting \u0432 Ocelot, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0441\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u043c\u0438 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 .NET<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u0440\u0430\u0441\u0442\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0441\u0435\u0440\u0432\u0438\u0441, \u0442\u0430\u043a \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0435\u0442 \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u0445, \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438. <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u043a \u0432 .NET, \u0438 \u043e\u0431\u0441\u0443\u0434\u0438\u043c \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c60\/cab\/f13\/c60cabf13054ae9b298e973cef12475a.gif\" width=\"640\" height=\"360\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c60\/cab\/f13\/c60cabf13054ae9b298e973cef12475a.gif\"\/><\/figure>\n<hr\/>\n<h2>Round Robin (\u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440)<\/h2>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440. \u0417\u0430\u043f\u0440\u043e\u0441\u044b \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e \u0446\u0438\u043a\u043b\u0443, \u0432\u043e \u0432\u0441\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430, 1 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c\u044e 1000 RPS<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/231\/5d8\/92a\/2315d892a7e9ca0494f7cb3c38c17407.gif\" width=\"640\" height=\"360\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/231\/5d8\/92a\/2315d892a7e9ca0494f7cb3c38c17407.gif\"\/><\/figure>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0441\u0435\u0440\u0432\u0438\u0441 \u043c\u0430\u043b, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u0430 1 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0435\u0440\u0435\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0414\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043b \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0434\u043e 5 RPS. <\/p>\n<pre><code class=\"json\">{   \"UpstreamPathTemplate\": \"\/Limited\/Service\/Request\",   \"UpstreamHttpMethod\": [ \"Get\" ],   \"DownstreamPathTemplate\": \"\/api\/Service\/Request\",   \"DownstreamScheme\": \"http\",   \"DownstreamHostAndPorts\": [     {       \"Host\": \"ratelimiting\",       \"Port\": 80     }   ],   \"RateLimitOptions\": {     \"EnableRateLimiting\": true,     \"Period\": \"1s\",     \"PeriodTimespan\": 2,     \"Limit\": 5   } }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0448\u043b\u044e\u0437 Ocelot \u0437\u0430 Period 1s \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u0434\u043e \u043f\u044f\u0442\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 (Limit)<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d3b\/8a4\/04d\/d3b8a404de5fa942e87f6012caad0838.gif\" width=\"640\" height=\"280\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d3b\/8a4\/04d\/d3b8a404de5fa942e87f6012caad0838.gif\"\/><\/figure>\n<p>\u0427\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u0430\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 429 (\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432)<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 \u0447\u0442\u043e\u0431\u044b \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u043b \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<details class=\"spoiler\">\n<summary>Hidden text<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0434\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435<a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/tutorials\/first-web-api?view=aspnetcore-8.0&amp;tabs=visual-studio#create-a-web-project\" rel=\"noopener noreferrer nofollow\"> Web API \u043f\u0440\u043e\u0435\u043a\u0442<\/a>, \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0433\u0430\u043b\u043e\u0447\u043a\u0443 &#171;\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 Docker&#187;. <\/p>\n<p>\u00a0\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 ServiceController:<\/p>\n<pre><code class=\"cs\">[Route(\"api\/[controller]\")] [ApiController] [EnableRateLimiting(\"fixed\")] public class ServiceController : ControllerBase {     [HttpGet(\"Request\")]     public async Task&lt;IActionResult> Request()     {         string request = HttpContext.Request.GetDisplayUrl();         return Ok($\"Success_from_{request}\");     } }<\/code><\/pre>\n<p>\u0412 Program \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 RateLimiter \u0438\u0437 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0438\u043c\u0435\u043d Microsoft.AspNetCore.RateLimiting.<\/p>\n<pre><code class=\"cs\">... builder.Services.AddRateLimiter(rateLimiterOptions => {    rateLimiterOptions.RejectionStatusCode = StatusCodes.Status429TooManyRequests;    rateLimiterOptions.AddFixedWindowLimiter(\"fixed\", options =>    {       options.PermitLimit = int.Parse(builder.Configuration[\"RateLimit\"]!);       options.Window = TimeSpan.FromSeconds(1);    }); }); ... app.UseRateLimiter(); ...<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043d\u0435 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0448\u043b\u044e\u0437\u0430 Ocelot, \u0430 \u0432 \u0441\u0430\u043c\u043e\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 .NET. \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u0438 \u043c\u0430\u0448\u0438\u043d\u044b, \u043d\u0443\u0436\u043d\u044b \u0440\u0430\u0437\u043d\u044b\u0435 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 Limit-\u0430<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0448\u043b\u044e\u0437 Ocelot<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 Web API \u043f\u0440\u043e\u0435\u043a\u0442<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c nuget \u043f\u0430\u043a\u0435\u0442 Ocelot: <\/p>\n<pre><code class=\"bash\">dotnet add package Ocelot<\/code><\/pre>\n<p>\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c Ocelot \u0432 Program<\/p>\n<pre><code class=\"cs\">... builder.Configuration.AddJsonFile(\"ocelot.json\", optional: false, reloadOnChange: true); builder.Services.AddOcelot(builder.Configuration); ... await app.UseOcelot(); ...<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e ocelot.json<\/p>\n<pre><code class=\"json\">{   \"UpstreamPathTemplate\": \"\/LimitedRoundRobin\/Service\/Request\",   \"UpstreamHttpMethod\": [ \"Get\" ],   \"DownstreamPathTemplate\": \"\/api\/Service\/Request\",   \"DownstreamScheme\": \"http\",   \"DownstreamHostAndPorts\": [     {       \"Host\": \"limitedroundrobinnode1\",       \"Port\": 80     },     {       \"Host\": \"limitedroundrobinnode2\",       \"Port\": 80     },     {       \"Host\": \"limitedroundrobinnode3\",       \"Port\": 80     }   ],   \"LoadBalancerOptions\": {     \"Type\": \"RoundRobin\"   } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fd1\/2d4\/82e\/fd12d482eb75249c478843dacd8f9725.gif\" width=\"640\" height=\"360\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fd1\/2d4\/82e\/fd12d482eb75249c478843dacd8f9725.gif\"\/><\/figure>\n<p>\u0412 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, 3 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 (\u0441\u043b\u0430\u0431\u044b\u0439, \u0441\u0440\u0435\u0434\u043d\u0438\u0439, \u043c\u043e\u0449\u043d\u044b\u0439). \u0427\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u0437-\u0437\u0430 rate limiter middleware.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u044d\u0442\u043e \u043e\u0431\u043e\u0439\u0442\u0438, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 <strong>Weighted Round Robin<\/strong>. \u0418\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0435\u0441, \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0432\u0435\u0441\u043e\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u041d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c \u0447\u0442\u043e, Ocelot \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 weighted round robin, \u043a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Nginx. \u042f \u043e\u0431\u043e\u0448\u0435\u043b \u044d\u0442\u043e \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>Hidden text<\/summary>\n<div class=\"spoiler__content\">\n<p>{<br \/> &#171;UpstreamPathTemplate&#187;: &#171;\/WeightedRoundRobin\/Service\/Request&#187;,<br \/> &#171;UpstreamHttpMethod&#187;: [ &#171;Get&#187; ],<br \/> &#171;DownstreamPathTemplate&#187;: &#171;\/api\/Service\/Request&#187;,<br \/> &#171;DownstreamScheme&#187;: &#171;http&#187;,<br \/> &#171;DownstreamHostAndPorts&#187;: [<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode1&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode2&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode2&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode3&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode3&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode3&#187;,<br \/> &#171;Port&#187;: 80<br \/> }<br \/> ],<br \/> &#171;LoadBalancerOptions&#187;: {<br \/> &#171;Type&#187;: &#171;RoundRobin&#187;<br \/> }<br \/> }<\/p>\n<\/div>\n<\/details>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c6c\/7e8\/467\/c6c7e84672dcc3ffa982d5c1f8bf8224.gif\" width=\"640\" height=\"280\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c6c\/7e8\/467\/c6c7e84672dcc3ffa982d5c1f8bf8224.gif\"\/><\/figure>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0441\u043b\u0430\u0431\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 1 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430 \u0446\u0438\u043a\u043b, \u0441\u0440\u0435\u0434\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 2, \u0441\u0438\u043b\u044c\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 3<\/p>\n<p>\u041d\u0443\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u0432 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 \u043d\u0435 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0438 \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0435\u043d\u044f\u0435\u0442 \u0432\u0435\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438. <\/p>\n<h2>Least connections<\/h2>\n<p>\u0428\u043b\u044e\u0437 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0441 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0412 ocelot \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c Type LeastConnection \u0432 LoadBalancerOptions<\/p>\n<pre><code class=\"json\">\"LoadBalancerOptions\": {   \"Type\": \"LeastConnection\" }<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c68\/790\/58b\/c6879058ba98fd9b7bb3bfc7b00d77b3.gif\" width=\"640\" height=\"360\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c68\/790\/58b\/c6879058ba98fd9b7bb3bfc7b00d77b3.gif\"\/><\/figure>\n<p>\u041c\u043e\u0436\u043d\u043e \u0435\u0449\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 Balancer, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c Least connections, \u0437\u0430\u0434\u0430\u0432 \u0432\u0435\u0441 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u0430\u0431\u044b\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c \u043d\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u043e\u043e\u0431\u0449\u0435. Ocelot <a href=\"https:\/\/ocelot.readthedocs.io\/en\/latest\/features\/loadbalancer.html#custom-load-balancers\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 <\/a>\u043d\u0430\u043c \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c <\/p>\n<p>\u0412\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0437 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u0432\u044b\u043b\u043e\u0436\u0438\u043b \u043d\u0430 \u0441\u0432\u043e\u0435\u043c \u0441\u0430\u0439\u0442\u0435, \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 <a href=\"http:\/\/sandbox.codewithaman.net\/LoadBalancerDemonstration\" rel=\"noopener noreferrer nofollow\">http:\/\/sandbox.codewithaman.net\/LoadBalancerDemonstration<\/a> \u0438 \u043f\u043e\u0442\u044b\u043a\u0430\u0442\u044c \u0441\u0430\u043c\u0438\u043c \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/590\/efc\/370\/590efc370885270e841d0845f2a1b989.gif\" width=\"640\" height=\"280\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/590\/efc\/370\/590efc370885270e841d0845f2a1b989.gif\"\/><\/figure>\n<p>\u0412 Ocelot \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0448\u043b\u044e\u0437\u0430 (\u043d\u0435 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430) \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0438<\/p>\n<pre><code class=\"json\">\"FileCacheOptions\": {     \"TtlSeconds\": 10   }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0434\u043b\u044f API \u0437\u0430\u043f\u0440\u043e\u0441 \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0438 10 \u0441\u0435\u043a\u0443\u043d\u0434, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043a\u044d\u0448 \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f<\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e Ocelot <a href=\"https:\/\/ocelot.readthedocs.io\/en\/latest\/features\/servicediscovery.html\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 <\/a>\u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0430\u0448\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0431\u0443\u0434\u0443\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u0430\u0448\u0438\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/781708\/\"> https:\/\/habr.com\/ru\/articles\/781708\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u043e\u0433\u0434\u0430 \u0440\u0430\u0441\u0442\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0441\u0435\u0440\u0432\u0438\u0441, \u0442\u0430\u043a \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u0441\u0435\u0440\u0432\u0435\u0440 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0435\u0442 \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0432 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u043c \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u0445, \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438. <\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430\u0433\u0440\u0443\u0437\u043e\u043a \u0432 .NET, \u0438 \u043e\u0431\u0441\u0443\u0434\u0438\u043c \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445.<\/p>\n<figure class=\"full-width\"><\/figure>\n<hr\/>\n<h2>Round Robin (\u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440)<\/h2>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440. \u0417\u0430\u043f\u0440\u043e\u0441\u044b \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e \u0446\u0438\u043a\u043b\u0443, \u0432\u043e \u0432\u0441\u0435 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430, 1 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c\u044e 1000 RPS<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0441\u0435\u0440\u0432\u0438\u0441 \u043c\u0430\u043b, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u0430 1 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.<\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0438\u0441 \u0431\u0435\u0440\u0435\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0414\u043b\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u044d\u0442\u043e\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u043b \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u0434\u043e 5 RPS. <\/p>\n<pre><code class=\"json\">{   \"UpstreamPathTemplate\": \"\/Limited\/Service\/Request\",   \"UpstreamHttpMethod\": [ \"Get\" ],   \"DownstreamPathTemplate\": \"\/api\/Service\/Request\",   \"DownstreamScheme\": \"http\",   \"DownstreamHostAndPorts\": [     {       \"Host\": \"ratelimiting\",       \"Port\": 80     }   ],   \"RateLimitOptions\": {     \"EnableRateLimiting\": true,     \"Period\": \"1s\",     \"PeriodTimespan\": 2,     \"Limit\": 5   } }<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0448\u043b\u044e\u0437 Ocelot \u0437\u0430 Period 1s \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u0434\u043e \u043f\u044f\u0442\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 (Limit)<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0427\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u0430\u044e\u0442 \u043e\u0448\u0438\u0431\u043a\u0443 429 (\u0421\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432)<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 \u0447\u0442\u043e\u0431\u044b \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0449\u0438\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u043b \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438<\/p>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f:<\/p>\n<details class=\"spoiler\">\n<summary>Hidden text<\/summary>\n<div class=\"spoiler__content\">\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0434\u0443 \u0441\u0435\u0440\u0432\u0438\u0441\u0430.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435<a href=\"https:\/\/learn.microsoft.com\/en-us\/aspnet\/core\/tutorials\/first-web-api?view=aspnetcore-8.0&amp;tabs=visual-studio#create-a-web-project\" rel=\"noopener noreferrer nofollow\"> Web API \u043f\u0440\u043e\u0435\u043a\u0442<\/a>, \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0433\u0430\u043b\u043e\u0447\u043a\u0443 &#171;\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 Docker&#187;. <\/p>\n<p>\u00a0\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 ServiceController:<\/p>\n<pre><code class=\"cs\">[Route(\"api\/[controller]\")] [ApiController] [EnableRateLimiting(\"fixed\")] public class ServiceController : ControllerBase {     [HttpGet(\"Request\")]     public async Task&lt;IActionResult> Request()     {         string request = HttpContext.Request.GetDisplayUrl();         return Ok($\"Success_from_{request}\");     } }<\/code><\/pre>\n<p>\u0412 Program \u0434\u043e\u0431\u0430\u0432\u044c\u0442\u0435 RateLimiter \u0438\u0437 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0438\u043c\u0435\u043d Microsoft.AspNetCore.RateLimiting.<\/p>\n<pre><code class=\"cs\">... builder.Services.AddRateLimiter(rateLimiterOptions => {    rateLimiterOptions.RejectionStatusCode = StatusCodes.Status429TooManyRequests;    rateLimiterOptions.AddFixedWindowLimiter(\"fixed\", options =>    {       options.PermitLimit = int.Parse(builder.Configuration[\"RateLimit\"]!);       options.Window = TimeSpan.FromSeconds(1);    }); }); ... app.UseRateLimiter(); ...<\/code><\/pre>\n<p>\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043d\u0443\u044e \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043d\u0435 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0448\u043b\u044e\u0437\u0430 Ocelot, \u0430 \u0432 \u0441\u0430\u043c\u043e\u043c \u0441\u0435\u0440\u0432\u0438\u0441\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 .NET. \u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u0432 \u0441\u0435\u0440\u0432\u0438\u0441\u0430, \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u0438 \u043c\u0430\u0448\u0438\u043d\u044b, \u043d\u0443\u0436\u043d\u044b \u0440\u0430\u0437\u043d\u044b\u0435 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 Limit-\u0430<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0448\u043b\u044e\u0437 Ocelot<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 Web API \u043f\u0440\u043e\u0435\u043a\u0442<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c nuget \u043f\u0430\u043a\u0435\u0442 Ocelot: <\/p>\n<pre><code class=\"bash\">dotnet add package Ocelot<\/code><\/pre>\n<p>\u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c Ocelot \u0432 Program<\/p>\n<pre><code class=\"cs\">... builder.Configuration.AddJsonFile(\"ocelot.json\", optional: false, reloadOnChange: true); builder.Services.AddOcelot(builder.Configuration); ... await app.UseOcelot(); ...<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e ocelot.json<\/p>\n<pre><code class=\"json\">{   \"UpstreamPathTemplate\": \"\/LimitedRoundRobin\/Service\/Request\",   \"UpstreamHttpMethod\": [ \"Get\" ],   \"DownstreamPathTemplate\": \"\/api\/Service\/Request\",   \"DownstreamScheme\": \"http\",   \"DownstreamHostAndPorts\": [     {       \"Host\": \"limitedroundrobinnode1\",       \"Port\": 80     },     {       \"Host\": \"limitedroundrobinnode2\",       \"Port\": 80     },     {       \"Host\": \"limitedroundrobinnode3\",       \"Port\": 80     }   ],   \"LoadBalancerOptions\": {     \"Type\": \"RoundRobin\"   } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<figure class=\"full-width\"><\/figure>\n<p>\u0412 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u0438 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, 3 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 (\u0441\u043b\u0430\u0431\u044b\u0439, \u0441\u0440\u0435\u0434\u043d\u0438\u0439, \u043c\u043e\u0449\u043d\u044b\u0439). \u0427\u0430\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0438\u0437-\u0437\u0430 rate limiter middleware.<\/p>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u044d\u0442\u043e \u043e\u0431\u043e\u0439\u0442\u0438, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 <strong>Weighted Round Robin<\/strong>. \u0418\u0434\u0435\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0435\u0441, \u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0432\u0435\u0441\u043e\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432. \u041d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0432 \u0442\u043e\u043c \u0447\u0442\u043e, Ocelot \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 weighted round robin, \u043a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Nginx. \u042f \u043e\u0431\u043e\u0448\u0435\u043b \u044d\u0442\u043e \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>Hidden text<\/summary>\n<div class=\"spoiler__content\">\n<p>{<br \/> &#171;UpstreamPathTemplate&#187;: &#171;\/WeightedRoundRobin\/Service\/Request&#187;,<br \/> &#171;UpstreamHttpMethod&#187;: [ &#171;Get&#187; ],<br \/> &#171;DownstreamPathTemplate&#187;: &#171;\/api\/Service\/Request&#187;,<br \/> &#171;DownstreamScheme&#187;: &#171;http&#187;,<br \/> &#171;DownstreamHostAndPorts&#187;: [<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode1&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode2&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode2&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode3&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode3&#187;,<br \/> &#171;Port&#187;: 80<br \/> },<br \/> {<br \/> &#171;Host&#187;: &#171;limitedroundrobinnode3&#187;,<br \/> &#171;Port&#187;: 80<br \/> }<br \/> ],<br \/> &#171;LoadBalancerOptions&#187;: {<br \/> &#171;Type&#187;: &#171;RoundRobin&#187;<br \/> }<br \/> }<\/p>\n<\/div>\n<\/details>\n<figure class=\"full-width\"><\/figure>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0441\u043b\u0430\u0431\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 1 \u0437\u0430\u043f\u0440\u043e\u0441 \u0437\u0430 \u0446\u0438\u043a\u043b, \u0441\u0440\u0435\u0434\u043d\u0438\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 2, \u0441\u0438\u043b\u044c\u043d\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440 3<\/p>\n<p>\u041d\u0443\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f \u0432 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 \u043d\u0435 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0438 \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0435\u043f\u043b\u043e\u0445\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0435\u043d\u044f\u0435\u0442 \u0432\u0435\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438. <\/p>\n<h2>Least connections<\/h2>\n<p>\u0428\u043b\u044e\u0437 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043d\u043e\u0432\u044b\u0435 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0443 \u0441 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0439.<\/p>\n<p>\u0412 ocelot \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c Type LeastConnection \u0432 LoadBalancerOptions<\/p>\n<pre><code class=\"json\">\"LoadBalancerOptions\": {   \"Type\": \"LeastConnection\" }<\/code><\/pre>\n<figure class=\"full-width\"><\/figure>\n<p>\u041c\u043e\u0436\u043d\u043e \u0435\u0449\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u0432\u043e\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 Balancer, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c Least connections, \u0437\u0430\u0434\u0430\u0432 \u0432\u0435\u0441 \u0434\u043b\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u0430\u0431\u044b\u043c \u0441\u0435\u0440\u0432\u0435\u0440\u0430\u043c \u043d\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u043e\u043e\u0431\u0449\u0435. Ocelot <a href=\"https:\/\/ocelot.readthedocs.io\/en\/latest\/features\/loadbalancer.html#custom-load-balancers\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 <\/a>\u043d\u0430\u043c \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c <\/p>\n<p>\u0412\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0437 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u044f \u0432\u044b\u043b\u043e\u0436\u0438\u043b \u043d\u0430 \u0441\u0432\u043e\u0435\u043c \u0441\u0430\u0439\u0442\u0435, \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 <a href=\"http:\/\/sandbox.codewithaman.net\/LoadBalancerDemonstration\" rel=\"noopener noreferrer nofollow\">http:\/\/sandbox.codewithaman.net\/LoadBalancerDemonstration<\/a> \u0438 \u043f\u043e\u0442\u044b\u043a\u0430\u0442\u044c \u0441\u0430\u043c\u0438\u043c \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0412 Ocelot \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0448\u043b\u044e\u0437\u0430 (\u043d\u0435 \u043d\u0430 \u0441\u0442\u043e\u0440\u043e\u043d\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430) \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0438<\/p>\n<pre><code class=\"json\">\"FileCacheOptions\": {     \"TtlSeconds\": 10   }<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0434\u043b\u044f API \u0437\u0430\u043f\u0440\u043e\u0441 \u043a\u044d\u0448\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0438 10 \u0441\u0435\u043a\u0443\u043d\u0434, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043a\u044d\u0448 \u0441\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f<\/p>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e Ocelot <a href=\"https:\/\/ocelot.readthedocs.io\/en\/latest\/features\/servicediscovery.html\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 <\/a>\u043d\u0430\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0442\u044c \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0430\u0448\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0431\u0443\u0434\u0443\u0442 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u0430\u0448\u0438\u0445 \u0441\u0435\u0440\u0432\u0435\u0440\u043e\u0432<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/781708\/\"> https:\/\/habr.com\/ru\/articles\/781708\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-363391","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/363391","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=363391"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/363391\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=363391"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=363391"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=363391"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}