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..52adf782 --- /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..041c3a11 --- /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/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/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..fe762936 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №3 "Интеграционное тестирование" + Вариант №48 "SQS, Minio" + Выполнена Ненашевым Дмитрием 6513 + Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..7900af49 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "https://localhost:7270/employee" } diff --git a/Cloud.API/Cloud.API.csproj b/Cloud.API/Cloud.API.csproj new file mode 100644 index 00000000..624f25ab --- /dev/null +++ b/Cloud.API/Cloud.API.csproj @@ -0,0 +1,22 @@ + + + + net10.0 + enable + enable + + + + + + + + + + + + + + + + diff --git a/Cloud.API/Controllers/EmployeeController.cs b/Cloud.API/Controllers/EmployeeController.cs new file mode 100644 index 00000000..944868bb --- /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/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..89d309c3 --- /dev/null +++ b/Cloud.API/Messaging/SqsProducerService.cs @@ -0,0 +1,46 @@ +using System.Text.Json; +using Amazon.SQS; +using Amazon.SQS.Model; +using Cloud.Api.Models; + +namespace Cloud.Api.Messaging; + +/// +/// Сервис отправки сгенерированного сотрудника в очередь SQS +/// +/// Клиент SQS +/// Конфигурация приложения +/// Логгер +public class SqsProducerService( + IAmazonSQS sqsClient, + IConfiguration configuration, + ILogger logger + ) : IProducerService +{ + private readonly string _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) + { + 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/Models/Employee.cs b/Cloud.API/Models/Employee.cs new file mode 100644 index 00000000..9729e095 --- /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..3a791909 --- /dev/null +++ b/Cloud.API/Program.cs @@ -0,0 +1,35 @@ +using Amazon.SQS; +using Cloud.Api.Services; +using Cloud.Api.Messaging; +using LocalStack.Client.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("redis"); + +builder.Services.AddControllers(); +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); + +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + +builder.Services.AddLocalStack(builder.Configuration); +builder.Services.AddAwsService(); + +var app = builder.Build(); + +app.MapDefaultEndpoints(); + +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseHttpsRedirection(); +app.MapControllers(); + +app.Run(); \ No newline at end of file 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..c70933bb --- /dev/null +++ b/Cloud.API/Services/EmployeeGenerator.cs @@ -0,0 +1,58 @@ +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 static readonly string[] _professions = { "Developer", "Manager", "Analyst", "Designer", "QA" }; + + 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(); + 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(_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) => + { + var suffix = e.Position.Split(' ')[0]; + 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)) + .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..5ec9df02 --- /dev/null +++ b/Cloud.API/Services/EmployeeService.cs @@ -0,0 +1,89 @@ +using Cloud.Api.Messaging; +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, + IProducerService producer, + IDistributedCache cache, + IConfiguration configuration, + ILogger logger) : IEmployeeService +{ + 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 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 producer.SendMessage(employee); + 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..1e98d747 --- /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..ffa63cb0 --- /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..1a7f697b --- /dev/null +++ b/Cloud.API/appsettings.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "CacheKeyPrefix": "employee", + "CacheTtlMinutes": 15 +} diff --git a/Cloud.AppHost/AppHost.cs b/Cloud.AppHost/AppHost.cs new file mode 100644 index 00000000..f0802bfe --- /dev/null +++ b/Cloud.AppHost/AppHost.cs @@ -0,0 +1,57 @@ +using Amazon; +using Aspire.Hosting.LocalStack.Container; + +var builder = DistributedApplication.CreateBuilder(args); + +var redis = builder.AddRedis("redis") + .WithRedisInsight(containerName: "redis-insight"); + +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) + .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 new file mode 100644 index 00000000..4c84c41b --- /dev/null +++ b/Cloud.AppHost/Cloud.AppHost.csproj @@ -0,0 +1,32 @@ + + + + Exe + net10.0 + enable + enable + 8ba2b54b-4ef9-4e56-9f8f-d0397cc693b5 + + + + + + + + + + + + + + + + + + + + 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/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..a6b256bb --- /dev/null +++ b/Cloud.AppHost/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "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..34c5904d --- /dev/null +++ b/Cloud.EventSink/Controller/S3StorageController.cs @@ -0,0 +1,64 @@ +using Cloud.EventSink.S3; +using Microsoft.AspNetCore.Mvc; +using System.Text.Json.Nodes; + +namespace Cloud.EventSink.Controller; + +/// +/// Контроллер для получения списка файлов и скачивания файлов из объектного хранилища +/// +/// Сервис для работы с S3 хранилищем +/// Логгер +[ApiController] +[Route("api/s3")] +public class S3StorageController( + IS3Service s3Service, + ILogger logger + ) : ControllerBase +{ + /// + /// Метод получения списка названий всех файлов в 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..d159b11d --- /dev/null +++ b/Cloud.EventSink/Messaging/SqsConsumerService.cs @@ -0,0 +1,63 @@ +using Amazon.SQS; +using Amazon.SQS.Model; +using Cloud.EventSink.S3; + +namespace Cloud.EventSink.Messaging; + +/// +/// Фоновая служба, читающая SQS сообщения и сохраняющая их в S3. +/// +/// Клиент SQS +/// Фабрика scope для создания экземпляров сервисов на каждое сообщение +/// Конфигурация приложения +/// Логгер +public sealed class SqsConsumerService( + IAmazonSQS sqsClient, + IServiceScopeFactory scopeFactory, + IConfiguration configuration, + ILogger logger + ) : BackgroundService +{ + 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()) + { + 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..35eb8a39 --- /dev/null +++ b/Cloud.EventSink/S3/S3Service.cs @@ -0,0 +1,97 @@ +using System.Net; +using System.Text; +using System.Text.Json.Nodes; +using Minio; +using Minio.DataModel.Args; + +namespace Cloud.EventSink.S3; + +/// +/// Служба, реализующая интерфейс IS3Service для Minio +/// +/// Клиент Minio +/// Конфигурация приложения +/// Логгер +public class S3Service( + IMinioClient client, + IConfiguration configuration, + ILogger logger + ) : IS3Service +{ + 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); + 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.ServiceDefaults/Cloud.ServiceDefaults.csproj b/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj new file mode 100644 index 00000000..aafd57a7 --- /dev/null +++ b/Cloud.ServiceDefaults/Cloud.ServiceDefaults.csproj @@ -0,0 +1,23 @@ + + + + net10.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/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/IntegrationTests.cs b/Cloud.Tests/IntegrationTests.cs new file mode 100644 index 00000000..73e13c3b --- /dev/null +++ b/Cloud.Tests/IntegrationTests.cs @@ -0,0 +1,225 @@ +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(ITestOutputHelper output) : IAsyncLifetime +{ + private static readonly JsonSerializerOptions _jsonOptions = new() + { + PropertyNameCaseInsensitive = true + }; + + private DistributedApplication? _app; + private HttpClient? _gatewayClient; + private HttpClient? _s3Client; + + /// + 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.sln b/CloudDevelopment.sln deleted file mode 100644 index cb48241d..00000000 --- a/CloudDevelopment.sln +++ /dev/null @@ -1,25 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.14.36811.4 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Client.Wasm", "Client.Wasm\Client.Wasm.csproj", "{AE7EEA74-2FE0-136F-D797-854FD87E022A}" -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 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {90FE6B04-8381-437E-893A-FEBA1DA10AEE} - EndGlobalSection -EndGlobal diff --git a/CloudDevelopment.slnx b/CloudDevelopment.slnx new file mode 100644 index 00000000..d8dbf5e5 --- /dev/null +++ b/CloudDevelopment.slnx @@ -0,0 +1,9 @@ + + + + + + + + +