-
Notifications
You must be signed in to change notification settings - Fork 50
Митерев Дмитрий Лаб. 2 Группа 6513 #84
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
Closed
Closed
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="..\ProjectGenerator\ProjectGenerator.ServiceDefaults\ProjectGenerator.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,43 @@ | ||
| using Ocelot.LoadBalancer.Interfaces; | ||
| using Ocelot.Responses; | ||
| using Ocelot.Values; | ||
|
|
||
| namespace Api.Gateway.LoadBalancers; | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Балансировщик нагрузки на основе параметра запроса <c>id</c>. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Алгоритм: индекс реплики = id % количество_реплик. | ||
| /// Это гарантирует, что один и тот же идентификатор всегда попадает | ||
| /// на одну и ту же реплику (sticky routing по id). | ||
| /// </remarks> | ||
| /// <param name="serviceProviderFactory"> | ||
| /// Делегат, возвращающий список доступных реплик сервиса. | ||
| /// Вызывается при каждом запросе, чтобы учитывать динамически | ||
| /// изменяющийся пул экземпляров. | ||
| /// </param> | ||
| public class QueryBasedLoadBalancer(Func<Task<List<Service>>> serviceProviderFactory) : ILoadBalancer | ||
| { | ||
| public string Type => nameof(QueryBasedLoadBalancer); | ||
|
|
||
| public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext context) | ||
| { | ||
| var services = await serviceProviderFactory(); | ||
|
|
||
| if (services.Count == 0) | ||
| throw new InvalidOperationException("No available downstream services"); | ||
|
|
||
| if (!context.Request.Query.TryGetValue("id", out var idValue) || | ||
| !int.TryParse(idValue, out var id) || | ||
| id < 0) | ||
| throw new InvalidOperationException("Query parameter 'id' is missing or invalid"); | ||
|
|
||
| var index = id % services.Count; | ||
|
|
||
| return new OkResponse<ServiceHostAndPort>(services[index].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,33 @@ | ||
| using Api.Gateway.LoadBalancers; | ||
| 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<QueryBasedLoadBalancer>((_, _, provider) => new(provider.GetAsync)); | ||
|
|
||
|
|
||
| var trustedUrls = builder.Configuration.GetSection("CorsOrigins").Get<string[]>() ?? []; | ||
| builder.Services.AddCors(options => | ||
| { | ||
| options.AddDefaultPolicy(policy => | ||
| { | ||
| policy.WithOrigins(trustedUrls) | ||
| .WithMethods("GET") | ||
| .WithHeaders("Content-Type"); | ||
| }); | ||
| }); | ||
|
|
||
| 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:62081", | ||
| "sslPort": 44391 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5200", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7190;http://localhost:5200", | ||
| "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,13 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "CorsOrigins": [ | ||
| "http://localhost:5127", | ||
| "https://localhost:7282" | ||
| ] | ||
| } |
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": "/generate", | ||
| "UpstreamHttpMethod": [ "GET" ], | ||
| "DownstreamPathTemplate": "/api/generate", | ||
| "DownstreamScheme": "https", | ||
| "DownstreamHostAndPorts": [ | ||
| { "Host": "localhost", "Port": 5000 }, | ||
| { "Host": "localhost", "Port": 5001 }, | ||
| { "Host": "localhost", "Port": 5002 }, | ||
| { "Host": "localhost", "Port": 5003 }, | ||
| { "Host": "localhost", "Port": 5004 } | ||
| ], | ||
| "LoadBalancerOptions": { | ||
| "Type": "QueryBasedLoadBalancer" | ||
| } | ||
| } | ||
| ] | ||
| } |
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:7190/generate" | ||
| } | ||
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,18 @@ | ||
| using ProjectGenerator.Api.Services; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.AddServiceDefaults(); | ||
| builder.AddRedisDistributedCache("cache"); | ||
|
|
||
| builder.Services.AddSingleton<ISoftwareProjectGenerator, SoftwareProjectGenerator>(); | ||
| builder.Services.AddScoped<ISoftwareProjectService, SoftwareProjectService>(); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| app.MapDefaultEndpoints(); | ||
|
|
||
| app.MapGet("/api/generate", async (int id, ISoftwareProjectService service) => | ||
| await service.GetOrGenerate(id)); | ||
|
|
||
| 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,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\ProjectGenerator.Domain\ProjectGenerator.Domain.csproj" /> | ||
| <ProjectReference Include="..\ProjectGenerator\ProjectGenerator.ServiceDefaults\ProjectGenerator.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Folder Include="Services\" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="9.5.2" /> | ||
| <PackageReference Include="Bogus" Version="35.6.5" /> | ||
| </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,38 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "iisSettings": { | ||
| "windowsAuthentication": false, | ||
| "anonymousAuthentication": true, | ||
| "iisExpress": { | ||
| "applicationUrl": "http://localhost:10098", | ||
| "sslPort": 44392 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "http://localhost:5283", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:7046;http://localhost:5283", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "IIS Express": { | ||
| "commandName": "IISExpress", | ||
| "launchBrowser": true, | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| } | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
ProjectGenerator.Api/Services/ISoftwareProjectGenerator.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,14 @@ | ||
| using ProjectGenerator.Domain.Models; | ||
|
|
||
| namespace ProjectGenerator.Api.Services; | ||
|
|
||
| /// <summary> | ||
| /// Интерфейс генератора программных проектов | ||
| /// </summary> | ||
| public interface ISoftwareProjectGenerator | ||
| { | ||
| /// <summary> | ||
| /// Генерирует программный проект с указанным идентификатором | ||
| /// </summary> | ||
| public SoftwareProject Generate(int id); | ||
| } |
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 @@ | ||
| using ProjectGenerator.Domain.Models; | ||
|
|
||
| namespace ProjectGenerator.Api.Services; | ||
|
|
||
| /// <summary> | ||
| /// Интерфейс сервиса программных проектов | ||
| /// </summary> | ||
| public interface ISoftwareProjectService | ||
| { | ||
| /// <summary> | ||
| /// Получает программный проект по идентификатору из кэша или генерирует новый | ||
| /// </summary> | ||
| public Task<SoftwareProject> GetOrGenerate(int id); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.