-
Notifications
You must be signed in to change notification settings - Fork 50
Белякова Вероника Лаб. 2 Группа 6511 #78
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
Cat-sandwich
wants to merge
7
commits into
itsecd:main
Choose a base branch
from
Cat-sandwich: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
7 commits
Select commit
Hold shift + click to select a range
c8bf712
Создан класс по предметной области, написан генератор и сервис
Cat-sandwich 8b4f875
внесены изменения
Cat-sandwich 4754541
Update README.md
Cat-sandwich 0c10853
Внесены изменения
Cat-sandwich cd5d05c
Merge branch 'main' of https://github.com/Cat-sandwich/cloud-development
Cat-sandwich 15ea719
2 лр - выполнена балансировка алгоритмом Query Based
Cat-sandwich 63577f7
Обновленный ридми под вторую лабу
Cat-sandwich 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:5200/api/employee" | ||
| } | ||
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,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="..\Employee.ServiceDefaults\Employee.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
69 changes: 69 additions & 0 deletions
69
Employee.ApiGateway/LoadBalancer/QueryBasedLoadBalanser.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,69 @@ | ||
| using Ocelot.Values; | ||
| using Ocelot.Responses; | ||
| using Ocelot.LoadBalancer.Errors; | ||
| using Ocelot.LoadBalancer.Interfaces; | ||
| using Ocelot.ServiceDiscovery.Providers; | ||
|
|
||
| namespace Employee.ApiGateway.LoadBalancer; | ||
|
|
||
| /// <summary> | ||
| /// Балансировщик нагрузки, выбирающий реплику по значению параметра "id" | ||
| /// </summary> | ||
| /// <param name="serviceDiscovery">Провайдер для получения списка доступных сервисов</param> | ||
| public class QueryBasedLoadBalancer(IServiceDiscoveryProvider serviceDiscovery) : ILoadBalancer | ||
| { | ||
| private const string IdQuery = "id"; | ||
|
|
||
| public string Type => nameof(QueryBasedLoadBalancer); | ||
|
|
||
| /// <summary> | ||
| /// Функция выбора сервиса по параметру "id" | ||
| /// </summary> | ||
| /// <param name="httpContext">Контекст HTTP-запроса</param> | ||
| /// <returns>Адрес выбранного сервиса или ошибка</returns> | ||
| public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext) | ||
| { | ||
| var services = await serviceDiscovery.GetAsync(); | ||
|
|
||
| if (services is null || services.Count == 0) | ||
| { | ||
| return new ErrorResponse<ServiceHostAndPort>( | ||
| new ServicesAreNullError("Нет доступных сервисов")); | ||
| } | ||
|
|
||
| var idResult = TryGetValidId(httpContext.Request.Query); | ||
|
|
||
| if (!idResult.IsSuccess) | ||
| { | ||
| return new ErrorResponse<ServiceHostAndPort>( | ||
| new UnableToFindLoadBalancerError(idResult.ErrorMessage)); | ||
| } | ||
|
|
||
| var id = idResult.Value; | ||
|
|
||
| var index = id % services.Count; | ||
| var selected = services[index]; | ||
|
|
||
| return new OkResponse<ServiceHostAndPort>(selected.HostAndPort); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Функция проверки параметра запроса | ||
| /// </summary> | ||
| /// <param name="query">Запрос</param> | ||
| /// <returns>Значение id и сообщение об ошибке</returns> | ||
| private static (bool IsSuccess, int Value, string ErrorMessage) TryGetValidId(IQueryCollection query) | ||
| { | ||
| if (!query.TryGetValue(IdQuery, out var idValues) || string.IsNullOrWhiteSpace(idValues)) | ||
| return (false, 0, "Отсутствует или пустой параметр 'id'"); | ||
|
|
||
| if (!int.TryParse(idValues.First(), out var id)) | ||
| return (false, 0, "Параметр 'id' должен быть числом"); | ||
|
|
||
| if (id < 0) | ||
| return (false, 0, "Параметр 'id' не может быть отрицательным"); | ||
|
|
||
| return (true, id, string.Empty); | ||
| } | ||
| 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,44 @@ | ||
| using Employee.ApiGateway.LoadBalancer; | ||
| using Ocelot.DependencyInjection; | ||
| using Ocelot.Middleware; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); | ||
|
|
||
| var generators = builder.Configuration.GetSection("Generators").Get<string[]>() ?? []; | ||
|
|
||
| var overrides = new List<KeyValuePair<string, string?>>(); | ||
|
|
||
| for (var i = 0; i < generators.Length; i++) | ||
| { | ||
| var serviceName = generators[i]; | ||
| var url = builder.Configuration[$"services:{serviceName}:http:0"]; | ||
|
|
||
| if (string.IsNullOrWhiteSpace(url)) | ||
| continue; | ||
|
|
||
| if (!Uri.TryCreate(url, UriKind.Absolute, out var uri)) | ||
| continue; | ||
|
|
||
| overrides.Add(new KeyValuePair<string, string?>( | ||
| $"Routes:0:DownstreamHostAndPorts:{i}:Host", uri.Host)); | ||
|
|
||
| overrides.Add(new KeyValuePair<string, string?>( | ||
| $"Routes:0:DownstreamHostAndPorts:{i}:Port", uri.Port.ToString())); | ||
| } | ||
|
|
||
| if (overrides.Any()) | ||
|
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. if (overrides.Count != 0) |
||
| { | ||
| builder.Configuration.AddInMemoryCollection(overrides); | ||
| } | ||
|
|
||
| builder.Services | ||
| .AddOcelot(builder.Configuration) | ||
| .AddCustomLoadBalancer((route, sp) => | ||
| new QueryBasedLoadBalancer(sp)); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| 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,38 @@ | ||
| { | ||
| "$schema": "http://json.schemastore.org/launchsettings.json", | ||
| "iisSettings": { | ||
| "windowsAuthentication": false, | ||
| "anonymousAuthentication": true, | ||
| "iisExpress": { | ||
| "applicationUrl": "http://localhost:50608", | ||
| "sslPort": 44382 | ||
| } | ||
| }, | ||
| "profiles": { | ||
| "http": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "http://localhost:5200", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development" | ||
| } | ||
| }, | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": false, | ||
| "applicationUrl": "https://localhost:7041;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,11 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "GlobalConfiguration": { | ||
| "BaseUrl": "http://localhost:5200" | ||
| } | ||
| } |
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,9 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*" | ||
| } |
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,28 @@ | ||
| { | ||
| "Generators": [ "generator-1", "generator-2", "generator-3" ], | ||
| "Routes": [ | ||
| { | ||
| "DownstreamPathTemplate": "/api/employee", | ||
| "DownstreamScheme": "http", | ||
| "DownstreamHostAndPorts": [ | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5201 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5202 | ||
| }, | ||
| { | ||
| "Host": "localhost", | ||
| "Port": 5203 | ||
| } | ||
| ], | ||
| "UpstreamPathTemplate": "/api/employee", | ||
| "UpstreamHttpMethod": [ "GET" ], | ||
| "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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\Employee.ServiceDefaults\Employee.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Aspire.StackExchange.Redis.DistributedCaching" Version="13.1.2" /> | ||
| <PackageReference Include="Bogus" Version="35.6.5" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="10.1.5" /> | ||
| <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="10.1.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,58 @@ | ||
| namespace Employee.ApiService.Models; | ||
|
|
||
| /// <summary> | ||
| /// Класс сотрудник компании | ||
| /// </summary> | ||
| public class EmployeeModel | ||
| { | ||
|
|
||
| /// <summary> | ||
| /// Идентификатор сотрудника в системе | ||
| /// </summary> | ||
| public required int Id { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// ФИО | ||
| /// </summary> | ||
| public required string Name { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Должность | ||
| /// </summary> | ||
| public required string Position { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Отдел | ||
| /// </summary> | ||
| public required string Department { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Дата приема | ||
| /// </summary> | ||
| public required DateOnly DateAdmission { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Оклад | ||
| /// </summary> | ||
| public required decimal Salary { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Электронная почта | ||
| /// </summary> | ||
| public required string Email { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Номер телефона | ||
| /// </summary> | ||
| public required string Phone { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Индикатор увольнения | ||
| /// </summary> | ||
| public bool DismissalIndicator { get; set; } = false; | ||
|
|
||
| /// <summary> | ||
| /// Дата увольнения | ||
| /// </summary> | ||
| public DateOnly? DateDismissal { get; set; } | ||
| } |
Oops, something went wrong.
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.
А сюда нужно добавить cors и настроить его так, чтобы разрешенным origin был клиент