Skip to content
Closed
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
1 change: 1 addition & 0 deletions Client.Wasm/Client.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Configurations>Debug;Release;123</Configurations>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 6 additions & 4 deletions Client.Wasm/Components/StudentCard.razor
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
</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>№8 "Сотрудник компании"</Strong>
</UnorderedListItem>
<UnorderedListItem>Выполнена <Strong>Земляновым Владимиром 6511</Strong> </UnorderedListItem>
<UnorderedListItem><Link To="https://github.com/AvtoBBus/cloud-development">Ссылка на форк</Link></UnorderedListItem>
</UnorderedList>
</CardBody>
</Card>
12 changes: 6 additions & 6 deletions Client.Wasm/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@
"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"
}
},
"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"
}
}
}
}
}
4 changes: 2 additions & 2 deletions Client.Wasm/wwwroot/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
}
},
"AllowedHosts": "*",
"BaseAddress": ""
}
"BaseAddress": "http://localhost:5200/employee"
}
35 changes: 35 additions & 0 deletions CloudDevelopment.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,51 @@ 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
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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
17 changes: 17 additions & 0 deletions CompanyEmployees.ApiGateway/CompanyEmployees.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>
<ProjectReference Include="..\CompanyEmployees.ServiceDefaults\CompanyEmployees.ServiceDefaults.csproj" />
</ItemGroup>

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

</Project>
50 changes: 50 additions & 0 deletions CompanyEmployees.ApiGateway/LoadBalancer/QueryBased.cs
Original file line number Diff line number Diff line change
@@ -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<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext)
{
var services = await serviceDiscovery.GetAsync();

if (services is null)
return new ErrorResponse<ServiceHostAndPort>(
new ServicesAreNullError("Service discovery returned null"));

if (services.Count == 0)
return new ErrorResponse<ServiceHostAndPort>(
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<ServiceHostAndPort>(services[id % services.Count].HostAndPort);
}

private static OkResponse<ServiceHostAndPort> SelectRandomService(List<Service> currentServices)
{
var randomIndex = Random.Shared.Next(currentServices.Count);
return new OkResponse<ServiceHostAndPort>(currentServices[randomIndex].HostAndPort);
}
public void Release(ServiceHostAndPort hostAndPort) { }
}
37 changes: 37 additions & 0 deletions CompanyEmployees.ApiGateway/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using CompanyEmployees.ApiGateway.LoadBalancer;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration
Comment on lines +5 to +7
Copy link

Choose a reason for hiding this comment

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

Тут нужно будет добавить AddServiceDefaults();

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

var generators = builder.Configuration.GetSection("Generators").Get<string[]>() ?? [];

var addressOverrides = new List<KeyValuePair<string, string?>>();

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();

await app.UseOcelot();
await app.RunAsync();
14 changes: 14 additions & 0 deletions CompanyEmployees.ApiGateway/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
12 changes: 12 additions & 0 deletions CompanyEmployees.ApiGateway/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Ocelot": "Debug"
}
},
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5200"
}
}
9 changes: 9 additions & 0 deletions CompanyEmployees.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": "*"
}
28 changes: 28 additions & 0 deletions CompanyEmployees.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": "/employee",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5201
},
{
"Host": "localhost",
"Port": 5202
},
{
"Host": "localhost",
"Port": 5203
}
],
"UpstreamPathTemplate": "/employee",
"UpstreamHttpMethod": [ "GET" ],
"LoadBalancerOptions": {
"Type": "QueryBased"
}
}
]
}
27 changes: 27 additions & 0 deletions CompanyEmployees.AppHost/CompanyEmployees.AppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<Sdk Name="Aspire.AppHost.Sdk" Version="9.5.2" />

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
<Configurations>Debug;Release;123</Configurations>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="9.5.2" />
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.5.2" />
<PackageReference Include="Aspire.Hosting.Redis" Version="9.5.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Client.Wasm\Client.Wasm.csproj" />
<ProjectReference Include="..\CompanyEmployees.ApiGateway\CompanyEmployees.ApiGateway.csproj" />
<ProjectReference Include="..\CompanyEmployees.Generator\CompanyEmployees.Generator.csproj" />
<ProjectReference Include="..\CompanyEmployees.ServiceDefaults\CompanyEmployees.ServiceDefaults.csproj" />
</ItemGroup>

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

var redis = builder.AddRedis("redis")
.WithRedisCommander();

var gatewayPort = builder.Configuration.GetValue<int>("GatewayPort");
var gateway = builder
.AddProject<Projects.CompanyEmployees_ApiGateway>("companyemployees-apigateway")
.WithExternalHttpEndpoints();

for (var i = 0; i < 3; ++i)
{
var currGenerator = builder.AddProject<Projects.CompanyEmployees_Generator>($"generator-{i + 1}")
.WithEndpoint("http", endpoint => endpoint.Port = gatewayPort + 1 + i)
.WithReference(redis)
.WaitFor(redis);

gateway
.WithReference(currGenerator)
.WaitFor(currGenerator);
}

builder.AddProject<Projects.Client_Wasm>("client")
.WithReference(gateway)
.WaitFor(gateway);

builder.Build().Run();
29 changes: 29 additions & 0 deletions CompanyEmployees.AppHost/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}
8 changes: 8 additions & 0 deletions CompanyEmployees.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"
}
}
}
Loading