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
17 changes: 17 additions & 0 deletions ApiGateway/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="23.4.2" />
</ItemGroup>

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

</Project>
6 changes: 6 additions & 0 deletions ApiGateway/ApiGateway.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@ApiGateway_HostAddress = http://localhost:5248

GET {{ApiGateway_HostAddress}}/weatherforecast/
Accept: application/json

###
64 changes: 64 additions & 0 deletions ApiGateway/LoadBalancing/WeightedRoundRobinBalancer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Ocelot.LoadBalancer.LoadBalancers;
using Ocelot.Responses;
using Ocelot.Values;

namespace ApiGateway.LoadBalancing;

public class WeightedRoundRobinBalancer : ILoadBalancer
{
public string Type => nameof(WeightedRoundRobinBalancer);

private readonly List<ServiceHostAndPort> _expandedHosts;
private readonly object _lock = new();
private int _currentIndex = 0;

public WeightedRoundRobinBalancer(IConfiguration configuration)
{
var hosts = new List<(string host, int port, int weight)>();

AddHostFromConfig(configuration, "generation-service-1", 3, hosts);
AddHostFromConfig(configuration, "generation-service-2", 2, hosts);
AddHostFromConfig(configuration, "generation-service-3", 1, hosts);

_expandedHosts = new List<ServiceHostAndPort>();
foreach (var (host, port, weight) in hosts)
{
for (var i = 0; i < weight; i++)
{
_expandedHosts.Add(new ServiceHostAndPort(host, port));
}
}

if (_expandedHosts.Count == 0)
{
_expandedHosts.Add(new ServiceHostAndPort("localhost", 5229));
}
}

private static void AddHostFromConfig(
IConfiguration config,
string serviceName,
int weight,
List<(string, int, int)> hosts)
{
var url = config[$"services:{serviceName}:http:0"];
if (string.IsNullOrEmpty(url)) return;

var uri = new Uri(url);
hosts.Add((uri.Host, uri.Port, weight));
}

public Task<Response<ServiceHostAndPort>> LeaseAsync(HttpContext httpContext)
{
lock (_lock)
{
var host = _expandedHosts[_currentIndex];
_currentIndex = (_currentIndex + 1) % _expandedHosts.Count;

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

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

var builder = WebApplication.CreateBuilder(args);

builder.AddServiceDefaults();

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

builder.Services
.AddOcelot(builder.Configuration)
.AddCustomLoadBalancer(
(route, serviceDiscoveryProvider) =>
new WeightedRoundRobinBalancer(builder.Configuration));

var app = builder.Build();

app.MapDefaultEndpoints();

await app.UseOcelot();

app.Run();
41 changes: 41 additions & 0 deletions ApiGateway/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:35782",
"sslPort": 44338
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5248",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7243;http://localhost:5248",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"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"
}
}
}
16 changes: 16 additions & 0 deletions ApiGateway/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Kestrel": {
"Endpoints": {
"Http": {
"Url": "http://localhost:5555"
}
}
}
}
35 changes: 35 additions & 0 deletions ApiGateway/ocelot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"Routes": [
{
"UpstreamPathTemplate": "/contracts/{everything}",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamPathTemplate": "/contracts/{everything}",
"DownstreamScheme": "http",
"LoadBalancerOptions": {
"Type": "WeightedRoundRobinBalancer"
},
"DownstreamHostAndPorts": [
{
"Host": "placeholder",
"Port": 80
}
]
},
{
"UpstreamPathTemplate": "/contracts",
"UpstreamHttpMethod": [ "Get" ],
"DownstreamPathTemplate": "/contracts",
"DownstreamScheme": "http",
"LoadBalancerOptions": {
"Type": "WeightedRoundRobinBalancer"
},
"DownstreamHostAndPorts": [
{
"Host": "placeholder",
"Port": 80
}
]
}
],
"GlobalConfiguration": {}
}
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.4.11626.88
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}") = "CloudDevelopment.AppHost", "CloudDevelopment\CloudDevelopment.AppHost\CloudDevelopment.AppHost.csproj", "{3505653B-9833-43DD-8E51-1D359D201914}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CloudDevelopment.ServiceDefaults", "CloudDevelopment\CloudDevelopment.ServiceDefaults\CloudDevelopment.ServiceDefaults.csproj", "{18868854-E44C-A570-242C-63484B3BC379}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenerationService", "GenerationService\GenerationService.csproj", "{34140EB6-F1B4-5A29-B45A-DBC1635966E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiGateway", "ApiGateway\ApiGateway.csproj", "{ABE66B30-BD47-A7B2-B17B-5BA67959B026}"
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
{3505653B-9833-43DD-8E51-1D359D201914}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3505653B-9833-43DD-8E51-1D359D201914}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3505653B-9833-43DD-8E51-1D359D201914}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3505653B-9833-43DD-8E51-1D359D201914}.Release|Any CPU.Build.0 = Release|Any CPU
{18868854-E44C-A570-242C-63484B3BC379}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{18868854-E44C-A570-242C-63484B3BC379}.Debug|Any CPU.Build.0 = Debug|Any CPU
{18868854-E44C-A570-242C-63484B3BC379}.Release|Any CPU.ActiveCfg = Release|Any CPU
{18868854-E44C-A570-242C-63484B3BC379}.Release|Any CPU.Build.0 = Release|Any CPU
{34140EB6-F1B4-5A29-B45A-DBC1635966E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{34140EB6-F1B4-5A29-B45A-DBC1635966E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{34140EB6-F1B4-5A29-B45A-DBC1635966E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{34140EB6-F1B4-5A29-B45A-DBC1635966E5}.Release|Any CPU.Build.0 = Release|Any CPU
{ABE66B30-BD47-A7B2-B17B-5BA67959B026}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ABE66B30-BD47-A7B2-B17B-5BA67959B026}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ABE66B30-BD47-A7B2-B17B-5BA67959B026}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ABE66B30-BD47-A7B2-B17B-5BA67959B026}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
28 changes: 28 additions & 0 deletions CloudDevelopment/CloudDevelopment.AppHost/AppHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var builder = DistributedApplication.CreateBuilder(args);

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

builder.AddProject<Projects.Client_Wasm>("client-wasm");

var generation1 = builder.AddProject<Projects.GenerationService>("generation-service-1")
.WithReference(redis)
.WaitFor(redis);

var generation2 = builder.AddProject<Projects.GenerationService>("generation-service-2")
.WithReference(redis)
.WaitFor(redis);

var generation3 = builder.AddProject<Projects.GenerationService>("generation-service-3")
.WithReference(redis)
.WaitFor(redis);

builder.AddProject<Projects.ApiGateway>("api-gateway")
.WithReference(generation1)
.WithReference(generation2)
.WithReference(generation3)
.WaitFor(generation1)
.WaitFor(generation2)
.WaitFor(generation3);

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Aspire.AppHost.Sdk/13.2.2">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>40038ea2-2aa4-401d-a7e5-218c78b6c0c5</UserSecretsId>
</PropertyGroup>

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

<ItemGroup>
<ProjectReference Include="..\..\Client.Wasm\Client.Wasm.csproj" />
<ProjectReference Include="..\..\ApiGateway\ApiGateway.csproj" />
<ProjectReference Include="..\..\GenerationService\GenerationService.csproj" />

</ItemGroup>

</Project>
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:17149;http://localhost:15072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21077",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23195",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22234"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15072",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19047",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18197",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20205"
}
}
}
}
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/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.4.0" />
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="10.4.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.15.2" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.15.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.15.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.15.0" />
</ItemGroup>

</Project>
Loading