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": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var builder = DistributedApplication.CreateBuilder(args);

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UserSecretsId>4f5bff93-99dc-402b-a5df-e4e9dff13701</UserSecretsId>
</PropertyGroup>

</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:17104;http://localhost:15036",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21272",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "https://localhost:23225",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22133"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15036",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"ASPIRE_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19066",
"ASPIRE_DASHBOARD_MCP_ENDPOINT_URL": "http://localhost:18081",
"ASPIRE_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20069"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "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,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