From 6526d7172fdb94b8ffe9a7f037a15d80b934482d Mon Sep 17 00:00:00 2001 From: neygenius Date: Wed, 6 May 2026 04:44:13 +0400 Subject: [PATCH 1/8] =?UTF-8?q?=D0=92=D1=81=D1=82=D0=B0=D0=BB=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=83=D1=82=D1=8C=20=D0=B8=D1=81=D1=82=D0=B8?= =?UTF-8?q?=D0=BD=D1=8B=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client.Wasm/Components/StudentCard.razor | 8 +- Client.Wasm/wwwroot/appsettings.json | 2 +- Cloud.API/Cloud.API.csproj | 19 +++ Cloud.API/Controllers/EmployeeController.cs | 39 ++++++ Cloud.API/Models/Employee.cs | 48 +++++++ Cloud.API/Program.cs | 41 ++++++ Cloud.API/Properties/launchSettings.json | 41 ++++++ Cloud.API/Services/EmployeeGenerator.cs | 63 +++++++++ Cloud.API/Services/EmployeeService.cs | 86 ++++++++++++ Cloud.API/Services/IEmployeeGenerator.cs | 15 +++ Cloud.API/Services/IEmployeeService.cs | 17 +++ Cloud.API/appsettings.Development.json | 8 ++ Cloud.API/appsettings.json | 10 ++ Cloud.AppHost/AppHost.cs | 13 ++ Cloud.AppHost/Cloud.AppHost.csproj | 21 +++ Cloud.AppHost/Properties/launchSettings.json | 31 +++++ Cloud.AppHost/appsettings.Development.json | 8 ++ Cloud.AppHost/appsettings.json | 9 ++ .../Cloud.ServiceDefaults.csproj | 23 ++++ Cloud.ServiceDefaults/Extensions.cs | 126 ++++++++++++++++++ CloudDevelopment.sln | 22 ++- 21 files changed, 643 insertions(+), 7 deletions(-) create mode 100644 Cloud.API/Cloud.API.csproj create mode 100644 Cloud.API/Controllers/EmployeeController.cs create mode 100644 Cloud.API/Models/Employee.cs create mode 100644 Cloud.API/Program.cs create mode 100644 Cloud.API/Properties/launchSettings.json create mode 100644 Cloud.API/Services/EmployeeGenerator.cs create mode 100644 Cloud.API/Services/EmployeeService.cs create mode 100644 Cloud.API/Services/IEmployeeGenerator.cs create mode 100644 Cloud.API/Services/IEmployeeService.cs create mode 100644 Cloud.API/appsettings.Development.json create mode 100644 Cloud.API/appsettings.json create mode 100644 Cloud.AppHost/AppHost.cs create mode 100644 Cloud.AppHost/Cloud.AppHost.csproj create mode 100644 Cloud.AppHost/Properties/launchSettings.json create mode 100644 Cloud.AppHost/appsettings.Development.json create mode 100644 Cloud.AppHost/appsettings.json create mode 100644 Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj create mode 100644 Cloud.ServiceDefaults/Extensions.cs diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..7061d426 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №1 "Кэширование" + Вариант №48 "Сотрудник компании" + Выполнена Ненашевым Дмитрием 6513 + Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..afe61cc6 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "https://localhost:7297/employee" } diff --git a/Cloud.API/Cloud.API.csproj b/Cloud.API/Cloud.API.csproj new file mode 100644 index 00000000..33d796a2 --- /dev/null +++ b/Cloud.API/Cloud.API.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + diff --git a/Cloud.API/Controllers/EmployeeController.cs b/Cloud.API/Controllers/EmployeeController.cs new file mode 100644 index 00000000..773370fd --- /dev/null +++ b/Cloud.API/Controllers/EmployeeController.cs @@ -0,0 +1,39 @@ +using Cloud.API.Services; +using Cloud.API.Models; +using Microsoft.AspNetCore.Mvc; + +namespace Cloud.API.Controllers; + +/// +/// Контроллер для получения сотрудника компании по id +/// +/// Сервис получения сотрудника компании +/// Логгер +[ApiController] +[Route("employee")] +public class EmployeesController( + IEmployeeService employeeService, + ILogger logger + ) : ControllerBase +{ + /// + /// Метод для получения сотрудника компании по id + /// + /// Идентификатор сотрудника + /// Информация о сотруднике компании + /// Успешное получение сотрудника компании + /// Некорректный id сотрудника + [ProducesResponseType(typeof(Employee), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] + [HttpGet] + public async Task> GetEmployee([FromQuery] int id) + { + if (id <= 0) + return BadRequest(new { error = "Id must be a positive number" }); + + logger.LogInformation("HTTP GET /employee, id: {employeeId}", id); + + var employee = await employeeService.GetOrGenerateAsync(id); + return Ok(employee); + } +} diff --git a/Cloud.API/Models/Employee.cs b/Cloud.API/Models/Employee.cs new file mode 100644 index 00000000..3fabc4d5 --- /dev/null +++ b/Cloud.API/Models/Employee.cs @@ -0,0 +1,48 @@ +namespace Cloud.API.Models; + +/// +/// Информация о сотруднике компании +/// +public class Employee +{ + /// + /// Идентификатор сотрудника в системе + /// + public required int Id { get; set; } + /// + /// ФИО + /// + public required string FullName { get; set; } + /// + /// Должность + /// + public required string Position { get; set; } + /// + /// Отдел + /// + public required string Department { get; set; } + /// + /// Дата приема + /// + public DateOnly HireDate { get; set; } + /// + /// Зарплата + /// + public required decimal Salary { get; set; } + /// + /// Электронная почта + /// + public required string Email { get; set; } + /// + /// Номер телефона + /// + public required string PhoneNumber { get; set; } + /// + /// Индикатор увольнения + /// + public required bool IsFired { get; set; } + /// + /// Дата увольнения + /// + public DateOnly? FiredDate { get; set; } +} diff --git a/Cloud.API/Program.cs b/Cloud.API/Program.cs new file mode 100644 index 00000000..5d35a555 --- /dev/null +++ b/Cloud.API/Program.cs @@ -0,0 +1,41 @@ +using Cloud.Api.Services; +using Cloud.API.Services; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("redis"); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddSingleton(); +builder.Services.AddScoped(); + +builder.Services.AddCors(options => +{ + options.AddPolicy("LocalPolicy", policy => + { + policy + .AllowAnyOrigin() + .WithHeaders("Content-Type") + .WithMethods("GET"); + }); +}); + +var app = builder.Build(); + +app.MapDefaultEndpoints(); + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); +app.UseCors("LocalPolicy"); +app.MapControllers(); + +app.Run(); diff --git a/Cloud.API/Properties/launchSettings.json b/Cloud.API/Properties/launchSettings.json new file mode 100644 index 00000000..5704cb0e --- /dev/null +++ b/Cloud.API/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:13373", + "sslPort": 44310 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5291", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7297;http://localhost:5291", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Cloud.API/Services/EmployeeGenerator.cs b/Cloud.API/Services/EmployeeGenerator.cs new file mode 100644 index 00000000..cf2de372 --- /dev/null +++ b/Cloud.API/Services/EmployeeGenerator.cs @@ -0,0 +1,63 @@ +using Bogus; +using Bogus.DataSets; +using Cloud.API.Models; +using Cloud.API.Services; + +namespace Cloud.Api.Services; + +/// +/// Генератор сотрудника по заданному id +/// +/// Логгер +public class EmployeeGenerator( + ILogger logger + ) : IEmployeeGenerator +{ + private readonly Faker _faker = new Faker("ru") + .RuleFor(e => e.Id, _ => 0) + .RuleFor(e => e.FullName, f => + { + var gender = f.PickRandom(); + var firstName = f.Name.FirstName(gender); + var lastName = f.Name.LastName(gender); + var patronymic = $"{f.Name.FirstName(Name.Gender.Male)}{(gender == Name.Gender.Male ? "ович" : "овна")}"; + return $"{lastName} {firstName} {patronymic}"; + }) + .RuleFor(e => e.Position, f => + { + var professions = new[] { "Developer", "Manager", "Analyst", "Designer", "QA" }; + var suffixes = new[] { "Junior", "Middle", "Senior", "Lead" }; + var suffix = f.PickRandom(suffixes); + var profession = f.PickRandom(professions); + return $"{suffix} {profession}"; + }) + .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) => + { + var suffix = e.Position.Split(' ')[0]; + decimal baseSalary = suffix switch + { + "Junior" => 50000, + "Middle" => 100000, + "Senior" => 150000, + "Lead" => 200000, + _ => 70000 + }; + return Math.Round(baseSalary + f.Random.Decimal(-5000, 25000), 2); + }) + .RuleFor(e => e.Email, (f, e) => f.Internet.Email(e.FullName)) + .RuleFor(e => e.PhoneNumber, f => f.Phone.PhoneNumber("+7(###)###-##-##")) + .RuleFor(e => e.IsFired, f => f.Random.Bool(0.2f)) + .RuleFor(e => e.FiredDate, (f, e) => + e.IsFired ? DateOnly.FromDateTime(f.Date.Between(e.HireDate.ToDateTime(TimeOnly.MinValue), DateTime.Now)) : null); + + /// + public Employee Generate(int id) + { + var employee = _faker.Generate(); + employee.Id = id; + logger.LogInformation("Generated employee with id {employeeId}", id); + return employee; + } +} \ No newline at end of file diff --git a/Cloud.API/Services/EmployeeService.cs b/Cloud.API/Services/EmployeeService.cs new file mode 100644 index 00000000..8532025b --- /dev/null +++ b/Cloud.API/Services/EmployeeService.cs @@ -0,0 +1,86 @@ +using Cloud.API.Models; +using Cloud.API.Services; +using Microsoft.Extensions.Caching.Distributed; +using System.Text.Json; + +namespace Cloud.Api.Services; + +/// +/// Сервис для получения сотрудника компании по id с кэшированием +/// +/// Генератор сотрудника +/// Сервис кэширования +/// Конфигурация приложения +/// Логгер +public class EmployeeService( + IEmployeeGenerator generator, + IDistributedCache cache, + IConfiguration configuration, + ILogger logger) : IEmployeeService +{ + private const string CacheKeyPrefix = "employee"; + private readonly TimeSpan _cacheTtl = TimeSpan.FromMinutes(configuration.GetValue("CacheTtlMinutes", 30)); + + /// + public async Task GetOrGenerateAsync(int id) + { + var cacheKey = $"{CacheKeyPrefix}:{id}"; + + var cached = await GetFromCache(cacheKey); + if (cached is not null) + { + return cached; + } + + logger.LogInformation("Cache miss for employee {Id}, generating new data", id); + var employee = generator.Generate(id); + await SetToCache(cacheKey, employee); + return employee; + } + + /// + /// Метод получения сотрудника из кэша + /// + /// Ключ кэша + /// + /// Сотрудник, или null в случае ошибки или отсутствия данных в кэше + /// + private async Task GetFromCache(string cacheKey) + { + try + { + var cached = await cache.GetStringAsync(cacheKey); + if (cached is null) return null; + + logger.LogInformation("Cache hit for key {CacheKey}", cacheKey); + return JsonSerializer.Deserialize(cached); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Failed to read from cache for key {CacheKey}", cacheKey); + return null; + } + } + + /// + /// Сохранение сотрудника в кэш с обработкой ошибок при записи + /// + /// Ключ кэша + /// Сотрудник компании + private async Task SetToCache(string cacheKey, Employee employee) + { + try + { + var json = JsonSerializer.Serialize(employee); + await cache.SetStringAsync(cacheKey, json, new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheTtl + }); + logger.LogInformation("Cached employee {Id} with key {CacheKey}", employee.Id, cacheKey); + } + catch (Exception ex) + { + logger.LogWarning(ex, "Failed to write to cache for key {CacheKey}", cacheKey); + } + } +} \ No newline at end of file diff --git a/Cloud.API/Services/IEmployeeGenerator.cs b/Cloud.API/Services/IEmployeeGenerator.cs new file mode 100644 index 00000000..d5dbae74 --- /dev/null +++ b/Cloud.API/Services/IEmployeeGenerator.cs @@ -0,0 +1,15 @@ +using Cloud.API.Models; + +namespace Cloud.API.Services; + +/// +/// Интерфейс генератора сотрудника компании по id +/// +public interface IEmployeeGenerator +{ + /// + /// Генерирует сотрудника компании с указанным id + /// + /// Идентификатор сотрудника компании + public Employee Generate(int id); +} \ No newline at end of file diff --git a/Cloud.API/Services/IEmployeeService.cs b/Cloud.API/Services/IEmployeeService.cs new file mode 100644 index 00000000..9c7c9ceb --- /dev/null +++ b/Cloud.API/Services/IEmployeeService.cs @@ -0,0 +1,17 @@ +using Cloud.API.Models; + +namespace Cloud.API.Services; + +/// +/// Интерфейс сервис для получения сотрудника компании по id с кэшированием +/// +public interface IEmployeeService +{ + /// + /// Получпет сотрудника компании по id. Если сотрудник не найден, + /// то создает нового с данным id и сохраняет его в кэше + /// + /// Идентификатор сотрудника компании + /// + public Task GetOrGenerateAsync(int id); +} diff --git a/Cloud.API/appsettings.Development.json b/Cloud.API/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Cloud.API/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Cloud.API/appsettings.json b/Cloud.API/appsettings.json new file mode 100644 index 00000000..2d053cce --- /dev/null +++ b/Cloud.API/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "CacheTtlMinutes": 15 +} diff --git a/Cloud.AppHost/AppHost.cs b/Cloud.AppHost/AppHost.cs new file mode 100644 index 00000000..7a7281a5 --- /dev/null +++ b/Cloud.AppHost/AppHost.cs @@ -0,0 +1,13 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var redis = builder.AddRedis("redis") + .WithRedisInsight(containerName: "redis-insight"); + +var api = builder.AddProject("api") + .WithReference(redis) + .WaitFor(redis); + +var client = builder.AddProject("client") + .WaitFor(api); + +builder.Build().Run(); diff --git a/Cloud.AppHost/Cloud.AppHost.csproj b/Cloud.AppHost/Cloud.AppHost.csproj new file mode 100644 index 00000000..d5af025a --- /dev/null +++ b/Cloud.AppHost/Cloud.AppHost.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + enable + enable + 8ba2b54b-4ef9-4e56-9f8f-d0397cc693b5 + + + + + + + + + + + + + diff --git a/Cloud.AppHost/Properties/launchSettings.json b/Cloud.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..c9c7dc2b --- /dev/null +++ b/Cloud.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:17080;http://localhost:15004", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21117", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23081", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22239" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15004", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19208", + "ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18038", + "ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20234" + } + } + } +} diff --git a/Cloud.AppHost/appsettings.Development.json b/Cloud.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Cloud.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Cloud.AppHost/appsettings.json b/Cloud.AppHost/appsettings.json new file mode 100644 index 00000000..31c092aa --- /dev/null +++ b/Cloud.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj b/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj new file mode 100644 index 00000000..c66ef377 --- /dev/null +++ b/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj @@ -0,0 +1,23 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + + diff --git a/Cloud.ServiceDefaults/Extensions.cs b/Cloud.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..f1b21c13 --- /dev/null +++ b/Cloud.ServiceDefaults/Extensions.cs @@ -0,0 +1,126 @@ +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..ffcdf540 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.11723.231 stable 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}") = "Cloud.ServiceDefaults", "Cloud.ServiceDefaults\Cloud.ServiceDefaults.csproj", "{3E5DBFF7-87A5-4A49-B972-0213375B5DBB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloud.API", "Cloud.API\Cloud.API.csproj", "{1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloud.AppHost", "Cloud.AppHost\Cloud.AppHost.csproj", "{111D7662-EE9A-4AEC-9299-16A0239072CB}" +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 + {3E5DBFF7-87A5-4A49-B972-0213375B5DBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E5DBFF7-87A5-4A49-B972-0213375B5DBB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E5DBFF7-87A5-4A49-B972-0213375B5DBB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E5DBFF7-87A5-4A49-B972-0213375B5DBB}.Release|Any CPU.Build.0 = Release|Any CPU + {1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}.Release|Any CPU.Build.0 = Release|Any CPU + {111D7662-EE9A-4AEC-9299-16A0239072CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {111D7662-EE9A-4AEC-9299-16A0239072CB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {111D7662-EE9A-4AEC-9299-16A0239072CB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {111D7662-EE9A-4AEC-9299-16A0239072CB}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE From 8f77cd5df6dc0d139dbe1ef28bd355816e07913d Mon Sep 17 00:00:00 2001 From: neygenius Date: Wed, 6 May 2026 16:20:53 +0400 Subject: [PATCH 2/8] =?UTF-8?q?=D0=B2=D0=BD=D0=B5=D1=81=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client.Wasm/Client.Wasm.csproj | 6 ++-- Cloud.API/Cloud.API.csproj | 2 +- Cloud.API/Services/EmployeeGenerator.cs | 36 +++++++++---------- Cloud.API/Services/EmployeeService.cs | 4 +-- Cloud.API/appsettings.json | 1 + Cloud.AppHost/Cloud.AppHost.csproj | 6 ++-- .../Cloud.ServiceDefaults.csproj | 4 +-- CloudDevelopment.slnx | 6 ++++ 8 files changed, 34 insertions(+), 31 deletions(-) create mode 100644 CloudDevelopment.slnx diff --git a/Client.Wasm/Client.Wasm.csproj b/Client.Wasm/Client.Wasm.csproj index 0ba9f90c..66634ea4 100644 --- a/Client.Wasm/Client.Wasm.csproj +++ b/Client.Wasm/Client.Wasm.csproj @@ -1,7 +1,7 @@  - net8.0 + net10.0 enable enable @@ -14,8 +14,8 @@ - - + + diff --git a/Cloud.API/Cloud.API.csproj b/Cloud.API/Cloud.API.csproj index 33d796a2..03262b1b 100644 --- a/Cloud.API/Cloud.API.csproj +++ b/Cloud.API/Cloud.API.csproj @@ -1,7 +1,7 @@ - net8.0 + net10.0 enable enable diff --git a/Cloud.API/Services/EmployeeGenerator.cs b/Cloud.API/Services/EmployeeGenerator.cs index cf2de372..bd969af2 100644 --- a/Cloud.API/Services/EmployeeGenerator.cs +++ b/Cloud.API/Services/EmployeeGenerator.cs @@ -13,37 +13,33 @@ public class EmployeeGenerator( ILogger logger ) : IEmployeeGenerator { + private static readonly string[] _professions = { "Developer", "Manager", "Analyst", "Designer", "QA" }; + private static readonly string[] _suffixes = { "Junior", "Middle", "Senior", "Lead" }; + + private static readonly Dictionary _baseSalaryBySuffix = new() + { + ["Junior"] = 50000, + ["Middle"] = 100000, + ["Senior"] = 150000, + ["Lead"] = 200000 + }; + private readonly Faker _faker = new Faker("ru") .RuleFor(e => e.Id, _ => 0) .RuleFor(e => e.FullName, f => { var gender = f.PickRandom(); - var firstName = f.Name.FirstName(gender); - var lastName = f.Name.LastName(gender); - var patronymic = $"{f.Name.FirstName(Name.Gender.Male)}{(gender == Name.Gender.Male ? "ович" : "овна")}"; - return $"{lastName} {firstName} {patronymic}"; - }) - .RuleFor(e => e.Position, f => - { - var professions = new[] { "Developer", "Manager", "Analyst", "Designer", "QA" }; - var suffixes = new[] { "Junior", "Middle", "Senior", "Lead" }; - var suffix = f.PickRandom(suffixes); - var profession = f.PickRandom(professions); - return $"{suffix} {profession}"; + return $"{f.Name.LastName(gender)} {f.Name.FirstName(gender)} " + + $"{f.Name.FirstName(Name.Gender.Male)}{(gender == Name.Gender.Male ? "ович" : "овна")}"; + }) + .RuleFor(e => e.Position, 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) => { var suffix = e.Position.Split(' ')[0]; - decimal baseSalary = suffix switch - { - "Junior" => 50000, - "Middle" => 100000, - "Senior" => 150000, - "Lead" => 200000, - _ => 70000 - }; + var baseSalary = _baseSalaryBySuffix.GetValueOrDefault(suffix, 70000); return Math.Round(baseSalary + f.Random.Decimal(-5000, 25000), 2); }) .RuleFor(e => e.Email, (f, e) => f.Internet.Email(e.FullName)) diff --git a/Cloud.API/Services/EmployeeService.cs b/Cloud.API/Services/EmployeeService.cs index 8532025b..fa47004a 100644 --- a/Cloud.API/Services/EmployeeService.cs +++ b/Cloud.API/Services/EmployeeService.cs @@ -18,13 +18,13 @@ public class EmployeeService( IConfiguration configuration, ILogger logger) : IEmployeeService { - private const string CacheKeyPrefix = "employee"; + private readonly string _cacheKeyPrefix = configuration.GetValue("CacheKeyPrefix", "employee"); private readonly TimeSpan _cacheTtl = TimeSpan.FromMinutes(configuration.GetValue("CacheTtlMinutes", 30)); /// public async Task GetOrGenerateAsync(int id) { - var cacheKey = $"{CacheKeyPrefix}:{id}"; + var cacheKey = $"{_cacheKeyPrefix}:{id}"; var cached = await GetFromCache(cacheKey); if (cached is not null) diff --git a/Cloud.API/appsettings.json b/Cloud.API/appsettings.json index 2d053cce..1a7f697b 100644 --- a/Cloud.API/appsettings.json +++ b/Cloud.API/appsettings.json @@ -6,5 +6,6 @@ } }, "AllowedHosts": "*", + "CacheKeyPrefix": "employee", "CacheTtlMinutes": 15 } diff --git a/Cloud.AppHost/Cloud.AppHost.csproj b/Cloud.AppHost/Cloud.AppHost.csproj index d5af025a..d6584c3c 100644 --- a/Cloud.AppHost/Cloud.AppHost.csproj +++ b/Cloud.AppHost/Cloud.AppHost.csproj @@ -2,15 +2,15 @@ Exe - net8.0 + net10.0 enable enable 8ba2b54b-4ef9-4e56-9f8f-d0397cc693b5 - - + + diff --git a/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj b/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj index c66ef377..aafd57a7 100644 --- a/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj +++ b/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj @@ -1,7 +1,7 @@ - + - net8.0 + net10.0 enable enable true diff --git a/CloudDevelopment.slnx b/CloudDevelopment.slnx new file mode 100644 index 00000000..2e43695a --- /dev/null +++ b/CloudDevelopment.slnx @@ -0,0 +1,6 @@ + + + + + + From e4e6fa8a7ab54d33e8a579c8c25f4e51789bcd3d Mon Sep 17 00:00:00 2001 From: neygenius Date: Wed, 6 May 2026 16:39:46 +0400 Subject: [PATCH 3/8] =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BF=D0=BE=D0=BB=D0=B8=D1=82=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud.API/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cloud.API/Program.cs b/Cloud.API/Program.cs index 5d35a555..28785b39 100644 --- a/Cloud.API/Program.cs +++ b/Cloud.API/Program.cs @@ -18,7 +18,7 @@ options.AddPolicy("LocalPolicy", policy => { policy - .AllowAnyOrigin() + .SetIsOriginAllowed(origin => origin.StartsWith("https://localhost")) .WithHeaders("Content-Type") .WithMethods("GET"); }); From 6bb756ab3ac1a0b38cf5a2e72694117cb556e210 Mon Sep 17 00:00:00 2001 From: neygenius Date: Wed, 6 May 2026 17:11:52 +0400 Subject: [PATCH 4/8] =?UTF-8?q?=D0=B8=D0=B7=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D1=81=D1=8F=20=D0=BE=D1=82=20=D0=BB=D0=B8=D1=88=D0=BD=D0=B5?= =?UTF-8?q?=D0=B3=D0=BE=20=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud.API/Services/EmployeeGenerator.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Cloud.API/Services/EmployeeGenerator.cs b/Cloud.API/Services/EmployeeGenerator.cs index bd969af2..6e2a2444 100644 --- a/Cloud.API/Services/EmployeeGenerator.cs +++ b/Cloud.API/Services/EmployeeGenerator.cs @@ -14,7 +14,6 @@ ILogger logger ) : IEmployeeGenerator { private static readonly string[] _professions = { "Developer", "Manager", "Analyst", "Designer", "QA" }; - private static readonly string[] _suffixes = { "Junior", "Middle", "Senior", "Lead" }; private static readonly Dictionary _baseSalaryBySuffix = new() { @@ -33,7 +32,7 @@ ILogger logger $"{f.Name.FirstName(Name.Gender.Male)}{(gender == Name.Gender.Male ? "ович" : "овна")}"; }) - .RuleFor(e => e.Position, f => $"{f.PickRandom(_suffixes)} {f.PickRandom(_professions)}") + .RuleFor(e => e.Position, f => $"{f.PickRandom(_baseSalaryBySuffix.Keys.ToArray())} {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) => From 58594c917934c271e3efdb2472e39262fba6fbd6 Mon Sep 17 00:00:00 2001 From: neygenius Date: Thu, 7 May 2026 08:53:00 +0400 Subject: [PATCH 5/8] =?UTF-8?q?=D0=B2=D1=82=D0=BE=D1=80=D0=B0=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API.Gateway/API.Gateway.csproj | 17 +++++++ API.Gateway/LoadBalancers/WeightedRandom.cs | 51 +++++++++++++++++++++ API.Gateway/Program.cs | 34 ++++++++++++++ API.Gateway/Properties/launchSettings.json | 23 ++++++++++ API.Gateway/appsettings.Development.json | 8 ++++ API.Gateway/appsettings.json | 10 ++++ API.Gateway/ocelot.json | 36 +++++++++++++++ Client.Wasm/Components/StudentCard.razor | 2 +- Client.Wasm/wwwroot/appsettings.json | 2 +- Cloud.API/Program.cs | 12 ----- Cloud.AppHost/AppHost.cs | 15 ++++-- Cloud.AppHost/Cloud.AppHost.csproj | 1 + CloudDevelopment.slnx | 1 + 13 files changed, 194 insertions(+), 18 deletions(-) create mode 100644 API.Gateway/API.Gateway.csproj create mode 100644 API.Gateway/LoadBalancers/WeightedRandom.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..eb6be8c3 --- /dev/null +++ b/API.Gateway/API.Gateway.csproj @@ -0,0 +1,17 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + diff --git a/API.Gateway/LoadBalancers/WeightedRandom.cs b/API.Gateway/LoadBalancers/WeightedRandom.cs new file mode 100644 index 00000000..768d40ba --- /dev/null +++ b/API.Gateway/LoadBalancers/WeightedRandom.cs @@ -0,0 +1,51 @@ +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Responses; +using Ocelot.Values; + +namespace API.Gateway.LoadBalancers; + +/// +/// Балансировщик нагрузки на основе взвешенного случайного выбора (Weighted Random). +/// Каждой реплике назначается вероятность выбора. При поступлении запроса реплика выбирается случайно с учётом заданных вероятностей +/// +/// Фабрика для получения списка доступных сервисов +/// Конфигурация приложения +public class WeightedRandomLoadBalancer(Func>> services, IConfiguration configuration) + : ILoadBalancer +{ + private readonly double[] _cumulativeWeights = BuildCumulativeWeights( + configuration.GetSection("WeightedRandomWeights").Get() ?? [0.4, 0.3, 0.15, 0.1, 0.05]); + + public string Type => nameof(WeightedRandomLoadBalancer); + + public async Task> LeaseAsync(HttpContext httpContext) + { + var availableServices = await services(); + + if (availableServices.Count == 0) + throw new InvalidOperationException("No available downstream services"); + + var index = Array.BinarySearch(_cumulativeWeights, Random.Shared.NextDouble()); + if (index < 0) index = ~index; + + return new OkResponse( + availableServices[Math.Min(index, availableServices.Count - 1)].HostAndPort); + } + + public void Release(ServiceHostAndPort hostAndPort) { } + + /// + /// Строит массив кумулятивных весов на основе входных весов + /// + /// Веса + /// Каждый элемент результирующего массива равен сумме всех предыдущих весов включительно + private static double[] BuildCumulativeWeights(double[] weights) + { + var total = weights.Sum(); + var cumulative = new double[weights.Length]; + cumulative[0] = weights[0] / total; + for (var i = 1; i < weights.Length; i++) + cumulative[i] = cumulative[i - 1] + weights[i] / total; + return cumulative; + } +} \ No newline at end of file diff --git a/API.Gateway/Program.cs b/API.Gateway/Program.cs new file mode 100644 index 00000000..d9c97999 --- /dev/null +++ b/API.Gateway/Program.cs @@ -0,0 +1,34 @@ +using API.Gateway.LoadBalancers; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; + +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddCors(options => +{ + options.AddPolicy("LocalPolicy", policy => + { + policy + .SetIsOriginAllowed(origin => origin.StartsWith("https://localhost")) + .WithHeaders("Content-Type") + .WithMethods("GET"); + }); +}); + +builder.Configuration + .AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); + +builder.Services + .AddOcelot(builder.Configuration) + .AddCustomLoadBalancer((sp, _, discoveryProvider) => + { + var configuration = sp.GetRequiredService(); + return new WeightedRandomLoadBalancer(discoveryProvider.GetAsync, configuration); + }); + +var app = builder.Build(); + +app.UseCors("LocalPolicy"); +await app.UseOcelot(); + +await app.RunAsync(); \ No newline at end of file diff --git a/API.Gateway/Properties/launchSettings.json b/API.Gateway/Properties/launchSettings.json new file mode 100644 index 00000000..6f57f554 --- /dev/null +++ b/API.Gateway/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5296", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7270;http://localhost:5296", + "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..30ce458b --- /dev/null +++ b/API.Gateway/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "WeightedRandomWeights": [ 0.30, 0.25, 0.20, 0.15, 0.10 ] +} diff --git a/API.Gateway/ocelot.json b/API.Gateway/ocelot.json new file mode 100644 index 00000000..d2709715 --- /dev/null +++ b/API.Gateway/ocelot.json @@ -0,0 +1,36 @@ +{ + "Routes": [ + { + "UpstreamPathTemplate": "/employee", + "UpstreamHttpMethod": [ "GET" ], + "DownstreamPathTemplate": "/employee", + "DownstreamScheme": "https", + "LoadBalancerOptions": { + "Type": "WeightedRandomLoadBalancer" + }, + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 8000 + }, + { + "Host": "localhost", + "Port": 8001 + }, + { + "Host": "localhost", + "Port": 8002 + }, + { + "Host": "localhost", + "Port": 8003 + }, + { + "Host": "localhost", + "Port": 8004 + } + ] + } + ] +} + diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 7061d426..4c1fa8c6 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,7 +4,7 @@ - Номер №1 "Кэширование" + Номер №2 "Балансировка нагрузки" Вариант №48 "Сотрудник компании" Выполнена Ненашевым Дмитрием 6513 Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index afe61cc6..7900af49 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "https://localhost:7297/employee" + "BaseAddress": "https://localhost:7270/employee" } diff --git a/Cloud.API/Program.cs b/Cloud.API/Program.cs index 28785b39..0b1310e7 100644 --- a/Cloud.API/Program.cs +++ b/Cloud.API/Program.cs @@ -13,17 +13,6 @@ builder.Services.AddSingleton(); builder.Services.AddScoped(); -builder.Services.AddCors(options => -{ - options.AddPolicy("LocalPolicy", policy => - { - policy - .SetIsOriginAllowed(origin => origin.StartsWith("https://localhost")) - .WithHeaders("Content-Type") - .WithMethods("GET"); - }); -}); - var app = builder.Build(); app.MapDefaultEndpoints(); @@ -35,7 +24,6 @@ } app.UseHttpsRedirection(); -app.UseCors("LocalPolicy"); app.MapControllers(); app.Run(); diff --git a/Cloud.AppHost/AppHost.cs b/Cloud.AppHost/AppHost.cs index 7a7281a5..eb5220b3 100644 --- a/Cloud.AppHost/AppHost.cs +++ b/Cloud.AppHost/AppHost.cs @@ -3,11 +3,18 @@ var redis = builder.AddRedis("redis") .WithRedisInsight(containerName: "redis-insight"); -var api = builder.AddProject("api") - .WithReference(redis) - .WaitFor(redis); +var gateway = builder.AddProject("api-gateway"); + +for (var i = 0; i < 5; i++) +{ + var api = builder.AddProject($"api-{i}", launchProfileName: null) + .WithHttpsEndpoint(port: 8000 + i) + .WithReference(redis) + .WaitFor(redis); + gateway.WaitFor(api); +} var client = builder.AddProject("client") - .WaitFor(api); + .WaitFor(gateway); builder.Build().Run(); diff --git a/Cloud.AppHost/Cloud.AppHost.csproj b/Cloud.AppHost/Cloud.AppHost.csproj index d6584c3c..426e6be0 100644 --- a/Cloud.AppHost/Cloud.AppHost.csproj +++ b/Cloud.AppHost/Cloud.AppHost.csproj @@ -14,6 +14,7 @@ + diff --git a/CloudDevelopment.slnx b/CloudDevelopment.slnx index 2e43695a..dda0c1c5 100644 --- a/CloudDevelopment.slnx +++ b/CloudDevelopment.slnx @@ -1,4 +1,5 @@ + From a1b7a3373377c15e57b1a82d21a39c4da72b793d Mon Sep 17 00:00:00 2001 From: neygenius Date: Thu, 7 May 2026 17:03:27 +0400 Subject: [PATCH 6/8] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API.Gateway/LoadBalancers/WeightedRandom.cs | 2 +- API.Gateway/Program.cs | 2 +- Cloud.API/Controllers/EmployeeController.cs | 6 +-- Cloud.API/Models/Employee.cs | 2 +- Cloud.API/Program.cs | 1 - Cloud.API/Services/EmployeeGenerator.cs | 4 +- Cloud.API/Services/EmployeeService.cs | 4 +- Cloud.API/Services/IEmployeeGenerator.cs | 4 +- Cloud.API/Services/IEmployeeService.cs | 4 +- CloudDevelopment.sln | 43 --------------------- 10 files changed, 14 insertions(+), 58 deletions(-) delete mode 100644 CloudDevelopment.sln diff --git a/API.Gateway/LoadBalancers/WeightedRandom.cs b/API.Gateway/LoadBalancers/WeightedRandom.cs index 768d40ba..52adf782 100644 --- a/API.Gateway/LoadBalancers/WeightedRandom.cs +++ b/API.Gateway/LoadBalancers/WeightedRandom.cs @@ -2,7 +2,7 @@ using Ocelot.Responses; using Ocelot.Values; -namespace API.Gateway.LoadBalancers; +namespace Api.Gateway.LoadBalancers; /// /// Балансировщик нагрузки на основе взвешенного случайного выбора (Weighted Random). diff --git a/API.Gateway/Program.cs b/API.Gateway/Program.cs index d9c97999..041c3a11 100644 --- a/API.Gateway/Program.cs +++ b/API.Gateway/Program.cs @@ -1,4 +1,4 @@ -using API.Gateway.LoadBalancers; +using Api.Gateway.LoadBalancers; using Ocelot.DependencyInjection; using Ocelot.Middleware; diff --git a/Cloud.API/Controllers/EmployeeController.cs b/Cloud.API/Controllers/EmployeeController.cs index 773370fd..944868bb 100644 --- a/Cloud.API/Controllers/EmployeeController.cs +++ b/Cloud.API/Controllers/EmployeeController.cs @@ -1,8 +1,8 @@ -using Cloud.API.Services; -using Cloud.API.Models; +using Cloud.Api.Services; +using Cloud.Api.Models; using Microsoft.AspNetCore.Mvc; -namespace Cloud.API.Controllers; +namespace Cloud.Api.Controllers; /// /// Контроллер для получения сотрудника компании по id diff --git a/Cloud.API/Models/Employee.cs b/Cloud.API/Models/Employee.cs index 3fabc4d5..9729e095 100644 --- a/Cloud.API/Models/Employee.cs +++ b/Cloud.API/Models/Employee.cs @@ -1,4 +1,4 @@ -namespace Cloud.API.Models; +namespace Cloud.Api.Models; /// /// Информация о сотруднике компании diff --git a/Cloud.API/Program.cs b/Cloud.API/Program.cs index 0b1310e7..146936c8 100644 --- a/Cloud.API/Program.cs +++ b/Cloud.API/Program.cs @@ -1,5 +1,4 @@ using Cloud.Api.Services; -using Cloud.API.Services; var builder = WebApplication.CreateBuilder(args); diff --git a/Cloud.API/Services/EmployeeGenerator.cs b/Cloud.API/Services/EmployeeGenerator.cs index 6e2a2444..c70933bb 100644 --- a/Cloud.API/Services/EmployeeGenerator.cs +++ b/Cloud.API/Services/EmployeeGenerator.cs @@ -1,7 +1,7 @@ using Bogus; using Bogus.DataSets; -using Cloud.API.Models; -using Cloud.API.Services; +using Cloud.Api.Models; +using Cloud.Api.Services; namespace Cloud.Api.Services; diff --git a/Cloud.API/Services/EmployeeService.cs b/Cloud.API/Services/EmployeeService.cs index fa47004a..5bd6cb2c 100644 --- a/Cloud.API/Services/EmployeeService.cs +++ b/Cloud.API/Services/EmployeeService.cs @@ -1,5 +1,5 @@ -using Cloud.API.Models; -using Cloud.API.Services; +using Cloud.Api.Models; +using Cloud.Api.Services; using Microsoft.Extensions.Caching.Distributed; using System.Text.Json; diff --git a/Cloud.API/Services/IEmployeeGenerator.cs b/Cloud.API/Services/IEmployeeGenerator.cs index d5dbae74..1e98d747 100644 --- a/Cloud.API/Services/IEmployeeGenerator.cs +++ b/Cloud.API/Services/IEmployeeGenerator.cs @@ -1,6 +1,6 @@ -using Cloud.API.Models; +using Cloud.Api.Models; -namespace Cloud.API.Services; +namespace Cloud.Api.Services; /// /// Интерфейс генератора сотрудника компании по id diff --git a/Cloud.API/Services/IEmployeeService.cs b/Cloud.API/Services/IEmployeeService.cs index 9c7c9ceb..ffa63cb0 100644 --- a/Cloud.API/Services/IEmployeeService.cs +++ b/Cloud.API/Services/IEmployeeService.cs @@ -1,6 +1,6 @@ -using Cloud.API.Models; +using Cloud.Api.Models; -namespace Cloud.API.Services; +namespace Cloud.Api.Services; /// /// Интерфейс сервис для получения сотрудника компании по id с кэшированием diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln deleted file mode 100644 index ffcdf540..00000000 --- a/CloudDevelopment.sln +++ /dev/null @@ -1,43 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 18 -VisualStudioVersion = 18.5.11723.231 stable -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}") = "Cloud.ServiceDefaults", "Cloud.ServiceDefaults\Cloud.ServiceDefaults.csproj", "{3E5DBFF7-87A5-4A49-B972-0213375B5DBB}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloud.API", "Cloud.API\Cloud.API.csproj", "{1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloud.AppHost", "Cloud.AppHost\Cloud.AppHost.csproj", "{111D7662-EE9A-4AEC-9299-16A0239072CB}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {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 - {3E5DBFF7-87A5-4A49-B972-0213375B5DBB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3E5DBFF7-87A5-4A49-B972-0213375B5DBB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3E5DBFF7-87A5-4A49-B972-0213375B5DBB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3E5DBFF7-87A5-4A49-B972-0213375B5DBB}.Release|Any CPU.Build.0 = Release|Any CPU - {1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1AB2AD5D-A163-E9E2-9F67-03C98B44D46C}.Release|Any CPU.Build.0 = Release|Any CPU - {111D7662-EE9A-4AEC-9299-16A0239072CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {111D7662-EE9A-4AEC-9299-16A0239072CB}.Debug|Any CPU.Build.0 = Debug|Any CPU - {111D7662-EE9A-4AEC-9299-16A0239072CB}.Release|Any CPU.ActiveCfg = Release|Any CPU - {111D7662-EE9A-4AEC-9299-16A0239072CB}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {90FE6B04-8381-437E-893A-FEBA1DA10AEE} - EndGlobalSection -EndGlobal From d42169715b8169327991e17515c7a60491fdc640 Mon Sep 17 00:00:00 2001 From: neygenius Date: Fri, 8 May 2026 06:27:02 +0400 Subject: [PATCH 7/8] =?UTF-8?q?=D1=82=D1=80=D0=B5=D1=82=D1=8C=D1=8F=20?= =?UTF-8?q?=D0=BB=D0=B0=D0=B1=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Client.Wasm/Components/StudentCard.razor | 4 +- Cloud.API/Cloud.API.csproj | 7 +- Cloud.API/Messaging/IProducerService.cs | 15 ++ Cloud.API/Messaging/SqsProducerService.cs | 47 ++++ Cloud.API/Program.cs | 11 +- Cloud.API/Services/EmployeeService.cs | 5 +- Cloud.AppHost/AppHost.cs | 39 ++- Cloud.AppHost/Cloud.AppHost.csproj | 14 +- .../employee-queue-template.yaml | 31 +++ Cloud.AppHost/appsettings.json | 3 + Cloud.EventSink/Cloud.EventSink.csproj | 21 ++ .../Controller/S3StorageController.cs | 68 +++++ .../Messaging/SqsConsumerService.cs | 69 ++++++ Cloud.EventSink/Program.cs | 38 +++ .../Properties/launchSettings.json | 23 ++ Cloud.EventSink/S3/IS3Service.cs | 30 +++ Cloud.EventSink/S3/S3Service.cs | 99 ++++++++ Cloud.EventSink/appsettings.Development.json | 8 + Cloud.EventSink/appsettings.json | 9 + Cloud.Tests/Cloud.Tests.csproj | 36 +++ Cloud.Tests/IntegrationTest1.cs | 232 ++++++++++++++++++ CloudDevelopment.slnx | 2 + 22 files changed, 801 insertions(+), 10 deletions(-) create mode 100644 Cloud.API/Messaging/IProducerService.cs create mode 100644 Cloud.API/Messaging/SqsProducerService.cs create mode 100644 Cloud.AppHost/CloudFormation/employee-queue-template.yaml create mode 100644 Cloud.EventSink/Cloud.EventSink.csproj create mode 100644 Cloud.EventSink/Controller/S3StorageController.cs create mode 100644 Cloud.EventSink/Messaging/SqsConsumerService.cs create mode 100644 Cloud.EventSink/Program.cs create mode 100644 Cloud.EventSink/Properties/launchSettings.json create mode 100644 Cloud.EventSink/S3/IS3Service.cs create mode 100644 Cloud.EventSink/S3/S3Service.cs create mode 100644 Cloud.EventSink/appsettings.Development.json create mode 100644 Cloud.EventSink/appsettings.json create mode 100644 Cloud.Tests/Cloud.Tests.csproj create mode 100644 Cloud.Tests/IntegrationTest1.cs diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 4c1fa8c6..fe762936 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,8 +4,8 @@ - Номер №2 "Балансировка нагрузки" - Вариант №48 "Сотрудник компании" + Номер №3 "Интеграционное тестирование" + Вариант №48 "SQS, Minio" Выполнена Ненашевым Дмитрием 6513 Ссылка на форк diff --git a/Cloud.API/Cloud.API.csproj b/Cloud.API/Cloud.API.csproj index 03262b1b..624f25ab 100644 --- a/Cloud.API/Cloud.API.csproj +++ b/Cloud.API/Cloud.API.csproj @@ -7,9 +7,12 @@ - + + - + + + diff --git a/Cloud.API/Messaging/IProducerService.cs b/Cloud.API/Messaging/IProducerService.cs new file mode 100644 index 00000000..0fc5c277 --- /dev/null +++ b/Cloud.API/Messaging/IProducerService.cs @@ -0,0 +1,15 @@ +using Cloud.Api.Models; + +namespace Cloud.Api.Messaging; + +/// +/// Интерфейс сервиса отправки сгенерированного сотрудника в очередь SQS +/// +public interface IProducerService +{ + /// + /// Метод отправки сообщения в брокер + /// + /// Информация о сотруднике + public Task SendMessage(Employee employee); +} diff --git a/Cloud.API/Messaging/SqsProducerService.cs b/Cloud.API/Messaging/SqsProducerService.cs new file mode 100644 index 00000000..46d643e7 --- /dev/null +++ b/Cloud.API/Messaging/SqsProducerService.cs @@ -0,0 +1,47 @@ +using System.Text.Json; +using Amazon.SQS; +using Amazon.SQS.Model; +using Cloud.Api.Models; + +namespace Cloud.Api.Messaging; + +/// +/// Сервис отправки сгенерированного сотрудника в очередь SQS +/// +public class SqsProducerService : IProducerService +{ + private readonly IAmazonSQS _sqsClient; + private readonly string _queueUrl; + private readonly ILogger _logger; + private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); + + public SqsProducerService(IAmazonSQS sqsClient, IConfiguration configuration, ILogger logger) + { + _sqsClient = sqsClient; + _logger = logger; + _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] + ?? throw new KeyNotFoundException("SQS queue URL not found in configuration."); + } + + /// + public async Task SendMessage(Employee employee) + { + var json = JsonSerializer.Serialize(employee, _jsonOptions); + var request = new SendMessageRequest + { + QueueUrl = _queueUrl, + MessageBody = json + }; + + try + { + var response = await _sqsClient.SendMessageAsync(request); + _logger.LogInformation("Sent message for Employee {Id}, MessageId {MessageId}", employee.Id, response.MessageId); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error sending message for Employee {Id}", employee.Id); + throw; + } + } +} diff --git a/Cloud.API/Program.cs b/Cloud.API/Program.cs index 146936c8..3a791909 100644 --- a/Cloud.API/Program.cs +++ b/Cloud.API/Program.cs @@ -1,4 +1,7 @@ +using Amazon.SQS; using Cloud.Api.Services; +using Cloud.Api.Messaging; +using LocalStack.Client.Extensions; var builder = WebApplication.CreateBuilder(args); @@ -10,7 +13,11 @@ builder.Services.AddSwaggerGen(); builder.Services.AddSingleton(); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddAwsService(); var app = builder.Build(); @@ -25,4 +32,4 @@ app.UseHttpsRedirection(); app.MapControllers(); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/Cloud.API/Services/EmployeeService.cs b/Cloud.API/Services/EmployeeService.cs index 5bd6cb2c..5ec9df02 100644 --- a/Cloud.API/Services/EmployeeService.cs +++ b/Cloud.API/Services/EmployeeService.cs @@ -1,4 +1,5 @@ -using Cloud.Api.Models; +using Cloud.Api.Messaging; +using Cloud.Api.Models; using Cloud.Api.Services; using Microsoft.Extensions.Caching.Distributed; using System.Text.Json; @@ -14,6 +15,7 @@ namespace Cloud.Api.Services; /// Логгер public class EmployeeService( IEmployeeGenerator generator, + IProducerService producer, IDistributedCache cache, IConfiguration configuration, ILogger logger) : IEmployeeService @@ -34,6 +36,7 @@ public async Task GetOrGenerateAsync(int id) logger.LogInformation("Cache miss for employee {Id}, generating new data", id); var employee = generator.Generate(id); + await producer.SendMessage(employee); await SetToCache(cacheKey, employee); return employee; } diff --git a/Cloud.AppHost/AppHost.cs b/Cloud.AppHost/AppHost.cs index eb5220b3..f0802bfe 100644 --- a/Cloud.AppHost/AppHost.cs +++ b/Cloud.AppHost/AppHost.cs @@ -1,3 +1,6 @@ +using Amazon; +using Aspire.Hosting.LocalStack.Container; + var builder = DistributedApplication.CreateBuilder(args); var redis = builder.AddRedis("redis") @@ -5,16 +8,50 @@ var gateway = builder.AddProject("api-gateway"); +var awsConfig = builder.AddAWSSDKConfig() + .WithProfile("default") + .WithRegion(RegionEndpoint.EUCentral1); + +var localstack = builder + .AddLocalStack("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-queue-template.yaml", "cloud-employee") + .WithReference(awsConfig); + for (var i = 0; i < 5; i++) { var api = builder.AddProject($"api-{i}", launchProfileName: null) .WithHttpsEndpoint(port: 8000 + i) .WithReference(redis) - .WaitFor(redis); + .WithReference(awsResources) + .WithEnvironment("Settings__MessageBroker", "SQS") + .WaitFor(redis) + .WaitFor(awsResources); gateway.WaitFor(api); } +var minio = builder.AddMinioContainer("minio"); + +var eventSink = builder.AddProject("event-sink") + .WithReference(awsResources) + .WithReference(minio) + .WithEnvironment("Settings__MessageBroker", "SQS") + .WithEnvironment("Settings__S3Hosting", "Minio") + .WithEnvironment("AWS__Resources__MinioBucketName", "cloud-employee-bucket") + .WaitFor(minio) + .WaitFor(awsResources); + var client = builder.AddProject("client") .WaitFor(gateway); +builder.UseLocalStack(localstack); + builder.Build().Run(); diff --git a/Cloud.AppHost/Cloud.AppHost.csproj b/Cloud.AppHost/Cloud.AppHost.csproj index 426e6be0..4c84c41b 100644 --- a/Cloud.AppHost/Cloud.AppHost.csproj +++ b/Cloud.AppHost/Cloud.AppHost.csproj @@ -9,14 +9,24 @@ - - + + + + + + + + + + + Always + diff --git a/Cloud.AppHost/CloudFormation/employee-queue-template.yaml b/Cloud.AppHost/CloudFormation/employee-queue-template.yaml new file mode 100644 index 00000000..d9e6d98d --- /dev/null +++ b/Cloud.AppHost/CloudFormation/employee-queue-template.yaml @@ -0,0 +1,31 @@ +AWSTemplateFormatVersion: '2010-09-09' + +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/Cloud.AppHost/appsettings.json b/Cloud.AppHost/appsettings.json index 31c092aa..a6b256bb 100644 --- a/Cloud.AppHost/appsettings.json +++ b/Cloud.AppHost/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning", "Aspire.Hosting.Dcp": "Warning" } + }, + "LocalStack": { + "UseLocalStack": true } } diff --git a/Cloud.EventSink/Cloud.EventSink.csproj b/Cloud.EventSink/Cloud.EventSink.csproj new file mode 100644 index 00000000..f1a0a06e --- /dev/null +++ b/Cloud.EventSink/Cloud.EventSink.csproj @@ -0,0 +1,21 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + + + + diff --git a/Cloud.EventSink/Controller/S3StorageController.cs b/Cloud.EventSink/Controller/S3StorageController.cs new file mode 100644 index 00000000..9f206085 --- /dev/null +++ b/Cloud.EventSink/Controller/S3StorageController.cs @@ -0,0 +1,68 @@ +using Cloud.EventSink.S3; +using Microsoft.AspNetCore.Mvc; +using System.Text.Json.Nodes; + +namespace Cloud.EventSink.Controller; + +/// +/// Контроллер для получения списка файлов и скачивания файлов из объектного хранилища +/// +[ApiController] +[Route("api/s3")] +public class S3StorageController : ControllerBase +{ + private readonly IS3Service _s3Service; + private readonly ILogger _logger; + + public S3StorageController(IS3Service s3Service, ILogger logger) + { + _s3Service = s3Service; + _logger = logger; + } + + /// + /// Метод получения списка названий всех файлов в S3 хранилище + /// + /// Успешное получение списка названий всех файлов + /// Ошибка чтения файлов в объектном хранилище + [ProducesResponseType(typeof(List), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] + [HttpGet] + public async Task>> ListFiles() + { + try + { + var files = await _s3Service.GetFileList(); + return Ok(files); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error listing files"); + return StatusCode(500, ex.Message); + } + } + + /// + /// Метод получения содержимого файла по его наазванию + /// + /// Название файла в хранилище + /// Строковое представление файла + /// Успешное получение файла + /// Файл не найден в объектном хранилище + [ProducesResponseType(typeof(List), StatusCodes.Status200OK)] + [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)] + [HttpGet("{key}")] + public async Task> GetFile(string key) + { + try + { + var node = await _s3Service.DownloadFile(key); + return Ok(node); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error downloading file {Key}", key); + return NotFound(ex.Message); + } + } +} \ No newline at end of file diff --git a/Cloud.EventSink/Messaging/SqsConsumerService.cs b/Cloud.EventSink/Messaging/SqsConsumerService.cs new file mode 100644 index 00000000..52c2623b --- /dev/null +++ b/Cloud.EventSink/Messaging/SqsConsumerService.cs @@ -0,0 +1,69 @@ +using Amazon.SQS; +using Amazon.SQS.Model; +using Cloud.EventSink.S3; + +namespace Cloud.EventSink.Messaging; + +/// +/// Фоновая служба, читающая SQS сообщения и сохраняющая их в S3. +/// +public sealed class SqsConsumerService : BackgroundService +{ + private readonly IAmazonSQS _sqsClient; + private readonly IServiceScopeFactory _scopeFactory; + private readonly string _queueUrl; + private readonly ILogger _logger; + + public SqsConsumerService( + IAmazonSQS sqsClient, + IServiceScopeFactory scopeFactory, + IConfiguration configuration, + ILogger logger) + { + _sqsClient = sqsClient; + _scopeFactory = scopeFactory; + _logger = logger; + _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] + ?? throw new KeyNotFoundException("SQS queue URL not found in configuration."); + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + using (var startupScope = _scopeFactory.CreateScope()) + { + var s3 = startupScope.ServiceProvider.GetRequiredService(); + await s3.EnsureBucketExists(); + } + + _logger.LogInformation("SQS consumer started, polling queue: {QueueUrl}", _queueUrl); + + while (!stoppingToken.IsCancellationRequested) + { + var response = await _sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest + { + QueueUrl = _queueUrl, + MaxNumberOfMessages = 10, + WaitTimeSeconds = 20 + }, stoppingToken); + + if (response?.Messages is null || response.Messages.Count == 0) + continue; + + foreach (var message in response.Messages) + { + try + { + using var scope = _scopeFactory.CreateScope(); + var s3 = scope.ServiceProvider.GetRequiredService(); + await s3.UploadFile(message.Body); + await _sqsClient.DeleteMessageAsync(_queueUrl, message.ReceiptHandle, stoppingToken); + _logger.LogInformation("Processed message {MessageId}", message.MessageId); + } + catch (Exception ex) + { + _logger.LogError(ex, "Failed to process message {MessageId}", message.MessageId); + } + } + } + } +} \ No newline at end of file diff --git a/Cloud.EventSink/Program.cs b/Cloud.EventSink/Program.cs new file mode 100644 index 00000000..36d18c07 --- /dev/null +++ b/Cloud.EventSink/Program.cs @@ -0,0 +1,38 @@ +using Amazon.SQS; +using Cloud.EventSink.Messaging; +using Cloud.EventSink.S3; +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.AddMinioClient("minio"); + +builder.Services.AddSingleton(); +builder.Services.AddHostedService(); + +var app = builder.Build(); + +using (var scope = app.Services.CreateScope()) +{ + var s3Service = scope.ServiceProvider.GetRequiredService(); + await s3Service.EnsureBucketExists(); +} + +app.MapDefaultEndpoints(); + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.MapControllers(); +app.Run(); \ No newline at end of file diff --git a/Cloud.EventSink/Properties/launchSettings.json b/Cloud.EventSink/Properties/launchSettings.json new file mode 100644 index 00000000..1c873b7e --- /dev/null +++ b/Cloud.EventSink/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5262", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7023;http://localhost:5262", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Cloud.EventSink/S3/IS3Service.cs b/Cloud.EventSink/S3/IS3Service.cs new file mode 100644 index 00000000..ec125e2b --- /dev/null +++ b/Cloud.EventSink/S3/IS3Service.cs @@ -0,0 +1,30 @@ +using System.Text.Json.Nodes; + +namespace Cloud.EventSink.S3; + +/// +/// Интерфейс службы для работы с файлами в объектном хранилище +/// +public interface IS3Service +{ + /// + /// Метода отправки файла в S3 хранилище + /// + /// Строковая репрезентация сохраняемого файла + public Task UploadFile(string fileData); + /// + /// Метода получения списка файлов из объектного хранилища + /// + /// Список путей к файлам + public Task> GetFileList(); + /// + /// Метода получения файла из объектного хранилища + /// + /// Путь к файлу в хранилище + /// Строковое представление файла + public Task DownloadFile(string filePath); + /// + /// Метод проверки существования S3 хранилища и создания его, при необходимости + /// + public Task EnsureBucketExists(); +} diff --git a/Cloud.EventSink/S3/S3Service.cs b/Cloud.EventSink/S3/S3Service.cs new file mode 100644 index 00000000..2dfae148 --- /dev/null +++ b/Cloud.EventSink/S3/S3Service.cs @@ -0,0 +1,99 @@ +using System.Net; +using System.Text; +using System.Text.Json.Nodes; +using Minio; +using Minio.DataModel.Args; + +namespace Cloud.EventSink.S3; + +/// +/// Служба, реализующая интерфейс IS3Service для Minio +/// +public class S3Service : IS3Service +{ + private readonly string _bucketName; + private readonly IMinioClient _client; + private readonly ILogger _logger; + + public S3Service(IMinioClient client, IConfiguration configuration, ILogger logger) + { + _client = client; + _logger = logger; + _bucketName = configuration["AWS:Resources:MinioBucketName"] + ?? throw new KeyNotFoundException("S3 bucket name not found in configuration"); + } + + /// + public async Task> GetFileList() + { + _logger.LogInformation("Listing files in bucket {BucketName}", _bucketName); + var request = new ListObjectsArgs().WithBucket(_bucketName).WithPrefix("").WithRecursive(true); + var items = _client.ListObjectsEnumAsync(request); + var list = new List(); + await foreach (var item in items) + list.Add(item.Key); + return list; + } + + /// + public async Task UploadFile(string fileData) + { + var rootNode = JsonNode.Parse(fileData) ?? throw new ArgumentException("Invalid JSON"); + var id = rootNode["id"]?.GetValue() ?? throw new ArgumentException("JSON must contain 'id'"); + + var bytes = Encoding.UTF8.GetBytes(fileData); + using var stream = new MemoryStream(bytes); + var putRequest = new PutObjectArgs() + .WithBucket(_bucketName) + .WithStreamData(stream) + .WithObjectSize(bytes.Length) + .WithObject($"cloud_employee_{id}.json"); + + _logger.LogInformation("Uploading employee {Id} to bucket {BucketName}", id, _bucketName); + var response = await _client.PutObjectAsync(putRequest); + if (response.ResponseStatusCode != HttpStatusCode.OK) + { + _logger.LogError("Upload failed for employee {Id}, status {StatusCode}", id, response.ResponseStatusCode); + return false; + } + _logger.LogInformation("Successfully uploaded employee {Id}", id); + return true; + } + + /// + public async Task DownloadFile(string filePath) + { + _logger.LogInformation("Downloading {FilePath} from {BucketName}", filePath, _bucketName); + var memoryStream = new MemoryStream(); + var getRequest = new GetObjectArgs() + .WithBucket(_bucketName) + .WithObject(filePath) + .WithCallbackStream(async (stream, ct) => + { + await stream.CopyToAsync(memoryStream, ct); + memoryStream.Seek(0, SeekOrigin.Begin); + }); + + await _client.GetObjectAsync(getRequest); + using var reader = new StreamReader(memoryStream, Encoding.UTF8); + var content = reader.ReadToEnd(); + return JsonNode.Parse(content) ?? throw new InvalidOperationException("Downloaded file is not valid JSON"); + } + + /// + public async Task EnsureBucketExists() + { + _logger.LogInformation("Checking bucket existence: {BucketName}", _bucketName); + var existsArgs = new BucketExistsArgs().WithBucket(_bucketName); + var exists = await _client.BucketExistsAsync(existsArgs); + if (!exists) + { + _logger.LogInformation("Creating bucket: {BucketName}", _bucketName); + await _client.MakeBucketAsync(new MakeBucketArgs().WithBucket(_bucketName)); + } + else + { + _logger.LogInformation("Bucket already exists: {BucketName}", _bucketName); + } + } +} \ No newline at end of file diff --git a/Cloud.EventSink/appsettings.Development.json b/Cloud.EventSink/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Cloud.EventSink/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Cloud.EventSink/appsettings.json b/Cloud.EventSink/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/Cloud.EventSink/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/Cloud.Tests/Cloud.Tests.csproj b/Cloud.Tests/Cloud.Tests.csproj new file mode 100644 index 00000000..0a4c9eaf --- /dev/null +++ b/Cloud.Tests/Cloud.Tests.csproj @@ -0,0 +1,36 @@ + + + + net10.0 + enable + enable + false + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Cloud.Tests/IntegrationTest1.cs b/Cloud.Tests/IntegrationTest1.cs new file mode 100644 index 00000000..2e0d3cf9 --- /dev/null +++ b/Cloud.Tests/IntegrationTest1.cs @@ -0,0 +1,232 @@ +using Amazon.Runtime; +using Amazon.SQS; +using Amazon.SQS.Model; +using Aspire.Hosting; +using Cloud.Api.Models; +using Microsoft.Extensions.Logging; +using Projects; +using System.Text.Json; + +namespace Cloud.Tests; + +/// +/// Интеграционные тесты для проверки микросервисного пайплайна +/// +public class IntegrationTests : IAsyncLifetime +{ + private static readonly JsonSerializerOptions _jsonOptions = new() + { + PropertyNameCaseInsensitive = true + }; + + private DistributedApplication? _app; + private HttpClient? _gatewayClient; + private HttpClient? _s3Client; + + private readonly ITestOutputHelper _output; + + public IntegrationTests(ITestOutputHelper output) + { + _output = output; + } + + /// + public async ValueTask InitializeAsync() + { + var cancellationToken = TestContext.Current.CancellationToken; + + 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); + + _gatewayClient = _app.CreateHttpClient("api-gateway", "http"); + _s3Client = _app.CreateHttpClient("event-sink", "http"); + } + + /// + public async ValueTask DisposeAsync() + { + if (_app != null) + { + await _app.StopAsync(); + await _app.DisposeAsync(); + } + } + + /// + /// Проверка всего пайплайна: запрос через апи гейтвей, генерация сотрудника, сохранение в кэш, + /// отправка в SQS и сохранение файла в Minio + /// + [Fact] + public async Task SuccessPipelineTest() + { + const int id = 42; + + var response = await _gatewayClient!.GetAsync($"/employee?id={id}", TestContext.Current.CancellationToken); + response.EnsureSuccessStatusCode(); + var generatedEmployee = JsonSerializer.Deserialize( + await response.Content.ReadAsStringAsync(TestContext.Current.CancellationToken), _jsonOptions); + + var s3Employee = await GetEmployeeFromS3(id); + + Assert.NotNull(generatedEmployee); + Assert.NotNull(s3Employee); + Assert.Equal(generatedEmployee!.Id, s3Employee!.Id); + Assert.Equal(generatedEmployee.FullName, s3Employee.FullName); + } + + /// + /// Проверка того, что запросы с некорректным id возвращают 400 Bad Request + /// + /// id сотрудника + [Theory] + [InlineData("-1")] + [InlineData("qwe")] + public async Task IncorrectEmployeeIdTest(string id) + { + var request = $"/employee?id={id}"; + var response = await _gatewayClient!.GetAsync(request, TestContext.Current.CancellationToken); + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + } + + /// + /// Проверка того, что повторный запрос с тем же id возвращает идентичного сотрудника из кэша + /// + [Fact] + public async Task CachingTest() + { + const int id = 99; + var firstResponse = await _gatewayClient!.GetAsync($"/employee?id={id}", TestContext.Current.CancellationToken); + var firstEmployee = JsonSerializer.Deserialize( + await firstResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken), _jsonOptions); + + var secondResponse = await _gatewayClient!.GetAsync($"/employee?id={id}", TestContext.Current.CancellationToken); + var secondEmployee = JsonSerializer.Deserialize( + await secondResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken), _jsonOptions); + + Assert.NotNull(firstEmployee); + Assert.NotNull(secondEmployee); + Assert.Equal(firstEmployee!.Id, secondEmployee!.Id); + Assert.Equal(firstEmployee.FullName, secondEmployee.FullName); + + var firstJson = JsonSerializer.Serialize(firstEmployee); + var secondJson = JsonSerializer.Serialize(secondEmployee); + Assert.Equal(firstJson, secondJson); + } + + /// + /// Проверка получения сотрудника из S3 + /// + /// id сотрудника + /// Информация о сотруднике компании + /// Выбрасывается, если сотрудник не найден в S3 + private async Task GetEmployeeFromS3(int id) + { + var endTime = DateTime.UtcNow + TimeSpan.FromSeconds(15); + var fileName = $"cloud_employee_{id}.json"; + + while (DateTime.UtcNow < endTime) + { + var fileResponse = await _s3Client!.GetAsync($"/api/s3/{fileName}", TestContext.Current.CancellationToken); + if (fileResponse.IsSuccessStatusCode) + { + var content = await fileResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken); + return JsonSerializer.Deserialize(content, _jsonOptions); + } + await Task.Delay(500, TestContext.Current.CancellationToken); + } + + throw new TimeoutException($"File with id {id} not found in S3 within timeout."); + } + + /// + /// Проверка доступности всех реплик при массовых запросах через апи гейтвей + /// + [Fact] + public async Task WeightedDistributionTest() + { + const int totalRequests = 200; + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator + }; + var clients = new List(); + + for (var i = 0; i < 5; i++) + { + var client = new HttpClient(httpClientHandler) + { + BaseAddress = new Uri($"https://localhost:{8000 + i}") + }; + clients.Add(client); + } + + var tasks = new List>(); + for (var i = 0; i < totalRequests; i++) + { + var id = Random.Shared.Next(1, 10000); + tasks.Add(_gatewayClient!.GetAsync($"/employee?id={id}", TestContext.Current.CancellationToken)); + } + + var responses = await Task.WhenAll(tasks); + + Assert.All(responses, r => Assert.Equal(HttpStatusCode.OK, r.StatusCode)); + + foreach (var client in clients) + { + var healthResponse = await client.GetAsync("/employee?id=1", TestContext.Current.CancellationToken); + Assert.Equal(HttpStatusCode.OK, healthResponse.StatusCode); + } + } + + /// + /// Проверка устойчивости EventSink к некорректным сообщениям в очереди SQS + /// + [Fact] + public async Task DeadLetterQueueTest() + { + var badJson = """{"invalid": "data", "missing": "Id field"}"""; + + var queueUrl = Environment.GetEnvironmentVariable("AWS__Resources__SQSQueueUrl") + ?? "http://sqs.eu-central-1.localhost:4566/000000000000/employee-queue"; + + var sqsClient = new AmazonSQSClient( + new BasicAWSCredentials("dummy", "dummy"), + new AmazonSQSConfig { ServiceURL = "http://localhost:4566" }); + + await sqsClient.SendMessageAsync(new SendMessageRequest + { + QueueUrl = queueUrl, + MessageBody = badJson + }); + + await Task.Delay(5000, TestContext.Current.CancellationToken); + + var healthResponse = await _s3Client!.GetAsync("/api/s3", TestContext.Current.CancellationToken); + Assert.True(healthResponse.IsSuccessStatusCode, "EventSink should still be running"); + + var filesResponse = await _s3Client.GetAsync("/api/s3", TestContext.Current.CancellationToken); + var files = JsonSerializer.Deserialize>( + await filesResponse.Content.ReadAsStringAsync(TestContext.Current.CancellationToken)); + + Assert.NotNull(files); + Assert.DoesNotContain(files, f => f.Contains("invalid")); + + var id = Random.Shared.Next(50000, 60000); + var response = await _gatewayClient!.GetAsync($"/employee?id={id}", TestContext.Current.CancellationToken); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var s3Employee = await GetEmployeeFromS3(id); + Assert.NotNull(s3Employee); + Assert.Equal(id, s3Employee!.Id); + } +} \ No newline at end of file diff --git a/CloudDevelopment.slnx b/CloudDevelopment.slnx index dda0c1c5..d8dbf5e5 100644 --- a/CloudDevelopment.slnx +++ b/CloudDevelopment.slnx @@ -3,5 +3,7 @@ + + From b80a28a33244ee42909799ad7347d867963931c2 Mon Sep 17 00:00:00 2001 From: neygenius Date: Fri, 8 May 2026 18:22:03 +0400 Subject: [PATCH 8/8] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud.API/Messaging/SqsProducerService.cs | 29 ++++++----- .../Controller/S3StorageController.cs | 24 ++++------ .../Messaging/SqsConsumerService.cs | 44 ++++++++--------- Cloud.EventSink/S3/S3Service.cs | 48 +++++++++---------- ...ntegrationTest1.cs => IntegrationTests.cs} | 11 +---- 5 files changed, 68 insertions(+), 88 deletions(-) rename Cloud.Tests/{IntegrationTest1.cs => IntegrationTests.cs} (97%) diff --git a/Cloud.API/Messaging/SqsProducerService.cs b/Cloud.API/Messaging/SqsProducerService.cs index 46d643e7..89d309c3 100644 --- a/Cloud.API/Messaging/SqsProducerService.cs +++ b/Cloud.API/Messaging/SqsProducerService.cs @@ -8,20 +8,19 @@ namespace Cloud.Api.Messaging; /// /// Сервис отправки сгенерированного сотрудника в очередь SQS /// -public class SqsProducerService : IProducerService +/// Клиент SQS +/// Конфигурация приложения +/// Логгер +public class SqsProducerService( + IAmazonSQS sqsClient, + IConfiguration configuration, + ILogger logger + ) : IProducerService { - private readonly IAmazonSQS _sqsClient; - private readonly string _queueUrl; - private readonly ILogger _logger; - private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); + private readonly string _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] + ?? throw new KeyNotFoundException("SQS queue URL not found in configuration."); - public SqsProducerService(IAmazonSQS sqsClient, IConfiguration configuration, ILogger logger) - { - _sqsClient = sqsClient; - _logger = logger; - _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] - ?? throw new KeyNotFoundException("SQS queue URL not found in configuration."); - } + private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); /// public async Task SendMessage(Employee employee) @@ -35,12 +34,12 @@ public async Task SendMessage(Employee employee) try { - var response = await _sqsClient.SendMessageAsync(request); - _logger.LogInformation("Sent message for Employee {Id}, MessageId {MessageId}", employee.Id, response.MessageId); + var response = await sqsClient.SendMessageAsync(request); + logger.LogInformation("Sent message for Employee {Id}, MessageId {MessageId}", employee.Id, response.MessageId); } catch (Exception ex) { - _logger.LogError(ex, "Error sending message for Employee {Id}", employee.Id); + logger.LogError(ex, "Error sending message for Employee {Id}", employee.Id); throw; } } diff --git a/Cloud.EventSink/Controller/S3StorageController.cs b/Cloud.EventSink/Controller/S3StorageController.cs index 9f206085..34c5904d 100644 --- a/Cloud.EventSink/Controller/S3StorageController.cs +++ b/Cloud.EventSink/Controller/S3StorageController.cs @@ -7,19 +7,15 @@ namespace Cloud.EventSink.Controller; /// /// Контроллер для получения списка файлов и скачивания файлов из объектного хранилища /// +/// Сервис для работы с S3 хранилищем +/// Логгер [ApiController] [Route("api/s3")] -public class S3StorageController : ControllerBase +public class S3StorageController( + IS3Service s3Service, + ILogger logger + ) : ControllerBase { - private readonly IS3Service _s3Service; - private readonly ILogger _logger; - - public S3StorageController(IS3Service s3Service, ILogger logger) - { - _s3Service = s3Service; - _logger = logger; - } - /// /// Метод получения списка названий всех файлов в S3 хранилище /// @@ -32,12 +28,12 @@ public async Task>> ListFiles() { try { - var files = await _s3Service.GetFileList(); + var files = await s3Service.GetFileList(); return Ok(files); } catch (Exception ex) { - _logger.LogError(ex, "Error listing files"); + logger.LogError(ex, "Error listing files"); return StatusCode(500, ex.Message); } } @@ -56,12 +52,12 @@ public async Task> GetFile(string key) { try { - var node = await _s3Service.DownloadFile(key); + var node = await s3Service.DownloadFile(key); return Ok(node); } catch (Exception ex) { - _logger.LogError(ex, "Error downloading file {Key}", key); + logger.LogError(ex, "Error downloading file {Key}", key); return NotFound(ex.Message); } } diff --git a/Cloud.EventSink/Messaging/SqsConsumerService.cs b/Cloud.EventSink/Messaging/SqsConsumerService.cs index 52c2623b..d159b11d 100644 --- a/Cloud.EventSink/Messaging/SqsConsumerService.cs +++ b/Cloud.EventSink/Messaging/SqsConsumerService.cs @@ -7,39 +7,33 @@ namespace Cloud.EventSink.Messaging; /// /// Фоновая служба, читающая SQS сообщения и сохраняющая их в S3. /// -public sealed class SqsConsumerService : BackgroundService +/// Клиент SQS +/// Фабрика scope для создания экземпляров сервисов на каждое сообщение +/// Конфигурация приложения +/// Логгер +public sealed class SqsConsumerService( + IAmazonSQS sqsClient, + IServiceScopeFactory scopeFactory, + IConfiguration configuration, + ILogger logger + ) : BackgroundService { - private readonly IAmazonSQS _sqsClient; - private readonly IServiceScopeFactory _scopeFactory; - private readonly string _queueUrl; - private readonly ILogger _logger; - - public SqsConsumerService( - IAmazonSQS sqsClient, - IServiceScopeFactory scopeFactory, - IConfiguration configuration, - ILogger logger) - { - _sqsClient = sqsClient; - _scopeFactory = scopeFactory; - _logger = logger; - _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] - ?? throw new KeyNotFoundException("SQS queue URL not found in configuration."); - } + private readonly string _queueUrl = configuration["AWS:Resources:SQSQueueUrl"] + ?? throw new KeyNotFoundException("SQS queue URL not found in configuration."); protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - using (var startupScope = _scopeFactory.CreateScope()) + using (var startupScope = scopeFactory.CreateScope()) { var s3 = startupScope.ServiceProvider.GetRequiredService(); await s3.EnsureBucketExists(); } - _logger.LogInformation("SQS consumer started, polling queue: {QueueUrl}", _queueUrl); + logger.LogInformation("SQS consumer started, polling queue: {QueueUrl}", _queueUrl); while (!stoppingToken.IsCancellationRequested) { - var response = await _sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest + var response = await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest { QueueUrl = _queueUrl, MaxNumberOfMessages = 10, @@ -53,15 +47,15 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { try { - using var scope = _scopeFactory.CreateScope(); + using var scope = scopeFactory.CreateScope(); var s3 = scope.ServiceProvider.GetRequiredService(); await s3.UploadFile(message.Body); - await _sqsClient.DeleteMessageAsync(_queueUrl, message.ReceiptHandle, stoppingToken); - _logger.LogInformation("Processed message {MessageId}", message.MessageId); + await sqsClient.DeleteMessageAsync(_queueUrl, message.ReceiptHandle, stoppingToken); + logger.LogInformation("Processed message {MessageId}", message.MessageId); } catch (Exception ex) { - _logger.LogError(ex, "Failed to process message {MessageId}", message.MessageId); + logger.LogError(ex, "Failed to process message {MessageId}", message.MessageId); } } } diff --git a/Cloud.EventSink/S3/S3Service.cs b/Cloud.EventSink/S3/S3Service.cs index 2dfae148..35eb8a39 100644 --- a/Cloud.EventSink/S3/S3Service.cs +++ b/Cloud.EventSink/S3/S3Service.cs @@ -9,26 +9,24 @@ namespace Cloud.EventSink.S3; /// /// Служба, реализующая интерфейс IS3Service для Minio /// -public class S3Service : IS3Service +/// Клиент Minio +/// Конфигурация приложения +/// Логгер +public class S3Service( + IMinioClient client, + IConfiguration configuration, + ILogger logger + ) : IS3Service { - private readonly string _bucketName; - private readonly IMinioClient _client; - private readonly ILogger _logger; - - public S3Service(IMinioClient client, IConfiguration configuration, ILogger logger) - { - _client = client; - _logger = logger; - _bucketName = configuration["AWS:Resources:MinioBucketName"] - ?? throw new KeyNotFoundException("S3 bucket name not found in configuration"); - } + private readonly string _bucketName = configuration["AWS:Resources:MinioBucketName"] + ?? throw new KeyNotFoundException("S3 bucket name not found in configuration"); /// public async Task> GetFileList() { - _logger.LogInformation("Listing files in bucket {BucketName}", _bucketName); + logger.LogInformation("Listing files in bucket {BucketName}", _bucketName); var request = new ListObjectsArgs().WithBucket(_bucketName).WithPrefix("").WithRecursive(true); - var items = _client.ListObjectsEnumAsync(request); + var items = client.ListObjectsEnumAsync(request); var list = new List(); await foreach (var item in items) list.Add(item.Key); @@ -49,21 +47,21 @@ public async Task UploadFile(string fileData) .WithObjectSize(bytes.Length) .WithObject($"cloud_employee_{id}.json"); - _logger.LogInformation("Uploading employee {Id} to bucket {BucketName}", id, _bucketName); - var response = await _client.PutObjectAsync(putRequest); + logger.LogInformation("Uploading employee {Id} to bucket {BucketName}", id, _bucketName); + var response = await client.PutObjectAsync(putRequest); if (response.ResponseStatusCode != HttpStatusCode.OK) { - _logger.LogError("Upload failed for employee {Id}, status {StatusCode}", id, response.ResponseStatusCode); + logger.LogError("Upload failed for employee {Id}, status {StatusCode}", id, response.ResponseStatusCode); return false; } - _logger.LogInformation("Successfully uploaded employee {Id}", id); + logger.LogInformation("Successfully uploaded employee {Id}", id); return true; } /// public async Task DownloadFile(string filePath) { - _logger.LogInformation("Downloading {FilePath} from {BucketName}", filePath, _bucketName); + logger.LogInformation("Downloading {FilePath} from {BucketName}", filePath, _bucketName); var memoryStream = new MemoryStream(); var getRequest = new GetObjectArgs() .WithBucket(_bucketName) @@ -74,7 +72,7 @@ public async Task DownloadFile(string filePath) memoryStream.Seek(0, SeekOrigin.Begin); }); - await _client.GetObjectAsync(getRequest); + await client.GetObjectAsync(getRequest); using var reader = new StreamReader(memoryStream, Encoding.UTF8); var content = reader.ReadToEnd(); return JsonNode.Parse(content) ?? throw new InvalidOperationException("Downloaded file is not valid JSON"); @@ -83,17 +81,17 @@ public async Task DownloadFile(string filePath) /// public async Task EnsureBucketExists() { - _logger.LogInformation("Checking bucket existence: {BucketName}", _bucketName); + logger.LogInformation("Checking bucket existence: {BucketName}", _bucketName); var existsArgs = new BucketExistsArgs().WithBucket(_bucketName); - var exists = await _client.BucketExistsAsync(existsArgs); + var exists = await client.BucketExistsAsync(existsArgs); if (!exists) { - _logger.LogInformation("Creating bucket: {BucketName}", _bucketName); - await _client.MakeBucketAsync(new MakeBucketArgs().WithBucket(_bucketName)); + logger.LogInformation("Creating bucket: {BucketName}", _bucketName); + await client.MakeBucketAsync(new MakeBucketArgs().WithBucket(_bucketName)); } else { - _logger.LogInformation("Bucket already exists: {BucketName}", _bucketName); + logger.LogInformation("Bucket already exists: {BucketName}", _bucketName); } } } \ No newline at end of file diff --git a/Cloud.Tests/IntegrationTest1.cs b/Cloud.Tests/IntegrationTests.cs similarity index 97% rename from Cloud.Tests/IntegrationTest1.cs rename to Cloud.Tests/IntegrationTests.cs index 2e0d3cf9..73e13c3b 100644 --- a/Cloud.Tests/IntegrationTest1.cs +++ b/Cloud.Tests/IntegrationTests.cs @@ -12,7 +12,7 @@ namespace Cloud.Tests; /// /// Интеграционные тесты для проверки микросервисного пайплайна /// -public class IntegrationTests : IAsyncLifetime +public class IntegrationTests(ITestOutputHelper output) : IAsyncLifetime { private static readonly JsonSerializerOptions _jsonOptions = new() { @@ -23,13 +23,6 @@ public class IntegrationTests : IAsyncLifetime private HttpClient? _gatewayClient; private HttpClient? _s3Client; - private readonly ITestOutputHelper _output; - - public IntegrationTests(ITestOutputHelper output) - { - _output = output; - } - /// public async ValueTask InitializeAsync() { @@ -39,7 +32,7 @@ public async ValueTask InitializeAsync() builder.Configuration["DcpPublisher:RandomizePorts"] = "false"; builder.Services.AddLogging(logging => { - logging.AddXUnit(_output); + logging.AddXUnit(output); logging.SetMinimumLevel(LogLevel.Debug); logging.AddFilter("Aspire.Hosting.Dcp", LogLevel.Debug); logging.AddFilter("Aspire.Hosting", LogLevel.Debug);