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
13 changes: 13 additions & 0 deletions ApiGateway/ApiGateway.csproj
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лишний проект

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<ProjectReference Include="..\CompanyEmployee.ServiceDefaults\CompanyEmployee.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions ApiGateway/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();
var app = builder.Build();

app.MapDefaultEndpoints();

app.MapGet("/", () => "Hello World!");

app.Run();
38 changes: 38 additions & 0 deletions ApiGateway/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:49541",
"sslPort": 44328
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5176",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7163;http://localhost:5176",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions ApiGateway/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 ApiGateway/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
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>№1 "Кэширование"</Strong></UnorderedListItem>
<UnorderedListItem>Вариант <Strong>№5 "Сотрудник компании"</Strong></UnorderedListItem>
<UnorderedListItem>Выполнена <Strong>Дубовой Ксенией 6511</Strong> </UnorderedListItem>
<UnorderedListItem><Link To="https://github.com/kseniaDubova/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
16 changes: 8 additions & 8 deletions Client.Wasm/wwwroot/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"BaseAddress": ""
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"BaseAddress": "http://localhost:5212/api/CompanyEmployee"
}
28 changes: 26 additions & 2 deletions CloudDevelopment.sln
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.36811.4
# Visual Studio Version 18
VisualStudioVersion = 18.3.11520.95
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}") = "CompanyEmployee.ApiService", "CompanyEmployee.ApiService\CompanyEmployee.ApiService.csproj", "{744675CA-BC69-5513-0A1B-3BCC004F02F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployee.AppHost", "CompanyEmployee.AppHost\CompanyEmployee.AppHost.csproj", "{B9ED18C6-0485-F2DF-93F0-2F0EACE5381F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployee.ServiceDefaults", "CompanyEmployee.ServiceDefaults\CompanyEmployee.ServiceDefaults.csproj", "{60C547C0-C951-4270-1D2E-4BB68A5739B6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CompanyEmployee.ApiGateway", "CompanyEmployee.ApiGateway\CompanyEmployee.ApiGateway.csproj", "{D0087FC1-B713-A582-9A28-3814270CFDAD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -15,6 +23,22 @@ 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
{744675CA-BC69-5513-0A1B-3BCC004F02F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{744675CA-BC69-5513-0A1B-3BCC004F02F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{744675CA-BC69-5513-0A1B-3BCC004F02F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{744675CA-BC69-5513-0A1B-3BCC004F02F1}.Release|Any CPU.Build.0 = Release|Any CPU
{B9ED18C6-0485-F2DF-93F0-2F0EACE5381F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B9ED18C6-0485-F2DF-93F0-2F0EACE5381F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B9ED18C6-0485-F2DF-93F0-2F0EACE5381F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B9ED18C6-0485-F2DF-93F0-2F0EACE5381F}.Release|Any CPU.Build.0 = Release|Any CPU
{60C547C0-C951-4270-1D2E-4BB68A5739B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{60C547C0-C951-4270-1D2E-4BB68A5739B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60C547C0-C951-4270-1D2E-4BB68A5739B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60C547C0-C951-4270-1D2E-4BB68A5739B6}.Release|Any CPU.Build.0 = Release|Any CPU
{D0087FC1-B713-A582-9A28-3814270CFDAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D0087FC1-B713-A582-9A28-3814270CFDAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D0087FC1-B713-A582-9A28-3814270CFDAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D0087FC1-B713-A582-9A28-3814270CFDAD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
17 changes: 17 additions & 0 deletions CompanyEmployee.ApiGateway/CompanyEmployee.ApiGateway.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Ocelot" Version="24.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CompanyEmployee.ServiceDefaults\CompanyEmployee.ServiceDefaults.csproj" />
</ItemGroup>

</Project>
58 changes: 58 additions & 0 deletions CompanyEmployee.ApiGateway/LoadBalancer/WeightedRoundRobin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Ocelot.LoadBalancer.Errors;
using Ocelot.LoadBalancer.Interfaces;
using Ocelot.Responses;
using Ocelot.ServiceDiscovery.Providers;
using Ocelot.Values;

namespace CompanyEmployee.ApiGateway.LoadBalancer;

public class WeightedRoundRobin : ILoadBalancer
{
private static List<ServiceHostAndPort> _expandedList = new();
private static int _index = 0;
private static readonly object _lock = new();
private static bool _initialized = false;

public string Type => nameof(WeightedRoundRobin);

public WeightedRoundRobin(
List<ServiceHostAndPort> services,
IConfiguration config)
{
if (_initialized) return;

var weights = config
.GetSection("LoadBalancerWeights")
.Get<Dictionary<string, int>>() ?? new();


foreach (var s in services)
{

var key = s.DownstreamPort.ToString();
var weight = weights.TryGetValue(key, out var w) ? w : 1;

for (var i = 0; i < weight; i++)
{
_expandedList.Add(s);
}
}
Comment on lines +29 to +39
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Лучше алгоритмически вычислять какой сервис нужно использовать, в таком варианте если веса у сервисов будут большие, то получится огромный список


_initialized = true;

}

public Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext context)
{
lock (_lock)
{
var s = _expandedList[_index];
_index = (_index + 1) % _expandedList.Count;

return Task.FromResult<Response<ServiceHostAndPort>>(
new OkResponse<ServiceHostAndPort>(s));
}
}

public void Release(ServiceHostAndPort hostAndPort) { }
}
28 changes: 28 additions & 0 deletions CompanyEmployee.ApiGateway/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using CompanyEmployee.ApiGateway.LoadBalancer;
using Ocelot.DependencyInjection;
using Ocelot.LoadBalancer.Interfaces;
using Ocelot.Middleware;
using Ocelot.Values;

var builder = WebApplication.CreateBuilder(args);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не хватает builder.AddServiceDefaults();

builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);

builder.Services
.AddOcelot(builder.Configuration)
.AddCustomLoadBalancer((route, _) =>
{
var config = builder.Configuration;

var services = route.DownstreamAddresses
.Select(h => new ServiceHostAndPort(h.Host, h.Port))
.ToList();

return new WeightedRoundRobin(services, config);
});

var app = builder.Build();

await app.UseOcelot();

app.Run();
38 changes: 38 additions & 0 deletions CompanyEmployee.ApiGateway/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:26883",
"sslPort": 44388
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5212",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7092;http://localhost:5212",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
11 changes: 11 additions & 0 deletions CompanyEmployee.ApiGateway/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"GlobalConfiguration": {
"BaseUrl": "https://localhost:5212"
}
}
14 changes: 14 additions & 0 deletions CompanyEmployee.ApiGateway/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"LoadBalancerWeights": {
"5213": 1,
"5214": 2,
"5215": 1
},
"AllowedHosts": "*"
}
28 changes: 28 additions & 0 deletions CompanyEmployee.ApiGateway/ocelot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"Generators": [ "generator-1", "generator-2", "generator-3" ],
"Routes": [
{
"DownstreamPathTemplate": "/api/CompanyEmployee",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5213
},
{
"Host": "localhost",
"Port": 5214
},
{
"Host": "localhost",
"Port": 5215
}
],
Comment on lines +7 to +20
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если поменять порты в aspire, то перестанет работать, по-хорошему можно перезаписывать эту конфигурацию на основе переменных окружения, которые приходят из aspire, в них будут адреса сервисов из-за .WithReference

"UpstreamPathTemplate": "/api/CompanyEmployee",
"UpstreamHttpMethod": [ "GET" ],
"LoadBalancerOptions": {
"Type": "WeightedRoundRobin"
}
}
]
}
Loading
Loading