-
Notifications
You must be signed in to change notification settings - Fork 50
Замотохина Мария Лаб. 2 Группа 6511 #91
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
ZamotohinaMaria
wants to merge
15
commits into
itsecd:main
Choose a base branch
from
ZamotohinaMaria:laba2
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
15 commits
Select commit
Hold shift + click to select a range
7e92a0d
создание проекта
ZamotohinaMaria 0bacc4c
Реализована 1 лаба
ZamotohinaMaria 9d5edc1
Update Readme
ZamotohinaMaria 2566b27
Update Readme and .gitignore
ZamotohinaMaria 4f54d1c
undate .gitignore
ZamotohinaMaria cf5044b
update images
ZamotohinaMaria 97a2696
Update README.md
ZamotohinaMaria 37e4251
mini update
ZamotohinaMaria 585b448
Пакет правок по ЛР 1 - 1
ZamotohinaMaria 450d621
Правка ЛР 1 №2: убрала папку wwwroot, настроила логи
ZamotohinaMaria 27cdb95
Создана ветка для второй лабы и новый проект ApiGateway
ZamotohinaMaria b953be1
2 лаба готовченко
ZamotohinaMaria 3c79b45
Update README.md
ZamotohinaMaria 05231a3
Update README.md
ZamotohinaMaria ed9ba41
Пакет правок по ЛР 2 №1
ZamotohinaMaria 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
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": "http://localhost:5100/medicalpatient-generator" | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using Ocelot.LoadBalancer.Errors; | ||
| using Ocelot.LoadBalancer.Interfaces; | ||
| using Ocelot.Responses; | ||
| using Ocelot.ServiceDiscovery.Providers; | ||
| using Ocelot.Values; | ||
|
|
||
| namespace MedicalPatient.ApiGateway.Balancer; | ||
|
|
||
| public class WeightedRoundRobin(IServiceDiscoveryProvider serviceDiscovery, IReadOnlyList<int>? weights = null) | ||
| : ILoadBalancer | ||
| { | ||
| private readonly int[] _weights = (weights is { Count: > 0 } ? [.. weights] : []); | ||
| private int _cursor = -1; | ||
|
|
||
| private List<int>? _wheel; | ||
| private int _lastServicesCount = -1; | ||
|
|
||
| public string Type => nameof(WeightedRoundRobin); | ||
|
|
||
| public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext) | ||
| { | ||
| var services = await serviceDiscovery.GetAsync(); | ||
|
|
||
| if (services is null) | ||
| return new ErrorResponse<ServiceHostAndPort>( | ||
| new ServicesAreNullError("Service discovery returned null")); | ||
|
|
||
| if (services.Count == 0) | ||
| return new ErrorResponse<ServiceHostAndPort>( | ||
| new ServicesAreNullError("No downstream services are available")); | ||
|
|
||
| if (_wheel == null || _lastServicesCount != services.Count) | ||
| { | ||
| _wheel = BuildWheel(services.Count); | ||
| _lastServicesCount = services.Count; | ||
| } | ||
|
|
||
| var next = (uint)Interlocked.Increment(ref _cursor); | ||
| var wheelIndex = (int)(next % (uint)_wheel.Count); | ||
| var serviceIndex = _wheel[wheelIndex]; | ||
|
|
||
| return new OkResponse<ServiceHostAndPort>(services[serviceIndex].HostAndPort); | ||
| } | ||
|
|
||
| private List<int> BuildWheel(int servicesCount) | ||
| { | ||
| var wheel = new List<int>(); | ||
|
|
||
| for (var i = 0; i < servicesCount; i++) | ||
| { | ||
| var weight = i < _weights.Length ? _weights[i] : 1; | ||
| if (weight < 1) weight = 1; | ||
|
|
||
| for (var j = 0; j < weight; j++) | ||
| wheel.Add(i); | ||
| } | ||
|
|
||
| if (wheel.Count == 0) | ||
| wheel.Add(0); | ||
|
|
||
| return wheel; | ||
| } | ||
|
|
||
| public void Release(ServiceHostAndPort hostAndPort) { } | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
MedicalPatient.ApiGateway/MedicalPatient.ApiGateway.csproj
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="..\MedicalPatient.AppHost\MedicalPatient.AppHost.ServiceDefaults\MedicalPatient.AppHost.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,64 @@ | ||
| using MedicalPatient.ApiGateway.Balancer; | ||
| using MedicalPatient.AppHost.ServiceDefaults; | ||
| using Ocelot.DependencyInjection; | ||
| using Ocelot.Middleware; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
|
Comment on lines
+6
to
+7
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.AddServiceDefaults(); | ||
|
|
||
| var allowedOrigins = builder.Configuration.GetSection("AllowedOrigins").Get<string[]>() ?? | ||
| ["https://localhost:7282", "http://localhost:5127"]; | ||
|
|
||
| builder.Services.AddCors(options => | ||
| { | ||
| options.AddPolicy("ClientCorsPolicy", policy => | ||
| { | ||
| policy.WithOrigins(allowedOrigins) | ||
| .WithMethods("GET", "POST", "PUT", "DELETE") | ||
| .WithHeaders("Authorization", "Content-Type") | ||
| .AllowCredentials(); | ||
| }); | ||
| }); | ||
|
|
||
| builder.Configuration | ||
| .AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); | ||
|
|
||
| var generators = builder.Configuration.GetSection("Generators").Get<string[]>() ?? []; | ||
|
|
||
| var configuredWeights = builder.Configuration | ||
| .GetSection("Routes:0:DownstreamHostWeights") | ||
| .GetChildren() | ||
| .Select(x => x.GetValue("Weight", 1)) | ||
| .Where(w => w > 0) | ||
| .ToArray(); | ||
| var weights = configuredWeights.Length > 0 ? configuredWeights : [3, 2, 1]; | ||
|
|
||
| var addressOverrides = new List<KeyValuePair<string, string?>>(); | ||
|
|
||
| for (var i = 0; i < generators.Length; ++i) | ||
| { | ||
| var name = generators[i]; | ||
| var url = builder.Configuration[$"services:{name}:http:0"]; | ||
|
|
||
| if (!string.IsNullOrEmpty(url) && Uri.TryCreate(url, UriKind.Absolute, out var uri)) | ||
| { | ||
| addressOverrides.Add(new($"Routes:0:DownstreamHostAndPorts:{i}:Host", uri.Host)); | ||
| addressOverrides.Add(new($"Routes:0:DownstreamHostAndPorts:{i}:Port", uri.Port.ToString())); | ||
| } | ||
| } | ||
|
|
||
| if (addressOverrides.Count > 0) | ||
| builder.Configuration.AddInMemoryCollection(addressOverrides); | ||
|
|
||
| builder.Services | ||
| .AddOcelot(builder.Configuration) | ||
| .AddCustomLoadBalancer<WeightedRoundRobin>((route, serviceDiscovery) => | ||
| new WeightedRoundRobin(serviceDiscovery, weights)); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.UseCors("ClientCorsPolicy"); | ||
|
|
||
| await app.UseOcelot(); | ||
| await app.RunAsync(); | ||
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,23 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5100", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7293;http://localhost:5100", | ||
| "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,11 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "GlobalConfiguration": { | ||
| "BaseUrl": "http://localhost:5100" | ||
| } | ||
| } |
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,14 @@ | ||
| { | ||
| "AllowedOrigins": [ | ||
| "https://localhost:7282", | ||
| "http://localhost:5127" | ||
| ], | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "Generators": [ "generator-1", "generator-2", "generator-3" ] | ||
| } |
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,44 @@ | ||
| { | ||
| "Routes": [ | ||
| { | ||
| "DownstreamPathTemplate": "/medicalpatient-generator", | ||
| "DownstreamScheme": "http", | ||
| "DownstreamHostAndPorts": [ | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5101 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5102 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5103 | ||
| } | ||
| ], | ||
| "UpstreamPathTemplate": "/medicalpatient-generator", | ||
| "UpstreamHttpMethod": [ "GET" ], | ||
| "LoadBalancerOptions": { | ||
| "Type": "WeightedRoundRobin" | ||
| }, | ||
| "DownstreamHostWeights": [ | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5101, | ||
| "Weight": 3 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5102, | ||
| "Weight": 2 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5103, | ||
| "Weight": 1 | ||
| } | ||
| ] | ||
| } | ||
| ] | ||
| } |
25 changes: 25 additions & 0 deletions
25
MedicalPatient.AppHost/MedicalPatient.AppHost.AppHost/MedicalPatient.AppHost.AppHost.csproj
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,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <Sdk Name="Aspire.AppHost.Sdk" Version="9.5.2" /> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| <UserSecretsId>fe743bf2-08eb-41c7-817b-f144b739dce3</UserSecretsId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.Hosting" Version="9.5.2" /> | ||
| <PackageReference Include="Aspire.Hosting.AppHost" Version="9.5.2" /> | ||
| <PackageReference Include="Aspire.Hosting.Redis" Version="9.5.2" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\Client.Wasm\Client.Wasm.csproj" /> | ||
| <ProjectReference Include="..\..\MedicalPatient.ApiGateway\MedicalPatient.ApiGateway.csproj" /> | ||
| <ProjectReference Include="..\..\MedicalPatient.Generator\MedicalPatient.Generator.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
39 changes: 39 additions & 0 deletions
39
MedicalPatient.AppHost/MedicalPatient.AppHost.AppHost/Program.cs
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,39 @@ | ||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var redis = builder.AddRedis("redis") | ||
| .WithRedisCommander(); | ||
|
|
||
| var generator1 = builder.AddProject<Projects.MedicalPatient_Generator>("generator-1") | ||
| .WithEndpoint("http", endpoint => endpoint.Port = 5101) | ||
| .WithReference(redis) | ||
| .WaitFor(redis) | ||
| .WithExternalHttpEndpoints(); | ||
|
|
||
| var generator2 = builder.AddProject<Projects.MedicalPatient_Generator>("generator-2") | ||
| .WithEndpoint("http", endpoint => endpoint.Port = 5102) | ||
| .WithReference(redis) | ||
| .WaitFor(redis) | ||
| .WithExternalHttpEndpoints(); | ||
|
|
||
| var generator3 = builder.AddProject<Projects.MedicalPatient_Generator>("generator-3") | ||
| .WithEndpoint("http", endpoint => endpoint.Port = 5103) | ||
| .WithReference(redis) | ||
| .WaitFor(redis) | ||
| .WithExternalHttpEndpoints(); | ||
|
|
||
| var gateway = builder | ||
| .AddProject<Projects.MedicalPatient_ApiGateway>("medicalpatient-apigateway") | ||
| .WithReference(generator1) | ||
| .WithReference(generator2) | ||
| .WithReference(generator3) | ||
| .WithExternalHttpEndpoints() | ||
| .WaitFor(generator1) | ||
| .WaitFor(generator2) | ||
| .WaitFor(generator3); | ||
|
|
||
| builder.AddProject<Projects.Client_Wasm>("client") | ||
| .WithReference(gateway) | ||
| .WaitFor(gateway); | ||
|
|
||
| builder.Build().Run(); | ||
|
|
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.
Эта реализация лучше, чем вариант при котором делается список из сервисов, но большой список в памяти можно и здесь получить, если будут большие веса
Но можно и так оставить