-
Notifications
You must be signed in to change notification settings - Fork 52
Лаврук Маргарита Лаб. 2 Группа 6512 #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mlavrukk
wants to merge
3
commits into
itsecd:main
Choose a base branch
from
Mlavrukk:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Ocelot" Version="24.1.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\VehicleVault\VehicleVault.ServiceDefaults\VehicleVault.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| using Api.Gateway; | ||
| using Ocelot.DependencyInjection; | ||
| using Ocelot.Middleware; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
| builder.Services.AddServiceDiscovery(); | ||
| builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); | ||
| builder.Services.AddOcelot() | ||
| .AddCustomLoadBalancer((sp, _, provider) => | ||
| new WeightedRoundRobin(provider.GetAsync, sp.GetRequiredService<IConfiguration>())); | ||
|
|
||
| var allowedOrigins = builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>() ?? []; | ||
|
|
||
| builder.Services.AddCors(options => | ||
| { | ||
| options.AddDefaultPolicy(policy => | ||
| { | ||
| policy.WithOrigins(allowedOrigins) | ||
| .AllowAnyHeader() | ||
| .WithMethods("GET"); | ||
| }); | ||
| }); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.UseCors(); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| await app.UseOcelot(); | ||
|
|
||
| app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "iisSettings": { | ||
| "windowsAuthentication": false, | ||
| "anonymousAuthentication": true, | ||
| "iisExpress": { | ||
| "applicationUrl": "http://localhost:6371", | ||
| "sslPort": 44374 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5116", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7145;http://localhost:5116", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using Ocelot.LoadBalancer.Interfaces; | ||
| using Ocelot.Responses; | ||
| using Ocelot.Values; | ||
|
|
||
| namespace Api.Gateway; | ||
|
|
||
| /// <summary> | ||
| /// Балансировщик нагрузки «Взвешенная карусель» (Weighted Round Robin). | ||
| /// Реплики перебираются циклически; каждая обрабатывает ровно <c>W</c> запросов подряд, | ||
| /// где <c>W</c> — её вес из секции <c>LoadBalancer:Weights</c> в конфигурации (0-based). | ||
| /// </summary> | ||
| /// <param name="services">Делегат для получения списка доступных реплик сервиса.</param> | ||
| /// <param name="configuration">Конфигурация приложения с секцией <c>LoadBalancer:Weights</c>.</param> | ||
| public class WeightedRoundRobin( | ||
| Func<Task<List<Service>>> services, | ||
| IConfiguration configuration) : ILoadBalancer | ||
| { | ||
| public string Type => nameof(WeightedRoundRobin); | ||
|
|
||
| private readonly int[] _weights = configuration.GetSection("LoadBalancer:Weights").Get<int[]>() ?? []; | ||
| private readonly object _lock = new(); | ||
| private int _index; | ||
| private int _used; | ||
|
|
||
| public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext context) | ||
| { | ||
| var pool = await services(); | ||
|
|
||
| if (pool.Count == 0) | ||
| throw new InvalidOperationException("No available downstream services"); | ||
|
|
||
| lock (_lock) | ||
| { | ||
| if (_index >= pool.Count) _index = 0; | ||
|
|
||
| if (_used >= Weight(_index)) | ||
| { | ||
| _index = (_index + 1) % pool.Count; | ||
| _used = 0; | ||
| } | ||
|
|
||
| _used++; | ||
| return new OkResponse<ServiceHostAndPort>(pool[_index].HostAndPort); | ||
| } | ||
| } | ||
|
|
||
| public void Release(ServiceHostAndPort hostAndPort) { } | ||
|
|
||
| /// <summary>Возвращает вес реплики по индексу. Если не задан или не положителен — возвращает 1.</summary> | ||
| /// <param name="i">Индекс реплики.</param> | ||
| private int Weight(int i) => i < _weights.Length && _weights[i] > 0 ? _weights[i] : 1; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "Cors": { | ||
| "AllowedOrigins": [ | ||
| "http://localhost:5127", | ||
| "https://localhost:7282" | ||
| ] | ||
| }, | ||
| "LoadBalancer": { | ||
| "Weights": [ 3, 2, 1, 1, 1 ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| { | ||
| "Routes": [ | ||
| { | ||
| "UpstreamPathTemplate": "/vehicle", | ||
| "UpstreamHttpMethod": [ "GET" ], | ||
| "DownstreamPathTemplate": "/api/vehicle", | ||
| "DownstreamScheme": "https", | ||
| "DownstreamHostAndPorts": [ | ||
| { "Host": "localhost", "Port": 8000 }, | ||
| { "Host": "localhost", "Port": 8001 }, | ||
| { "Host": "localhost", "Port": 8002 }, | ||
| { "Host": "localhost", "Port": 8003 }, | ||
| { "Host": "localhost", "Port": 8004 } | ||
| ], | ||
| "LoadBalancerOptions": { | ||
| "Type": "WeightedRoundRobin" | ||
| } | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,5 +6,5 @@ | |
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "BaseAddress": "" | ||
| "BaseAddress": "https://localhost:7145/vehicle" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Думаю в этом случае
_usedтоже нужно обнулять