From 0a20b75f1e31f0eb8ed667ddb4bb44e62d57a35e Mon Sep 17 00:00:00 2001 From: "dianaryzhenkova1@gmail.com" Date: Wed, 8 Apr 2026 20:29:46 +0400 Subject: [PATCH 1/6] =?UTF-8?q?=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=BE=D0=B9=201?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AspireApp.ApiService.csproj | 16 +++ AspireApp/AspireApp.ApiService/Program.cs | 37 ++++++ .../Properties/launchSettings.json | 23 ++++ .../appsettings.Development.json | 8 ++ .../AspireApp.ApiService/appsettings.json | 9 ++ AspireApp/AspireApp.AppHost/AppHost.cs | 13 ++ .../AspireApp.AppHost.csproj | 24 ++++ .../Properties/launchSettings.json | 29 +++++ .../appsettings.Development.json | 8 ++ AspireApp/AspireApp.AppHost/appsettings.json | 9 ++ .../AspireApp.ServiceDefaults.csproj | 22 ++++ .../AspireApp.ServiceDefaults/Extensions.cs | 119 ++++++++++++++++++ Client.Wasm/Components/StudentCard.razor | 8 +- Client.Wasm/wwwroot/appsettings.json | 2 +- CloudDevelopment.sln | 18 +++ ServiceApi/Entities/Employee.cs | 69 ++++++++++ ServiceApi/Generator/EmployeeGenerator.cs | 118 +++++++++++++++++ .../Generator/EmployeeGeneratorService.cs | 68 ++++++++++ .../Generator/IEmployeeGeneratorService.cs | 12 ++ ServiceApi/Program.cs | 20 +++ ServiceApi/Properties/launchSettings.json | 38 ++++++ ServiceApi/Service.Api.csproj | 16 +++ ServiceApi/appsettings.Development.json | 8 ++ ServiceApi/appsettings.json | 9 ++ 24 files changed, 698 insertions(+), 5 deletions(-) create mode 100644 AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj create mode 100644 AspireApp/AspireApp.ApiService/Program.cs create mode 100644 AspireApp/AspireApp.ApiService/Properties/launchSettings.json create mode 100644 AspireApp/AspireApp.ApiService/appsettings.Development.json create mode 100644 AspireApp/AspireApp.ApiService/appsettings.json create mode 100644 AspireApp/AspireApp.AppHost/AppHost.cs create mode 100644 AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj create mode 100644 AspireApp/AspireApp.AppHost/Properties/launchSettings.json create mode 100644 AspireApp/AspireApp.AppHost/appsettings.Development.json create mode 100644 AspireApp/AspireApp.AppHost/appsettings.json create mode 100644 AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj create mode 100644 AspireApp/AspireApp.ServiceDefaults/Extensions.cs create mode 100644 ServiceApi/Entities/Employee.cs create mode 100644 ServiceApi/Generator/EmployeeGenerator.cs create mode 100644 ServiceApi/Generator/EmployeeGeneratorService.cs create mode 100644 ServiceApi/Generator/IEmployeeGeneratorService.cs create mode 100644 ServiceApi/Program.cs create mode 100644 ServiceApi/Properties/launchSettings.json create mode 100644 ServiceApi/Service.Api.csproj create mode 100644 ServiceApi/appsettings.Development.json create mode 100644 ServiceApi/appsettings.json diff --git a/AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj b/AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj new file mode 100644 index 00000000..a811a636 --- /dev/null +++ b/AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj @@ -0,0 +1,16 @@ + + + + net8.0 + enable + enable + + + + + + + + + + diff --git a/AspireApp/AspireApp.ApiService/Program.cs b/AspireApp/AspireApp.ApiService/Program.cs new file mode 100644 index 00000000..c6c2a5e0 --- /dev/null +++ b/AspireApp/AspireApp.ApiService/Program.cs @@ -0,0 +1,37 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add service defaults & Aspire client integrations. +builder.AddServiceDefaults(); + +// Add services to the container. +builder.Services.AddProblemDetails(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +app.UseExceptionHandler(); + +string[] summaries = ["Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"]; + +app.MapGet("/weatherforecast", () => +{ + var forecast = Enumerable.Range(1, 5).Select(index => + new WeatherForecast + ( + DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + Random.Shared.Next(-20, 55), + summaries[Random.Shared.Next(summaries.Length)] + )) + .ToArray(); + return forecast; +}) +.WithName("GetWeatherForecast"); + +app.MapDefaultEndpoints(); + +app.Run(); + +record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) +{ + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); +} diff --git a/AspireApp/AspireApp.ApiService/Properties/launchSettings.json b/AspireApp/AspireApp.ApiService/Properties/launchSettings.json new file mode 100644 index 00000000..47b1820d --- /dev/null +++ b/AspireApp/AspireApp.ApiService/Properties/launchSettings.json @@ -0,0 +1,23 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "http://localhost:5435", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": false, + "applicationUrl": "https://localhost:7447;http://localhost:5435", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/AspireApp/AspireApp.ApiService/appsettings.Development.json b/AspireApp/AspireApp.ApiService/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/AspireApp/AspireApp.ApiService/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/AspireApp/AspireApp.ApiService/appsettings.json b/AspireApp/AspireApp.ApiService/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/AspireApp/AspireApp.ApiService/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/AspireApp/AspireApp.AppHost/AppHost.cs b/AspireApp/AspireApp.AppHost/AppHost.cs new file mode 100644 index 00000000..54b3941e --- /dev/null +++ b/AspireApp/AspireApp.AppHost/AppHost.cs @@ -0,0 +1,13 @@ +var builder = DistributedApplication.CreateBuilder(args); + +var cache = builder.AddRedis("employee-cache") + .WithRedisInsight(containerName: "employee-insight"); + +var service = builder.AddProject("service-api") + .WithReference(cache, "RedisCache") + .WaitFor(cache); + +builder.AddProject("employee-wasm") + .WaitFor(service); + +builder.Build().Run(); diff --git a/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj new file mode 100644 index 00000000..0d6320b5 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj @@ -0,0 +1,24 @@ + + + + + + Exe + net8.0 + enable + enable + true + 8acee786-1688-40c1-943e-5186ce386476 + + + + + + + + + + + + + diff --git a/AspireApp/AspireApp.AppHost/Properties/launchSettings.json b/AspireApp/AspireApp.AppHost/Properties/launchSettings.json new file mode 100644 index 00000000..82c959fa --- /dev/null +++ b/AspireApp/AspireApp.AppHost/Properties/launchSettings.json @@ -0,0 +1,29 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "profiles": { + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:17096;http://localhost:15155", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21139", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22017" + } + }, + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:15155", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development", + "DOTNET_ENVIRONMENT": "Development", + "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19197", + "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20116" + } + } + } +} diff --git a/AspireApp/AspireApp.AppHost/appsettings.Development.json b/AspireApp/AspireApp.AppHost/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/AspireApp/AspireApp.AppHost/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/AspireApp/AspireApp.AppHost/appsettings.json b/AspireApp/AspireApp.AppHost/appsettings.json new file mode 100644 index 00000000..31c092aa --- /dev/null +++ b/AspireApp/AspireApp.AppHost/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning", + "Aspire.Hosting.Dcp": "Warning" + } + } +} diff --git a/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj b/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj new file mode 100644 index 00000000..6c036a13 --- /dev/null +++ b/AspireApp/AspireApp.ServiceDefaults/AspireApp.ServiceDefaults.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + true + + + + + + + + + + + + + + + diff --git a/AspireApp/AspireApp.ServiceDefaults/Extensions.cs b/AspireApp/AspireApp.ServiceDefaults/Extensions.cs new file mode 100644 index 00000000..13151bf4 --- /dev/null +++ b/AspireApp/AspireApp.ServiceDefaults/Extensions.cs @@ -0,0 +1,119 @@ +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 .NET 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 +{ + 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() + // 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("/health"); + + // Only health checks tagged with the "live" tag must pass for app to be considered alive + app.MapHealthChecks("/alive", new HealthCheckOptions + { + Predicate = r => r.Tags.Contains("live") + }); + } + + return app; + } +} diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 661f1181..065320bf 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,10 +4,10 @@ - Номер №X "Название лабораторной" - Вариант №Х "Название варианта" - Выполнена Фамилией Именем 65ХХ - Ссылка на форк + Номер №1 "Кэширование" + Вариант №32 "Сотрудник компании" + Выполнена Рыженковой Дианой 6512 + Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index d1fe7ab3..378fb252 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "" + "BaseAddress": "https://localhost:7099/employee" } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index cb48241d..98a8e888 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -5,6 +5,12 @@ 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}") = "AspireApp.AppHost", "AspireApp\AspireApp.AppHost\AspireApp.AppHost.csproj", "{2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireApp.ServiceDefaults", "AspireApp\AspireApp.ServiceDefaults\AspireApp.ServiceDefaults.csproj", "{A0BC68FF-8BE6-4035-BF3B-561CB4631B57}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.Api", "ServiceApi\Service.Api.csproj", "{888B9F35-3A72-42E9-A176-BA7A19ED878C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +21,18 @@ Global {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Debug|Any CPU.Build.0 = Debug|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.ActiveCfg = Release|Any CPU {AE7EEA74-2FE0-136F-D797-854FD87E022A}.Release|Any CPU.Build.0 = Release|Any CPU + {2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2CF45C7F-89EA-48F5-8350-A59AF1F82BC7}.Release|Any CPU.Build.0 = Release|Any CPU + {A0BC68FF-8BE6-4035-BF3B-561CB4631B57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A0BC68FF-8BE6-4035-BF3B-561CB4631B57}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A0BC68FF-8BE6-4035-BF3B-561CB4631B57}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A0BC68FF-8BE6-4035-BF3B-561CB4631B57}.Release|Any CPU.Build.0 = Release|Any CPU + {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ServiceApi/Entities/Employee.cs b/ServiceApi/Entities/Employee.cs new file mode 100644 index 00000000..6bac3f41 --- /dev/null +++ b/ServiceApi/Entities/Employee.cs @@ -0,0 +1,69 @@ +using System.Text.Json.Serialization; + +namespace Service.Api.Entities; + +/// +/// Сотрудник компании +/// +public class Employee +{ + /// + /// Идентификатор сотрудника в системе + /// + [JsonPropertyName("id")] + public int Id { get; set; } + + /// + /// ФИО + /// + [JsonPropertyName("fullName")] + public string? FullName { get; set; } + + /// + /// Должность + /// + [JsonPropertyName("position")] + public string? Position { get; set; } + + /// + /// Отдел + /// + [JsonPropertyName("department")] + public string? Department { get; set; } + + /// + /// Дата приема + /// + [JsonPropertyName("hireDate")] + public DateOnly HireDate { get; set; } + + /// + /// Оклад + /// + [JsonPropertyName("salary")] + public decimal Salary { get; set; } + + /// + /// Электронная почта + /// + [JsonPropertyName("email")] + public string? Email { get; set; } + + /// + /// Номер телефона + /// + [JsonPropertyName("phone")] + public string? Phone { get; set; } + + /// + /// Индикатор увольнения + /// + [JsonPropertyName("isFired")] + public bool IsFired { get; set; } + + /// + /// Дата увольнения + /// + [JsonPropertyName("fireDate")] + public DateOnly? FireDate { get; set; } +} \ No newline at end of file diff --git a/ServiceApi/Generator/EmployeeGenerator.cs b/ServiceApi/Generator/EmployeeGenerator.cs new file mode 100644 index 00000000..560bde05 --- /dev/null +++ b/ServiceApi/Generator/EmployeeGenerator.cs @@ -0,0 +1,118 @@ +using Bogus; +using Bogus.DataSets; +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Генератор случайных сотрудников компании +/// +public static class EmployeeGenerator +{ + private static readonly Faker _faker; + + /// + /// Справочник профессий + /// + private static readonly string[] _professions = + { + "Developer", + "Manager", + "Analyst", + "Tester", + "Administrator", + "Designer" + }; + + /// + /// Справочник суффиксов + /// + private static readonly string[] _levels = + { + "Junior", + "Middle", + "Senior" + }; + + static EmployeeGenerator() + { + _faker = new Faker("ru") + + .RuleFor(e => e.Id, f => f.IndexFaker + 1) + + // ФИО + .RuleFor(e => e.FullName, f => + { + var gender = f.PickRandom(); + + var firstName = f.Name.FirstName(gender); + var lastName = f.Name.LastName(gender); + + var patronymicSuffix = gender == Name.Gender.Male ? "ович" : "овна"; + var patronymic = f.Name.FirstName(Name.Gender.Male) + patronymicSuffix; + + return $"{lastName} {firstName} {patronymic}"; + }) + + // Должность + .RuleFor(e => e.Position, f => + { + var level = f.PickRandom(_levels); + var profession = f.PickRandom(_professions); + + return $"{level} {profession}"; + }) + + // Отдел + .RuleFor(e => e.Department, f => f.Commerce.Department()) + + // Дата приема (не более 10 лет назад) + .RuleFor(e => e.HireDate, + f => DateOnly.FromDateTime(f.Date.Past(10))) + + // Оклад (зависит от уровня) + .RuleFor(e => e.Salary, (f, e) => + { + if (e.Position!.Contains("Junior")) + return Math.Round(f.Random.Decimal(50000, 90000), 2); + + if (e.Position.Contains("Middle")) + return Math.Round(f.Random.Decimal(90000, 150000), 2); + + return Math.Round(f.Random.Decimal(150000, 250000), 2); + }) + + // Email + .RuleFor(e => e.Email, f => f.Internet.Email()) + + // Телефон + .RuleFor(e => e.Phone, + f => f.Phone.PhoneNumber("+7(###)###-##-##")) + + // Индикатор увольнения + .RuleFor(e => e.IsFired, + f => f.Random.Bool(0.2f)) // 20% сотрудников уволены + + // Дата увольнения + .RuleFor(e => e.FireDate, (f, e) => + { + if (!e.IsFired) + return null; + + return DateOnly.FromDateTime( + f.Date.Between( + e.HireDate.ToDateTime(TimeOnly.MinValue), + DateTime.Now)); + }); + } + + /// + /// Генерирует одного случайного сотрудника + /// + public static Employee Generate(int id) + { + var employee = _faker.Generate(); + employee.Id = id; + return employee; + } +} \ No newline at end of file diff --git a/ServiceApi/Generator/EmployeeGeneratorService.cs b/ServiceApi/Generator/EmployeeGeneratorService.cs new file mode 100644 index 00000000..dbd3a6b5 --- /dev/null +++ b/ServiceApi/Generator/EmployeeGeneratorService.cs @@ -0,0 +1,68 @@ +using Microsoft.Extensions.Caching.Distributed; +using Service.Api.Entities; +using StackExchange.Redis; +using System.Reflection.Emit; +using System.Runtime.CompilerServices; +using System.Text.Json; +namespace Service.Api.Generator; + +public class EmployeeGeneratorService(IDistributedCache cache, ILogger logger, IConfiguration configuration) : IEmployeeGeneratorService +{ + /// + /// Время инициализации кэша + /// + private readonly TimeSpan _cacheExpiration = int.TryParse(configuration["CacheExpiration"], out var sec) + ? TimeSpan.FromSeconds(sec) + : TimeSpan.FromSeconds(3600); + + public async Task ProcessEmployee(int id) + { + logger.LogInformation("Обработка сотрудника {id} ", id); + try + { + logger.LogInformation("Trying to get employee {id} from cache", id); + var employee = await RetrieveFromCache(id); + if (employee != null) + { + logger.LogInformation("Employee {id} was found in cache", id); + return employee; + } + logger.LogInformation("No employee {id} in cache. Generating employee", id); + employee = EmployeeGenerator.Generate(id); + logger.LogInformation("Populating the cache with employee {id}", id); + await PopulateCache(employee); + return employee; + } + catch (Exception ex) + { + logger.LogError(ex, "Exception occurred during employee {id} processing", id); + throw; + } + + } + + /// + /// Пытается достать сотрудника из кэша + /// + private async Task RetrieveFromCache(int id) + { + var json = await cache.GetStringAsync(id.ToString()); + if (string.IsNullOrEmpty(json)) + return null; + return JsonSerializer.Deserialize(json); + } + + /// + /// Кладет сотрудника в кэш + /// + private async Task PopulateCache(Employee employee) + { + var json = JsonSerializer.Serialize(employee); + await cache.SetStringAsync(employee.Id.ToString(), json, + new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = _cacheExpiration + }); + } + +} diff --git a/ServiceApi/Generator/IEmployeeGeneratorService.cs b/ServiceApi/Generator/IEmployeeGeneratorService.cs new file mode 100644 index 00000000..8ae6fda8 --- /dev/null +++ b/ServiceApi/Generator/IEmployeeGeneratorService.cs @@ -0,0 +1,12 @@ +using Service.Api.Entities; + +namespace Service.Api.Generator; + +/// +/// Интерфейс для записи юзкейса по обработке сотрудников компании +/// +public interface IEmployeeGeneratorService +{ + Task ProcessEmployee(int id); + +} diff --git a/ServiceApi/Program.cs b/ServiceApi/Program.cs new file mode 100644 index 00000000..6e0dec3c --- /dev/null +++ b/ServiceApi/Program.cs @@ -0,0 +1,20 @@ +using Service.Api.Generator; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); +builder.AddRedisDistributedCache("RedisCache"); + +builder.Services.AddScoped(); +builder.Services.AddCors(options => options.AddDefaultPolicy(policy => +{ + policy.AllowAnyOrigin(); + policy.AllowAnyMethod(); + policy.AllowAnyHeader(); +})); + +var app = builder.Build(); +app.MapDefaultEndpoints(); +app.MapGet("/employee", (IEmployeeGeneratorService service, int id) => service.ProcessEmployee(id)); +app.UseCors(); +app.Run(); diff --git a/ServiceApi/Properties/launchSettings.json b/ServiceApi/Properties/launchSettings.json new file mode 100644 index 00000000..d3afdc35 --- /dev/null +++ b/ServiceApi/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:11792", + "sslPort": 44309 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5081", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7099;http://localhost:5081", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/ServiceApi/Service.Api.csproj b/ServiceApi/Service.Api.csproj new file mode 100644 index 00000000..fad90748 --- /dev/null +++ b/ServiceApi/Service.Api.csproj @@ -0,0 +1,16 @@ + + + + net8.0 + enable + enable + + + + + + + + + + diff --git a/ServiceApi/appsettings.Development.json b/ServiceApi/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/ServiceApi/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/ServiceApi/appsettings.json b/ServiceApi/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/ServiceApi/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} From c13f8362047919559c52d14b6f01df6f4bb20b73 Mon Sep 17 00:00:00 2001 From: DianaRyz <114705840+DianaRyz@users.noreply.github.com> Date: Wed, 8 Apr 2026 20:44:41 +0400 Subject: [PATCH 2/6] Delete directory --- .../AspireApp.ApiService.csproj | 16 -------- AspireApp/AspireApp.ApiService/Program.cs | 37 ------------------- .../Properties/launchSettings.json | 23 ------------ .../appsettings.Development.json | 8 ---- .../AspireApp.ApiService/appsettings.json | 9 ----- 5 files changed, 93 deletions(-) delete mode 100644 AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj delete mode 100644 AspireApp/AspireApp.ApiService/Program.cs delete mode 100644 AspireApp/AspireApp.ApiService/Properties/launchSettings.json delete mode 100644 AspireApp/AspireApp.ApiService/appsettings.Development.json delete mode 100644 AspireApp/AspireApp.ApiService/appsettings.json diff --git a/AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj b/AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj deleted file mode 100644 index a811a636..00000000 --- a/AspireApp/AspireApp.ApiService/AspireApp.ApiService.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - - net8.0 - enable - enable - - - - - - - - - - diff --git a/AspireApp/AspireApp.ApiService/Program.cs b/AspireApp/AspireApp.ApiService/Program.cs deleted file mode 100644 index c6c2a5e0..00000000 --- a/AspireApp/AspireApp.ApiService/Program.cs +++ /dev/null @@ -1,37 +0,0 @@ -var builder = WebApplication.CreateBuilder(args); - -// Add service defaults & Aspire client integrations. -builder.AddServiceDefaults(); - -// Add services to the container. -builder.Services.AddProblemDetails(); - -var app = builder.Build(); - -// Configure the HTTP request pipeline. -app.UseExceptionHandler(); - -string[] summaries = ["Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"]; - -app.MapGet("/weatherforecast", () => -{ - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast"); - -app.MapDefaultEndpoints(); - -app.Run(); - -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} diff --git a/AspireApp/AspireApp.ApiService/Properties/launchSettings.json b/AspireApp/AspireApp.ApiService/Properties/launchSettings.json deleted file mode 100644 index 47b1820d..00000000 --- a/AspireApp/AspireApp.ApiService/Properties/launchSettings.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "$schema": "https://json.schemastore.org/launchsettings.json", - "profiles": { - "http": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": false, - "applicationUrl": "http://localhost:5435", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "https": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": false, - "applicationUrl": "https://localhost:7447;http://localhost:5435", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - } - } -} diff --git a/AspireApp/AspireApp.ApiService/appsettings.Development.json b/AspireApp/AspireApp.ApiService/appsettings.Development.json deleted file mode 100644 index 0c208ae9..00000000 --- a/AspireApp/AspireApp.ApiService/appsettings.Development.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - } -} diff --git a/AspireApp/AspireApp.ApiService/appsettings.json b/AspireApp/AspireApp.ApiService/appsettings.json deleted file mode 100644 index 10f68b8c..00000000 --- a/AspireApp/AspireApp.ApiService/appsettings.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "Logging": { - "LogLevel": { - "Default": "Information", - "Microsoft.AspNetCore": "Warning" - } - }, - "AllowedHosts": "*" -} From 9f9c65582975e651f98d8a79db28ab5352232cce Mon Sep 17 00:00:00 2001 From: "dianaryzhenkova1@gmail.com" Date: Wed, 8 Apr 2026 20:48:24 +0400 Subject: [PATCH 3/6] fix cors --- ServiceApi/Program.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ServiceApi/Program.cs b/ServiceApi/Program.cs index 6e0dec3c..b02167c4 100644 --- a/ServiceApi/Program.cs +++ b/ServiceApi/Program.cs @@ -8,9 +8,9 @@ builder.Services.AddScoped(); builder.Services.AddCors(options => options.AddDefaultPolicy(policy => { - policy.AllowAnyOrigin(); - policy.AllowAnyMethod(); - policy.AllowAnyHeader(); + policy.WithOrigins("https://localhost:36905") + .WithMethods("GET") + .AllowAnyHeader(); })); var app = builder.Build(); From fbce79814694a3b60ed2d6ef71cf762fbb89790e Mon Sep 17 00:00:00 2001 From: "dianaryzhenkova1@gmail.com" Date: Wed, 8 Apr 2026 21:41:46 +0400 Subject: [PATCH 4/6] fix --- Client.Wasm/wwwroot/appsettings.json | 2 +- ServiceApi/Program.cs | 2 +- ServiceApi/Properties/launchSettings.json | 27 +---------------------- 3 files changed, 3 insertions(+), 28 deletions(-) diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index 378fb252..39898806 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "https://localhost:7099/employee" + "BaseAddress": "http://localhost:7099/employee" } diff --git a/ServiceApi/Program.cs b/ServiceApi/Program.cs index b02167c4..dfb2905e 100644 --- a/ServiceApi/Program.cs +++ b/ServiceApi/Program.cs @@ -8,7 +8,7 @@ builder.Services.AddScoped(); builder.Services.AddCors(options => options.AddDefaultPolicy(policy => { - policy.WithOrigins("https://localhost:36905") + policy.WithOrigins("https://localhost:5127", "http://localhost:5127", "https://localhost:7282") .WithMethods("GET") .AllowAnyHeader(); })); diff --git a/ServiceApi/Properties/launchSettings.json b/ServiceApi/Properties/launchSettings.json index d3afdc35..1fb877b8 100644 --- a/ServiceApi/Properties/launchSettings.json +++ b/ServiceApi/Properties/launchSettings.json @@ -1,35 +1,10 @@ { - "$schema": "http://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:11792", - "sslPort": 44309 - } - }, "profiles": { "http": { "commandName": "Project", "dotnetRunMessages": true, "launchBrowser": true, - "applicationUrl": "http://localhost:5081", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "https": { - "commandName": "Project", - "dotnetRunMessages": true, - "launchBrowser": true, - "applicationUrl": "https://localhost:7099;http://localhost:5081", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "IIS Express": { - "commandName": "IISExpress", - "launchBrowser": true, + "applicationUrl": "http://localhost:7099", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } From c12b2f2a809e3afdd160eae4d07e38743552082b Mon Sep 17 00:00:00 2001 From: "dianaryzhenkova1@gmail.com" Date: Wed, 22 Apr 2026 12:57:43 +0400 Subject: [PATCH 5/6] fix --- AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj | 6 +++--- ServiceApi/Generator/EmployeeGenerator.cs | 6 +----- ServiceApi/Generator/EmployeeGeneratorService.cs | 3 --- ServiceApi/Service.Api.csproj | 2 +- 4 files changed, 5 insertions(+), 12 deletions(-) diff --git a/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj index 0d6320b5..2a31dad0 100644 --- a/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj +++ b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj @@ -1,6 +1,6 @@ - + Exe @@ -12,8 +12,8 @@ - - + + diff --git a/ServiceApi/Generator/EmployeeGenerator.cs b/ServiceApi/Generator/EmployeeGenerator.cs index 560bde05..2f344ec8 100644 --- a/ServiceApi/Generator/EmployeeGenerator.cs +++ b/ServiceApi/Generator/EmployeeGenerator.cs @@ -38,7 +38,6 @@ static EmployeeGenerator() { _faker = new Faker("ru") - .RuleFor(e => e.Id, f => f.IndexFaker + 1) // ФИО .RuleFor(e => e.FullName, f => @@ -99,10 +98,7 @@ static EmployeeGenerator() if (!e.IsFired) return null; - return DateOnly.FromDateTime( - f.Date.Between( - e.HireDate.ToDateTime(TimeOnly.MinValue), - DateTime.Now)); + return f.Date.BetweenDateOnly(e.HireDate, DateOnly.FromDateTime(DateTime.Now)); }); } diff --git a/ServiceApi/Generator/EmployeeGeneratorService.cs b/ServiceApi/Generator/EmployeeGeneratorService.cs index dbd3a6b5..64c9f2a7 100644 --- a/ServiceApi/Generator/EmployeeGeneratorService.cs +++ b/ServiceApi/Generator/EmployeeGeneratorService.cs @@ -1,8 +1,5 @@ using Microsoft.Extensions.Caching.Distributed; using Service.Api.Entities; -using StackExchange.Redis; -using System.Reflection.Emit; -using System.Runtime.CompilerServices; using System.Text.Json; namespace Service.Api.Generator; diff --git a/ServiceApi/Service.Api.csproj b/ServiceApi/Service.Api.csproj index fad90748..784bdefd 100644 --- a/ServiceApi/Service.Api.csproj +++ b/ServiceApi/Service.Api.csproj @@ -7,7 +7,7 @@ - + From 3205b09ccc71696605d8a68f5dce7b0f7971764a Mon Sep 17 00:00:00 2001 From: "dianaryzhenkova1@gmail.com" Date: Fri, 24 Apr 2026 00:58:09 +0400 Subject: [PATCH 6/6] =?UTF-8?q?=D0=92=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=BD=D0=BE=D0=B9=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Api.Gateway/Api.Gateway.csproj | 15 ++++++++ Api.Gateway/LoadBalancer/WeightedRandom.cs | 36 ++++++++++++++++++ Api.Gateway/Program.cs | 29 ++++++++++++++ Api.Gateway/Properties/launchSettings.json | 38 +++++++++++++++++++ Api.Gateway/appsettings.Development.json | 8 ++++ Api.Gateway/appsettings.json | 14 +++++++ Api.Gateway/ocelot.json | 23 +++++++++++ AspireApp/AspireApp.AppHost/AppHost.cs | 17 +++++++-- .../AspireApp.AppHost.csproj | 1 + Client.Wasm/Components/StudentCard.razor | 2 +- Client.Wasm/wwwroot/appsettings.json | 2 +- CloudDevelopment.sln | 6 +++ ServiceApi/Program.cs | 9 +---- 13 files changed, 187 insertions(+), 13 deletions(-) create mode 100644 Api.Gateway/Api.Gateway.csproj create mode 100644 Api.Gateway/LoadBalancer/WeightedRandom.cs create mode 100644 Api.Gateway/Program.cs create mode 100644 Api.Gateway/Properties/launchSettings.json create mode 100644 Api.Gateway/appsettings.Development.json create mode 100644 Api.Gateway/appsettings.json create mode 100644 Api.Gateway/ocelot.json diff --git a/Api.Gateway/Api.Gateway.csproj b/Api.Gateway/Api.Gateway.csproj new file mode 100644 index 00000000..921dca54 --- /dev/null +++ b/Api.Gateway/Api.Gateway.csproj @@ -0,0 +1,15 @@ + + + + net8.0 + enable + enable + + + + + + + + + diff --git a/Api.Gateway/LoadBalancer/WeightedRandom.cs b/Api.Gateway/LoadBalancer/WeightedRandom.cs new file mode 100644 index 00000000..cb12b404 --- /dev/null +++ b/Api.Gateway/LoadBalancer/WeightedRandom.cs @@ -0,0 +1,36 @@ +using Ocelot.LoadBalancer.Interfaces; +using Ocelot.Responses; +using Ocelot.Values; + +namespace Api.Gateway.LoadBalancer; + +/// +/// Балансировка случайным образом с весами +/// +public class WeightedRandom : ILoadBalancer +{ + private readonly Func>> _services = null!; + private static readonly object _locker = new(); + + private readonly int[] _values = null!; + + public string Type => nameof(WeightedRandom); + public WeightedRandom(Func>> services, IConfiguration configuration) + { + _services = services; + var frequencies = configuration.GetSection("LoadBalancer:WeightedRandom:Weights").Get(); + if (frequencies == null || frequencies.Length == 0) + throw new InvalidOperationException("Weights is empty or null. Add weights to configuration"); + _values = [.. Enumerable.Range(0, frequencies.Length).Zip(frequencies, (val, freq) => Enumerable.Repeat(val, freq)).SelectMany(x => x)]; + } + public async Task> LeaseAsync(HttpContext httpContext) + { + var services = await _services.Invoke(); + lock (_locker) + { + Random.Shared.Shuffle(_values); + return new OkResponse(services[_values.First()].HostAndPort); + } + } + public void Release(ServiceHostAndPort hostAndPort) { } +} \ No newline at end of file diff --git a/Api.Gateway/Program.cs b/Api.Gateway/Program.cs new file mode 100644 index 00000000..21889099 --- /dev/null +++ b/Api.Gateway/Program.cs @@ -0,0 +1,29 @@ +using Api.Gateway.LoadBalancer; +using Ocelot.DependencyInjection; +using Ocelot.Middleware; + +var builder = WebApplication.CreateBuilder(args); + +builder.AddServiceDefaults(); + +builder.Services.AddServiceDiscovery(); +builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true); +builder.Services.AddOcelot() + .AddCustomLoadBalancer((serviceProvider, _, discoveryProvider) => + { + var configuration = serviceProvider.GetRequiredService(); + return new WeightedRandom(discoveryProvider.GetAsync, configuration); + }); + + +builder.Services.AddCors(options => options.AddDefaultPolicy(policy => +{ + policy.WithOrigins("https://localhost:5127", "http://localhost:5127", "https://localhost:7282") + .WithMethods("GET") + .AllowAnyHeader(); +})); + +var app = builder.Build(); +app.UseCors(); +await app.UseOcelot(); +app.Run(); diff --git a/Api.Gateway/Properties/launchSettings.json b/Api.Gateway/Properties/launchSettings.json new file mode 100644 index 00000000..2de3e6c5 --- /dev/null +++ b/Api.Gateway/Properties/launchSettings.json @@ -0,0 +1,38 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:42987", + "sslPort": 44379 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "http://localhost:5086", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "applicationUrl": "https://localhost:7099;http://localhost:5086", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Api.Gateway/appsettings.Development.json b/Api.Gateway/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/Api.Gateway/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Api.Gateway/appsettings.json b/Api.Gateway/appsettings.json new file mode 100644 index 00000000..9699729e --- /dev/null +++ b/Api.Gateway/appsettings.json @@ -0,0 +1,14 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "LoadBalancer": { + "WeightedRandom": { + "Weights": [ 1, 2, 3, 2, 1 ] + } + } +} diff --git a/Api.Gateway/ocelot.json b/Api.Gateway/ocelot.json new file mode 100644 index 00000000..72647816 --- /dev/null +++ b/Api.Gateway/ocelot.json @@ -0,0 +1,23 @@ +{ + "Routes": [ + { + "UpstreamPathTemplate": "/employee", + "UpstreamHttpMethod": [ "GET" ], + "DownstreamPathTemplate": "/employee", + "DownstreamScheme": "https", + "DownstreamHostAndPorts": [ + { "Host": "localhost", "Port": 7170 }, + { "Host": "localhost", "Port": 7171 }, + { "Host": "localhost", "Port": 7172 }, + { "Host": "localhost", "Port": 7173 }, + { "Host": "localhost", "Port": 7174 } + ], + "LoadBalancerOptions": { + "Type": "WeightedRandom" + } + } + ], + "GlobalConfiguration": { + "BaseUrl": "https://localhost:7099" + } +} \ No newline at end of file diff --git a/AspireApp/AspireApp.AppHost/AppHost.cs b/AspireApp/AspireApp.AppHost/AppHost.cs index 54b3941e..e6fb82f0 100644 --- a/AspireApp/AspireApp.AppHost/AppHost.cs +++ b/AspireApp/AspireApp.AppHost/AppHost.cs @@ -3,11 +3,20 @@ var cache = builder.AddRedis("employee-cache") .WithRedisInsight(containerName: "employee-insight"); -var service = builder.AddProject("service-api") - .WithReference(cache, "RedisCache") - .WaitFor(cache); +var gateway = builder.AddProject("api-gateway"); + +for (var i = 0; i < 5; i++) +{ + var service = builder.AddProject($"employee-api-{i + 1}", launchProfileName: null) + .WithHttpsEndpoint(7170 + i) + .WithReference(cache, "RedisCache") + .WaitFor(cache); + gateway.WaitFor(service); +} builder.AddProject("employee-wasm") - .WaitFor(service); + .WaitFor(gateway); + + builder.Build().Run(); diff --git a/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj index 2a31dad0..3067cc4f 100644 --- a/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj +++ b/AspireApp/AspireApp.AppHost/AspireApp.AppHost.csproj @@ -17,6 +17,7 @@ + diff --git a/Client.Wasm/Components/StudentCard.razor b/Client.Wasm/Components/StudentCard.razor index 065320bf..9b541250 100644 --- a/Client.Wasm/Components/StudentCard.razor +++ b/Client.Wasm/Components/StudentCard.razor @@ -4,7 +4,7 @@ - Номер №1 "Кэширование" + Номер №2 "Балансировка нагрузки" Вариант №32 "Сотрудник компании" Выполнена Рыженковой Дианой 6512 Ссылка на форк diff --git a/Client.Wasm/wwwroot/appsettings.json b/Client.Wasm/wwwroot/appsettings.json index 39898806..378fb252 100644 --- a/Client.Wasm/wwwroot/appsettings.json +++ b/Client.Wasm/wwwroot/appsettings.json @@ -6,5 +6,5 @@ } }, "AllowedHosts": "*", - "BaseAddress": "http://localhost:7099/employee" + "BaseAddress": "https://localhost:7099/employee" } diff --git a/CloudDevelopment.sln b/CloudDevelopment.sln index 98a8e888..3323be18 100644 --- a/CloudDevelopment.sln +++ b/CloudDevelopment.sln @@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspireApp.ServiceDefaults", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Service.Api", "ServiceApi\Service.Api.csproj", "{888B9F35-3A72-42E9-A176-BA7A19ED878C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Api.Gateway", "Api.Gateway\Api.Gateway.csproj", "{FE44C324-0DA5-4434-8963-E9A17DC65ED8}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -33,6 +35,10 @@ Global {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Debug|Any CPU.Build.0 = Debug|Any CPU {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Release|Any CPU.ActiveCfg = Release|Any CPU {888B9F35-3A72-42E9-A176-BA7A19ED878C}.Release|Any CPU.Build.0 = Release|Any CPU + {FE44C324-0DA5-4434-8963-E9A17DC65ED8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FE44C324-0DA5-4434-8963-E9A17DC65ED8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FE44C324-0DA5-4434-8963-E9A17DC65ED8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FE44C324-0DA5-4434-8963-E9A17DC65ED8}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ServiceApi/Program.cs b/ServiceApi/Program.cs index dfb2905e..58c7e5df 100644 --- a/ServiceApi/Program.cs +++ b/ServiceApi/Program.cs @@ -6,15 +6,10 @@ builder.AddRedisDistributedCache("RedisCache"); builder.Services.AddScoped(); -builder.Services.AddCors(options => options.AddDefaultPolicy(policy => -{ - policy.WithOrigins("https://localhost:5127", "http://localhost:5127", "https://localhost:7282") - .WithMethods("GET") - .AllowAnyHeader(); -})); + var app = builder.Build(); app.MapDefaultEndpoints(); app.MapGet("/employee", (IEmployeeGeneratorService service, int id) => service.ProcessEmployee(id)); -app.UseCors(); + app.Run();