From fb79f52ee8046d78f8b65716468d98ac445bc9db Mon Sep 17 00:00:00 2001 From: Chuck-man Date: Thu, 19 Mar 2026 18:36:53 +0400 Subject: [PATCH 1/7] Done --- Client.Wasm/Components/StudentCard.razor | 8 +- Client.Wasm/Properties/launchSettings.json | 6 +- Client.Wasm/wwwroot/appsettings.json | 2 +- CloudDevelopment.AppHost/AppHost.cs | 13 ++ .../CloudDevelopment.AppHost.csproj | 21 +++ .../Properties/launchSettings.json | 31 +++++ .../appsettings.Development.json | 8 ++ CloudDevelopment.AppHost/appsettings.json | 9 ++ .../CloudDevelopment.ServiceDefaults.csproj | 22 +++ .../Extensions.cs | 127 ++++++++++++++++++ CloudDevelopment.sln | 22 ++- Service.Api/Caching/CacheService.cs | 52 +++++++ Service.Api/Caching/ICacheService.cs | 23 ++++ Service.Api/Entities/Employee.cs | 69 ++++++++++ Service.Api/Generator/EmployeeGenerator.cs | 67 +++++++++ Service.Api/Generator/EmployeeService.cs | 45 +++++++ Service.Api/Generator/IEmployeeService.cs | 16 +++ Service.Api/Program.cs | 24 ++++ Service.Api/Properties/launchSettings.json | 38 ++++++ Service.Api/Service.Api.csproj | 19 +++ Service.Api/appsettings.Development.json | 8 ++ Service.Api/appsettings.json | 9 ++ 22 files changed, 629 insertions(+), 10 deletions(-) create mode 100644 CloudDevelopment.AppHost/AppHost.cs create mode 100644 CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj create mode 100644 CloudDevelopment.AppHost/Properties/launchSettings.json create mode 100644 CloudDevelopment.AppHost/appsettings.Development.json create mode 100644 CloudDevelopment.AppHost/appsettings.json create mode 100644 CloudDevelopment.ServiceDefaults/CloudDevelopment.ServiceDefaults.csproj create mode 100644 CloudDevelopment.ServiceDefaults/Extensions.cs create mode 100644 Service.Api/Caching/CacheService.cs create mode 100644 Service.Api/Caching/ICacheService.cs create mode 100644 Service.Api/Entities/Employee.cs create mode 100644 Service.Api/Generator/EmployeeGenerator.cs create mode 100644 Service.Api/Generator/EmployeeService.cs create mode 100644 Service.Api/Generator/IEmployeeService.cs create mode 100644 Service.Api/Program.cs create mode 100644 Service.Api/Properties/launchSettings.json create mode 100644 Service.Api/Service.Api.csproj create mode 100644 Service.Api/appsettings.Development.json create mode 100644 Service.Api/appsettings.json diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..bd2e7d42 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №1. "Кэширование" + Вариант №40 "Сотрудник Компании" + Выполнена Золотилов Никита 6513 + Ссылка на форк diff --git a/Client.Wasm/Properties/launchSettings.json b/Client.Wasm/Properties/launchSettings.json index 0d824ea7..60120ec3 100644 --- a/Client.Wasm/Properties/launchSettings.json +++ b/Client.Wasm/Properties/launchSettings.json @@ -12,7 +12,7 @@ "http": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "applicationUrl": "http://localhost:5127", "environmentVariables": { @@ -22,7 +22,7 @@ "https": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "applicationUrl": "https://localhost:7282;http://localhost:5127", "environmentVariables": { @@ -31,7 +31,7 @@ }, "IIS Express": { "commandName": "IISExpress", - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..9c11ece9 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "https://localhost:7111/employee" } diff --git a/CloudDevelopment.AppHost/AppHost.cs b/CloudDevelopment.AppHost/AppHost.cs new file mode 100644 index 00000000..c8e352e8 --- /dev/null +++ b/CloudDevelopment.AppHost/AppHost.cs @@ -0,0 +1,13 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var cache = builder.AddRedis("employee-cache") + .WithRedisInsight(containerName: "employee-insight"); + +var service = builder.AddProject("service-api") + .WithReference(cache, "RedisCache") + .WaitFor(cache); + +var client = builder.AddProject("employee") + .WaitFor(service); + +builder.Build().Run(); diff --git a/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj b/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj new file mode 100644 index 00000000..6188e7fc --- /dev/null +++ b/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + enable + enable + 4d29b81c-d306-4bbe-9b5e-f203441bda82 + + + + + + + + + + + + + diff --git a/CloudDevelopment.AppHost/Properties/launchSettings.json b/CloudDevelopment.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..596f0aad --- /dev/null +++ b/CloudDevelopment.AppHost/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17059;http://localhost:15263", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21068", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23203", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22043" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15263", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19234", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18255", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20127" + } + } + } +} diff --git a/CloudDevelopment.AppHost/appsettings.Development.json b/CloudDevelopment.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CloudDevelopment.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CloudDevelopment.AppHost/appsettings.json b/CloudDevelopment.AppHost/appsettings.json new file mode 100644 index 00000000..31c092aa --- /dev/null +++ b/CloudDevelopment.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/CloudDevelopment.ServiceDefaults/CloudDevelopment.ServiceDefaults.csproj b/CloudDevelopment.ServiceDefaults/CloudDevelopment.ServiceDefaults.csproj new file mode 100644 index 00000000..8ad67261 --- /dev/null +++ b/CloudDevelopment.ServiceDefaults/CloudDevelopment.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/CloudDevelopment.ServiceDefaults/Extensions.cs b/CloudDevelopment.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..b72c8753 --- /dev/null +++ b/CloudDevelopment.ServiceDefaults/Extensions.cs @@ -0,0 +1,127 @@ +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; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; + +namespace Microsoft.Extensions.Hosting; + +// Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry. +// This project should be referenced by each service project in your solution. +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults +public static class Extensions +{ + private const string HealthEndpointPath = "/health"; + private const string AlivenessEndpointPath = "/alive"; + + public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.ConfigureOpenTelemetry(); + + builder.AddDefaultHealthChecks(); + + builder.Services.AddServiceDiscovery(); + + builder.Services.ConfigureHttpClientDefaults(http => + { + // Turn on resilience by default + http.AddStandardResilienceHandler(); + + // Turn on service discovery by default + http.AddServiceDiscovery(); + }); + + // Uncomment the following to restrict the allowed schemes for service discovery. + // builder.Services.Configure(options => + // { + // options.AllowedSchemes = ["https"]; + // }); + + 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(tracing => + // Exclude health check requests from tracing + tracing.Filter = context => + !context.Request.Path.StartsWithSegments(HealthEndpointPath) + && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath) + ) + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) + //.AddGrpcClientInstrumentation() + .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) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) + //{ + // builder.Services.AddOpenTelemetry() + // .UseAzureMonitor(); + //} + + return builder; + } + + public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder + { + builder.Services.AddHealthChecks() + // Add a default liveness check to ensure app is responsive + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + // Adding health checks endpoints to applications in non-development environments has security implications. + // See https://aka.ms/dotnet/aspire/healthchecks for details before enabling these endpoints in non-development environments. + if (app.Environment.IsDevelopment()) + { + // All health checks must pass for app to be considered ready to accept traffic after starting + app.MapHealthChecks(HealthEndpointPath); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..4b2b6bd7 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -1,10 +1,16 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.14.36811.4 +# Visual Studio Version 18 +VisualStudioVersion = 18.5.11605.296 insiders MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Wasm", "Client.Wasm\Client.Wasm.csproj", "{AE7EEA74-2FE0-136F-D797-854FD87E022A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.Api", "Service.Api\Service.Api.csproj", "{80A9FC01-11CE-A33B-47BE-CACEC33F4A47}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudDevelopment.AppHost", "CloudDevelopment.AppHost\CloudDevelopment.AppHost.csproj", "{10372068-9964-4BA3-8F2A-4A334E5D1301}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudDevelopment.ServiceDefaults", "CloudDevelopment.ServiceDefaults\CloudDevelopment.ServiceDefaults.csproj", "{DC017A15-5E73-C618-2A78-CD0D64478DC9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +21,18 @@ Global {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.Build.0 = Debug|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.ActiveCfg = Release|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.Build.0 = Release|Any CPU + {80A9FC01-11CE-A33B-47BE-CACEC33F4A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {80A9FC01-11CE-A33B-47BE-CACEC33F4A47}.Debug|Any CPU.Build.0 = Debug|Any CPU + {80A9FC01-11CE-A33B-47BE-CACEC33F4A47}.Release|Any CPU.ActiveCfg = Release|Any CPU + {80A9FC01-11CE-A33B-47BE-CACEC33F4A47}.Release|Any CPU.Build.0 = Release|Any CPU + {10372068-9964-4BA3-8F2A-4A334E5D1301}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {10372068-9964-4BA3-8F2A-4A334E5D1301}.Debug|Any CPU.Build.0 = Debug|Any CPU + {10372068-9964-4BA3-8F2A-4A334E5D1301}.Release|Any CPU.ActiveCfg = Release|Any CPU + {10372068-9964-4BA3-8F2A-4A334E5D1301}.Release|Any CPU.Build.0 = Release|Any CPU + {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Service.Api/Caching/CacheService.cs b/Service.Api/Caching/CacheService.cs new file mode 100644 index 00000000..907f10b7 --- /dev/null +++ b/Service.Api/Caching/CacheService.cs @@ -0,0 +1,52 @@ +using Microsoft.Extensions.Caching.Distributed; +using Service.Api.Entities; +using System.Text.Json; + +namespace Service.Api.Caching; + +public class CacheService : ICacheService +{ + private readonly IDistributedCache _cache; + private readonly ILogger _logger; + private static readonly TimeSpan _cacheExpiration = TimeSpan.FromMinutes(5); + + public CacheService(IDistributedCache cache, ILogger logger) + { + _cache = cache; + _logger = logger; + } + + public async Task RetrieveFromCache(int id) + { + try + { + var json = await _cache.GetStringAsync(id.ToString()); + if (string.IsNullOrEmpty(json)) + return null; + return JsonSerializer.Deserialize(json); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error retrieving employee {EmployeeId} from cache", id); + return null; + } + } + + public async Task PopulateCache(Employee employee) + { + try + { + var json = JsonSerializer.Serialize(employee); + await _cache.SetStringAsync(employee.Id.ToString(), json, + new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheExpiration + }); + _logger.LogDebug("Successfully cached employee {EmployeeId}", employee.Id); + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to cache employee {EmployeeId}", employee.Id); + } + } +} diff --git a/Service.Api/Caching/ICacheService.cs b/Service.Api/Caching/ICacheService.cs new file mode 100644 index 00000000..71f6641c --- /dev/null +++ b/Service.Api/Caching/ICacheService.cs @@ -0,0 +1,23 @@ +using Service.Api.Entities; + +namespace Service.Api.Caching; + +/// +/// Интерфейс для работы с кэшем сотрудников компании +/// +public interface ICacheService +{ + /// + /// Пытается достать сотрудника из кэша + /// + /// Идентификатор + /// Сотрудника компании или null + public Task RetrieveFromCache(int id); + + /// + /// Кладёт сотрудника в кэш + /// + /// Сотрудник компании + /// + public Task PopulateCache(Employee employee); +} \ No newline at end of file diff --git a/Service.Api/Entities/Employee.cs b/Service.Api/Entities/Employee.cs new file mode 100644 index 00000000..bf5a224a --- /dev/null +++ b/Service.Api/Entities/Employee.cs @@ -0,0 +1,69 @@ +using System.Text.Json.Serialization; + +namespace Service.Api.Entities; + +/// +/// Сотрудник компании +/// +public class Employee +{ + /// + /// Идентификатор + /// + [JsonPropertyName("id")] + public int Id { get; set; } + + /// + /// ФИО + /// + [JsonPropertyName("fullName")] + public required string FullName { get; set; } + + /// + /// Должность + /// + [JsonPropertyName("post")] + public required string Post { get; set; } + + /// + /// Отдел + /// + [JsonPropertyName("department")] + public required string Department { get; set; } + + /// + /// Дата приема + /// + [JsonPropertyName("hireDate ")] + public required DateOnly HireDate { get; set; } + + /// + /// Оклад + /// + [JsonPropertyName("salary")] + public required decimal Salary { get; set; } + + /// + /// Электронная почта + /// + [JsonPropertyName("email")] + public required string Email { get; set; } + + /// + /// Номер телефона + /// + [JsonPropertyName("phone")] + public required string Phone { get; set; } + + /// + /// Индикатор увольнения + /// + [JsonPropertyName("isFired")] + public required bool IsFired { get; set; } + + /// + /// Дата увольнения + /// + [JsonPropertyName("fireDate ")] + public DateOnly? FireDate { get; set; } +} diff --git a/Service.Api/Generator/EmployeeGenerator.cs b/Service.Api/Generator/EmployeeGenerator.cs new file mode 100644 index 00000000..6d905a13 --- /dev/null +++ b/Service.Api/Generator/EmployeeGenerator.cs @@ -0,0 +1,67 @@ +using Bogus; +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Генератор сотрудников компании со случайными свойствами +/// +public static class EmployeeGenerator +{ + /// + /// Справочник категорий профессий + /// + private static readonly string[] _professions = { "Developer", "Manager", "Analyst", "Designer", "QA" }; + + /// + /// Справочник категорий суффиксов должностей + /// + private static readonly string[] _suffexies = { "Junior", "Middle", "Senior", "Lead" }; + + private static readonly Faker _faker = new Faker("ru") + .RuleFor(e => e.Id, f => f.IndexFaker + 1) + .RuleFor(e => e.FullName, f => f.Name.FullName()) + .RuleFor(e => e.Post, f => f.PickRandom(_suffexies) + " " + f.PickRandom(_professions)) + .RuleFor(e => e.Department, f => f.Commerce.Department()) + .RuleFor(e => e.HireDate, f => DateOnly.FromDateTime(f.Date.Past(10))) + .RuleFor(e => e.Salary, (f, e) => CalculateSalary(e.Post)) + .RuleFor(e => e.Email, f => f.Internet.Email()) + .RuleFor(e => e.Phone, f => f.Phone.PhoneNumber("+7(###)###-##-##")) + .RuleFor(e => e.IsFired, f => f.Random.Bool(0.2f)) + .RuleFor(e => e.FireDate, (f, e) => e.IsFired ? DateOnly.FromDateTime(f.Date.Past(1)) : null); + + /// + /// Метод вычисления оклада в зависимости от суффикса должности + /// + /// Должность + /// Оклад + private static decimal CalculateSalary(string position) + { + Faker faker = new(); + + return position switch + { + var p when p.Contains("Junior") => + Math.Round(faker.Random.Decimal(30000m, 60000m), 2), + var p when p.Contains("Middle") => + Math.Round(faker.Random.Decimal(60000m, 120000m), 2), + var p when p.Contains("Senior") => + Math.Round(faker.Random.Decimal(120000m, 200000m), 2), + var p when p.Contains("Lead") => + Math.Round(faker.Random.Decimal(150000m, 250000m), 2), + _ => Math.Round(faker.Random.Decimal(40000m, 100000m), 2) + }; + } + + /// + /// Метод генерации СК + /// + /// Идентификатор + /// Сотрудник компании + public static Employee Generate(int id) + { + var employee = _faker.Generate(); + employee.Id = id; + return employee; + } +} diff --git a/Service.Api/Generator/EmployeeService.cs b/Service.Api/Generator/EmployeeService.cs new file mode 100644 index 00000000..964473da --- /dev/null +++ b/Service.Api/Generator/EmployeeService.cs @@ -0,0 +1,45 @@ +using Service.Api.Entities; +using Service.Api.Caching; + +namespace Service.Api.Generator; + +/// +/// Служба для запуска юзкейса по обработке сотрудников компании +/// +/// Кэш +/// Логгер +public class EmployeeService(ICacheService cache, ILogger logger) : IEmployeeService +{ + /// + /// Обрабатывает запрос на получение данных о сотруднике компании + /// + /// Идентификатор + /// Сотрудника компании + public async Task ProcessEmployee(int id) + { + try + { + logger.LogInformation("Processing employee request for ID: {EmployeeId}", id); + + var employee = await cache.RetrieveFromCache(id); + if (employee != null) + { + logger.LogInformation("Cache HIT for employee {EmployeeId}", id); + return employee; + } + + logger.LogInformation("Cache MISS for employee {EmployeeId}. Generating new data.", id); + employee = EmployeeGenerator.Generate(id); + logger.LogInformation("Populating the cache with employee {id}", id); + + _ = Task.Run(() => cache.PopulateCache(employee)); + + return employee; + } + catch(Exception ex) + { + logger.LogError(ex, "Unexpected error while processing employee {EmployeeId}", id); + throw; + } + } +} diff --git a/Service.Api/Generator/IEmployeeService.cs b/Service.Api/Generator/IEmployeeService.cs new file mode 100644 index 00000000..f810d198 --- /dev/null +++ b/Service.Api/Generator/IEmployeeService.cs @@ -0,0 +1,16 @@ +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Интерфейс для запуска юзкейса по обработке сотрудников компании +/// +public interface IEmployeeService +{ + /// + /// Обработка запроса на генерацию сотрудника компании + /// + /// Идентификатор + /// Сотрудник компании + public Task ProcessEmployee(int id); +} diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs new file mode 100644 index 00000000..e452af08 --- /dev/null +++ b/Service.Api/Program.cs @@ -0,0 +1,24 @@ +using Service.Api.Generator; +using Service.Api.Caching; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("RedisCache"); + +builder.Services.AddScoped(); +builder.Services.AddScoped(); + +builder.Services.AddCors(options => options.AddDefaultPolicy(policy => +{ + policy.WithOrigins(["http://localhost:5127", "https://localhost:7282"]); + policy.WithMethods("GET"); + policy.WithHeaders("Content-Type"); +})); + +var app = builder.Build(); + +app.MapDefaultEndpoints(); +app.MapGet("/employee", (IEmployeeService service, int id) => service.ProcessEmployee(id)); +app.UseCors(); +app.Run(); diff --git a/Service.Api/Properties/launchSettings.json b/Service.Api/Properties/launchSettings.json new file mode 100644 index 00000000..908fc6dc --- /dev/null +++ b/Service.Api/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:58376", + "sslPort": 44394 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5088", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7111;http://localhost:5088", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Service.Api/Service.Api.csproj b/Service.Api/Service.Api.csproj new file mode 100644 index 00000000..27fa3c85 --- /dev/null +++ b/Service.Api/Service.Api.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + diff --git a/Service.Api/appsettings.Development.json b/Service.Api/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Service.Api/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Service.Api/appsettings.json b/Service.Api/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Service.Api/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From da987f64efe2662e12c4830a6995570826d0b899 Mon Sep 17 00:00:00 2001 From: Chuck-man Date: Tue, 24 Mar 2026 13:09:39 +0400 Subject: [PATCH 2/7] =?UTF-8?q?=D0=92=D0=BD=D1=91=D1=81=20=D0=B8=D1=81?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions.cs | 1 - Service.Api/Caching/CacheService.cs | 30 ++++++------ Service.Api/Caching/ICacheService.cs | 2 +- Service.Api/Generator/EmployeeGenerator.cs | 49 +++++++++---------- Service.Api/Generator/EmployeeService.cs | 12 ++--- Service.Api/Program.cs | 2 +- 6 files changed, 45 insertions(+), 51 deletions(-) diff --git a/CloudDevelopment.ServiceDefaults/Extensions.cs b/CloudDevelopment.ServiceDefaults/Extensions.cs index b72c8753..224d3d90 100644 --- a/CloudDevelopment.ServiceDefaults/Extensions.cs +++ b/CloudDevelopment.ServiceDefaults/Extensions.cs @@ -3,7 +3,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; -using Microsoft.Extensions.ServiceDiscovery; using OpenTelemetry; using OpenTelemetry.Metrics; using OpenTelemetry.Trace; diff --git a/Service.Api/Caching/CacheService.cs b/Service.Api/Caching/CacheService.cs index 907f10b7..928e1e7a 100644 --- a/Service.Api/Caching/CacheService.cs +++ b/Service.Api/Caching/CacheService.cs @@ -4,49 +4,51 @@ namespace Service.Api.Caching; -public class CacheService : ICacheService +/// +/// Служба для с кэшем сотрудников компании +/// +/// +/// +public class CacheService(IDistributedCache cache, ILogger logger) : ICacheService { - private readonly IDistributedCache _cache; - private readonly ILogger _logger; + /// + /// Время жизни кэша + /// private static readonly TimeSpan _cacheExpiration = TimeSpan.FromMinutes(5); - public CacheService(IDistributedCache cache, ILogger logger) - { - _cache = cache; - _logger = logger; - } - + /// public async Task RetrieveFromCache(int id) { try { - var json = await _cache.GetStringAsync(id.ToString()); + var json = await cache.GetStringAsync(id.ToString()); if (string.IsNullOrEmpty(json)) return null; return JsonSerializer.Deserialize(json); } catch (Exception ex) { - _logger.LogError(ex, "Error retrieving employee {EmployeeId} from cache", id); + logger.LogError(ex, "Error retrieving employee {EmployeeId} from cache", id); return null; } } + /// public async Task PopulateCache(Employee employee) { try { var json = JsonSerializer.Serialize(employee); - await _cache.SetStringAsync(employee.Id.ToString(), json, + await cache.SetStringAsync(employee.Id.ToString(), json, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = _cacheExpiration }); - _logger.LogDebug("Successfully cached employee {EmployeeId}", employee.Id); + logger.LogDebug("Successfully cached employee {EmployeeId}", employee.Id); } catch (Exception ex) { - _logger.LogError(ex, "Failed to cache employee {EmployeeId}", employee.Id); + logger.LogError(ex, "Failed to cache employee {EmployeeId}", employee.Id); } } } diff --git a/Service.Api/Caching/ICacheService.cs b/Service.Api/Caching/ICacheService.cs index 71f6641c..c4251484 100644 --- a/Service.Api/Caching/ICacheService.cs +++ b/Service.Api/Caching/ICacheService.cs @@ -20,4 +20,4 @@ public interface ICacheService /// Сотрудник компании /// public Task PopulateCache(Employee employee); -} \ No newline at end of file +} diff --git a/Service.Api/Generator/EmployeeGenerator.cs b/Service.Api/Generator/EmployeeGenerator.cs index 6d905a13..84407727 100644 --- a/Service.Api/Generator/EmployeeGenerator.cs +++ b/Service.Api/Generator/EmployeeGenerator.cs @@ -13,46 +13,43 @@ public static class EmployeeGenerator /// private static readonly string[] _professions = { "Developer", "Manager", "Analyst", "Designer", "QA" }; + /// + /// Словарь связи категории должности с заработной платой + /// + private static readonly Dictionary _salaryRanges = new() + { + ["Junior"] = (30000m, 60000m), + ["Middle"] = (60000m, 120000m), + ["Senior"] = (120000m, 200000m), + ["Lead"] = (150000m, 250000m) + }; + /// /// Справочник категорий суффиксов должностей /// - private static readonly string[] _suffexies = { "Junior", "Middle", "Senior", "Lead" }; + private static readonly string[] _suffixes = [.. _salaryRanges.Keys]; private static readonly Faker _faker = new Faker("ru") .RuleFor(e => e.Id, f => f.IndexFaker + 1) .RuleFor(e => e.FullName, f => f.Name.FullName()) - .RuleFor(e => e.Post, f => f.PickRandom(_suffexies) + " " + f.PickRandom(_professions)) + .RuleFor(e => e.Post, f => f.PickRandom(_suffixes) + " " + f.PickRandom(_professions)) .RuleFor(e => e.Department, f => f.Commerce.Department()) .RuleFor(e => e.HireDate, f => DateOnly.FromDateTime(f.Date.Past(10))) - .RuleFor(e => e.Salary, (f, e) => CalculateSalary(e.Post)) + .RuleFor(e => e.Salary, (f, e) => + { + var suffix = _suffixes.FirstOrDefault(s => e.Post.Contains(s)); + if (suffix != null) + { + var (min, max) = _salaryRanges[suffix]; + return Math.Round(f.Random.Decimal(min, max), 2); + } + return Math.Round(f.Random.Decimal(40000m, 100000m), 2); + }) .RuleFor(e => e.Email, f => f.Internet.Email()) .RuleFor(e => e.Phone, f => f.Phone.PhoneNumber("+7(###)###-##-##")) .RuleFor(e => e.IsFired, f => f.Random.Bool(0.2f)) .RuleFor(e => e.FireDate, (f, e) => e.IsFired ? DateOnly.FromDateTime(f.Date.Past(1)) : null); - /// - /// Метод вычисления оклада в зависимости от суффикса должности - /// - /// Должность - /// Оклад - private static decimal CalculateSalary(string position) - { - Faker faker = new(); - - return position switch - { - var p when p.Contains("Junior") => - Math.Round(faker.Random.Decimal(30000m, 60000m), 2), - var p when p.Contains("Middle") => - Math.Round(faker.Random.Decimal(60000m, 120000m), 2), - var p when p.Contains("Senior") => - Math.Round(faker.Random.Decimal(120000m, 200000m), 2), - var p when p.Contains("Lead") => - Math.Round(faker.Random.Decimal(150000m, 250000m), 2), - _ => Math.Round(faker.Random.Decimal(40000m, 100000m), 2) - }; - } - /// /// Метод генерации СК /// diff --git a/Service.Api/Generator/EmployeeService.cs b/Service.Api/Generator/EmployeeService.cs index 964473da..22a940b0 100644 --- a/Service.Api/Generator/EmployeeService.cs +++ b/Service.Api/Generator/EmployeeService.cs @@ -1,5 +1,5 @@ -using Service.Api.Entities; -using Service.Api.Caching; +using Service.Api.Caching; +using Service.Api.Entities; namespace Service.Api.Generator; @@ -10,11 +10,7 @@ namespace Service.Api.Generator; /// Логгер public class EmployeeService(ICacheService cache, ILogger logger) : IEmployeeService { - /// - /// Обрабатывает запрос на получение данных о сотруднике компании - /// - /// Идентификатор - /// Сотрудника компании + /// public async Task ProcessEmployee(int id) { try @@ -36,7 +32,7 @@ public async Task ProcessEmployee(int id) return employee; } - catch(Exception ex) + catch (Exception ex) { logger.LogError(ex, "Unexpected error while processing employee {EmployeeId}", id); throw; diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs index e452af08..860869bb 100644 --- a/Service.Api/Program.cs +++ b/Service.Api/Program.cs @@ -1,5 +1,5 @@ -using Service.Api.Generator; using Service.Api.Caching; +using Service.Api.Generator; var builder = WebApplication.CreateBuilder(args); From 083164601114718e92afe17f5b1c2afcfe95770b Mon Sep 17 00:00:00 2001 From: Chuck-man Date: Tue, 24 Mar 2026 15:27:28 +0400 Subject: [PATCH 3/7] =?UTF-8?q?=D0=9E=D0=B1=D0=BE=D0=B6=D0=B0=D1=8E=20?= =?UTF-8?q?=D1=8D=D1=82=D0=B8=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D1=80=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Service.Api/Caching/CacheService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Service.Api/Caching/CacheService.cs b/Service.Api/Caching/CacheService.cs index 928e1e7a..59c97963 100644 --- a/Service.Api/Caching/CacheService.cs +++ b/Service.Api/Caching/CacheService.cs @@ -7,8 +7,8 @@ namespace Service.Api.Caching; /// /// Служба для с кэшем сотрудников компании /// -/// -/// +/// Кэш +/// Логгер public class CacheService(IDistributedCache cache, ILogger logger) : ICacheService { /// From 360396f78883e5631f09de667645b51b849ac584 Mon Sep 17 00:00:00 2001 From: Chuck-man Date: Wed, 8 Apr 2026 02:50:31 +0400 Subject: [PATCH 4/7] Lab2 done --- Api.GateWay/Api.GateWay.csproj | 15 +++++++ .../LoadBalancers/WeightedRoundRobin.cs | 45 +++++++++++++++++++ Api.GateWay/Program.cs | 26 +++++++++++ Api.GateWay/Properties/launchSettings.json | 38 ++++++++++++++++ Api.GateWay/appsettings.Development.json | 8 ++++ Api.GateWay/appsettings.json | 9 ++++ Api.GateWay/ocelot.json | 35 +++++++++++++++ Client.Wasm/Components/StudentCard.razor | 2 +- Client.Wasm/wwwroot/appsettings.json | 2 +- CloudDevelopment.AppHost/AppHost.cs | 17 +++++-- .../CloudDevelopment.AppHost.csproj | 1 + CloudDevelopment.sln | 8 +++- Service.Api/Program.cs | 8 ---- 13 files changed, 199 insertions(+), 15 deletions(-) create mode 100644 Api.GateWay/Api.GateWay.csproj create mode 100644 Api.GateWay/LoadBalancers/WeightedRoundRobin.cs create mode 100644 Api.GateWay/Program.cs create mode 100644 Api.GateWay/Properties/launchSettings.json create mode 100644 Api.GateWay/appsettings.Development.json create mode 100644 Api.GateWay/appsettings.json create mode 100644 Api.GateWay/ocelot.json diff --git a/Api.GateWay/Api.GateWay.csproj b/Api.GateWay/Api.GateWay.csproj new file mode 100644 index 00000000..d660cf11 --- /dev/null +++ b/Api.GateWay/Api.GateWay.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + + + + + + + + + diff --git a/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs b/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs new file mode 100644 index 00000000..716afbbb --- /dev/null +++ b/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs @@ -0,0 +1,45 @@ +using Ocelot.LoadBalancer.LoadBalancers; +using Ocelot.Responses; +using Ocelot.Values; + +namespace Api.GateWay.LoadBalancers; + +/// +/// Балансировщик нагрузки на основе параметров запроса +/// +/// Функция получения списка доступных сервисов +public class WeightedRoundRobin(Func>> services) : ILoadBalancer +{ + private readonly Func>> _services = services; + private readonly int[] _weights = [1, 2, 3, 2, 1]; + private int _currentIndex = -1; + private int _remainingCalls = 0; + + public string Type => nameof(WeightedRoundRobin); + + private static readonly object _lock = new(); + + public async Task> LeaseAsync(HttpContext httpContext) + { + var services = await _services.Invoke(); + if (services == null || services.Count == 0) + return new ErrorResponse( + new ServicesAreEmptyError("No services available")); + + lock (_lock) + { + if (_currentIndex == -1 || _remainingCalls == 0) + { + _currentIndex = (_currentIndex + 1) % services.Count; + _remainingCalls = _weights[_currentIndex]; + } + + var selectedService = services[_currentIndex]; + _remainingCalls--; + + return new OkResponse(selectedService.HostAndPort); + } + } + + public void Release(ServiceHostAndPort hostAndPort) { } +} diff --git a/Api.GateWay/Program.cs b/Api.GateWay/Program.cs new file mode 100644 index 00000000..7f5b2d8d --- /dev/null +++ b/Api.GateWay/Program.cs @@ -0,0 +1,26 @@ +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((_, _, provider) => new(provider.GetAsync)); + +builder.Services.AddCors(options => options.AddDefaultPolicy(policy => +{ + policy.WithOrigins(["http://localhost:5127", "https://localhost:7282"]); + policy.WithMethods("GET"); + policy.WithHeaders("Content-Type"); +})); + +var app = builder.Build(); + +app.UseCors(); + +await app.UseOcelot(); + +app.Run(); diff --git a/Api.GateWay/Properties/launchSettings.json b/Api.GateWay/Properties/launchSettings.json new file mode 100644 index 00000000..79e9512d --- /dev/null +++ b/Api.GateWay/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:9811", + "sslPort": 44389 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5142", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7147;http://localhost:5142", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Api.GateWay/appsettings.Development.json b/Api.GateWay/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Api.GateWay/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Api.GateWay/appsettings.json b/Api.GateWay/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Api.GateWay/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Api.GateWay/ocelot.json b/Api.GateWay/ocelot.json new file mode 100644 index 00000000..5d8524c4 --- /dev/null +++ b/Api.GateWay/ocelot.json @@ -0,0 +1,35 @@ +{ + "Routes": [ + { + "UpstreamPathTemplate": "/employee", + "UpstreamHttpMethod": [ "GET" ], + "DownstreamPathTemplate": "/employee", + "DownstreamScheme": "https", + "LoadBalancerOptions": { + "Type": "WeightedRoundRobin" + }, + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 15000 + }, + { + "Host": "localhost", + "Port": 15001 + }, + { + "Host": "localhost", + "Port": 15002 + }, + { + "Host": "localhost", + "Port": 15003 + }, + { + "Host": "localhost", + "Port": 15004 + } + ] + } + ] +} \ No newline at end of file diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index bd2e7d42..5ea88f0e 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,7 +4,7 @@ - Номер №1. "Кэширование" + Номер №2 "Балансировка нагрузки" Вариант №40 "Сотрудник Компании" Выполнена Золотилов Никита 6513 Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index 9c11ece9..ea68b3ea 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "https://localhost:7111/employee" + "BaseAddress": "https://localhost:7147/employee" } diff --git a/CloudDevelopment.AppHost/AppHost.cs b/CloudDevelopment.AppHost/AppHost.cs index c8e352e8..3fd214fb 100644 --- a/CloudDevelopment.AppHost/AppHost.cs +++ b/CloudDevelopment.AppHost/AppHost.cs @@ -1,13 +1,22 @@ +using StackExchange.Redis; + var builder = DistributedApplication.CreateBuilder(args); var cache = builder.AddRedis("employee-cache") .WithRedisInsight(containerName: "employee-insight"); -var service = builder.AddProject("service-api") - .WithReference(cache, "RedisCache") - .WaitFor(cache); +var gateway = builder.AddProject("api-gateway"); + +for (var i = 0; i < 5; i++) +{ + var service = builder.AddProject($"service-api-{i}", launchProfileName: null) + .WithHttpsEndpoint(port: 15000 + i) + .WithReference(cache, "RedisCache") + .WaitFor(cache); + gateway.WaitFor(service); +} var client = builder.AddProject("employee") - .WaitFor(service); + .WaitFor(gateway); builder.Build().Run(); diff --git a/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj b/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj index 6188e7fc..2dc9c221 100644 --- a/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj +++ b/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj @@ -14,6 +14,7 @@ + diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index 4b2b6bd7..c5096709 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 18 -VisualStudioVersion = 18.5.11605.296 insiders +VisualStudioVersion = 18.5.11605.296 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Wasm", "Client.Wasm\Client.Wasm.csproj", "{AE7EEA74-2FE0-136F-D797-854FD87E022A}" EndProject @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudDevelopment.AppHost", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudDevelopment.ServiceDefaults", "CloudDevelopment.ServiceDefaults\CloudDevelopment.ServiceDefaults.csproj", "{DC017A15-5E73-C618-2A78-CD0D64478DC9}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.GateWay", "Api.GateWay\Api.GateWay.csproj", "{C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Debug|Any CPU.Build.0 = Debug|Any CPU {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Release|Any CPU.ActiveCfg = Release|Any CPU {DC017A15-5E73-C618-2A78-CD0D64478DC9}.Release|Any CPU.Build.0 = Release|Any CPU + {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs index 860869bb..4d8d140b 100644 --- a/Service.Api/Program.cs +++ b/Service.Api/Program.cs @@ -9,16 +9,8 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); -builder.Services.AddCors(options => options.AddDefaultPolicy(policy => -{ - policy.WithOrigins(["http://localhost:5127", "https://localhost:7282"]); - policy.WithMethods("GET"); - policy.WithHeaders("Content-Type"); -})); - var app = builder.Build(); app.MapDefaultEndpoints(); app.MapGet("/employee", (IEmployeeService service, int id) => service.ProcessEmployee(id)); -app.UseCors(); app.Run(); From 290112a6cfa2a8ebcf2715e72638ac1ce41cf268 Mon Sep 17 00:00:00 2001 From: Chuck-man Date: Thu, 7 May 2026 21:40:15 +0400 Subject: [PATCH 5/7] lab3 --- .../LoadBalancers/WeightedRoundRobin.cs | 2 +- .../Aspire.AppHost.Tests.csproj | 27 +++ Aspire.AppHost.Tests/IntegrationTests.cs | 161 ++++++++++++++++++ Client.Wasm/Client.Wasm.csproj | 2 +- Client.Wasm/Components/StudentCard.razor | 2 +- CloudDevelopment.AppHost/AppHost.cs | 35 +++- .../CloudDevelopment.AppHost.csproj | 17 +- .../CloudFormation/employee-template.yaml | 32 ++++ CloudDevelopment.AppHost/appsettings.json | 3 + CloudDevelopment.sln | 12 ++ Event.Sink/Controllers/S3StorageController.cs | 69 ++++++++ Event.Sink/Event.Sink.csproj | 24 +++ Event.Sink/Messaging/SQSConsumerService.cs | 106 ++++++++++++ Event.Sink/Program.cs | 37 ++++ Event.Sink/Properties/launchSettings.json | 40 +++++ Event.Sink/Storage/IS3Service.cs | 34 ++++ Event.Sink/Storage/S3MinioService.cs | 142 +++++++++++++++ Event.Sink/appsettings.Development.json | 8 + Event.Sink/appsettings.json | 9 + Service.Api/Generator/EmployeeService.cs | 4 +- Service.Api/Messaging/IProducerService.cs | 15 ++ Service.Api/Messaging/ProducerService.cs | 58 +++++++ Service.Api/Program.cs | 7 + Service.Api/Service.Api.csproj | 2 + 24 files changed, 840 insertions(+), 8 deletions(-) create mode 100644 Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj create mode 100644 Aspire.AppHost.Tests/IntegrationTests.cs create mode 100644 CloudDevelopment.AppHost/CloudFormation/employee-template.yaml create mode 100644 Event.Sink/Controllers/S3StorageController.cs create mode 100644 Event.Sink/Event.Sink.csproj create mode 100644 Event.Sink/Messaging/SQSConsumerService.cs create mode 100644 Event.Sink/Program.cs create mode 100644 Event.Sink/Properties/launchSettings.json create mode 100644 Event.Sink/Storage/IS3Service.cs create mode 100644 Event.Sink/Storage/S3MinioService.cs create mode 100644 Event.Sink/appsettings.Development.json create mode 100644 Event.Sink/appsettings.json create mode 100644 Service.Api/Messaging/IProducerService.cs create mode 100644 Service.Api/Messaging/ProducerService.cs diff --git a/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs b/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs index 716afbbb..3790c23e 100644 --- a/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs +++ b/Api.GateWay/LoadBalancers/WeightedRoundRobin.cs @@ -41,5 +41,5 @@ public async Task> LeaseAsync(HttpContext httpConte } } - public void Release(ServiceHostAndPort hostAndPort) { } + public void Release(ServiceHostAndPort hostAndPort) { } } diff --git a/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj b/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj new file mode 100644 index 00000000..cc105413 --- /dev/null +++ b/Aspire.AppHost.Tests/Aspire.AppHost.Tests.csproj @@ -0,0 +1,27 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + diff --git a/Aspire.AppHost.Tests/IntegrationTests.cs b/Aspire.AppHost.Tests/IntegrationTests.cs new file mode 100644 index 00000000..8288cb0b --- /dev/null +++ b/Aspire.AppHost.Tests/IntegrationTests.cs @@ -0,0 +1,161 @@ +using Aspire.Hosting; +using Aspire.Hosting.Testing; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Service.Api.Entities; +using System.Text.Json; +using Xunit.Abstractions; +using System.Collections.Concurrent; + +namespace Aspire.AppHost.Tests; + +/// +/// Интеграционные тесты для проверки микросервисного пайплайна: +/// API → SQS → Event.Sink → MinIO +/// +/// Служба журналирования юнит-тестов +public class IntegrationTests(ITestOutputHelper output) : IAsyncLifetime +{ + private static readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true }; + + private DistributedApplication? _app; + + /// + public async Task InitializeAsync() + { + var cancellationToken = CancellationToken.None; + var builder = await DistributedApplicationTestingBuilder + .CreateAsync(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); + } + + /// + /// Основной тест: запрос сотрудника через gateway → файл появляется в Minio, + /// данные в ответе API и в объектном хранилище совпадают. + /// + [Fact] + public async Task EmployeePipeline_ApiToStorage_Success() + { + var id = new Random().Next(1, 100); + + using var gatewayClient = _app!.CreateHttpClient("api-gateway", "http"); + using var gatewayResponse = await gatewayClient.GetAsync($"/employee?id={id}"); + gatewayResponse.EnsureSuccessStatusCode(); + var apiEmployee = JsonSerializer.Deserialize( + await gatewayResponse.Content.ReadAsStringAsync(), _jsonOptions); + + await Task.Delay(TimeSpan.FromSeconds(5)); + + using var storageClient = _app!.CreateHttpClient("employee-sink", "http"); + using var listResponse = await storageClient.GetAsync("/api/files"); + listResponse.EnsureSuccessStatusCode(); + var fileList = JsonSerializer.Deserialize>( + await listResponse.Content.ReadAsStringAsync()); + + using var fileResponse = await storageClient.GetAsync($"/api/files/employee_{id}.json"); + fileResponse.EnsureSuccessStatusCode(); + var s3Employee = JsonSerializer.Deserialize( + await fileResponse.Content.ReadAsStringAsync(), _jsonOptions); + + Assert.NotNull(fileList); + Assert.Single(fileList); + Assert.Equal($"employee_{id}.json", fileList![0]); + + Assert.NotNull(apiEmployee); + Assert.NotNull(s3Employee); + Assert.Equal(id, s3Employee!.Id); + Assert.Equivalent(apiEmployee, s3Employee); + } + + /// + /// Проверка устойчивости: некорректный запрос (без id) не попадает в очередь + /// и не создаёт мусорных файлов. + /// + [Fact] + public async Task InvalidRequest_DoesNotCreateFile() + { + using var gatewayClient = _app!.CreateHttpClient("api-gateway", "http"); + + using var badResponse = await gatewayClient.GetAsync("/employee"); + Assert.False(badResponse.IsSuccessStatusCode); + + await Task.Delay(TimeSpan.FromSeconds(3)); + + using var storageClient = _app!.CreateHttpClient("employee-sink", "http"); + using var listResponse = await storageClient.GetAsync("/api/files"); + listResponse.EnsureSuccessStatusCode(); + var files = JsonSerializer.Deserialize>(await listResponse.Content.ReadAsStringAsync()); + + Assert.NotNull(files); + Assert.DoesNotContain(files!, f => f.StartsWith("employee_0") || f.Equals("employee_.json")); + } + + /// + /// Параллельная отправка нескольких сотрудников: все файлы должны быть созданы, + /// содержимое совпадает с ответами API. + /// + [Fact] + public async Task ConcurrentRequests_AllEmployeesStored() + { + const int concurrentCount = 5; + var ids = Enumerable.Range(1, concurrentCount).Select(_ => new Random().Next(200, 300)).Distinct().ToArray(); + var results = new ConcurrentDictionary(); + + using var gatewayClient = _app!.CreateHttpClient("api-gateway", "http"); + + var tasks = ids.Select(async id => + { + using var response = await gatewayClient.GetAsync($"/employee?id={id}"); + response.EnsureSuccessStatusCode(); + var emp = JsonSerializer.Deserialize( + await response.Content.ReadAsStringAsync(), _jsonOptions); + results[id] = emp; + }); + await Task.WhenAll(tasks); + + await Task.Delay(TimeSpan.FromSeconds(8)); + + using var storageClient = _app!.CreateHttpClient("employee-sink", "http"); + using var listResponse = await storageClient.GetAsync("/api/files"); + listResponse.EnsureSuccessStatusCode(); + var fileList = JsonSerializer.Deserialize>(await listResponse.Content.ReadAsStringAsync()); + + Assert.NotNull(fileList); + foreach (var id in ids) + { + var expectedFileName = $"employee_{id}.json"; + Assert.Contains(expectedFileName, fileList!); + + using var fileResponse = await storageClient.GetAsync($"/api/files/{expectedFileName}"); + fileResponse.EnsureSuccessStatusCode(); + var storedEmployee = JsonSerializer.Deserialize( + await fileResponse.Content.ReadAsStringAsync(), _jsonOptions); + + Assert.NotNull(storedEmployee); + Assert.Equal(id, storedEmployee!.Id); + Assert.True(results.TryGetValue(id, out var apiEmployee)); + Assert.Equivalent(apiEmployee, storedEmployee); + } + } + + /// + public async Task DisposeAsync() + { + if (_app is not null) + { + await _app.StopAsync(); + await _app.DisposeAsync(); + } + } +} \ No newline at end of file diff --git a/Client.Wasm/Client.Wasm.csproj b/Client.Wasm/Client.Wasm.csproj index 0ba9f90c..a221285d 100644 --- a/Client.Wasm/Client.Wasm.csproj +++ b/Client.Wasm/Client.Wasm.csproj @@ -13,7 +13,7 @@ - + diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 5ea88f0e..9cd21467 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,7 +4,7 @@ - Номер №2 "Балансировка нагрузки" + Номер №3 Интеграционное тестирование Вариант №40 "Сотрудник Компании" Выполнена Золотилов Никита 6513 Ссылка на форк diff --git a/CloudDevelopment.AppHost/AppHost.cs b/CloudDevelopment.AppHost/AppHost.cs index 3fd214fb..2ac18640 100644 --- a/CloudDevelopment.AppHost/AppHost.cs +++ b/CloudDevelopment.AppHost/AppHost.cs @@ -1,4 +1,5 @@ -using StackExchange.Redis; +using Amazon; +using Aspire.Hosting.LocalStack.Container; var builder = DistributedApplication.CreateBuilder(args); @@ -7,16 +8,46 @@ var gateway = builder.AddProject("api-gateway"); +var awsConfig = builder.AddAWSSDKConfig() + .WithProfile("default") + .WithRegion(RegionEndpoint.EUCentral1); + +var localStack = builder + .AddLocalStack("employee-localstack", awsConfig: awsConfig, configureContainer: container => + { + container.Lifetime = ContainerLifetime.Session; + container.DebugLevel = 1; + container.LogLevel = LocalStackLogLevel.Debug; + container.Port = 4566; + container.AdditionalEnvironmentVariables.Add("DEBUG", "1"); + }); + +var awsResources = builder.AddAWSCloudFormationTemplate("resources", "CloudFormation\\employee-template.yaml", "employee") + .WithReference(awsConfig); + +var minio = builder.AddMinioContainer("employee-minio"); + for (var i = 0; i < 5; i++) { var service = builder.AddProject($"service-api-{i}", launchProfileName: null) .WithHttpsEndpoint(port: 15000 + i) .WithReference(cache, "RedisCache") - .WaitFor(cache); + .WithReference(awsResources) + .WaitFor(cache) + .WaitFor(awsResources); gateway.WaitFor(service); } var client = builder.AddProject("employee") .WaitFor(gateway); +builder.AddProject("employee-sink") + .WithReference(awsResources) + .WithReference(minio) + .WithEnvironment("AWS__Resources__MinioBucketName", "employee-bucket") + .WaitFor(awsResources) + .WaitFor(minio); + +builder.UseLocalStack(localStack); + builder.Build().Run(); diff --git a/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj b/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj index 2dc9c221..16132bb7 100644 --- a/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj +++ b/CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj @@ -9,14 +9,27 @@ - - + + + + + + + + + + + + + Always + + diff --git a/CloudDevelopment.AppHost/CloudFormation/employee-template.yaml b/CloudDevelopment.AppHost/CloudFormation/employee-template.yaml new file mode 100644 index 00000000..685eb030 --- /dev/null +++ b/CloudDevelopment.AppHost/CloudFormation/employee-template.yaml @@ -0,0 +1,32 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: 'CloudFormation template for employee project' + +Parameters: + QueueName: + Type: String + Description: Name for the SQS queue + Default: 'employee-queue' + +Resources: + EmployeeQueue: + Type: AWS::SQS::Queue + Properties: + QueueName: !Ref QueueName + VisibilityTimeout: 30 + MessageRetentionPeriod: 345600 + DelaySeconds: 0 + ReceiveMessageWaitTimeSeconds: 0 + Tags: + - Key: Name + Value: !Ref QueueName + - Key: Environment + Value: LocalDev + +Outputs: + SQSQueueArn: + Description: ARN of the SQS queue + Value: !GetAtt EmployeeQueue.Arn + + SQSQueueUrl: + Description: URL of the SQS queue + Value: !Ref EmployeeQueue \ No newline at end of file diff --git a/CloudDevelopment.AppHost/appsettings.json b/CloudDevelopment.AppHost/appsettings.json index 31c092aa..a6b256bb 100644 --- a/CloudDevelopment.AppHost/appsettings.json +++ b/CloudDevelopment.AppHost/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning", "Aspire.Hosting.Dcp": "Warning" } + }, + "LocalStack": { + "UseLocalStack": true } } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index c5096709..b2787967 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -13,6 +13,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudDevelopment.ServiceDef EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.GateWay", "Api.GateWay\Api.GateWay.csproj", "{C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Event.Sink", "Event.Sink\Event.Sink.csproj", "{7FFF40C3-40F4-1344-C1E9-0979075A9AD3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Aspire.AppHost.Tests", "Aspire.AppHost.Tests\Aspire.AppHost.Tests.csproj", "{53974365-3678-FAB6-8CF8-A97B45620415}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -39,6 +43,14 @@ Global {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Debug|Any CPU.Build.0 = Debug|Any CPU {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Release|Any CPU.ActiveCfg = Release|Any CPU {C99E72F4-9BA7-7D56-C88E-FB28534EFCB6}.Release|Any CPU.Build.0 = Release|Any CPU + {7FFF40C3-40F4-1344-C1E9-0979075A9AD3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7FFF40C3-40F4-1344-C1E9-0979075A9AD3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7FFF40C3-40F4-1344-C1E9-0979075A9AD3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7FFF40C3-40F4-1344-C1E9-0979075A9AD3}.Release|Any CPU.Build.0 = Release|Any CPU + {53974365-3678-FAB6-8CF8-A97B45620415}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53974365-3678-FAB6-8CF8-A97B45620415}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53974365-3678-FAB6-8CF8-A97B45620415}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53974365-3678-FAB6-8CF8-A97B45620415}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Event.Sink/Controllers/S3StorageController.cs b/Event.Sink/Controllers/S3StorageController.cs new file mode 100644 index 00000000..a4af5ce1 --- /dev/null +++ b/Event.Sink/Controllers/S3StorageController.cs @@ -0,0 +1,69 @@ +using Event.Sink.Storage; +using Microsoft.AspNetCore.Mvc; +using System.Text; +using System.Text.Json.Nodes; + +namespace Event.Sink.Controllers; + +/// +/// Контроллер для взаимодействия с S3 +/// +/// Служба для работы с S3 +/// Логгер +[ApiController] +[Route("api/files")] +public class S3StorageController(IS3Service storageService, ILogger logger) : ControllerBase +{ + /// + /// Получает список хранящихся в S3 файлов + /// + /// Список ключей файлов + [HttpGet] + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(500)] + public async Task>> ListFiles() + { + logger.LogInformation("The {method} method of the {controller} controller has been called", nameof(ListFiles), nameof(S3StorageController)); + try + { + var list = await storageService.GetFileList(); + logger.LogInformation("Received a list of {count} files from the bucket", list.Count); + return Ok(list); + } + catch (Exception ex) + { + logger.LogError(ex, "Error when executing the {method} method of the {controller} controller", nameof(ListFiles), nameof(S3StorageController)); + return BadRequest(ex.Message); + } + } + + /// + /// Получает строковое представление хранящегося в S3 файла + /// + /// Ключ файла + /// Строковое представление файла + [HttpGet("{key}")] + [ProducesResponseType(typeof(JsonNode), 200)] + [ProducesResponseType(404)] + [ProducesResponseType(500)] + public async Task> GetFile(string key) + { + logger.LogInformation("The {method} method of the {controller} controller has been called", nameof(GetFile), nameof(S3StorageController)); + try + { + var node = await storageService.DownloadFile(key); + logger.LogInformation("Received JSON of {size} bytes", Encoding.UTF8.GetByteCount(node.ToJsonString())); + return Ok(node); + } + catch (InvalidOperationException ex) when (ex.Message.Contains("Error occurred downloading")) + { + logger.LogWarning(ex, "The {key} file was not found", key); + return NotFound($"File '{key}' not found"); + } + catch (Exception ex) + { + logger.LogError(ex, "Error downloading the {key} file", key); + return BadRequest(ex.Message); + } + } +} diff --git a/Event.Sink/Event.Sink.csproj b/Event.Sink/Event.Sink.csproj new file mode 100644 index 00000000..35e63896 --- /dev/null +++ b/Event.Sink/Event.Sink.csproj @@ -0,0 +1,24 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + + + + diff --git a/Event.Sink/Messaging/SQSConsumerService.cs b/Event.Sink/Messaging/SQSConsumerService.cs new file mode 100644 index 00000000..be01de01 --- /dev/null +++ b/Event.Sink/Messaging/SQSConsumerService.cs @@ -0,0 +1,106 @@ +using Amazon.SQS; +using Amazon.SQS.Model; +using Event.Sink.Storage; + +namespace Event.Sink.Messaging; + +/// +/// Клиентская служба для приема сррьщений из очереди SQS +/// +/// Клиент SQS +/// Фабрика контекста +/// Конфигурация +/// Логгер +public class SqsConsumerService(IAmazonSQS sqsClient, + IServiceScopeFactory scopeFactory, + IConfiguration configuration, + ILogger logger) : BackgroundService +{ + private readonly string _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] + ?? throw new KeyNotFoundException("SQS queue name was not found in configuration"); + + /// + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + logger.LogInformation("SQS consumer service started."); + + while (!stoppingToken.IsCancellationRequested) + { + var response = await sqsClient.ReceiveMessageAsync( + new ReceiveMessageRequest + { + QueueUrl = _queueUrl, + MaxNumberOfMessages = 10, + WaitTimeSeconds = 5 + }, stoppingToken); + + if (response?.Messages == null || response.Messages.Count == 0) + { + logger.LogWarning("Received null or empty from {queue}", _queueUrl); + continue; + } + + logger.LogInformation("Received {count} messages", response.Messages.Count); + + foreach (var message in response.Messages) + { + await ProcessMessageAsync(message, stoppingToken); + } + + logger.LogInformation("Batch of {count} messages processed", response.Messages.Count); + } + } + + /// + /// Сохраняет тело сообщения в объектное хранилище и удаляет сообщение из очереди + /// + /// Сообщение SQS + /// Токен отмены + /// + private async Task ProcessMessageAsync(Message message, CancellationToken stoppingToken) + { + try + { + logger.LogInformation("Processing message: {messageId}", message.MessageId); + + using var scope = scopeFactory.CreateScope(); + var s3Service = scope.ServiceProvider.GetRequiredService(); + var uploaded = await s3Service.UploadFile(message.Body); + + if (uploaded) + { + await sqsClient.DeleteMessageAsync(_queueUrl, message.ReceiptHandle, stoppingToken); + logger.LogInformation("Message {messageId} processed and deleted", message.MessageId); + } + else + { + logger.LogWarning("UploadFile returned false for message {messageId}, deleting to avoid loop", message.MessageId); + await SafeDeleteMessageAsync(message.ReceiptHandle, message.MessageId, stoppingToken); + } + } + catch (Exception ex) + { + logger.LogError(ex, "Error processing message: {messageId}", message.MessageId); + await SafeDeleteMessageAsync(message.ReceiptHandle, message.MessageId, stoppingToken); + } + } + + /// + /// Удаляет сообщение из очереди + /// + /// Идентификатор получения сообщения выдаваемый SQS при чтении + /// Идентификатор сообщения + /// Токен отмены + /// + private async Task SafeDeleteMessageAsync(string receiptHandle, string messageId, CancellationToken stoppingToken) + { + try + { + await sqsClient.DeleteMessageAsync(_queueUrl, receiptHandle, stoppingToken); + } + catch (Exception deleteEx) + { + logger.LogError(deleteEx, "Failed to delete message {messageId}", messageId); + } + } +} \ No newline at end of file diff --git a/Event.Sink/Program.cs b/Event.Sink/Program.cs new file mode 100644 index 00000000..631a9b9b --- /dev/null +++ b/Event.Sink/Program.cs @@ -0,0 +1,37 @@ +using Amazon.SQS; +using Event.Sink.Messaging; +using Event.Sink.Storage; +using LocalStack.Client.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddAwsService(); +builder.Services.AddHostedService(); + +builder.AddMinioClient("employee-minio"); +builder.Services.AddScoped(); + +var app = builder.Build(); + +using (var scope = app.Services.CreateScope()) +{ + var storage = scope.ServiceProvider.GetRequiredService(); + await storage.EnsureBucketExists(); +} + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.MapDefaultEndpoints(); +app.MapControllers(); +app.Run(); \ No newline at end of file diff --git a/Event.Sink/Properties/launchSettings.json b/Event.Sink/Properties/launchSettings.json new file mode 100644 index 00000000..5c34fedd --- /dev/null +++ b/Event.Sink/Properties/launchSettings.json @@ -0,0 +1,40 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:12660", + "sslPort": 44375 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5225", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7178;http://localhost:5225", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Event.Sink/Storage/IS3Service.cs b/Event.Sink/Storage/IS3Service.cs new file mode 100644 index 00000000..b53b4668 --- /dev/null +++ b/Event.Sink/Storage/IS3Service.cs @@ -0,0 +1,34 @@ +using System.Text.Json.Nodes; + +namespace Event.Sink.Storage; + +/// +/// Интерфейс службы для манипуляции файлами в объектном хранилище +/// +public interface IS3Service +{ + /// + /// Отправляет файл в хранилище + /// + /// Строковое представление сохраняемого файла + public Task UploadFile(string fileData); + + /// + /// Получает список всех файлов из хранилища + /// + /// Список путей к файлам + public Task> GetFileList(); + + /// + /// Получает строковое представление файла из хранилища + /// + /// Путь к файлу в бакете + /// Строковое представление прочтенного файла + public Task DownloadFile(string filePath); + + /// + /// Создает S3 бакет при необходимости + /// + /// + public Task EnsureBucketExists(); +} diff --git a/Event.Sink/Storage/S3MinioService.cs b/Event.Sink/Storage/S3MinioService.cs new file mode 100644 index 00000000..e2678c88 --- /dev/null +++ b/Event.Sink/Storage/S3MinioService.cs @@ -0,0 +1,142 @@ +using Minio; +using Minio.DataModel.Args; +using System.Net; +using System.Text; +using System.Text.Json.Nodes; + +namespace Event.Sink.Storage; + +/// +/// Служба для манипуляции файлами в объектном хранилище Minio +/// +/// Minio клиент +/// Конфигурация +/// Логгер +public class S3MinioService(IMinioClient client, IConfiguration configuration, ILogger logger) : IS3Service +{ + private readonly string _bucketName = configuration["AWS:Resources:MinioBucketName"] + ?? throw new KeyNotFoundException("Minio bucket name was not found in configuration"); + + /// + public async Task> GetFileList() + { + var list = new List(); + var request = new ListObjectsArgs() + .WithBucket(_bucketName) + .WithPrefix("") + .WithRecursive(true); + logger.LogInformation("Requesting a list of files in the bucket {bucket}", _bucketName); + var responseList = client.ListObjectsEnumAsync(request); + + await foreach (var item in responseList) + { + if (item != null) + list.Add(item.Key); + } + + if (list.Count == 0) + logger.LogWarning("The bucket {bucket} is empty or does not contain any objects", _bucketName); + else + logger.LogInformation("{count} files received from the {bucket} bucket", list.Count, _bucketName); + + return list; + } + + /// + public async Task UploadFile(string fileData) + { + var rootNode = JsonNode.Parse(fileData) ?? throw new ArgumentException("Passed string is not a valid JSON"); + var id = rootNode["id"]?.GetValue() ?? throw new ArgumentException("Passed JSON has invalid structure"); + + var bytes = Encoding.UTF8.GetBytes(fileData); + using var stream = new MemoryStream(bytes); + stream.Seek(0, SeekOrigin.Begin); + + logger.LogInformation("Starting uploading the employee {file} to the bucket {bucket}", id, _bucketName); + var request = new PutObjectArgs() + .WithBucket(_bucketName) + .WithStreamData(stream) + .WithObjectSize(bytes.Length) + .WithObject($"employee_{id}.json"); + + try + { + var response = await client.PutObjectAsync(request); + if (response.ResponseStatusCode == HttpStatusCode.OK) + { + logger.LogInformation("Employee {Id} successfully uploaded to bucket {bucket}", id, _bucketName); + return true; + } + + logger.LogError("Couldn't upload employee {Id}: status {statusCode}", id, response.ResponseStatusCode); + return false; + } + catch (Exception ex) + { + logger.LogError(ex, "Error when uploading an employee {Id}", id); + return false; + } + } + + /// + public async Task DownloadFile(string key) + { + logger.LogInformation("Starting downloading the {file} file from the {bucket} bucket", key, _bucketName); + + try + { + var memoryStream = new MemoryStream(); + + var request = new GetObjectArgs() + .WithBucket(_bucketName) + .WithObject(key) + .WithCallbackStream(async (stream, cancellationToken) => + { + await stream.CopyToAsync(memoryStream, cancellationToken); + memoryStream.Seek(0, SeekOrigin.Begin); + }); + + var response = await client.GetObjectAsync(request); + + if (response == null) + { + logger.LogError("Couldn't download the file {file}", key); + throw new InvalidOperationException($"Error occurred downloading {key}"); + } + using var reader = new StreamReader(memoryStream, Encoding.UTF8); + return JsonNode.Parse(reader.ReadToEnd()) ?? throw new InvalidOperationException("Downloaded document is not a valid JSON"); + } + catch (Exception ex) + { + logger.LogError(ex, "Error when downloading a file {file}", key); + throw; + } + } + + /// + public async Task EnsureBucketExists() + { + logger.LogInformation("Checking the existence of a bucket {bucket}", _bucketName); + try + { + var request = new BucketExistsArgs() + .WithBucket(_bucketName); + + var exists = await client.BucketExistsAsync(request); + if (!exists) + { + logger.LogInformation("Creating bucket {bucket}", _bucketName); + var createRequest = new MakeBucketArgs() + .WithBucket(_bucketName); + await client.MakeBucketAsync(createRequest); + return; + } + logger.LogInformation("Bucket {bucket} already exists", _bucketName); + } + catch (Exception ex) + { + logger.LogError(ex, "An unhandled error when checking a bucket {bucket}", _bucketName); + throw; + } + } +} \ No newline at end of file diff --git a/Event.Sink/appsettings.Development.json b/Event.Sink/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Event.Sink/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Event.Sink/appsettings.json b/Event.Sink/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Event.Sink/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Service.Api/Generator/EmployeeService.cs b/Service.Api/Generator/EmployeeService.cs index 22a940b0..8b86414d 100644 --- a/Service.Api/Generator/EmployeeService.cs +++ b/Service.Api/Generator/EmployeeService.cs @@ -1,5 +1,6 @@ using Service.Api.Caching; using Service.Api.Entities; +using Service.Api.Messaging; namespace Service.Api.Generator; @@ -8,7 +9,7 @@ namespace Service.Api.Generator; /// /// Кэш /// Логгер -public class EmployeeService(ICacheService cache, ILogger logger) : IEmployeeService +public class EmployeeService(ICacheService cache, ILogger logger, IProducerService messagingService) : IEmployeeService { /// public async Task ProcessEmployee(int id) @@ -26,6 +27,7 @@ public async Task ProcessEmployee(int id) logger.LogInformation("Cache MISS for employee {EmployeeId}. Generating new data.", id); employee = EmployeeGenerator.Generate(id); + await messagingService.SendMessage(employee); logger.LogInformation("Populating the cache with employee {id}", id); _ = Task.Run(() => cache.PopulateCache(employee)); diff --git a/Service.Api/Messaging/IProducerService.cs b/Service.Api/Messaging/IProducerService.cs new file mode 100644 index 00000000..2476c8cf --- /dev/null +++ b/Service.Api/Messaging/IProducerService.cs @@ -0,0 +1,15 @@ +using Service.Api.Entities; + +namespace Service.Api.Messaging; + +/// +/// Интерфейс для отправки генерируемых сотрудников в брокер сообщений +/// +public interface IProducerService +{ + /// + /// Отправляет сообщение в брокер + /// + /// Сотрудник компании + public Task SendMessage(Employee employee); +} diff --git a/Service.Api/Messaging/ProducerService.cs b/Service.Api/Messaging/ProducerService.cs new file mode 100644 index 00000000..6b367a01 --- /dev/null +++ b/Service.Api/Messaging/ProducerService.cs @@ -0,0 +1,58 @@ +using Amazon.SQS; +using Service.Api.Entities; +using System.Net; +using System.Text.Json; + +namespace Service.Api.Messaging; + +/// +/// Класс для отправки сообщений в брокер +/// +/// Клиент SQS +/// Конфигурация +/// Логгер +public class ProducerService(IAmazonSQS client, IConfiguration configuration, ILogger logger) : IProducerService +{ + private readonly string _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] + ?? throw new KeyNotFoundException("SQS queue link was not found in configuration"); + + private readonly int _maxRetries = 3; + private readonly TimeSpan _initialDelay = TimeSpan.FromSeconds(1); + + /// + public async Task SendMessage(Employee employee) + { + var delay = _initialDelay; + + for (var attempt = 1; attempt <= _maxRetries; attempt++) + { + try + { + var json = JsonSerializer.Serialize(employee); + var response = await client.SendMessageAsync(_queueUrl, json); + + if (response.HttpStatusCode == HttpStatusCode.OK) + { + logger.LogInformation("Employee {Id} sent to SQS queue (attempt {attempt})", employee.Id, attempt); + return; + } + + logger.LogWarning("Send returned {statusCode} on attempt {attempt}", response.HttpStatusCode, attempt); + } + catch (Exception ex) when (attempt < _maxRetries) + { + logger.LogWarning(ex, "Attempt {attempt} failed, retrying...", attempt); + } + + if (attempt < _maxRetries) + { + logger.LogDebug("Waiting {delay} before next retry", delay); + await Task.Delay(delay); + delay = delay * 2; + } + } + + logger.LogError("Failed to send employee {Id} after {maxRetries} attempts", employee.Id, _maxRetries); + } + +} diff --git a/Service.Api/Program.cs b/Service.Api/Program.cs index 4d8d140b..1b7eb4f6 100644 --- a/Service.Api/Program.cs +++ b/Service.Api/Program.cs @@ -1,5 +1,8 @@ +using Amazon.SQS; +using LocalStack.Client.Extensions; using Service.Api.Caching; using Service.Api.Generator; +using Service.Api.Messaging; var builder = WebApplication.CreateBuilder(args); @@ -9,6 +12,10 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddAwsService(); +builder.Services.AddScoped(); + var app = builder.Build(); app.MapDefaultEndpoints(); diff --git a/Service.Api/Service.Api.csproj b/Service.Api/Service.Api.csproj index 27fa3c85..eac317c4 100644 --- a/Service.Api/Service.Api.csproj +++ b/Service.Api/Service.Api.csproj @@ -9,6 +9,8 @@ + + From a87e36fb9c35c8b7d10ca258038661e09a330cf3 Mon Sep 17 00:00:00 2001 From: Chuck-man Date: Fri, 8 May 2026 23:48:05 +0400 Subject: [PATCH 6/7] =?UTF-8?q?=D0=A3=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B5=20=D0=BF=D0=B0=D0=BA?= =?UTF-8?q?=D0=B5=D1=82=D1=8B=20=D0=B8=20=D0=B7=D0=BB=D0=BE=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=83=D1=87=D0=BD=D1=8B=D0=B5=20=D1=81=D1=82=D1=80=D0=B5?= =?UTF-8?q?=D0=BB=D0=BA=D0=B8=20(=D0=BD=D0=B0=20=D0=B2=D1=81=D1=8F=D0=BA?= =?UTF-8?q?=D0=B8=D0=B9=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aspire.AppHost.Tests/IntegrationTests.cs | 2 +- Event.Sink/Event.Sink.csproj | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/Aspire.AppHost.Tests/IntegrationTests.cs b/Aspire.AppHost.Tests/IntegrationTests.cs index 8288cb0b..b4d224d4 100644 --- a/Aspire.AppHost.Tests/IntegrationTests.cs +++ b/Aspire.AppHost.Tests/IntegrationTests.cs @@ -11,7 +11,7 @@ namespace Aspire.AppHost.Tests; /// /// Интеграционные тесты для проверки микросервисного пайплайна: -/// API → SQS → Event.Sink → MinIO +/// API -> SQS -> Event.Sink -> MinIO /// /// Служба журналирования юнит-тестов public class IntegrationTests(ITestOutputHelper output) : IAsyncLifetime diff --git a/Event.Sink/Event.Sink.csproj b/Event.Sink/Event.Sink.csproj index 35e63896..4eb041a0 100644 --- a/Event.Sink/Event.Sink.csproj +++ b/Event.Sink/Event.Sink.csproj @@ -10,10 +10,6 @@ - - - - From 2b36b26f63d06f3dfc7708c891c4c4b6b02e0bd5 Mon Sep 17 00:00:00 2001 From: Chuck-man Date: Sat, 9 May 2026 00:09:31 +0400 Subject: [PATCH 7/7] =?UTF-8?q?=D0=97=D0=B0=D0=B1=D1=8B=D0=BB=20=D0=B5?= =?UTF-8?q?=D1=89=D1=91=20=D0=BE=D0=B4=D0=BD=D1=83=20"=E2=86=92"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Aspire.AppHost.Tests/IntegrationTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Aspire.AppHost.Tests/IntegrationTests.cs b/Aspire.AppHost.Tests/IntegrationTests.cs index b4d224d4..ef1aec67 100644 --- a/Aspire.AppHost.Tests/IntegrationTests.cs +++ b/Aspire.AppHost.Tests/IntegrationTests.cs @@ -41,7 +41,7 @@ public async Task InitializeAsync() } /// - /// Основной тест: запрос сотрудника через gateway → файл появляется в Minio, + /// Основной тест: запрос сотрудника через gateway -> файл появляется в Minio, /// данные в ответе API и в объектном хранилище совпадают. /// [Fact]