diff --git a/.gitignore b/.gitignore index ce892922..bdb5e93d 100644 --- a/.gitignore +++ b/.gitignore @@ -416,3 +416,9 @@ FodyWeavers.xsd *.msix *.msm *.msp + +/minio-data +/CompanyEmployees.AppHost/minio-data/.minio.sys/tmp/808628fb-0b20-4cea-9ca9-1b28fea8d489 +/CompanyEmployees.AppHost/minio-data/.minio.sys/tmp/05678231-c811-49c5-9e05-55188d7b2e92 +/CompanyEmployees.AppHost/minio-data/.minio.sys/tmp +/CompanyEmployees.AppHost/minio-data/.minio.sys/buckets/company-employee/.usage-cache.bin/e1e86355-5b8e-4d71-b0b2-a9cc2939b942 diff --git a/Client.Wasm/Client.Wasm.csproj b/Client.Wasm/Client.Wasm.csproj index 0ba9f90c..0c323bf4 100644 --- a/Client.Wasm/Client.Wasm.csproj +++ b/Client.Wasm/Client.Wasm.csproj @@ -4,6 +4,7 @@ net8.0 enable enable + Debug;Release;123 diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..cfde0e65 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,12 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №2 "Балансировка нагрузки" + + Вариант №8 "Сотрудник компании" + + Выполнена Земляновым Владимиром 6511 + Ссылка на форк diff --git a/Client.Wasm/Properties/launchSettings.json b/Client.Wasm/Properties/launchSettings.json index 0d824ea7..6ad691ad 100644 --- a/Client.Wasm/Properties/launchSettings.json +++ b/Client.Wasm/Properties/launchSettings.json @@ -12,9 +12,9 @@ "http": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "http://localhost:5127", + "applicationUrl": "http://localhost:5128", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -22,20 +22,20 @@ "https": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "https://localhost:7282;http://localhost:5127", + "applicationUrl": "https://localhost:7283;http://localhost:5128", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "IIS Express": { "commandName": "IISExpress", - "launchBrowser": true, + "launchBrowser": false, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } -} +} \ No newline at end of file diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..e8ce9103 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" -} + "BaseAddress": "http://localhost:5200/employee" +} \ No newline at end of file diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..aa94057e 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -5,16 +5,67 @@ 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 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployees.AppHost", "CompanyEmployees.AppHost\CompanyEmployees.AppHost.csproj", "{96FC536F-9C20-45C4-B624-F3E2DEE96BA3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployees.Generator", "CompanyEmployees.Generator\CompanyEmployees.Generator.csproj", "{418E6A76-7DE1-04F1-F18A-9A7C9ED44857}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployees.ServiceDefaults", "CompanyEmployees.ServiceDefaults\CompanyEmployees.ServiceDefaults.csproj", "{53C4EDF4-E3C2-A484-171F-78A5FD7190F6}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployees.ApiGateway", "CompanyEmployees.ApiGateway\CompanyEmployees.ApiGateway.csproj", "{A1370E88-073B-C8F9-1C33-D962F1D383BD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployees.FileService", "CompanyEmployees.FileService\CompanyEmployees.FileService.csproj", "{079C1DF9-133E-FD81-C1FA-1FABEBFB1101}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployees.Tests", "CompanyEmployee.Tests\CompanyEmployees.Tests.csproj", "{F9C9F199-85EA-B6C8-1B29-47442974A133}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution + 123|Any CPU = 123|Any CPU Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AE7EEA74-2FE0-136F-D797-854FD87E022A}.123|Any CPU.ActiveCfg = 123|Any CPU + {AE7EEA74-2FE0-136F-D797-854FD87E022A}.123|Any CPU.Build.0 = 123|Any CPU {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 + {96FC536F-9C20-45C4-B624-F3E2DEE96BA3}.123|Any CPU.ActiveCfg = 123|Any CPU + {96FC536F-9C20-45C4-B624-F3E2DEE96BA3}.123|Any CPU.Build.0 = 123|Any CPU + {96FC536F-9C20-45C4-B624-F3E2DEE96BA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {96FC536F-9C20-45C4-B624-F3E2DEE96BA3}.Debug|Any CPU.Build.0 = Debug|Any CPU + {96FC536F-9C20-45C4-B624-F3E2DEE96BA3}.Release|Any CPU.ActiveCfg = Release|Any CPU + {96FC536F-9C20-45C4-B624-F3E2DEE96BA3}.Release|Any CPU.Build.0 = Release|Any CPU + {418E6A76-7DE1-04F1-F18A-9A7C9ED44857}.123|Any CPU.ActiveCfg = Release|Any CPU + {418E6A76-7DE1-04F1-F18A-9A7C9ED44857}.123|Any CPU.Build.0 = Release|Any CPU + {418E6A76-7DE1-04F1-F18A-9A7C9ED44857}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {418E6A76-7DE1-04F1-F18A-9A7C9ED44857}.Debug|Any CPU.Build.0 = Debug|Any CPU + {418E6A76-7DE1-04F1-F18A-9A7C9ED44857}.Release|Any CPU.ActiveCfg = Release|Any CPU + {418E6A76-7DE1-04F1-F18A-9A7C9ED44857}.Release|Any CPU.Build.0 = Release|Any CPU + {53C4EDF4-E3C2-A484-171F-78A5FD7190F6}.123|Any CPU.ActiveCfg = 123|Any CPU + {53C4EDF4-E3C2-A484-171F-78A5FD7190F6}.123|Any CPU.Build.0 = 123|Any CPU + {53C4EDF4-E3C2-A484-171F-78A5FD7190F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53C4EDF4-E3C2-A484-171F-78A5FD7190F6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53C4EDF4-E3C2-A484-171F-78A5FD7190F6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53C4EDF4-E3C2-A484-171F-78A5FD7190F6}.Release|Any CPU.Build.0 = Release|Any CPU + {A1370E88-073B-C8F9-1C33-D962F1D383BD}.123|Any CPU.ActiveCfg = Debug|Any CPU + {A1370E88-073B-C8F9-1C33-D962F1D383BD}.123|Any CPU.Build.0 = Debug|Any CPU + {A1370E88-073B-C8F9-1C33-D962F1D383BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A1370E88-073B-C8F9-1C33-D962F1D383BD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A1370E88-073B-C8F9-1C33-D962F1D383BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A1370E88-073B-C8F9-1C33-D962F1D383BD}.Release|Any CPU.Build.0 = Release|Any CPU + {079C1DF9-133E-FD81-C1FA-1FABEBFB1101}.123|Any CPU.ActiveCfg = Debug|Any CPU + {079C1DF9-133E-FD81-C1FA-1FABEBFB1101}.123|Any CPU.Build.0 = Debug|Any CPU + {079C1DF9-133E-FD81-C1FA-1FABEBFB1101}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {079C1DF9-133E-FD81-C1FA-1FABEBFB1101}.Debug|Any CPU.Build.0 = Debug|Any CPU + {079C1DF9-133E-FD81-C1FA-1FABEBFB1101}.Release|Any CPU.ActiveCfg = Release|Any CPU + {079C1DF9-133E-FD81-C1FA-1FABEBFB1101}.Release|Any CPU.Build.0 = Release|Any CPU + {F9C9F199-85EA-B6C8-1B29-47442974A133}.123|Any CPU.ActiveCfg = Debug|Any CPU + {F9C9F199-85EA-B6C8-1B29-47442974A133}.123|Any CPU.Build.0 = Debug|Any CPU + {F9C9F199-85EA-B6C8-1B29-47442974A133}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F9C9F199-85EA-B6C8-1B29-47442974A133}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F9C9F199-85EA-B6C8-1B29-47442974A133}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F9C9F199-85EA-B6C8-1B29-47442974A133}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/CompanyEmployee.Tests/CompanyEmployees.Tests.csproj b/CompanyEmployee.Tests/CompanyEmployees.Tests.csproj new file mode 100644 index 00000000..ec17788a --- /dev/null +++ b/CompanyEmployee.Tests/CompanyEmployees.Tests.csproj @@ -0,0 +1,26 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + + + + + + diff --git a/CompanyEmployee.Tests/Fixture.cs b/CompanyEmployee.Tests/Fixture.cs new file mode 100644 index 00000000..bf2ae34a --- /dev/null +++ b/CompanyEmployee.Tests/Fixture.cs @@ -0,0 +1,121 @@ +using Amazon.Runtime; +using Amazon.S3; +using Amazon.S3.Model; +using Aspire.Hosting; +using Aspire.Hosting.ApplicationModel; +using Aspire.Hosting.Testing; +using Xunit; + +namespace CompanyEmployees.Tests; + +/// +/// Фикстура, поднимающая AppHost для интеграционных тестов. +/// +public class Fixture : IAsyncLifetime +{ + public DistributedApplication App { get; private set; } = null!; + public AmazonS3Client S3Client { get; private set; } = null!; + + public string sqsUrl = ""; + + public async Task InitializeAsync() + { + var appHost = await DistributedApplicationTestingBuilder.CreateAsync(); + + appHost.Services.ConfigureHttpClientDefaults(http => + http.AddStandardResilienceHandler(options => + { + options.TotalRequestTimeout.Timeout = TimeSpan.FromMinutes(3); + options.AttemptTimeout.Timeout = TimeSpan.FromSeconds(60); + options.CircuitBreaker.SamplingDuration = TimeSpan.FromMinutes(3); + options.Retry.MaxRetryAttempts = 10; + options.Retry.Delay = TimeSpan.FromSeconds(3); + })); + + App = await appHost.BuildAsync(); + await App.StartAsync(); + + await Task.WhenAll( + App.ResourceNotifications.WaitForResourceAsync("minio"), + App.ResourceNotifications.WaitForResourceAsync("elasticmq"), + App.ResourceNotifications.WaitForResourceAsync("companyemployees-apigateway"), + App.ResourceNotifications.WaitForResourceAsync("company-employee-fileservice") + ).WaitAsync(TimeSpan.FromMinutes(5)); + + await Task.Delay(TimeSpan.FromSeconds(5)); + + using var minioClient = App.CreateHttpClient("minio", "http"); + var minioUrl = minioClient.BaseAddress!.ToString().TrimEnd('/'); + + + var sqsHttpClient = App.CreateHttpClient("elasticmq", "http"); + sqsUrl = sqsHttpClient.BaseAddress?.ToString().TrimEnd('/'); + + + S3Client = new AmazonS3Client( + new BasicAWSCredentials("minioadmin", "minioadmin"), + new AmazonS3Config + { + ServiceURL = minioUrl, + ForcePathStyle = true, + UseHttp = true, + AuthenticationRegion = "us-east-1" + }); + + var doesExist = await Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async(S3Client, "company-employee"); + + if (!doesExist) + { + await S3Client.PutBucketAsync(new PutBucketRequest + { + BucketName = "company-employee" + }); + } + + } + + public async Task> WaitForS3ObjectAsync(string key, int maxAttempts = 15) + { + for (var i = 0; i < maxAttempts; i++) + { + await Task.Delay(TimeSpan.FromSeconds(2)); + + try + { + var doesExist = await Amazon.S3.Util.AmazonS3Util.DoesS3BucketExistV2Async(S3Client, "company-employee"); + + var response = await S3Client.ListObjectsV2Async(new ListObjectsV2Request + { + BucketName = "company-employee", + Prefix = key, + } + ); + + if (response.S3Objects is not null && response.S3Objects.Count > 0) + return response.S3Objects; + } + catch (AmazonS3Exception ex) when (ex.Message.Contains("NoSuchBucket")) + { + Console.WriteLine($"Bucket not ready yet, attempt {i + 1}/{maxAttempts}"); + } + catch (Exception ex) + { + Console.WriteLine($"Error listing objects: {ex.Message}"); + } + } + + return []; + } + + public async Task DisposeAsync() + { + S3Client?.Dispose(); + + try + { + await App.DisposeAsync().AsTask().WaitAsync(TimeSpan.FromSeconds(30)); + } + catch (TimeoutException) { } + catch (OperationCanceledException) { } + } +} \ No newline at end of file diff --git a/CompanyEmployee.Tests/IntegrationTests.cs b/CompanyEmployee.Tests/IntegrationTests.cs new file mode 100644 index 00000000..e8bf007c --- /dev/null +++ b/CompanyEmployee.Tests/IntegrationTests.cs @@ -0,0 +1,281 @@ +using Amazon.Runtime; +using Amazon.S3; +using Amazon.S3.Model; +using Amazon.SQS; +using Amazon.SQS.Model; +using Aspire.Hosting.Testing; +using CompanyEmployees.Generator.Models; +using System.Net; +using System.Text.Json; +using System.Text.Json.Nodes; +using Xunit; + +namespace CompanyEmployees.Tests; + +/// +/// Интеграционные тесты, проверяющие корректную совместную работу всех сервисов. +/// +public class IntegrationTests(Fixture fixture) : IClassFixture +{ + private static readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true }; + + private string _bucketName = "company-employee"; + + /// + /// Тест для проверки того, что данные генерируются добросовестно + /// + [Fact] + public async Task GetEmployee_TestValid() + { + using var client = fixture.App.CreateHttpClient("companyemployees-apigateway", "http"); + + using var response = await client.GetAsync("/employee?id=1"); + + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var employee = await response.Content.ReadFromJsonAsync(_jsonOptions); + + Assert.NotNull(employee); + Assert.Equal(1, employee.Id); + Assert.False(string.IsNullOrEmpty(employee.FullName)); + Assert.False(string.IsNullOrEmpty(employee.Position)); + Assert.False(string.IsNullOrEmpty(employee.Section)); + Assert.False(string.IsNullOrEmpty(employee.Email)); + Assert.False(string.IsNullOrEmpty(employee.PhoneNumber)); + Assert.True(employee.Salary > 0); + + var isDismissal = employee.Dismissal; + + Assert.True((isDismissal && employee.DismissalDate is not null) || (!isDismissal && employee.DismissalDate is null)); + } + + /// + /// Тест для проверки совпадения объектов сгенерированных с одинаковым Id + /// + [Fact] + public async Task GetEmployee_CheckEmployessWithEqualIDs() + { + var randomId = Random.Shared.Next(10, 20); + using var client = fixture.App.CreateHttpClient("companyemployees-apigateway", "http"); + + var employee = await client.GetFromJsonAsync($"/employee?id={randomId}", _jsonOptions); + var employeeRepeat = await client.GetFromJsonAsync($"/employee?id={randomId}", _jsonOptions); + + Assert.NotNull(employee); + Assert.NotNull(employeeRepeat); + + Assert.Equal(employee.Id, employeeRepeat.Id); + Assert.Equal(employee.FullName, employeeRepeat.FullName); + Assert.Equal(employee.Position, employeeRepeat.Position); + Assert.Equal(employee.Section, employeeRepeat.Section); + Assert.Equal(employee.Email, employeeRepeat.Email); + Assert.Equal(employee.PhoneNumber, employeeRepeat.PhoneNumber); + Assert.Equal(employee.AdmissionDate.ToString(), employeeRepeat.AdmissionDate.ToString()); + } + + /// + /// Тест для проверки не совпадения объектов сгенерированных с разными Id + /// + [Fact] + public async Task GetEmployee_CheckEmployessWithNotEqualIDs() + { + using var client = fixture.App.CreateHttpClient("companyemployees-apigateway", "http"); + + var employee201 = await client.GetFromJsonAsync("/employee?id=201", _jsonOptions); + var employee202 = await client.GetFromJsonAsync("/employee?id=202", _jsonOptions); + + Assert.NotNull(employee201); + Assert.NotNull(employee202); + Assert.Equal(201, employee201.Id); + Assert.Equal(202, employee202.Id); + Assert.NotEqual(employee201.FullName, employee202.FullName); + Assert.NotEqual(employee201.PhoneNumber, employee202.PhoneNumber); + } + + /// + /// Тест для проверки балансировщика + /// + [Fact] + public async Task GetEmployee_CheckLoadBalancer() + { + using var client = fixture.App.CreateHttpClient("companyemployees-apigateway", "http"); + + var baseId = Random.Shared.Next(100, 200); + var tasks = Enumerable.Range(0, 6) + .Select(i => client.GetAsync($"/employee?id={baseId + i}")); + + var responses = await Task.WhenAll(tasks); + + Assert.All(responses, r => Assert.Equal(HttpStatusCode.OK, r.StatusCode)); + } + + /// + /// Тест для проверки пайплана: + /// Gateway → Generator → SQS → FileService → Minio. + /// + [Fact] + public async Task GetEmployee_CheckPipeline() + { + var id = 23; + var expectedKey = $"employee-{id}.json"; + + using var client = fixture.App.CreateHttpClient("companyemployees-apigateway", "http"); + using var response = await client.GetAsync($"/employee?id={id}"); + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + + var objects = await fixture.WaitForS3ObjectAsync(expectedKey); + + Assert.NotEmpty(objects); + } + + [Fact] + public async Task Debug_MinioConnection() + { + // Проверяем, видит ли FileService тот же MinIO + using var client = fixture.App.CreateHttpClient("company-employee-fileservice", "http"); + + // Добавьте эндпоинт в FileService для диагностики + var response = await client.GetAsync("/health"); + + // Также проверяем через S3 клиент + var buckets = await fixture.S3Client.ListBucketsAsync(); + Console.WriteLine($"Found {buckets.Buckets.Count} buckets:"); + foreach (var bucket in buckets.Buckets) + { + Console.WriteLine($"- {bucket.BucketName}"); + } + + // Проверяем, может ли FileService писать в MinIO + var testKey = "test.txt"; + await fixture.S3Client.PutObjectAsync(new PutObjectRequest + { + BucketName = "company-employee", + Key = testKey, + ContentBody = "test" + }); + + var objects = await fixture.S3Client.ListObjectsV2Async(new ListObjectsV2Request + { + BucketName = "company-employee" + }); + + Assert.True(objects.S3Objects.Count > 0, "No objects found in bucket"); + Console.WriteLine($"Successfully wrote to MinIO, found {objects.S3Objects.Count} objects"); + } + + [Fact] + public async Task Debug_SqsQueue() + { + // Создаем SQS клиент + var sqsConfig = new AmazonSQSConfig + { + ServiceURL = fixture.sqsUrl, + AuthenticationRegion = "us-east-1" + }; + using var sqsClient = new AmazonSQSClient( + new BasicAWSCredentials("test", "test"), + sqsConfig); + + // Получаем URL очереди + try + { + var getQueueUrlResponse = await sqsClient.GetQueueUrlAsync("employees"); + Console.WriteLine($"✅ Queue URL: {getQueueUrlResponse.QueueUrl}"); + + // Проверяем атрибуты очереди + var attributes = await sqsClient.GetQueueAttributesAsync( + getQueueUrlResponse.QueueUrl, + new List { "ApproximateNumberOfMessages", "QueueArn" }); + + Console.WriteLine($"Queue ARN: {attributes.QueueARN}"); + Console.WriteLine($"Messages in queue: {attributes.ApproximateNumberOfMessages}"); + + // Отправляем тестовое сообщение напрямую в SQS + var testMessage = new { Id = 999, FullName = "Test User", Position = "Tester" }; + var sendResponse = await sqsClient.SendMessageAsync(new SendMessageRequest + { + QueueUrl = getQueueUrlResponse.QueueUrl, + MessageBody = JsonSerializer.Serialize(testMessage) + }); + + Console.WriteLine($"✅ Test message sent, MD5: {sendResponse.MD5OfMessageBody}"); + + // Ждем и проверяем, что сообщение появилось + await Task.Delay(2000); + + var attributesAfter = await sqsClient.GetQueueAttributesAsync( + getQueueUrlResponse.QueueUrl, + new List { "ApproximateNumberOfMessages" }); + Console.WriteLine($"Messages after send: {attributesAfter.ApproximateNumberOfMessages}"); + } + catch (Exception ex) + { + Console.WriteLine($"❌ Error: {ex.Message}"); + } + } + + /// + /// Тест для проверки данных сохранённых в бакете + /// + [Fact] + public async Task GetEmployee_CheckBucketData() + { + var id = 12; + var expectedKey = $"employee-{id}.json"; + + using var client = fixture.App.CreateHttpClient("companyemployees-apigateway", "http"); + var employee = await client.GetFromJsonAsync($"/employee?id={id}", _jsonOptions); + Assert.NotNull(employee); + + await Task.Delay(TimeSpan.FromSeconds(10)); + + var response = await fixture.S3Client.ListObjectsV2Async(new ListObjectsV2Request + { + BucketName = "company-employee" + }); + + var objects = await fixture.WaitForS3ObjectAsync(expectedKey); + Assert.NotEmpty(objects); + + var getResponse = await fixture.S3Client.GetObjectAsync(_bucketName, expectedKey); + using var reader = new StreamReader(getResponse.ResponseStream); + var json = await reader.ReadToEndAsync(); + var cached = JsonNode.Parse(json)?.AsObject(); + + Assert.NotNull(cached); + Assert.Equal(id, cached["id"]!.GetValue()); + Assert.Equal(employee.FullName, cached["fullName"]!.GetValue()); + Assert.Equal(employee.Email, cached["email"]!.GetValue()); + Assert.Equal(employee.Salary, cached["salary"]!.GetValue()); + } + + /// + /// Тест для проверки избежания дублирования данных в Minio + /// + [Fact] + public async Task GetEmployee_CheckNotDuplicate() + { + var id = 23; + var expectedKey = $"employee-{id}.json"; + + using var client = fixture.App.CreateHttpClient("companyemployees-apigateway", "http"); + + using var firstResponse = await client.GetAsync($"/employee?id={id}"); + firstResponse.EnsureSuccessStatusCode(); + var objectsAfterFirst = await fixture.WaitForS3ObjectAsync(expectedKey); + Assert.NotEmpty(objectsAfterFirst); + + using var secondResponse = await client.GetAsync($"/employee?id={id}"); + secondResponse.EnsureSuccessStatusCode(); + + await Task.Delay(TimeSpan.FromSeconds(5)); + + var listResponse = await fixture.S3Client.ListObjectsV2Async(new ListObjectsV2Request + { + BucketName = _bucketName, + Prefix = expectedKey + }); + + Assert.Single(listResponse.S3Objects); + } +} \ No newline at end of file diff --git a/CompanyEmployee.Tests/Properties/launchSettings.json b/CompanyEmployee.Tests/Properties/launchSettings.json new file mode 100644 index 00000000..cda96a6f --- /dev/null +++ b/CompanyEmployee.Tests/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:64848", + "sslPort": 44342 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5049", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7120;http://localhost:5049", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/CompanyEmployee.Tests/appsettings.Development.json b/CompanyEmployee.Tests/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CompanyEmployee.Tests/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CompanyEmployee.Tests/appsettings.json b/CompanyEmployee.Tests/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/CompanyEmployee.Tests/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/CompanyEmployees.ApiGateway/CompanyEmployees.ApiGateway.csproj b/CompanyEmployees.ApiGateway/CompanyEmployees.ApiGateway.csproj new file mode 100644 index 00000000..b5eae40a --- /dev/null +++ b/CompanyEmployees.ApiGateway/CompanyEmployees.ApiGateway.csproj @@ -0,0 +1,17 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + diff --git a/CompanyEmployees.ApiGateway/LoadBalancer/QueryBased.cs b/CompanyEmployees.ApiGateway/LoadBalancer/QueryBased.cs new file mode 100644 index 00000000..ec8d2cbc --- /dev/null +++ b/CompanyEmployees.ApiGateway/LoadBalancer/QueryBased.cs @@ -0,0 +1,50 @@ +using Ocelot.LoadBalancer.Errors; +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Responses; +using Ocelot.ServiceDiscovery.Providers; +using Ocelot.Values; + +namespace CompanyEmployees.ApiGateway.LoadBalancer; +public class QueryBased(IServiceDiscoveryProvider serviceDiscovery) + : ILoadBalancer +{ + private const string IdQueryParamName = "id"; + + public string Type => nameof(QueryBased); + + public async Task> LeaseAsync(HttpContext httpContext) + { + var services = await serviceDiscovery.GetAsync(); + + if (services is null) + return new ErrorResponse( + new ServicesAreNullError("Service discovery returned null")); + + if (services.Count == 0) + return new ErrorResponse( + new ServicesAreNullError("No downstream services are available")); + + var query = httpContext.Request.Query; + + if (!query.TryGetValue(IdQueryParamName, out var idValues) || idValues.Count <= 0) + { + return SelectRandomService(services); + } + + var idStr = idValues.First(); + + if (string.IsNullOrEmpty(idStr) || !int.TryParse(idStr, out var id) || id < 0) + { + return SelectRandomService(services); + } + + return new OkResponse(services[id % services.Count].HostAndPort); + } + + private static OkResponse SelectRandomService(List currentServices) + { + var randomIndex = Random.Shared.Next(currentServices.Count); + return new OkResponse(currentServices[randomIndex].HostAndPort); + } + public void Release(ServiceHostAndPort hostAndPort) { } +} \ No newline at end of file diff --git a/CompanyEmployees.ApiGateway/Program.cs b/CompanyEmployees.ApiGateway/Program.cs new file mode 100644 index 00000000..0b54a86b --- /dev/null +++ b/CompanyEmployees.ApiGateway/Program.cs @@ -0,0 +1,43 @@ +using CompanyEmployees.ApiGateway.LoadBalancer; +using CompanyEmployees.ServiceDefaults; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +builder.Configuration + .AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); + +var generators = builder.Configuration.GetSection("Generators").Get() ?? []; + +var addressOverrides = new List>(); + +for (var i = 0; i < generators.Length; ++i) +{ + var name = generators[i]; + var url = builder.Configuration[$"services:{name}:http:0"]; + + if (!string.IsNullOrEmpty(url) && Uri.TryCreate(url, UriKind.Absolute, out var uri)) + { + addressOverrides.Add(new($"Routes:0:DownstreamHostAndPorts:{i}:Host", uri.Host)); + addressOverrides.Add(new($"Routes:0:DownstreamHostAndPorts:{i}:Port", uri.Port.ToString())); + } +} + +if (addressOverrides.Count > 0) + builder.Configuration.AddInMemoryCollection(addressOverrides); + +builder.Services + .AddOcelot(builder.Configuration) + .AddCustomLoadBalancer((route, serviceDiscovery) => + new QueryBased(serviceDiscovery)); + +var app = builder.Build(); + +app.MapDefaultEndpoints(); + +await app.UseOcelot(); + +app.Run(); \ No newline at end of file diff --git a/CompanyEmployees.ApiGateway/Properties/launchSettings.json b/CompanyEmployees.ApiGateway/Properties/launchSettings.json new file mode 100644 index 00000000..7387ba5c --- /dev/null +++ b/CompanyEmployees.ApiGateway/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5200", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} \ No newline at end of file diff --git a/CompanyEmployees.ApiGateway/appsettings.Development.json b/CompanyEmployees.ApiGateway/appsettings.Development.json new file mode 100644 index 00000000..3579dea2 --- /dev/null +++ b/CompanyEmployees.ApiGateway/appsettings.Development.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Ocelot": "Debug" + } + }, + "GlobalConfiguration": { + "BaseUrl": "http://localhost:5200" + } +} \ No newline at end of file diff --git a/CompanyEmployees.ApiGateway/appsettings.json b/CompanyEmployees.ApiGateway/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/CompanyEmployees.ApiGateway/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/CompanyEmployees.ApiGateway/ocelot.json b/CompanyEmployees.ApiGateway/ocelot.json new file mode 100644 index 00000000..c41c83ac --- /dev/null +++ b/CompanyEmployees.ApiGateway/ocelot.json @@ -0,0 +1,28 @@ +{ + "Generators": [ "generator-1", "generator-2", "generator-3" ], + "Routes": [ + { + "DownstreamPathTemplate": "/employee", + "DownstreamScheme": "http", + "DownstreamHostAndPorts": [ + { + "Host": "localhost", + "Port": 5201 + }, + { + "Host": "localhost", + "Port": 5202 + }, + { + "Host": "localhost", + "Port": 5203 + } + ], + "UpstreamPathTemplate": "/employee", + "UpstreamHttpMethod": [ "GET" ], + "LoadBalancerOptions": { + "Type": "QueryBased" + } + } + ] +} diff --git a/CompanyEmployees.AppHost/CompanyEmployees.AppHost.csproj b/CompanyEmployees.AppHost/CompanyEmployees.AppHost.csproj new file mode 100644 index 00000000..ba4101e1 --- /dev/null +++ b/CompanyEmployees.AppHost/CompanyEmployees.AppHost.csproj @@ -0,0 +1,32 @@ + + + + + + net8.0 + enable + enable + enable + true + Debug;Release;123 + + + + + + + + + + + + + + + + + + + + + diff --git a/CompanyEmployees.AppHost/Program.cs b/CompanyEmployees.AppHost/Program.cs new file mode 100644 index 00000000..c12a67ff --- /dev/null +++ b/CompanyEmployees.AppHost/Program.cs @@ -0,0 +1,53 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var redis = builder.AddRedis("redis") + .WithRedisCommander(); + +var minioAccessKey = builder.Configuration["MinIO:AccessKey"]!; +var minioSecretKey = builder.Configuration["MinIO:SecretKey"]!; + +var minio = builder.AddMinioContainer("minio") + .WithEnvironment("MINIO_ROOT_USER", minioAccessKey) + .WithEnvironment("MINIO_ROOT_PASSWORD", minioSecretKey) + //.WithBindMount("minio-data", "/data") + .WaitFor(redis); + +minio.WithEndpoint("http", endpoint => endpoint.Port = 9000); +minio.WithEndpoint("console", endpoint => endpoint.Port = 9001); + +var sqs = builder.AddContainer("elasticmq", "softwaremill/elasticmq-native") + .WithHttpEndpoint(targetPort: 9324, name: "http") + .WithHttpHealthCheck("/?Action=ListQueues", endpointName: "http"); + +var gatewayPort = builder.Configuration.GetValue("GatewayPort"); +var gateway = builder + .AddProject("companyemployees-apigateway") + .WithExternalHttpEndpoints(); + +for (var i = 0; i < 3; ++i) +{ + var currGenerator = builder.AddProject($"generator-{i + 1}") + .WithEndpoint("http", endpoint => endpoint.Port = gatewayPort + 1 + i) + .WithReference(redis) + .WaitFor(redis) + .WithEnvironment("Sqs__ServiceUrl", sqs.GetEndpoint("http")) + .WaitFor(sqs); + + gateway + .WithReference(currGenerator) + .WaitFor(currGenerator); +} + +var fileService = builder.AddProject("company-employee-fileservice") + .WithEnvironment("Sqs__ServiceUrl", sqs.GetEndpoint("http")) + .WithEnvironment("MinIO__BucketName", "company-employee") + .WithEnvironment("BucketName", "company-employee") + .WithReference(minio) + .WaitFor(sqs) + .WaitFor(minio); + +builder.AddProject("client") + .WithReference(gateway) + .WaitFor(gateway); + +builder.Build().Run(); \ No newline at end of file diff --git a/CompanyEmployees.AppHost/Properties/launchSettings.json b/CompanyEmployees.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..09cd82db --- /dev/null +++ b/CompanyEmployees.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17170;http://localhost:15170", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21170", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22170" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15170", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19170", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20170" + } + } + } +} \ No newline at end of file diff --git a/CompanyEmployees.AppHost/appsettings.Development.json b/CompanyEmployees.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CompanyEmployees.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CompanyEmployees.AppHost/appsettings.json b/CompanyEmployees.AppHost/appsettings.json new file mode 100644 index 00000000..8a44f4b2 --- /dev/null +++ b/CompanyEmployees.AppHost/appsettings.json @@ -0,0 +1,14 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + }, + "GatewayPort": 5200, + "MinIO": { + "AccessKey": "minioadmin", + "SecretKey": "minioadmin" + } +} diff --git a/CompanyEmployees.FileService/CompanyEmployees.FileService.csproj b/CompanyEmployees.FileService/CompanyEmployees.FileService.csproj new file mode 100644 index 00000000..3d5e68bd --- /dev/null +++ b/CompanyEmployees.FileService/CompanyEmployees.FileService.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + diff --git a/CompanyEmployees.FileService/Configuration/MinioConfiguration.cs b/CompanyEmployees.FileService/Configuration/MinioConfiguration.cs new file mode 100644 index 00000000..541fc579 --- /dev/null +++ b/CompanyEmployees.FileService/Configuration/MinioConfiguration.cs @@ -0,0 +1,10 @@ +namespace CompanyEmployees.FileService.Configuration; + +public class MinioConfiguration +{ + public string Endpoint { get; set; } = "localhost:9000"; + public string AccessKey { get; set; } = "minioadmin"; + public string SecretKey { get; set; } = "minioadmin"; + public bool UseSSL { get; set; } = false; + public string BucketName { get; set; } = "company-employee"; +} diff --git a/CompanyEmployees.FileService/Models/EmployeeMessage.cs b/CompanyEmployees.FileService/Models/EmployeeMessage.cs new file mode 100644 index 00000000..cb33c09a --- /dev/null +++ b/CompanyEmployees.FileService/Models/EmployeeMessage.cs @@ -0,0 +1,14 @@ +namespace CompanyEmployees.FileService.Models; + +public record EmployeeMessage( + int Id, + string FullName, + string Position, + string Section, + DateOnly AdmissionDate, + decimal Salary, + string Email, + string PhoneNumber, + bool Dismissal, + DateOnly? DismissalDate +); diff --git a/CompanyEmployees.FileService/Program.cs b/CompanyEmployees.FileService/Program.cs new file mode 100644 index 00000000..35d21fa5 --- /dev/null +++ b/CompanyEmployees.FileService/Program.cs @@ -0,0 +1,68 @@ +using Amazon.Runtime; +using Amazon.S3; +using Amazon.SQS; +using CompanyEmployees.FileService.Configuration; +using CompanyEmployees.FileService.Services; +using CompanyEmployees.ServiceDefaults; +using MassTransit; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +var minioSettings = builder.Configuration.GetSection("MinIO"); + +// , +var minioAccessKey = minioSettings.Get()?.AccessKey; +var minioSecretKey = minioSettings.Get()?.SecretKey; +var minioEndpoint = minioSettings.Get()?.Endpoint; + +builder.Services.AddSingleton(minioSettings).Configure(minioSettings); + +builder.Services.AddSingleton(_ => new AmazonS3Client( + new BasicAWSCredentials(minioAccessKey, minioSecretKey), + new AmazonS3Config + { + ServiceURL = minioEndpoint, + ForcePathStyle = true, + AuthenticationRegion = "us-east-1" + } +)); + +var sqsServiceUrl = builder.Configuration["SQS:ServiceUrl"] ?? "http://localhost:9324"; + +builder.Services.AddHostedService(); + +builder.Services.AddMassTransit(x => +{ + x.AddConsumer(); + + x.UsingAmazonSqs((context, cfg) => + { + cfg.Host("us-east-1", h => + { + h.AccessKey("test"); + h.SecretKey("test"); + h.Config(new AmazonSQSConfig + { + ServiceURL = sqsServiceUrl, + AuthenticationRegion = "us-east-1" + }); + }); + + cfg.ReceiveEndpoint("employees", e => + { + e.ConfigureConsumeTopology = false; + e.UseRawJsonDeserializer(RawSerializerOptions.AnyMessageType); + e.Consumer(context); + }); + }); +}); + +builder.Services.AddHostedService(); + +var app = builder.Build(); + +app.MapDefaultEndpoints(); + +app.Run(); \ No newline at end of file diff --git a/CompanyEmployees.FileService/Properties/launchSettings.json b/CompanyEmployees.FileService/Properties/launchSettings.json new file mode 100644 index 00000000..2c364ee0 --- /dev/null +++ b/CompanyEmployees.FileService/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:64732", + "sslPort": 44310 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5269", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7208;http://localhost:5269", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/CompanyEmployees.FileService/Services/EmployeeConsumer.cs b/CompanyEmployees.FileService/Services/EmployeeConsumer.cs new file mode 100644 index 00000000..9e9b2988 --- /dev/null +++ b/CompanyEmployees.FileService/Services/EmployeeConsumer.cs @@ -0,0 +1,43 @@ +using Amazon.S3; +using Amazon.S3.Model; +using MassTransit; +using System.Text.Json; +using CompanyEmployees.FileService.Models; + +namespace CompanyEmployees.FileService.Services; + +public class EmployeeConsumer( + IAmazonS3 s3Client, + ILogger logger, + IConfiguration configuration +) : IConsumer +{ + private readonly string _bucketName = configuration["MinIO:BucketName"] ?? "company-employee"; + private static readonly JsonSerializerOptions _jsonOptions = new() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + + public async Task Consume(ConsumeContext context) + { + try + { + var employee = context.Message; + var fileName = $"employee-{employee.Id}.json"; + var json = JsonSerializer.Serialize(employee, _jsonOptions); + + var response = await s3Client.PutObjectAsync(new PutObjectRequest + { + BucketName = _bucketName, + Key = fileName, + ContentBody = json, + ContentType = "application/json" + }, context.CancellationToken); + + logger.LogInformation("Saved employee {EmployeeId} to minio with filename: {FileName}", employee.Id, fileName); + } catch (Exception ex) + { + logger.LogError(ex.Message); + } + } +} \ No newline at end of file diff --git a/CompanyEmployees.FileService/Services/Minio.cs b/CompanyEmployees.FileService/Services/Minio.cs new file mode 100644 index 00000000..c810bc92 --- /dev/null +++ b/CompanyEmployees.FileService/Services/Minio.cs @@ -0,0 +1,33 @@ +using Amazon.S3; +using Amazon.S3.Model; + +namespace CompanyEmployees.FileService.Services; + +public class MinioInitializer(IAmazonS3 s3Client, ILogger logger, IConfiguration configuration) : BackgroundService +{ + + private readonly string _bucketName = configuration["MinIO:BucketName"] ?? "company-employee"; + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + try + { + await s3Client.PutBucketAsync(new PutBucketRequest { BucketName = _bucketName }, stoppingToken); + logger.LogInformation("Minio bucket '{BucketName}' ready", _bucketName); + return; + } + catch (AmazonS3Exception ex) when (ex.StatusCode == System.Net.HttpStatusCode.Conflict) + { + logger.LogInformation("Minio bucket '{BucketName}' already exists", _bucketName); + return; + } + catch (Exception ex) when (!stoppingToken.IsCancellationRequested) + { + logger.LogWarning(ex, "Failed to initialize Minio bucket, retrying in 3 seconds..."); + await Task.Delay(TimeSpan.FromSeconds(3), stoppingToken); + } + } + } +} \ No newline at end of file diff --git a/CompanyEmployees.FileService/Services/SQSService.cs b/CompanyEmployees.FileService/Services/SQSService.cs new file mode 100644 index 00000000..f8a17d06 --- /dev/null +++ b/CompanyEmployees.FileService/Services/SQSService.cs @@ -0,0 +1,32 @@ +namespace CompanyEmployees.FileService.Services; + +public class SqsService(IConfiguration configuration, ILogger logger) : IHostedService +{ + public async Task StartAsync(CancellationToken cancellationToken) + { + var sqsUrl = configuration["SQS:ServiceUrl"] ?? "http://localhost:9324"; + + using var httpClient = new HttpClient { Timeout = TimeSpan.FromSeconds(5) }; + + var deadline = DateTime.UtcNow.AddMinutes(2); + + while (DateTime.UtcNow < deadline && !cancellationToken.IsCancellationRequested) + { + try + { + await httpClient.GetAsync($"{sqsUrl}/?Action=ListQueues", cancellationToken); + logger.LogInformation("SQS is ready at {Url}", sqsUrl); + return; + } + catch (Exception ex) when (!cancellationToken.IsCancellationRequested) + { + logger.LogWarning(ex, "SQS not ready at {Url}, retrying in 2 seconds...", sqsUrl); + await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken); + } + } + + logger.LogError("SQS did not become ready within the timeout"); + } + + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; +} \ No newline at end of file diff --git a/CompanyEmployees.FileService/appsettings.Development.json b/CompanyEmployees.FileService/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CompanyEmployees.FileService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CompanyEmployees.FileService/appsettings.json b/CompanyEmployees.FileService/appsettings.json new file mode 100644 index 00000000..d38aef02 --- /dev/null +++ b/CompanyEmployees.FileService/appsettings.json @@ -0,0 +1,19 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "MinIO": { + "Endpoint": "http://localhost:9000", + "AccessKey": "minioadmin", + "SecretKey": "minioadmin", + "UseSSL": false, + "BucketName": "company-employee" + }, + "SQS": { + "ServiceUrl": "http://localhost:4566" + } +} diff --git a/CompanyEmployees.Generator/CompanyEmployees.Generator.csproj b/CompanyEmployees.Generator/CompanyEmployees.Generator.csproj new file mode 100644 index 00000000..c0447bd6 --- /dev/null +++ b/CompanyEmployees.Generator/CompanyEmployees.Generator.csproj @@ -0,0 +1,21 @@ + + + + net8.0 + enable + enable + Debug;Release;123 + + + + + + + + + + + + + + diff --git a/CompanyEmployees.Generator/Models/CompanyEmployee.cs b/CompanyEmployees.Generator/Models/CompanyEmployee.cs new file mode 100644 index 00000000..82440823 --- /dev/null +++ b/CompanyEmployees.Generator/Models/CompanyEmployee.cs @@ -0,0 +1,64 @@ +namespace CompanyEmployees.Generator.Models; + +/// +/// Модель сотрудника компании +/// +public class CompanyEmployeeModel +{ + /// + /// Идентификатор сотрудника в системе + /// + public int Id { get; set; } + + /// + /// ФИО + ///
+ /// Конкатенация фамилии, имени и отчества через пробел + ///
+ ///
+ public required string FullName { get; set; } + + /// + /// Должность
+ /// Выбирается из справочника профессий (“Developer”, “Manager”, “Analyst” и т.д.) и справочника суффиксов к ним (“Junior”, “Middle”, “Senior” и т.д.) + ///
+ public required string Position { get; set; } + + /// + /// Отдел + /// + public required string Section { get; set; } + + /// + /// Дата приема
+ /// Ограничение: не более 10 лет назад от текущей даты + ///
+ public required DateOnly AdmissionDate { get; set; } + + /// + /// Оклад
+ /// Значение коррелирует с суффиксом должности + ///
+ public required decimal Salary { get; set; } + + /// + /// Электронная почта + /// + public required string Email { get; set; } + + /// + /// Номер телефона формата +7(***)***-**-** + /// + public required string PhoneNumber { get; set; } + + /// + /// Индикатор увольнения + /// + public required bool Dismissal { get; set; } = false; + + /// + /// Дата увольнения
+ /// При отсутствии индикатора увольнения дата увольнения не заполняется + ///
+ public DateOnly? DismissalDate { get; set; } +} diff --git a/CompanyEmployees.Generator/Program.cs b/CompanyEmployees.Generator/Program.cs new file mode 100644 index 00000000..68f1d5cb --- /dev/null +++ b/CompanyEmployees.Generator/Program.cs @@ -0,0 +1,86 @@ +using CompanyEmployees.Generator.Services; +using CompanyEmployees.ServiceDefaults; +using Serilog; +using MassTransit; +using Amazon.SQS; +using LocalStack.Client.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +builder.AddRedisDistributedCache("redis"); + +builder.Services.AddSingleton(); +builder.Services.AddScoped(); + +builder.Services.AddCors(options => +{ + options.AddDefaultPolicy(policy => + { + policy.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + }); +}); + +var sqsServiceUrl = builder.Configuration["Sqs:ServiceUrl"]; +if (!string.IsNullOrEmpty(sqsServiceUrl)) +{ + builder.Services.AddMassTransit(x => + { + x.UsingAmazonSqs((_, cfg) => + { + cfg.Host("us-east-1", h => + { + h.AccessKey("test"); + h.SecretKey("test"); + h.Config(new AmazonSQSConfig + { + ServiceURL = sqsServiceUrl, + AuthenticationRegion = "us-east-1" + }); + }); + cfg.UseRawJsonSerializer(RawSerializerOptions.AnyMessageType); + }); + }); + + builder.Services.AddScoped(); +} + +var app = builder.Build(); + +app.UseCors(); +app.UseSerilogRequestLogging(); + +app.MapDefaultEndpoints(); + +app.MapGet("/employee", async ( + int id, + CompanyEmployeeService service, + ILogger logger, + CancellationToken cancellationToken) => +{ + logger.LogInformation("Received request for company employee with ID: {Id}", id); + + if (id <= 0) + { + logger.LogWarning("Received invalid ID: {Id}", id); + return Results.BadRequest(new { error = "ID must be a positive number" }); + } + + try + { + var application = await service.GetByIdAsync(id, cancellationToken); + + return Results.Ok(application); + } + catch (Exception ex) + { + logger.LogError(ex, "Error while getting company employee {Id}", id); + return Results.Problem("An error occurred while processing the request"); + } +}) +.WithName("GetCompanyEmployee"); + +app.Run(); \ No newline at end of file diff --git a/CompanyEmployees.Generator/Properties/launchSettings.json b/CompanyEmployees.Generator/Properties/launchSettings.json new file mode 100644 index 00000000..8c4696c2 --- /dev/null +++ b/CompanyEmployees.Generator/Properties/launchSettings.json @@ -0,0 +1,14 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5171", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/CompanyEmployees.Generator/Services/CompanyEmployeeGenerator.cs b/CompanyEmployees.Generator/Services/CompanyEmployeeGenerator.cs new file mode 100644 index 00000000..b307e8e1 --- /dev/null +++ b/CompanyEmployees.Generator/Services/CompanyEmployeeGenerator.cs @@ -0,0 +1,102 @@ +using Bogus; +using Bogus.DataSets; +using CompanyEmployees.Generator.Models; + +namespace CompanyEmployees.Generator.Services; + +/// +/// Генератор сотрудника по идентификатору +/// +/// Логгер +public class CompanyEmployeeGenerator(ILogger logger) +{ + /// + /// Справочник профессий сотрудников + /// + private static readonly string[] _positionProfessions = + [ + "Developer", + "Manager", + "Analyst", + "DevOps Engineer", + "QA Engineer", + "System Architect" + ]; + + private readonly Faker _faker = new Faker("ru") + .RuleFor(x => x.FullName, GenerateEmployeeFullName) + .RuleFor(x => x.Position, f => $"{f.PickRandom(_positionProfessions)} {f.PickRandom()}") + .RuleFor(x => x.Section, f => f.Commerce.Department()) + .RuleFor(x => x.AdmissionDate, f => f.Date.PastDateOnly(10)) + .RuleFor(x => x.Salary, GenerateEmployeeSalary) + .RuleFor(x => x.Email, f => f.Internet.Email()) + .RuleFor(x => x.PhoneNumber, f => f.Phone.PhoneNumber("+7(###)###-##-##")) + .RuleFor(x => x.Dismissal, f => f.Random.Bool()) + .RuleFor(x => x.DismissalDate, (f, employeeObject) => + employeeObject.Dismissal + ? f.Date.BetweenDateOnly(employeeObject.AdmissionDate, DateOnly.FromDateTime(DateTime.UtcNow)) + : null + ); + + /// + /// Минимальное значение оклада + /// + private const decimal SalaryMin = 100; + + /// + /// Максимальное значение оклада + /// + private const decimal SalaryMax = 200; + + /// + /// Справочник суффиксов для профессий + /// + private enum PositionSuffix + { + Junior = 0, + Middle = 1, + Senior = 2, + TeamLead = 3 + }; + + /// + /// Словарь множителей на основе суффиксов профессий + /// + private static readonly Dictionary _positionSuffixSalaryMultipliers = new() + { + [PositionSuffix.Junior] = 1m, + [PositionSuffix.Middle] = 1.5m, + [PositionSuffix.Senior] = 2.1m, + [PositionSuffix.TeamLead] = 2.6m + }; + + /// + /// Функция для генерации сотрудника на основе параметра id + /// + /// Идентификатор сотрудника в системе + /// Сгенерированные данные о сотруднике + public CompanyEmployeeModel Generate(int id) + { + logger.LogInformation("Start generating company employee application with ID: {Id}", id); + return _faker.UseSeed(id).RuleFor(x => x.Id, _ => id).Generate(); + } + + private static string GenerateEmployeeFullName(Faker faker) + { + var gender = faker.Person.Gender; + var firstName = faker.Name.FirstName(gender); + var lastName = faker.Name.LastName(gender); + var patronymic = faker.Name.FirstName(Name.Gender.Male) + (gender == Name.Gender.Male ? "еевич" : "еевна"); + + return string.Join(' ', firstName, lastName, patronymic); + } + + private static decimal GenerateEmployeeSalary(Faker faker, CompanyEmployeeModel employeeObject) + { + var baseSalary = faker.Random.Decimal(SalaryMin, SalaryMax); + var salaryMap = _positionSuffixSalaryMultipliers + .FirstOrDefault(pair => employeeObject.Position.Contains(pair.Key.ToString()), new KeyValuePair(PositionSuffix.Junior, 1m)); + + return Math.Round(baseSalary * salaryMap.Value, 2); + } +} diff --git a/CompanyEmployees.Generator/Services/CompanyEmployeeService.cs b/CompanyEmployees.Generator/Services/CompanyEmployeeService.cs new file mode 100644 index 00000000..b19ad3b8 --- /dev/null +++ b/CompanyEmployees.Generator/Services/CompanyEmployeeService.cs @@ -0,0 +1,72 @@ +using CompanyEmployees.Generator.Models; +using Microsoft.Extensions.Caching.Distributed; +using System.Text.Json; + +namespace CompanyEmployees.Generator.Services; + +/// +/// Сервис для работы с сотрудниками компании
+/// Каждый сгенерированный сотрудник сохраняется в кэш, если ранее он небыл получен
+/// В ином случае значение берется из кэша +///
+/// Генератор необходимый для создания сотрудника +/// Кэш хранящий ранее сгенерированных сотрудников +/// Логгер +public class CompanyEmployeeService( + CompanyEmployeeGenerator generator, + IDistributedCache cache, + ILogger logger, + IConfiguration config, + IEmployeePublisher? employeePublisher = null +) +{ + private readonly TimeSpan _cacheExpiration = TimeSpan.FromMinutes(config.GetSection("CacheSetting").GetValue("CacheExpirationMinutes", 5)); + private const string CacheKeyPrefix = "company-employee:"; + + /// + /// Функция для генерации или взятия из кэша сотрудника по id + /// + /// Id сотрудника + /// Токен для отмены операции + /// + public async Task GetByIdAsync(int id, CancellationToken cancellationToken = default) + { + var cacheKey = $"{CacheKeyPrefix}{id}"; + + logger.LogInformation("Request for company employee application with ID: {Id}", id); + + var cachedData = await cache.GetStringAsync(cacheKey, cancellationToken); + + if (!string.IsNullOrEmpty(cachedData)) + { + logger.LogInformation("Company employee application {Id} found in cache", id); + var cachedApplication = JsonSerializer.Deserialize(cachedData); + if (cachedApplication != null) return cachedApplication; + } + + logger.LogInformation("Company employee application {Id} not found in cache, generating new one", id); + + var application = generator.Generate(id); + + var serializedData = JsonSerializer.Serialize(application); + var cacheOptions = new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheExpiration + }; + + await cache.SetStringAsync(cacheKey, serializedData, cacheOptions, cancellationToken); + + if (employeePublisher is not null) + { + await employeePublisher.PublishAsync(application, cancellationToken); + logger.LogInformation("Publisher complete task"); + } + + logger.LogInformation( + "Company employee application {Id} saved to cache with TTL {CacheExpiration} minutes", + id, + _cacheExpiration.TotalMinutes); + + return application; + } +} diff --git a/CompanyEmployees.Generator/Services/EmployeePublisher.cs b/CompanyEmployees.Generator/Services/EmployeePublisher.cs new file mode 100644 index 00000000..c86e3e27 --- /dev/null +++ b/CompanyEmployees.Generator/Services/EmployeePublisher.cs @@ -0,0 +1,27 @@ +using CompanyEmployees.Generator.Models; +using MassTransit; + +namespace CompanyEmployees.Generator.Services; + +public class EmployeePublisher( + ISendEndpointProvider sendEndpointProvider, + ILogger logger) : IEmployeePublisher +{ + public Task PublishAsync(CompanyEmployeeModel employee, CancellationToken cancellationToken = default) + { + return Task.Run(async () => + { + try + { + var sendEndpoint = await sendEndpointProvider.GetSendEndpoint(new Uri("queue:employees")); + await sendEndpoint.Send(employee, cancellationToken); + + logger.LogInformation("Sent employee {EmployeeId} to SQS queue", employee.Id); + } + catch (Exception ex) + { + logger.LogError(ex, "Failed to send employee {EmployeeId} to SQS", employee.Id); + } + }, cancellationToken); + } +} diff --git a/CompanyEmployees.Generator/Services/IEmployeePublisher.cs b/CompanyEmployees.Generator/Services/IEmployeePublisher.cs new file mode 100644 index 00000000..be52212e --- /dev/null +++ b/CompanyEmployees.Generator/Services/IEmployeePublisher.cs @@ -0,0 +1,8 @@ +using CompanyEmployees.Generator.Models; + +namespace CompanyEmployees.Generator.Services; + +public interface IEmployeePublisher +{ + public Task PublishAsync(CompanyEmployeeModel employee, CancellationToken cancellationToken = default); +} \ No newline at end of file diff --git a/CompanyEmployees.Generator/appsettings.Development.json b/CompanyEmployees.Generator/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/CompanyEmployees.Generator/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/CompanyEmployees.Generator/appsettings.json b/CompanyEmployees.Generator/appsettings.json new file mode 100644 index 00000000..bd27b3da --- /dev/null +++ b/CompanyEmployees.Generator/appsettings.json @@ -0,0 +1,11 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "CacheSetting": { + "CacheExpirationMinutes": 5 + } +} diff --git a/CompanyEmployees.ServiceDefaults/CompanyEmployees.ServiceDefaults.csproj b/CompanyEmployees.ServiceDefaults/CompanyEmployees.ServiceDefaults.csproj new file mode 100644 index 00000000..0d65e7b0 --- /dev/null +++ b/CompanyEmployees.ServiceDefaults/CompanyEmployees.ServiceDefaults.csproj @@ -0,0 +1,29 @@ + + + + net8.0 + enable + enable + true + Debug;Release;123 + + + + + + + + + + + + + + + + + + + + + diff --git a/CompanyEmployees.ServiceDefaults/Extensions.cs b/CompanyEmployees.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..96cb5f7a --- /dev/null +++ b/CompanyEmployees.ServiceDefaults/Extensions.cs @@ -0,0 +1,146 @@ +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Diagnostics.HealthChecks; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Diagnostics.HealthChecks; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using OpenTelemetry; +using OpenTelemetry.Metrics; +using OpenTelemetry.Trace; +using Serilog; +using Serilog.Sinks.OpenTelemetry; + +namespace CompanyEmployees.ServiceDefaults; + +public static class Extensions +{ + public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBuilder builder) + { + builder.ConfigureSerilog(); + builder.ConfigureOpenTelemetry(); + builder.AddDefaultHealthChecks(); + builder.Services.AddServiceDiscovery(); + builder.Services.ConfigureHttpClientDefaults(http => + { + http.AddStandardResilienceHandler(); + http.AddServiceDiscovery(); + }); + + return builder; + } + + public static IHostApplicationBuilder ConfigureSerilog(this IHostApplicationBuilder builder) + { + var loggerConfig = new LoggerConfiguration() + .ReadFrom.Configuration(builder.Configuration) + .Enrich.FromLogContext() + .Enrich.WithEnvironmentName() + .Enrich.WithThreadId() + .Enrich.WithMachineName() + .WriteTo.Console(outputTemplate: + "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}"); + + var otlpEndpoint = builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]; + if (!string.IsNullOrWhiteSpace(otlpEndpoint)) + { + loggerConfig.WriteTo.OpenTelemetry(options => + { + options.Endpoint = otlpEndpoint; + options.Protocol = OtlpProtocol.Grpc; + options.ResourceAttributes = new Dictionary + { + ["service.name"] = builder.Configuration["OTEL_SERVICE_NAME"] + ?? builder.Environment.ApplicationName + }; + }); + } + + Log.Logger = loggerConfig.CreateLogger(); + + builder.Services.AddSerilog(); + + return builder; + } + + public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicationBuilder builder) + { + builder.Services.AddOpenTelemetry() + .WithMetrics(metrics => + { + metrics.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation() + .AddRuntimeInstrumentation(); + }) + .WithTracing(tracing => + { + tracing.AddAspNetCoreInstrumentation() + .AddHttpClientInstrumentation(); + }); + + builder.AddOpenTelemetryExporters(); + + return builder; + } + + private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostApplicationBuilder builder) + { + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + + if (useOtlpExporter) + { + builder.Services.AddOpenTelemetry().UseOtlpExporter(); + } + + return builder; + } + + public static IHostApplicationBuilder AddDefaultHealthChecks(this IHostApplicationBuilder builder) + { + builder.Services.AddHealthChecks() + .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); + + return builder; + } + + public const string CorsPolicyName = "GatewayPolicy"; + + public static IHostApplicationBuilder AddDefaultCors(this IHostApplicationBuilder builder) + { + builder.Services.AddCors(options => + { + options.AddPolicy(CorsPolicyName, policy => + { + if (builder.Environment.IsDevelopment()) + { + policy.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader(); + } + else + { + var allowedOrigins = builder.Configuration + .GetSection("Cors:AllowedOrigins") + .Get() ?? []; + + policy.WithOrigins(allowedOrigins) + .WithMethods("GET") + .WithHeaders("Content-Type", "Authorization"); + } + }); + }); + + return builder; + } + + public static WebApplication MapDefaultEndpoints(this WebApplication app) + { + app.MapHealthChecks("/health"); + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + + return app; + } +} \ No newline at end of file diff --git a/README.md b/README.md index dcaa5eb7..7eaebf0a 100644 --- a/README.md +++ b/README.md @@ -1,128 +1,88 @@ -# Современные технологии разработки программного обеспечения -[Таблица с успеваемостью](https://docs.google.com/spreadsheets/d/1an43o-iqlq4V_kDtkr_y7DC221hY9qdhGPrpII27sH8/edit?usp=sharing) - -## Задание -### Цель -Реализация проекта микросервисного бекенда. - -### Задачи -* Реализация межсервисной коммуникации, -* Изучение работы с брокерами сообщений, -* Изучение архитектурных паттернов, -* Изучение работы со средствами оркестрации на примере .NET Aspire, -* Повторение основ работы с системами контроля версий, -* Интеграционное тестирование. - -### Лабораторные работы +# Приложение генератор "Сотрудника компании" + +## 📝 Лабораторная работа №1 + +Реализация микросервиса генерации сотрудника компании с кэшированием и оркестрацией на базе .NET Aspire. + +### Цели +- Реализация генератора данных на основе **Bogus** с детерминированным seed +- Кэширование результатов с помощью **IDistributedCache** и **Redis** +- Структурированное логирование через **Serilog** +- Трассировка и метрики через **OpenTelemetry** +- Оркестрация сервисов через **.NET Aspire** + +### Детали варианта +**Вариант №8 — Сотрудник компании** + +| Параметр | Возможные значения | +|----------|----------| +| ФИО | Конкатенация фамилии, имени и отчества через пробел | +| Должность | Выбирается из справочника профессий (“Developer”, “Manager”, “Analyst” и т.д.) и справочника суффиксов к ним (“Junior”, “Middle”, “Senior” и т.д.) | +| Отдел | Отдел, в который определили сотрудника на его ближайшие несколько лет жизни | +| Дата приема | Дата когда человек подписал ~~приговор~~ договор. Ограничение: не более 10 лет, от текущей даты | +| Оклад | Значение коррелирует с суффиксом должности
- Junior = 1x
- Middle = 1.5x
- Senior = 2.1x
- Team Lead = 2.6x | +| Электронная почта | Почта сотрудника для связи | +| Номер телефона | Номер телефона сотрудника для связи | +| Дата увольнения | Данное поле присутствует только если инификатор увольнения `true` | + +### Реализованные фичи +- Добавлен сервис генерации сотрудников с кешированием +- Выполнена оркестрация Aspire +- Логирование основных действий + +### Скриншоты работащего приложения
-1. «Кэширование» - Реализация сервиса генерации контрактов, кэширование его ответов -
- -В рамках первой лабораторной работы необходимо: -* Реализовать сервис генерации контрактов на основе Bogus, -* Реализовать кеширование при помощи IDistributedCache и Redis, -* Реализовать структурное логирование сервиса генерации, -* Настроить оркестрацию Aspire. - + + Поднятые контейнеры + + image
-
-2. «Балансировка нагрузки» - Реализация апи гейтвея, настройка его работы -
- -В рамках второй лабораторной работы необходимо: -* Настроить оркестрацию на запуск нескольких реплик сервиса генерации, -* Реализовать апи гейтвей на основе Ocelot, -* Имплементировать алгоритм балансировки нагрузки согласно варианту. - -
-
-
-3. «Интеграционное тестирование» - Реализация файлового сервиса и объектного хранилища, интеграционное тестирование бекенда -
-В рамках третьей лабораторной работы необходимо: -* Добавить в оркестрацию объектное хранилище, -* Реализовать файловый сервис, сериализующий сгенерированные данные в файлы и сохраняющий их в объектном хранилище, -* Реализовать отправку генерируемых данных в файловый сервис посредством брокера, -* Реализовать интеграционные тесты, проверяющие корректность работы всех сервисов бекенда вместе. - -
-
-4. (Опционально) «Переход на облачную инфраструктуру» - Перенос бекенда в Yandex Cloud -
- -В рамках четвертой лабораторной работы необходимо перенестиервисы на облако все ранее разработанные сервисы: -* Клиент - в хостинг через отдельный бакет Object Storage, -* Сервис генерации - в Cloud Function, -* Апи гейтвей - в Serverless Integration как API Gateway, -* Брокер сообщений - в Message Queue, -* Файловый сервис - в Cloud Function, -* Объектное хранилище - в отдельный бакет Object Storage, - -
+ + Пример полученного сотрудника + + image
-## Задание. Общая часть -**Обязательно**: -* Реализация серверной части на [.NET 8](https://learn.microsoft.com/ru-ru/dotnet/core/whats-new/dotnet-8/overview). -* Оркестрация проектов при помощи [.NET Aspire](https://learn.microsoft.com/ru-ru/dotnet/aspire/get-started/aspire-overview). -* Реализация сервиса генерации данных при помощи [Bogus](https://github.com/bchavez/Bogus). -* Реализация тестов с использованием [xUnit](https://xunit.net/?tabs=cs). -* Создание минимальной документации к проекту: страница на GitHub с информацией о задании, скриншоты приложения и прочая информация. - -**Факультативно**: -* Перенос бекенда на облачную инфраструктуру Yandex Cloud - -Внимательно прочитайте [дискуссии](https://github.com/itsecd/cloud-development/discussions/1) о том, как работает автоматическое распределение на ревью. -Сразу корректно называйте свои pr, чтобы они попали на ревью нужному преподавателю. - -По итогу работы в семестре должна получиться следующая информационная система:
-C4 диаграмма -Современные_технологии_разработки_ПО_drawio + + Трассировки и демонстрация работы кэширования + + image
-## Варианты заданий -Номер варианта задания присваивается в начале семестра. Изменить его нельзя. Каждый вариант имеет уникальную комбинацию из предметной области, базы данных и технологии для общения сервиса генерации данных и сервера апи. - -[Список вариантов](https://docs.google.com/document/d/1WGmLYwffTTaAj4TgFCk5bUyW3XKbFMiBm-DHZrfFWr4/edit?usp=sharing) -[Список предметных областей и алгоритмов балансировки](https://docs.google.com/document/d/1PLn2lKe4swIdJDZhwBYzxqFSu0AbY2MFY1SUPkIKOM4/edit?usp=sharing) - -## Схема сдачи +## 📝 Лабораторная работа №2 -На каждую из лабораторных работ необходимо сделать отдельный [Pull Request (PR)](https://docs.github.com/en/pull-requests). +Реализация ApiGateway, настройка его работы -Общая схема: -1. Сделать форк данного репозитория -2. Выполнить задание -3. Сделать PR в данный репозиторий -4. Исправить замечания после code review -5. Получить approve +### Цели +- Настроить оркестрацию на запуск нескольких реплик сервиса генерации +- Реализовать ApiGateway на основе **Ocelot** +- Имплементировать алгоритм балансировки **QueryBased** -## Критерии оценивания +### Реализованные фичи +- Реализован алгоритм балансировки +- Реализовн ApiGateway и подняты три реплики сервиса генерации -Конкурентный принцип. -Так как задания в первой лабораторной будут повторяться между студентами, то выделяются следующие показатели для оценки: -1. Скорость разработки -2. Качество разработки -3. Полнота выполнения задания - -Быстрее делаете PR - у вас преимущество. -Быстрее получаете Approve - у вас преимущество. -Выполните нечто немного выходящее за рамки проекта - у вас преимущество. -Не укладываетесь в дедлайн - получаете минимально возможный балл. - -### Шкала оценивания - -- **3 балла** за качество кода, из них: - - 2 балла - базовая оценка - - 1 балл (но не более) можно получить за выполнение любого из следующих пунктов: - - Реализация факультативного функционала - - Выполнение работы раньше других: первые 5 человек из каждой группы, которые сделали PR и получили approve, получают дополнительный балл - -## Вопросы и обратная связь по курсу +### Скриншоты работащего приложения +
+ + Поднятые контейнеры + + image +
-Чтобы задать вопрос по лабораторной, воспользуйтесь [соответствующим разделом дискуссий](https://github.com/itsecd/cloud-development/discussions/categories/questions) или заведите [ишью](https://github.com/itsecd/cloud-development/issues/new). -Если у вас появились идеи/пожелания/прочие полезные мысли по преподаваемой дисциплине, их можно оставить [здесь](https://github.com/itsecd/cloud-development/discussions/categories/ideas). +
+ + Граф + + image +
+
+ + Логи генерации + + image +