Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Api.GateWay/Api.GateWay.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Ocelot" Version="23.4.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CloudDevelopment.ServiceDefaults\CloudDevelopment.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
45 changes: 45 additions & 0 deletions Api.GateWay/LoadBalancers/WeightedRoundRobin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Responses;
using Ocelot.Values;

namespace Api.GateWay.LoadBalancers;

/// <summary>
/// Балансировщик нагрузки на основе параметров запроса
/// </summary>
/// <param name="services">Функция получения списка доступных сервисов</param>
public class WeightedRoundRobin(Func<Task<List<Service>>> services) : ILoadBalancer
{
private readonly Func<Task<List<Service>>> _services = services;
private readonly int[] _weights = [1, 2, 3, 2, 1];
private int _currentIndex = -1;
private int _remainingCalls = 0;

public string Type => nameof(WeightedRoundRobin);

private static readonly object _lock = new();

public async Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext)
{
var services = await _services.Invoke();
if (services == null || services.Count == 0)
return new ErrorResponse<ServiceHostAndPort>(
new ServicesAreEmptyError("No services available"));

lock (_lock)
{
if (_currentIndex == -1 || _remainingCalls == 0)
{
_currentIndex = (_currentIndex + 1) % services.Count;
_remainingCalls = _weights[_currentIndex];
}

var selectedService = services[_currentIndex];
_remainingCalls--;

return new OkResponse<ServiceHostAndPort>(selectedService.HostAndPort);
}
}

public void Release(ServiceHostAndPort hostAndPort) { }
}
26 changes: 26 additions & 0 deletions Api.GateWay/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Api.GateWay.LoadBalancers;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
builder.Services.AddServiceDiscovery();
builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);
builder.Services.AddOcelot()
.AddCustomLoadBalancer<WeightedRoundRobin>((_, _, provider) => new(provider.GetAsync));

builder.Services.AddCors(options => options.AddDefaultPolicy(policy =>
{
policy.WithOrigins(["http://localhost:5127", "https://localhost:7282"]);
policy.WithMethods("GET");
policy.WithHeaders("Content-Type");
}));

var app = builder.Build();

app.UseCors();

await app.UseOcelot();

app.Run();
38 changes: 38 additions & 0 deletions Api.GateWay/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:9811",
"sslPort": 44389
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5142",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7147;http://localhost:5142",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions Api.GateWay/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions Api.GateWay/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
35 changes: 35 additions & 0 deletions Api.GateWay/ocelot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"Routes": [
{
"UpstreamPathTemplate": "/employee",
"UpstreamHttpMethod": [ "GET" ],
"DownstreamPathTemplate": "/employee",
"DownstreamScheme": "https",
"LoadBalancerOptions": {
"Type": "WeightedRoundRobin"
},
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 15000
},
{
"Host": "localhost",
"Port": 15001
},
{
"Host": "localhost",
"Port": 15002
},
{
"Host": "localhost",
"Port": 15003
},
{
"Host": "localhost",
"Port": 15004
}
]
}
]
}
8 changes: 4 additions & 4 deletions Client.Wasm/Components/StudentCard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
</CardHeader>
<CardBody>
<UnorderedList Unstyled>
<UnorderedListItem>Номер <Strong>№X "Название лабораторной"</Strong></UnorderedListItem>
<UnorderedListItem>Вариант <Strong>№Х "Название варианта"</Strong></UnorderedListItem>
<UnorderedListItem>Выполнена <Strong>Фамилией Именем 65ХХ</Strong> </UnorderedListItem>
<UnorderedListItem><Link To="https://puginarug.com/">Ссылка на форк</Link></UnorderedListItem>
<UnorderedListItem>Номер <Strong>№2 "Балансировка нагрузки"</Strong></UnorderedListItem>
<UnorderedListItem>Вариант <Strong>№40 "Сотрудник Компании"</Strong></UnorderedListItem>
<UnorderedListItem>Выполнена <Strong>Золотилов Никита 6513</Strong> </UnorderedListItem>
<UnorderedListItem><Link To="https://github.com/Chuck-man/cloud-development">Ссылка на форк</Link></UnorderedListItem>
</UnorderedList>
</CardBody>
</Card>
6 changes: 3 additions & 3 deletions Client.Wasm/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "http://localhost:5127",
"environmentVariables": {
Expand All @@ -22,7 +22,7 @@
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"applicationUrl": "https://localhost:7282;http://localhost:5127",
"environmentVariables": {
Expand All @@ -31,7 +31,7 @@
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchBrowser": false,
"inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
Expand Down
2 changes: 1 addition & 1 deletion Client.Wasm/wwwroot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
}
},
"AllowedHosts": "*",
"BaseAddress": ""
"BaseAddress": "https://localhost:7147/employee"
}
22 changes: 22 additions & 0 deletions CloudDevelopment.AppHost/AppHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using StackExchange.Redis;

var builder = DistributedApplication.CreateBuilder(args);

var cache = builder.AddRedis("employee-cache")
.WithRedisInsight(containerName: "employee-insight");

var gateway = builder.AddProject<Projects.Api_GateWay>("api-gateway");

for (var i = 0; i < 5; i++)
{
var service = builder.AddProject<Projects.Service_Api>($"service-api-{i}", launchProfileName: null)
.WithHttpsEndpoint(port: 15000 + i)
.WithReference(cache, "RedisCache")
.WaitFor(cache);
gateway.WaitFor(service);
}

var client = builder.AddProject<Projects.Client_Wasm>("employee")
.WaitFor(gateway);

builder.Build().Run();
22 changes: 22 additions & 0 deletions CloudDevelopment.AppHost/CloudDevelopment.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Aspire.AppHost.Sdk/13.1.0">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>4d29b81c-d306-4bbe-9b5e-f203441bda82</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.Apphost" Version="9.5.0" />
<PackageReference Include="Aspire.Hosting.Redis" Version="9.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Api.GateWay\Api.GateWay.csproj" />
<ProjectReference Include="..\Client.Wasm\Client.Wasm.csproj" />
<ProjectReference Include="..\Service.Api\Service.Api.csproj" />
</ItemGroup>

</Project>
31 changes: 31 additions & 0 deletions CloudDevelopment.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17059;http://localhost:15263",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21068",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23203",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22043"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15263",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19234",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18255",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20127"
}
}
}
}
8 changes: 8 additions & 0 deletions CloudDevelopment.AppHost/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions CloudDevelopment.AppHost/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireSharedProject>true</IsAspireSharedProject>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />

<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="10.1.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="10.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.14.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.14.0" />
</ItemGroup>

</Project>
Loading
Loading