-
Notifications
You must be signed in to change notification settings - Fork 52
Солдатова Ксения Лаб. 3 Группа 6512 #161
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
KsSoldatova
wants to merge
10
commits into
itsecd:main
Choose a base branch
from
KsSoldatova: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
10 commits
Select commit
Hold shift + click to select a range
e8615a1
л.р. 1
KsSoldatova 2698885
fixing
KsSoldatova 3aca258
fixing 2
KsSoldatova fbe2fdf
lab 2
KsSoldatova 2f2f336
fixing
KsSoldatova b75ecbc
fixing
KsSoldatova 72d0c08
перенесла веса в конфиг, исправила discoveryProvider, добавила Servic…
KsSoldatova f12e4f8
добавлено исключение и динамическое определение количества серверов
KsSoldatova 3683eb0
lab 3
KsSoldatova 3163a42
fix
KsSoldatova 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,15 @@ | ||
| <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="..\AspireApp1\AspireApp1.ServiceDefaults\AspireApp.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,36 @@ | ||
| using Ocelot.LoadBalancer.Interfaces; | ||
| using Ocelot.Responses; | ||
| using Ocelot.Values; | ||
|
|
||
| namespace Api.Gateway.LoadBalancer; | ||
|
|
||
| /// <summary> | ||
| /// Балансировка случайным образом с весами | ||
| /// </summary> | ||
| public class WeightedRandom : ILoadBalancer | ||
| { | ||
| private readonly Func<Task<List<Service>>> _services = null!; | ||
| private static readonly object _locker = new(); | ||
|
|
||
| private readonly int[] _values = null!; | ||
|
|
||
| public string Type => nameof(WeightedRandom); | ||
| public WeightedRandom(Func<Task<List<Service>>> services, IConfiguration configuration) | ||
| { | ||
| _services = services; | ||
| var frequencies = configuration.GetSection("LoadBalancer:WeightedRandom:Weights").Get<int[]>(); | ||
| if (frequencies == null || frequencies.Length == 0) | ||
| throw new InvalidOperationException("LoadBalancer:WeightedRandom:Weights is empty or null. Add weights to configuration"); | ||
| _values = [.. Enumerable.Range(0, frequencies.Length).Zip(frequencies, (val, freq) => Enumerable.Repeat(val, freq)).SelectMany(x => x)]; | ||
| } | ||
| public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext) | ||
| { | ||
| var services = await _services.Invoke(); | ||
| lock (_locker) | ||
| { | ||
| Random.Shared.Shuffle(_values); | ||
| return new OkResponse<ServiceHostAndPort>(services[_values.First()].HostAndPort); | ||
| } | ||
| } | ||
| public void Release(ServiceHostAndPort hostAndPort) { } | ||
| } |
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,26 @@ | ||
| using Api.Gateway.LoadBalancer; | ||
| 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<WeightedRandom>((sp, _, discoveryProvider) => | ||
| { | ||
| var configuration = sp.GetRequiredService<IConfiguration>(); | ||
| return new WeightedRandom(discoveryProvider.GetAsync, configuration); | ||
| }); | ||
| builder.Services.AddCors(options => options.AddDefaultPolicy(policy => | ||
| { | ||
| policy.WithOrigins("http://localhost:5127") | ||
| .WithMethods("GET") | ||
| .AllowAnyHeader(); | ||
| })); | ||
|
|
||
| var app = builder.Build(); | ||
| app.UseCors(); | ||
| 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:25431", | ||
| "sslPort": 44359 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5190", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7034;http://localhost:5190", | ||
| "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,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,14 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "LoadBalancer": { | ||
| "WeightedRandom": { | ||
| "Weights": [1, 2, 3, 2, 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,35 @@ | ||
| { | ||
| "Routes": [ | ||
| { | ||
| "UpstreamPathTemplate": "/program-project", | ||
| "UpstreamHttpMethod": [ "GET" ], | ||
| "DownstreamHostAndPorts": [ | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 4440 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 4441 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 4442 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 4443 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 4444 | ||
| } | ||
| ], | ||
| "DownstreamPathTemplate": "/program-project", | ||
| "DownstreamScheme": "https", | ||
| "LoadBalancerOptions": { | ||
| "Type": "WeightedRandom" | ||
| } | ||
| } | ||
| ] | ||
| } |
33 changes: 33 additions & 0 deletions
33
AspireApp1/AspireApp1.AppHost.Tests/AspireApp.AppHost.Tests.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,33 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsPackable>false</IsPackable> | ||
| <IsTestProject>true</IsTestProject> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.Hosting.Testing" Version="9.5.0" /> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.4" /> | ||
| <PackageReference Include="MartinCostello.Logging.XUnit" Version="0.7.1" /> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" /> | ||
| <PackageReference Include="xunit" Version="2.9.3" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\AspireApp1.AppHost\AspireApp.AppHost.csproj" /> | ||
| <ProjectReference Include="..\..\ServiceApi\ServiceApi.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="System.Net" /> | ||
| <Using Include="Microsoft.Extensions.DependencyInjection" /> | ||
| <Using Include="Aspire.Hosting.ApplicationModel" /> | ||
| <Using Include="Aspire.Hosting.Testing" /> | ||
| <Using Include="Xunit" /> | ||
| </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,118 @@ | ||
| using Aspire.Hosting; | ||
| using Microsoft.Extensions.Logging; | ||
| using ServiceApi.Entities; | ||
| using System.Text.Json; | ||
| using Xunit.Abstractions; | ||
|
|
||
| namespace AspireApp.AppHost.Tests; | ||
|
|
||
| /// <summary> | ||
| /// Интеграционные тесты для проверки микросервисного пайплайна | ||
| /// </summary> | ||
| /// <param name="output">Служба журналирования юнит-тестов</param> | ||
| public class IntegrationTest(ITestOutputHelper output) : IAsyncLifetime | ||
| { | ||
| private DistributedApplication? _app; | ||
| private HttpClient? _gatewayClient; | ||
| private HttpClient? _fileClient; | ||
|
|
||
| /// <inheritdoc/> | ||
| public async Task InitializeAsync() | ||
| { | ||
| var cancellationToken = CancellationToken.None; | ||
| IDistributedApplicationTestingBuilder builder = await DistributedApplicationTestingBuilder.CreateAsync<Projects.AspireApp_AppHost>(cancellationToken); | ||
| builder.Configuration["DcpPublisher:RandomizePorts"] = "false"; | ||
| builder.Services.AddLogging(logging => | ||
| { | ||
| logging.AddXUnit(output); | ||
| logging.SetMinimumLevel(LogLevel.Debug); | ||
| logging.AddFilter("Aspire.Hosting.Dcp", LogLevel.Debug); | ||
| logging.AddFilter("Aspire.Hosting", LogLevel.Debug); | ||
| }); | ||
| _app = await builder.BuildAsync(cancellationToken); | ||
| await _app.StartAsync(cancellationToken); | ||
| _gatewayClient = _app.CreateHttpClient("api-gateway", "http"); | ||
| _fileClient = _app.CreateHttpClient("fileservice", "http"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Проверяет, что вызов гейтвея: | ||
| /// <list type="bullet"> | ||
| /// <item><description>В ответ отправляет сгенерированный программный проект</description></item> | ||
| /// <item><description>Сериализует программный проект в S3 хранилище</description></item> | ||
| /// <item><description>Данные в ответе гейтвея и в S3 идентичны</description></item> | ||
| /// </list> | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task GatewayCall_StoresProgramProjectInS3() | ||
| { | ||
| var id = Random.Shared.Next(1, 1000); | ||
| using var gatewayResponse = await _gatewayClient!.GetAsync($"/program-project?id={id}"); | ||
| var apiProject = JsonSerializer.Deserialize<ProgramProject>(await gatewayResponse.Content.ReadAsStringAsync()); | ||
|
|
||
| await Task.Delay(5000); | ||
| using var listResponse = await _fileClient!.GetAsync("/api/s3"); | ||
| var fileList = JsonSerializer.Deserialize<List<string>>(await listResponse.Content.ReadAsStringAsync()); | ||
| using var s3Response = await _fileClient!.GetAsync($"/api/s3/programproject_{id}.json"); | ||
| var s3Project = JsonSerializer.Deserialize<ProgramProject>(await s3Response.Content.ReadAsStringAsync()); | ||
|
|
||
| Assert.NotNull(fileList); | ||
| Assert.Single(fileList); | ||
| Assert.NotNull(apiProject); | ||
| Assert.NotNull(s3Project); | ||
| Assert.Equal(id, s3Project.Id); | ||
| Assert.Equivalent(apiProject, s3Project); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Проверяет, что после генерации двух разных программных проектов | ||
| /// в S3 хранилище появляются два разных файла | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task TwoDifferentIds_ProduceTwoFilesInS3() | ||
| { | ||
| var firstId = Random.Shared.Next(1, 500); | ||
| var secondId = Random.Shared.Next(501, 1000); | ||
|
|
||
| await _gatewayClient!.GetAsync($"/program-project?id={firstId}"); | ||
| await _gatewayClient!.GetAsync($"/program-project?id={secondId}"); | ||
|
|
||
| await Task.Delay(5000); | ||
| using var listResponse = await _fileClient!.GetAsync("/api/s3"); | ||
| var fileList = JsonSerializer.Deserialize<List<string>>(await listResponse.Content.ReadAsStringAsync()); | ||
|
|
||
| Assert.NotNull(fileList); | ||
| Assert.Equal(2, fileList.Count); | ||
| Assert.Contains($"programproject_{firstId}.json", fileList); | ||
| Assert.Contains($"programproject_{secondId}.json", fileList); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Проверяет, что повторный запрос того же программного проекта | ||
| /// обслуживается из кэша и не приводит к дублирующей отправке в S3 | ||
| /// </summary> | ||
| [Fact] | ||
| public async Task RepeatedRequest_DoesNotDuplicateFileInS3() | ||
| { | ||
| var id = Random.Shared.Next(1, 1000); | ||
|
|
||
| await _gatewayClient!.GetAsync($"/program-project?id={id}"); | ||
| await _gatewayClient!.GetAsync($"/program-project?id={id}"); | ||
| await _gatewayClient!.GetAsync($"/program-project?id={id}"); | ||
|
|
||
| await Task.Delay(5000); | ||
| using var listResponse = await _fileClient!.GetAsync("/api/s3"); | ||
| var fileList = JsonSerializer.Deserialize<List<string>>(await listResponse.Content.ReadAsStringAsync()); | ||
|
|
||
| Assert.NotNull(fileList); | ||
| Assert.Single(fileList); | ||
| Assert.Equal($"programproject_{id}.json", fileList[0]); | ||
| } | ||
|
|
||
| /// <inheritdoc/> | ||
| public async Task DisposeAsync() | ||
| { | ||
| await _app!.StopAsync(); | ||
| await _app.DisposeAsync(); | ||
| } | ||
| } | ||
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,33 @@ | ||
| <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>af866732-ee76-4a9f-b28a-2e8d52f5e153</UserSecretsId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.Hosting.AppHost" Version="9.5.2" /> | ||
| <PackageReference Include="Aspire.Hosting.Redis" Version="9.5.2" /> | ||
| <PackageReference Include="LocalStack.Aspire.Hosting" Version="9.5.3" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\Api.Gateway\Api.Gateway.csproj" /> | ||
| <ProjectReference Include="..\..\Client.Wasm\Client.Wasm.csproj" /> | ||
| <ProjectReference Include="..\..\FileService\FileService.csproj" /> | ||
| <ProjectReference Include="..\..\ServiceApi\ServiceApi.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <None Update="CloudFormation\programproject-template-sqs-s3.yaml"> | ||
| <CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
| </None> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
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.
Вроде этим тестом мы можем утверждать только то, что в объектном хранилище в итоге находится один объект с ключом
programproject_{id}.json. Но тест не доказывает, что при повторных запросах не происходит отправка в брокер сообщений и запись в объектное хранилище с перезаписью существующего объектаМожно просто уточнить комментарий к тесту, чтобы он не вводил в заблуждение