diff --git a/1.PNG b/1.PNG new file mode 100644 index 00000000..60d4c982 Binary files /dev/null and b/1.PNG differ diff --git a/ApiGateway/ApiGateway.csproj b/ApiGateway/ApiGateway.csproj new file mode 100644 index 00000000..c8100831 --- /dev/null +++ b/ApiGateway/ApiGateway.csproj @@ -0,0 +1,25 @@ + + + + net8.0 + enable + enable + ApiGateway + ApiGateway + + + + + + + + + + + + + Always + + + + diff --git a/ApiGateway/Configuration/WeightedRoundRobinOptions.cs b/ApiGateway/Configuration/WeightedRoundRobinOptions.cs new file mode 100644 index 00000000..d5547d71 --- /dev/null +++ b/ApiGateway/Configuration/WeightedRoundRobinOptions.cs @@ -0,0 +1,16 @@ +namespace ApiGateway.Configuration; + +public sealed class WeightedRoundRobinOptions +{ + public const string SectionName = "WeightedRoundRobin"; + + public List Nodes { get; init; } = new(); +} + +public sealed class ReplicaNodeOptions +{ + public string Host { get; init; } = string.Empty; + public int Port { get; init; } + public int Weight { get; init; } = 1; + public string ReplicaId { get; init; } = string.Empty; +} diff --git a/ApiGateway/LoadBalancing/WeightedRoundRobinBalancer.cs b/ApiGateway/LoadBalancing/WeightedRoundRobinBalancer.cs new file mode 100644 index 00000000..33425e09 --- /dev/null +++ b/ApiGateway/LoadBalancing/WeightedRoundRobinBalancer.cs @@ -0,0 +1,72 @@ +using ApiGateway.Configuration; +using Microsoft.Extensions.Options; +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Responses; +using Ocelot.Values; + +namespace ApiGateway.LoadBalancing; + +public sealed class WeightedRoundRobinBalancer : ILoadBalancer +{ + private static readonly object Sync = new(); + private readonly ILogger _logger; + private readonly List _rotation; + private int _currentIndex; + + public WeightedRoundRobinBalancer( + IOptions options, + ILogger logger) + { + _logger = logger; + _rotation = BuildRotation(options.Value.Nodes); + + if (_rotation.Count == 0) + { + throw new InvalidOperationException("Не настроены узлы для Weighted Round Robin балансировки."); + } + } + + public string Type => nameof(WeightedRoundRobinBalancer); + + public Task> LeaseAsync(HttpContext context) + { + lock (Sync) + { + if (_currentIndex >= _rotation.Count) + { + _currentIndex = 0; + } + + var next = _rotation[_currentIndex++]; + + _logger.LogInformation( + "Gateway routed request to {ReplicaAddress} by {BalancerType}", + next, + Type); + + return Task.FromResult>( + new OkResponse(next)); + } + } + + public void Release(ServiceHostAndPort hostAndPort) + { + } + + private static List BuildRotation(IEnumerable nodes) + { + var rotation = new List(); + + foreach (var node in nodes.Where(static n => !string.IsNullOrWhiteSpace(n.Host) && n.Port > 0)) + { + var normalizedWeight = Math.Max(1, node.Weight); + + for (var i = 0; i < normalizedWeight; i++) + { + rotation.Add(new ServiceHostAndPort(node.Host, node.Port)); + } + } + + return rotation; + } +} \ No newline at end of file diff --git a/ApiGateway/Program.cs b/ApiGateway/Program.cs new file mode 100644 index 00000000..dda7b95a --- /dev/null +++ b/ApiGateway/Program.cs @@ -0,0 +1,43 @@ +using ApiGateway.Configuration; +using ApiGateway.LoadBalancing; +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.Logging.ClearProviders(); +builder.Logging.AddJsonConsole(options => +{ + options.IncludeScopes = true; + options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffZ "; +}); + +builder.Services.Configure( + builder.Configuration.GetSection(WeightedRoundRobinOptions.SectionName)); + +builder.Services.AddOcelot(builder.Configuration) + .AddCustomLoadBalancer(sp => + new WeightedRoundRobinBalancer( + sp.GetRequiredService>(), + sp.GetRequiredService>())); + +builder.Services.AddCors(options => options.AddDefaultPolicy(policy => +{ + policy.WithOrigins(["http://localhost:5127", "https://localhost:7282"]); + policy.WithMethods("GET"); + policy.WithHeaders("Content-Type"); + policy.WithExposedHeaders("X-Service-Replica", "X-Service-Weight"); +})); + +var app = builder.Build(); + +app.UseCors(); +app.MapDefaultEndpoints(); + +await app.UseOcelot(); + +app.Run(); diff --git a/ApiGateway/Properties/launchSettings.json b/ApiGateway/Properties/launchSettings.json new file mode 100644 index 00000000..98385b13 --- /dev/null +++ b/ApiGateway/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:7200", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ApiGateway/appsettings.Development.json b/ApiGateway/appsettings.Development.json new file mode 100644 index 00000000..79cac825 --- /dev/null +++ b/ApiGateway/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Ocelot": "Information" + } + } +} diff --git a/ApiGateway/appsettings.json b/ApiGateway/appsettings.json new file mode 100644 index 00000000..cc426f6c --- /dev/null +++ b/ApiGateway/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Ocelot": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/ApiGateway/ocelot.json b/ApiGateway/ocelot.json new file mode 100644 index 00000000..87b30882 --- /dev/null +++ b/ApiGateway/ocelot.json @@ -0,0 +1,32 @@ +{ + "Routes": [ + { + "UpstreamPathTemplate": "/employee", + "UpstreamHttpMethod": [ "GET" ], + "DownstreamPathTemplate": "/employee", + "DownstreamScheme": "https", + "LoadBalancerOptions": { + "Type": "WeightedRoundRobinBalancer" + }, + "DownstreamHostAndPorts": [ + { "Host": "localhost", "Port": 15000 }, + { "Host": "localhost", "Port": 15001 }, + { "Host": "localhost", "Port": 15002 }, + { "Host": "localhost", "Port": 15003 }, + { "Host": "localhost", "Port": 15004 } + ] + } + ], + "GlobalConfiguration": { + "BaseUrl": "http://localhost:7200" + }, + "WeightedRoundRobin": { + "Nodes": [ + { "ReplicaId": "R1", "Host": "localhost", "Port": 15000, "Weight": 1 }, + { "ReplicaId": "R2", "Host": "localhost", "Port": 15001, "Weight": 2 }, + { "ReplicaId": "R3", "Host": "localhost", "Port": 15002, "Weight": 3 }, + { "ReplicaId": "R4", "Host": "localhost", "Port": 15003, "Weight": 2 }, + { "ReplicaId": "R5", "Host": "localhost", "Port": 15004, "Weight": 1 } + ] + } +} diff --git a/AspireApp/AspireApp.AppHost/AppHost.cs b/AspireApp/AspireApp.AppHost/AppHost.cs new file mode 100644 index 00000000..23814fdd --- /dev/null +++ b/AspireApp/AspireApp.AppHost/AppHost.cs @@ -0,0 +1,25 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var cache = builder.AddRedis("employee-cache") + .WithRedisInsight(containerName: "employee-insight"); + +var gateway = builder.AddProject("api-gateway"); + +var replicaWeights = new[] { 1, 2, 3, 2, 1 }; + +for (var i = 0; i < 5; i++) +{ + var service = builder.AddProject($"service-api-{i}", launchProfileName: null) + .WithHttpsEndpoint(port: 15000 + i) + .WithReference(cache, "RedisCache") + .WithEnvironment("ReplicaId", "R" + (i + 1)) + .WithEnvironment("ReplicaWeight", replicaWeights[i].ToString()) + .WaitFor(cache); + + gateway.WaitFor(service); +} + +builder.AddProject("employee") + .WaitFor(gateway); + +builder.Build().Run(); \ No newline at end of file diff --git a/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj new file mode 100644 index 00000000..3428abc4 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj @@ -0,0 +1,24 @@ + + + + + + Exe + net8.0 + enable + enable + true + + + + + + + + + + + + + + diff --git a/AspireApp/AspireApp.AppHost/Properties/launchSettings.json b/AspireApp/AspireApp.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..df45cce3 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/Properties/launchSettings.json @@ -0,0 +1,32 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17096;http://localhost:15155", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21139", + "DOTNET_DASHBOARD_OTLP_HTTP_ENDPOINT_URL": "https://localhost:21140", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22017" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15155", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_ALLOW_UNSECURED_TRANSPORT": "true", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19197", + "DOTNET_DASHBOARD_OTLP_HTTP_ENDPOINT_URL": "http://localhost:19198", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20116" + } + } + } +} diff --git a/AspireApp/AspireApp.AppHost/appsettings.Development.json b/AspireApp/AspireApp.AppHost/appsettings.Development.json new file mode 100644 index 00000000..167eb683 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Aspire.Hosting": "Information" + } + } +} diff --git a/AspireApp/AspireApp.AppHost/appsettings.json b/AspireApp/AspireApp.AppHost/appsettings.json new file mode 100644 index 00000000..167eb683 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Aspire.Hosting": "Information" + } + } +} diff --git a/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj b/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj new file mode 100644 index 00000000..530518fa --- /dev/null +++ b/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + diff --git a/AspireApp/AspireApp.ServiceDefaults/Extensions.cs b/AspireApp/AspireApp.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..e8399ba7 --- /dev/null +++ b/AspireApp/AspireApp.ServiceDefaults/Extensions.cs @@ -0,0 +1,101 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; +using OpenTelemetry.Logs; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +public static class Extensions +{ + public static TBuilder AddServiceDefaults(this TBuilder builder) + where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + http.AddStandardResilienceHandler(); + http.AddServiceDiscovery(); + }); + + return builder; + } + + public static TBuilder ConfigureOpenTelemetry(this TBuilder builder) + where TBuilder : IHostApplicationBuilder + { + builder.Logging.AddOpenTelemetry(logging => + { + logging.IncludeFormattedMessage = true; + logging.IncludeScopes = true; + }); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddSource(builder.Environment.ApplicationName) + .AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) + where TBuilder : IHostApplicationBuilder + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (!useOtlpExporter) + { + return builder; + } + + builder.Logging.AddOpenTelemetry(logging => logging.AddOtlpExporter()); + + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => metrics.AddOtlpExporter()) + .WithTracing(tracing => tracing.AddOtlpExporter()); + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) + where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + .AddCheck("self", () => HealthCheckResult.Healthy(), tags: ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + if (app.Environment.IsDevelopment()) + { + app.MapHealthChecks("/health"); + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = registration => registration.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/Client.Wasm/Components/DataCard.razor b/Client.Wasm/Components/DataCard.razor index c646a839..68c080d9 100644 --- a/Client.Wasm/Components/DataCard.razor +++ b/Client.Wasm/Components/DataCard.razor @@ -1,22 +1,34 @@ -@inject IConfiguration Configuration +@using System.Net.Http.Json +@using System.Text.Json.Nodes +@inject IConfiguration Configuration @inject HttpClient Client - Характеристики текущего объекта + Характеристики текущего объекта - + @if (!string.IsNullOrWhiteSpace(ErrorMessage)) + { + @ErrorMessage + } + + @if (!string.IsNullOrWhiteSpace(ReplicaInfo)) + { + @ReplicaInfo + } + +
- + # Характеристика Значение - + - @if(Value is null) + @if (Value is null) { 1 @@ -30,7 +42,7 @@ foreach (var property in array) { - @(Array.IndexOf(array, property)+1) + @(Array.IndexOf(array, property) + 1) @property.Key @property.Value?.ToString() @@ -40,10 +52,10 @@
- + - Запросить новый объект + Запросить новый объект @@ -51,10 +63,10 @@ Идентификатор нового объекта: - + - + @@ -63,12 +75,51 @@ @code { private JsonObject? Value { get; set; } - private int Id { get; set; } + private string? ErrorMessage { get; set; } + private string? ReplicaInfo { get; set; } + private int Id { get; set; } = 1; private async Task RequestNewData() { - var baseAddress = Configuration["BaseAddress"] ?? throw new KeyNotFoundException("Конфигурация клиента не содержит параметра BaseAddress"); - Value = await Client.GetFromJsonAsync($"{baseAddress}?id={Id}", new JsonSerializerOptions { }); - StateHasChanged(); + ErrorMessage = null; + ReplicaInfo = null; + + if (Id <= 0) + { + ErrorMessage = "Идентификатор должен быть больше нуля."; + return; + } + + var baseAddress = Configuration["BaseAddress"]; + if (string.IsNullOrWhiteSpace(baseAddress)) + { + ErrorMessage = "Конфигурация клиента не содержит параметра BaseAddress."; + return; + } + + try + { + using var response = await Client.GetAsync($"{baseAddress}?id={Id}"); + response.EnsureSuccessStatusCode(); + + Value = await response.Content.ReadFromJsonAsync(); + + var replica = response.Headers.TryGetValues("X-Service-Replica", out var replicaValues) + ? replicaValues.FirstOrDefault() + : null; + var weight = response.Headers.TryGetValues("X-Service-Weight", out var weightValues) + ? weightValues.FirstOrDefault() + : null; + + if (!string.IsNullOrWhiteSpace(replica)) + { + ReplicaInfo = $"Ответ пришёл от реплики {replica} (вес {weight ?? "1"}). Алгоритм: Weighted Round Robin."; + } + } + catch (Exception ex) + { + ErrorMessage = $"Не удалось получить данные: {ex.Message}"; + Value = null; + } } } diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..d944c11b 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -1,13 +1,17 @@  - Лабораторная работа + + Лабораторная работа + - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №2 "Балансировка нагрузки" + Вариант №28 "Сотрудник компании" + Выполнена Миронюк Матвеем 6512 + + Ссылка на форк + diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..d4f650b0 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "http://localhost:7200/employee" } diff --git a/README.md b/README.md index dcaa5eb7..50b98834 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,8 @@ * Повторение основ работы с системами контроля версий, * Интеграционное тестирование. -### Лабораторные работы -
-1. «Кэширование» - Реализация сервиса генерации контрактов, кэширование его ответов -
- -В рамках первой лабораторной работы необходимо: -* Реализовать сервис генерации контрактов на основе Bogus, -* Реализовать кеширование при помощи IDistributedCache и Redis, -* Реализовать структурное логирование сервиса генерации, -* Настроить оркестрацию Aspire. - -
+### Лабораторная работа +
2. «Балансировка нагрузки» - Реализация апи гейтвея, настройка его работы
@@ -34,32 +24,6 @@ * Реализовать апи гейтвей на основе Ocelot, * Имплементировать алгоритм балансировки нагрузки согласно варианту. -
-
-
-3. «Интеграционное тестирование» - Реализация файлового сервиса и объектного хранилища, интеграционное тестирование бекенда -
- -В рамках третьей лабораторной работы необходимо: -* Добавить в оркестрацию объектное хранилище, -* Реализовать файловый сервис, сериализующий сгенерированные данные в файлы и сохраняющий их в объектном хранилище, -* Реализовать отправку генерируемых данных в файловый сервис посредством брокера, -* Реализовать интеграционные тесты, проверяющие корректность работы всех сервисов бекенда вместе. - -
-
-
-4. (Опционально) «Переход на облачную инфраструктуру» - Перенос бекенда в Yandex Cloud -
- -В рамках четвертой лабораторной работы необходимо перенестиервисы на облако все ранее разработанные сервисы: -* Клиент - в хостинг через отдельный бакет Object Storage, -* Сервис генерации - в Cloud Function, -* Апи гейтвей - в Serverless Integration как API Gateway, -* Брокер сообщений - в Message Queue, -* Файловый сервис - в Cloud Function, -* Объектное хранилище - в отдельный бакет Object Storage, -
@@ -71,58 +35,46 @@ * Реализация тестов с использованием [xUnit](https://xunit.net/?tabs=cs). * Создание минимальной документации к проекту: страница на GitHub с информацией о задании, скриншоты приложения и прочая информация. -**Факультативно**: -* Перенос бекенда на облачную инфраструктуру Yandex Cloud -Внимательно прочитайте [дискуссии](https://github.com/itsecd/cloud-development/discussions/1) о том, как работает автоматическое распределение на ревью. -Сразу корректно называйте свои pr, чтобы они попали на ревью нужному преподавателю. +### Вариант №28 +| Доменная область | Балансировка | Брокер | Хостинг S3 | +| --- | --- | --- | --- | +| Сотрудник компании | Weighted Round Robin | SNS | Localstack | + -По итогу работы в семестре должна получиться следующая информационная система:
-C4 диаграмма -Современные_технологии_разработки_ПО_drawio + «Сотрудник компании» +Необходимо сгенерировать информацию о сотруднике компании со следующими характеристиками: + ++----+-------------------------------------------+-------------+ +| № | Название | Тип данных | ++----+-------------------------------------------+-------------+ +| 1 | Идентификатор сотрудника в системе | int | +| 2 | ФИО | string | +| 3 | Должность | string | +| 4 | Отдел | string | +| 5 | Дата приема | DateOnly | +| 6 | Оклад | decimal | +| 7 | Электронная почта | string | +| 8 | Номер телефона | string | +| 9 | Индикатор увольнения | bool | +| 10 | Дата увольнения | DateOnly? | ++----+-------------------------------------------+-------------+ + +Комментарии к характеристикам: +ФИО - конкатенация фамилии, имени и отчества через пробел, все из раздела Name +Должность выбирается из двух заранее подготовленных справочников - справочника профессий (“Developer”, “Manager”, “Analyst” и т.д.) и суффиксов к ним (“Junior”, “Middle”, “Senior” и т.д.) +Отдел берется из раздела Commerce +Дата приема берется не более, чем 10 лет назад от текущей даты +Генерация суммы оклада должна коррелировать с суффиксом должности и не иметь более 2 знаков в дробной части +Электронная почта генерируется из раздела Internet +При отсутствии индикатора увольнения дата увольнения не заполняется +Телефонный номер должен браться из раздела Phone и быть формата +7(***)***-**-**
-## Варианты заданий -Номер варианта задания присваивается в начале семестра. Изменить его нельзя. Каждый вариант имеет уникальную комбинацию из предметной области, базы данных и технологии для общения сервиса генерации данных и сервера апи. - -[Список вариантов](https://docs.google.com/document/d/1WGmLYwffTTaAj4TgFCk5bUyW3XKbFMiBm-DHZrfFWr4/edit?usp=sharing) -[Список предметных областей и алгоритмов балансировки](https://docs.google.com/document/d/1PLn2lKe4swIdJDZhwBYzxqFSu0AbY2MFY1SUPkIKOM4/edit?usp=sharing) - -## Схема сдачи - -На каждую из лабораторных работ необходимо сделать отдельный [Pull Request (PR)](https://docs.github.com/en/pull-requests). - -Общая схема: -1. Сделать форк данного репозитория -2. Выполнить задание -3. Сделать PR в данный репозиторий -4. Исправить замечания после code review -5. Получить approve - -## Критерии оценивания - -Конкурентный принцип. -Так как задания в первой лабораторной будут повторяться между студентами, то выделяются следующие показатели для оценки: -1. Скорость разработки -2. Качество разработки -3. Полнота выполнения задания - -Быстрее делаете PR - у вас преимущество. -Быстрее получаете Approve - у вас преимущество. -Выполните нечто немного выходящее за рамки проекта - у вас преимущество. -Не укладываетесь в дедлайн - получаете минимально возможный балл. - -### Шкала оценивания +### Скриншоты -- **3 балла** за качество кода, из них: - - 2 балла - базовая оценка - - 1 балл (но не более) можно получить за выполнение любого из следующих пунктов: - - Реализация факультативного функционала - - Выполнение работы раньше других: первые 5 человек из каждой группы, которые сделали PR и получили approve, получают дополнительный балл - -## Вопросы и обратная связь по курсу -Чтобы задать вопрос по лабораторной, воспользуйтесь [соответствующим разделом дискуссий](https://github.com/itsecd/cloud-development/discussions/categories/questions) или заведите [ишью](https://github.com/itsecd/cloud-development/issues/new). -Если у вас появились идеи/пожелания/прочие полезные мысли по преподаваемой дисциплине, их можно оставить [здесь](https://github.com/itsecd/cloud-development/discussions/categories/ideas). +##### Логи подтверждающие работу балансировки +![alt text](1.PNG) diff --git a/ServiceApi/Entities/Employee.cs b/ServiceApi/Entities/Employee.cs new file mode 100644 index 00000000..fec349fb --- /dev/null +++ b/ServiceApi/Entities/Employee.cs @@ -0,0 +1,39 @@ +using System.Text.Json.Serialization; + +namespace Service.Api.Entities; + +/// +/// Сотрудник компании. +/// +public sealed class Employee +{ + [JsonPropertyName("id")] + public int Id { get; set; } + + [JsonPropertyName("fullName")] + public string FullName { get; set; } = string.Empty; + + [JsonPropertyName("position")] + public string Position { get; set; } = string.Empty; + + [JsonPropertyName("department")] + public string Department { get; set; } = string.Empty; + + [JsonPropertyName("hireDate")] + public DateOnly HireDate { get; set; } + + [JsonPropertyName("salary")] + public decimal Salary { get; set; } + + [JsonPropertyName("email")] + public string Email { get; set; } = string.Empty; + + [JsonPropertyName("phone")] + public string Phone { get; set; } = string.Empty; + + [JsonPropertyName("isFired")] + public bool IsFired { get; set; } + + [JsonPropertyName("fireDate")] + public DateOnly? FireDate { get; set; } +} diff --git a/ServiceApi/Generator/EmployeeGenerator.cs b/ServiceApi/Generator/EmployeeGenerator.cs new file mode 100644 index 00000000..b3e7645e --- /dev/null +++ b/ServiceApi/Generator/EmployeeGenerator.cs @@ -0,0 +1,92 @@ +using Bogus; +using Bogus.DataSets; +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Генератор случайных сотрудников компании. +/// +public static class EmployeeGenerator +{ + private static readonly string[] ProfessionCatalog = + { + "Developer", + "Manager", + "Analyst", + "Tester", + "Administrator", + "Designer" + }; + + private static readonly string[] PositionLevels = + { + "Junior", + "Middle", + "Senior" + }; + + public static Employee Generate(int id) + { + var faker = new Faker("ru"); + + var gender = faker.PickRandom(); + var firstName = faker.Name.FirstName(gender); + var lastName = faker.Name.LastName(gender); + var patronymic = BuildPatronymic(faker.Name.FirstName(Name.Gender.Male), gender); + + var level = faker.PickRandom(PositionLevels); + var profession = faker.PickRandom(ProfessionCatalog); + var hireDate = faker.Date.Past(10, DateTime.Today); + var isFired = faker.Random.Bool(0.18f); + + DateOnly? fireDate = null; + if (isFired) + { + fireDate = DateOnly.FromDateTime(faker.Date.Between(hireDate, DateTime.Today)); + } + + return new Employee + { + Id = id, + FullName = $"{lastName} {firstName} {patronymic}", + Position = $"{level} {profession}", + Department = faker.Commerce.Department(), + HireDate = DateOnly.FromDateTime(hireDate), + Salary = CalculateSalary(level, faker), + Email = faker.Internet.Email(firstName, lastName), + Phone = faker.Phone.PhoneNumber("+7(###)###-##-##"), + IsFired = isFired, + FireDate = fireDate + }; + } + + private static string BuildPatronymic(string sourceName, Name.Gender gender) + { + if (string.IsNullOrWhiteSpace(sourceName)) + { + return gender == Name.Gender.Male ? "Иванович" : "Ивановна"; + } + + return gender switch + { + Name.Gender.Male when sourceName.EndsWith('й') => $"{sourceName[..^1]}евич", + Name.Gender.Female when sourceName.EndsWith('й') => $"{sourceName[..^1]}евна", + Name.Gender.Male => $"{sourceName}ович", + _ => $"{sourceName}овна" + }; + } + + private static decimal CalculateSalary(string level, Faker faker) + { + var value = level switch + { + "Junior" => faker.Random.Decimal(60_000m, 95_000m), + "Middle" => faker.Random.Decimal(100_000m, 170_000m), + "Senior" => faker.Random.Decimal(180_000m, 280_000m), + _ => faker.Random.Decimal(80_000m, 120_000m) + }; + + return Math.Round(value, 2, MidpointRounding.AwayFromZero); + } +} diff --git a/ServiceApi/Generator/EmployeeGeneratorService.cs b/ServiceApi/Generator/EmployeeGeneratorService.cs new file mode 100644 index 00000000..83fd818d --- /dev/null +++ b/ServiceApi/Generator/EmployeeGeneratorService.cs @@ -0,0 +1,82 @@ +using System.Text.Json; +using Microsoft.Extensions.Caching.Distributed; +using Service.Api.Entities; + +namespace Service.Api.Generator; + +public sealed class EmployeeGeneratorService( + IDistributedCache cache, + ILogger logger, + IConfiguration configuration) : IEmployeeGeneratorService +{ + private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web); + + private readonly TimeSpan _cacheExpiration = + TimeSpan.FromMinutes(configuration.GetValue("CacheExpirationMinutes") ?? 30); + + public async Task ProcessEmployee(int id, CancellationToken cancellationToken = default) + { + ArgumentOutOfRangeException.ThrowIfNegativeOrZero(id); + + var cacheKey = $"employee:{id}"; + + using var _ = logger.BeginScope(new Dictionary + { + ["EmployeeId"] = id, + ["CacheKey"] = cacheKey + }); + + logger.LogInformation("Employee request received"); + + try + { + var employee = await RetrieveFromCache(cacheKey, cancellationToken); + if (employee is not null) + { + logger.LogInformation("Cache hit. Returning employee from Redis"); + return employee; + } + + logger.LogInformation("Cache miss. Generating new employee"); + employee = EmployeeGenerator.Generate(id); + await PopulateCache(cacheKey, employee, cancellationToken); + + logger.LogInformation( + "Employee {EmployeeId} stored in cache for {CacheLifetimeMinutes} minutes", + id, + _cacheExpiration.TotalMinutes); + + return employee; + } + catch (Exception exception) + { + logger.LogError(exception, "Error while processing employee {EmployeeId}", id); + throw; + } + } + + private async Task RetrieveFromCache(string cacheKey, CancellationToken cancellationToken) + { + var json = await cache.GetStringAsync(cacheKey, cancellationToken); + if (string.IsNullOrWhiteSpace(json)) + { + return null; + } + + return JsonSerializer.Deserialize(json, JsonOptions); + } + + private async Task PopulateCache(string cacheKey, Employee employee, CancellationToken cancellationToken) + { + var json = JsonSerializer.Serialize(employee, JsonOptions); + + await cache.SetStringAsync( + cacheKey, + json, + new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheExpiration + }, + cancellationToken); + } +} diff --git a/ServiceApi/Generator/IEmployeeGeneratorService.cs b/ServiceApi/Generator/IEmployeeGeneratorService.cs new file mode 100644 index 00000000..c8de0288 --- /dev/null +++ b/ServiceApi/Generator/IEmployeeGeneratorService.cs @@ -0,0 +1,11 @@ +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Интерфейс обработки запросов на получение сотрудника. +/// +public interface IEmployeeGeneratorService +{ + Task ProcessEmployee(int id, CancellationToken cancellationToken = default); +} diff --git a/ServiceApi/Program.cs b/ServiceApi/Program.cs new file mode 100644 index 00000000..cc04e50a --- /dev/null +++ b/ServiceApi/Program.cs @@ -0,0 +1,78 @@ +using Service.Api.Generator; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("RedisCache"); + +builder.Logging.ClearProviders(); +builder.Logging.AddJsonConsole(options => +{ + options.IncludeScopes = true; + options.TimestampFormat = "yyyy-MM-ddTHH:mm:ss.fffZ "; +}); + +builder.Services.AddScoped(); + +builder.Services.AddCors(options => options.AddDefaultPolicy(policy => +{ + policy.SetIsOriginAllowed(origin => + Uri.TryCreate(origin, UriKind.Absolute, out var uri) + && uri.IsLoopback + && (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)) + .AllowAnyHeader() + .WithMethods("GET") + .WithExposedHeaders("X-Service-Replica", "X-Service-Weight"); +})); + +var app = builder.Build(); + +var replicaId = app.Configuration["ReplicaId"] ?? Environment.MachineName; +var replicaWeight = app.Configuration.GetValue("ReplicaWeight") ?? 1; + +app.UseCors(); +app.Use(async (context, next) => +{ + context.Response.Headers["X-Service-Replica"] = replicaId; + context.Response.Headers["X-Service-Weight"] = replicaWeight.ToString(); + await next(); +}); + +app.MapDefaultEndpoints(); + +app.MapGet("/", () => Results.Ok(new +{ + service = "Service.Api", + replica = replicaId, + weight = replicaWeight, + description = "Сервис генерации сотрудников компании", + endpoints = new[] { "/employee?id=1", "/employee/1" } +})); + +app.MapGet("/employee", async (IEmployeeGeneratorService service, ILoggerFactory loggerFactory, int id, CancellationToken cancellationToken) => +{ + var logger = loggerFactory.CreateLogger("ServiceApiEndpoints"); + logger.LogInformation("Replica {ReplicaId} received request for employee {EmployeeId}", replicaId, id); + + if (id <= 0) + { + return Results.BadRequest(new { message = "Идентификатор сотрудника должен быть больше нуля." }); + } + + return Results.Ok(await service.ProcessEmployee(id, cancellationToken)); +}); + +app.MapGet("/employee/{id:int}", async (IEmployeeGeneratorService service, ILoggerFactory loggerFactory, int id, CancellationToken cancellationToken) => +{ + var logger = loggerFactory.CreateLogger("ServiceApiEndpoints"); + logger.LogInformation("Replica {ReplicaId} received request for employee {EmployeeId}", replicaId, id); + + if (id <= 0) + { + return Results.BadRequest(new { message = "Идентификатор сотрудника должен быть больше нуля." }); + } + + return Results.Ok(await service.ProcessEmployee(id, cancellationToken)); +}); + +app.Run(); diff --git a/ServiceApi/Properties/launchSettings.json b/ServiceApi/Properties/launchSettings.json new file mode 100644 index 00000000..0641ca27 --- /dev/null +++ b/ServiceApi/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:7099", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ServiceApi/Service.Api.csproj b/ServiceApi/Service.Api.csproj new file mode 100644 index 00000000..e609ccc7 --- /dev/null +++ b/ServiceApi/Service.Api.csproj @@ -0,0 +1,20 @@ + + + + net8.0 + enable + enable + Service.Api + Service.Api + + + + + + + + + + + + diff --git a/ServiceApi/appsettings.Development.json b/ServiceApi/appsettings.Development.json new file mode 100644 index 00000000..6e16757b --- /dev/null +++ b/ServiceApi/appsettings.Development.json @@ -0,0 +1,12 @@ +{ + "CacheExpirationMinutes": 30, + "ConnectionStrings": { + "RedisCache": "localhost:6379" + }, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/ServiceApi/appsettings.json b/ServiceApi/appsettings.json new file mode 100644 index 00000000..5f1aabbb --- /dev/null +++ b/ServiceApi/appsettings.json @@ -0,0 +1,10 @@ +{ + "CacheExpirationMinutes": 30, + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/var.md b/var.md new file mode 100644 index 00000000..6c6c5662 --- /dev/null +++ b/var.md @@ -0,0 +1,7 @@ +# Выполнено +- настроен запуск нескольких реплик сервиса через .NET Aspire +- добавлен API Gateway на основе Ocelot +- реализован алгоритм Weighted Round Robin +- настроено распределение запросов между репликами по весам +- обновлена маршрутизация клиентского приложения через gateway +- выполнена проверка корректности балансировки через логи и трейсы \ No newline at end of file