-
Notifications
You must be signed in to change notification settings - Fork 12
Горшенин Дмитрий Лаб. 2 Группа 6511 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8fd9d9d
e1db504
c0518fa
90ecc6e
8d9edea
85e73d2
9e1bd91
7d74621
089ed21
700e4da
23f9ff6
2f7c365
ec0ea34
f9ad2b3
229601e
757e46a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| { | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| }, | ||
| "AllowedHosts": "*", | ||
| "BaseAddress": "https://localhost:7170/land-plot" | ||
| "BaseAddress": "http://localhost:5200/credit-application" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <Sdk Name="Aspire.AppHost.Sdk" Version="9.0.0" /> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| </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" /> | ||
|
Comment on lines
+3
to
+16
|
||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\CreditApplication.Gateway\CreditApplication.Gateway.csproj" /> | ||
| <ProjectReference Include="..\CreditApplication.Generator\CreditApplication.Generator.csproj" /> | ||
| <ProjectReference Include="..\Client.Wasm\Client.Wasm.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var redis = builder.AddRedis("redis"); | ||
|
|
||
| if (builder.Environment.IsDevelopment()) | ||
| redis.WithRedisCommander(); | ||
|
|
||
| var generator1 = builder.AddProject<Projects.CreditApplication_Generator>("generator-1") | ||
| .WithEndpoint("http", endpoint => endpoint.Port = 5101) | ||
| .WithReference(redis); | ||
|
|
||
| var generator2 = builder.AddProject<Projects.CreditApplication_Generator>("generator-2") | ||
| .WithEndpoint("http", endpoint => endpoint.Port = 5102) | ||
| .WithReference(redis); | ||
|
|
||
| var generator3 = builder.AddProject<Projects.CreditApplication_Generator>("generator-3") | ||
| .WithEndpoint("http", endpoint => endpoint.Port = 5103) | ||
| .WithReference(redis); | ||
|
|
||
| var gateway = builder.AddProject<Projects.CreditApplication_Gateway>("gateway") | ||
| .WithEndpoint("http", endpoint => endpoint.Port = 5200) | ||
| .WithReference(generator1) | ||
| .WithReference(generator2) | ||
| .WithReference(generator3) | ||
| .WithExternalHttpEndpoints() | ||
| .WaitFor(generator1) | ||
| .WaitFor(generator2) | ||
| .WaitFor(generator3); | ||
|
|
||
| builder.AddProject<Projects.Client_Wasm>("client") | ||
| .WithReference(gateway) | ||
| .WaitFor(gateway); | ||
|
|
||
| builder.Build().Run(); |
| 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" | ||
| } | ||
| } | ||
| } | ||
| } |
| 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,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,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Ocelot" Version="23.3.3" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\CreditApplication.ServiceDefaults\CreditApplication.ServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using Ocelot.LoadBalancer.LoadBalancers; | ||
| using Ocelot.Responses; | ||
| using Ocelot.ServiceDiscovery.Providers; | ||
| using Ocelot.Values; | ||
|
|
||
| namespace CreditApplication.Gateway.LoadBalancing; | ||
|
|
||
| public sealed class WeightedRandomLoadBalancer : ILoadBalancer | ||
| { | ||
| private readonly IServiceDiscoveryProvider _serviceDiscovery; | ||
| private readonly IReadOnlyDictionary<string, double> _weights; | ||
|
|
||
| public WeightedRandomLoadBalancer( | ||
| IServiceDiscoveryProvider serviceDiscovery, | ||
| IReadOnlyDictionary<string, double> weights) | ||
| { | ||
| _serviceDiscovery = serviceDiscovery; | ||
| _weights = weights; | ||
| } | ||
|
|
||
| public async Task<Response<ServiceHostAndPort>> Lease(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")); | ||
|
|
||
| return new OkResponse<ServiceHostAndPort>(SelectByWeight(services).HostAndPort); | ||
| } | ||
|
|
||
| private Service SelectByWeight(IList<Service> services) | ||
| { | ||
| var weighted = services | ||
| .Select(s => ( | ||
| Service: s, | ||
| Weight: _weights.TryGetValue( | ||
| $"{s.HostAndPort.DownstreamHost}:{s.HostAndPort.DownstreamPort}", | ||
| out var w) ? w : 1.0)) | ||
| .ToList(); | ||
|
|
||
| var total = weighted.Sum(x => x.Weight); | ||
|
|
||
| // If all weights are zero or missing, fall back to uniform random selection. | ||
| if (total <= 0) | ||
| return services[Random.Shared.Next(services.Count)]; | ||
|
|
||
| var roll = Random.Shared.NextDouble() * total; | ||
| var cumulative = 0.0; | ||
|
|
||
| foreach (var (service, weight) in weighted) | ||
| { | ||
| cumulative += weight; | ||
| if (roll < cumulative) | ||
| return service; | ||
| } | ||
|
|
||
| // Guard against floating-point rounding: cumulative may fall infinitesimally short of total. | ||
| return weighted[^1].Service; | ||
| } | ||
|
|
||
| // No connection tracking needed for stateless weighted random load balancing. | ||
| public void Release(ServiceHostAndPort hostAndPort) { } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This card states that lab work №1 («Кэширование») was completed, but this PR is for lab №2 (API Gateway + load balancing). If the UI is intended to reflect the current lab/PR, update the lab number/title accordingly so the client page matches the submission.