-
Notifications
You must be signed in to change notification settings - Fork 50
Дубова Ксения Лаб. 2 группа 6511 #88
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
base: main
Are you sure you want to change the base?
Changes from all commits
2db9f08
7cd452c
4dec7c7
d72b2e6
9f2a675
e4d7a53
1955fa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\CompanyEmployee.ServiceDefaults\CompanyEmployee.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
| var app = builder.Build(); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| app.MapGet("/", () => "Hello World!"); | ||
|
|
||
| app.Run(); |
| 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:49541", | ||
| "sslPort": 44328 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5176", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7163;http://localhost:5176", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "BaseAddress": "" | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "BaseAddress": "http://localhost:5212/api/CompanyEmployee" | ||
| } |
| 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="..\CompanyEmployee.ServiceDefaults\CompanyEmployee.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| using Ocelot.LoadBalancer.Errors; | ||
| using Ocelot.LoadBalancer.Interfaces; | ||
| using Ocelot.Responses; | ||
| using Ocelot.ServiceDiscovery.Providers; | ||
| using Ocelot.Values; | ||
kseniaDubova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| namespace CompanyEmployee.ApiGateway.LoadBalancer; | ||
|
|
||
| public class WeightedRoundRobin : ILoadBalancer | ||
| { | ||
| private static List<ServiceHostAndPort> _expandedList = new(); | ||
kseniaDubova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private static int _index = 0; | ||
| private static readonly object _lock = new(); | ||
| private static bool _initialized = false; | ||
|
|
||
| public string Type => nameof(WeightedRoundRobin); | ||
|
|
||
| public WeightedRoundRobin( | ||
| List<ServiceHostAndPort> services, | ||
| IConfiguration config) | ||
| { | ||
| if (_initialized) return; | ||
|
|
||
| var weights = config | ||
| .GetSection("LoadBalancerWeights") | ||
| .Get<Dictionary<string, int>>() ?? new(); | ||
kseniaDubova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| foreach (var s in services) | ||
| { | ||
|
|
||
| var key = s.DownstreamPort.ToString(); | ||
| var weight = weights.TryGetValue(key, out var w) ? w : 1; | ||
|
|
||
| for (var i = 0; i < weight; i++) | ||
| { | ||
| _expandedList.Add(s); | ||
| } | ||
| } | ||
|
Comment on lines
+29
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лучше алгоритмически вычислять какой сервис нужно использовать, в таком варианте если веса у сервисов будут большие, то получится огромный список |
||
|
|
||
| _initialized = true; | ||
|
|
||
| } | ||
|
|
||
| public Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext context) | ||
| { | ||
| lock (_lock) | ||
| { | ||
| var s = _expandedList[_index]; | ||
| _index = (_index + 1) % _expandedList.Count; | ||
|
|
||
| return Task.FromResult<Response<ServiceHostAndPort>>( | ||
| new OkResponse<ServiceHostAndPort>(s)); | ||
| } | ||
| } | ||
|
|
||
| public void Release(ServiceHostAndPort hostAndPort) { } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using CompanyEmployee.ApiGateway.LoadBalancer; | ||
| using Ocelot.DependencyInjection; | ||
| using Ocelot.LoadBalancer.Interfaces; | ||
kseniaDubova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| using Ocelot.Middleware; | ||
| using Ocelot.Values; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не хватает |
||
| builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); | ||
|
|
||
| builder.Services | ||
| .AddOcelot(builder.Configuration) | ||
| .AddCustomLoadBalancer((route, _) => | ||
| { | ||
| var config = builder.Configuration; | ||
|
|
||
| var services = route.DownstreamAddresses | ||
| .Select(h => new ServiceHostAndPort(h.Host, h.Port)) | ||
| .ToList(); | ||
|
|
||
| return new WeightedRoundRobin(services, config); | ||
| }); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| await app.UseOcelot(); | ||
|
|
||
| app.Run(); | ||
| 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:26883", | ||
| "sslPort": 44388 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5212", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7092;http://localhost:5212", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "GlobalConfiguration": { | ||
| "BaseUrl": "https://localhost:5212" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "LoadBalancerWeights": { | ||
| "5213": 1, | ||
| "5214": 2, | ||
| "5215": 1 | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| { | ||
| "Generators": [ "generator-1", "generator-2", "generator-3" ], | ||
| "Routes": [ | ||
| { | ||
| "DownstreamPathTemplate": "/api/CompanyEmployee", | ||
| "DownstreamScheme": "http", | ||
| "DownstreamHostAndPorts": [ | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5213 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5214 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5215 | ||
| } | ||
| ], | ||
|
Comment on lines
+7
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если поменять порты в aspire, то перестанет работать, по-хорошему можно перезаписывать эту конфигурацию на основе переменных окружения, которые приходят из aspire, в них будут адреса сервисов из-за |
||
| "UpstreamPathTemplate": "/api/CompanyEmployee", | ||
| "UpstreamHttpMethod": [ "GET" ], | ||
| "LoadBalancerOptions": { | ||
| "Type": "WeightedRoundRobin" | ||
| } | ||
| } | ||
| ] | ||
| } | ||
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.
Лишний проект