Skip to main content

Introduction

The AspNetRun Microservices project uses YARP (Yet Another Reverse Proxy) as its API Gateway implementation. YARP is a high-performance, customizable reverse proxy library built on ASP.NET Core.

Architecture

The YARP API Gateway acts as a single entry point for all client requests, routing them to the appropriate backend microservices:
Client → YARP API Gateway → Backend Microservices
                           ├─> Catalog Service
                           ├─> Basket Service
                           └─> Ordering Service

Key Features

Reverse Proxy Capabilities

  • Dynamic Routing: Routes requests to different microservices based on URL paths
  • Path Transformation: Strips service prefixes from URLs before forwarding
  • Load Balancing: Distributes traffic across multiple service instances
  • Rate Limiting: Protects services from excessive requests

Technology Stack

  • YARP Version: 2.1.0
  • .NET Version: 8.0
  • Configuration: JSON-based configuration in appsettings.json
  • Rate Limiting: Built-in ASP.NET Core rate limiting middleware

Project Structure

YarpApiGateway/
├── Program.cs                    # Application entry point and service configuration
├── YarpApiGateway.csproj        # Project file with YARP dependency
├── appsettings.json             # Production configuration (Docker)
├── appsettings.Local.json       # Local development configuration
├── appsettings.Development.json # Development environment settings
└── Dockerfile                   # Container image definition

Service Configuration

The API Gateway is configured in Program.cs:
using Microsoft.AspNetCore.RateLimiting;

var builder = WebApplication.CreateBuilder(args);

// Add YARP reverse proxy
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

// Add rate limiting
builder.Services.AddRateLimiter(rateLimiterOptions =>
{
    rateLimiterOptions.AddFixedWindowLimiter("fixed", options =>
    {
        options.Window = TimeSpan.FromSeconds(10);
        options.PermitLimit = 5;
    });
});

var app = builder.Build();

app.UseRateLimiter();
app.MapReverseProxy();

app.Run();

Supported Microservices

The API Gateway routes requests to three core microservices:
ServicePath PrefixBackend Address (Docker)Backend Address (Local)
Catalog/catalog-service/*http://catalog.api:8080http://localhost:6000
Basket/basket-service/*http://basket.api:8080http://localhost:6001
Ordering/ordering-service/*http://ordering.api:8080http://localhost:6003

Request Flow Example

  1. Client Request: GET http://gateway:8080/catalog-service/api/v1/products
  2. Route Matching: Gateway matches the /catalog-service/{**catch-all} pattern
  3. Path Transformation: Strips /catalog-service prefix → /api/v1/products
  4. Forwarding: Sends request to http://catalog.api:8080/api/v1/products
  5. Response: Gateway returns the catalog service response to the client

Deployment

Docker Deployment

The API Gateway runs in a Docker container:
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app
EXPOSE 8080
EXPOSE 8081
Exposed Ports:
  • 8080: HTTP endpoint
  • 8081: HTTPS endpoint (optional)

Local Development

For local development, use appsettings.Local.json with localhost addresses:
  • Catalog: http://localhost:6000
  • Basket: http://localhost:6001
  • Ordering: http://localhost:6003

Configuration Environments

Production (Docker)

Uses service names for DNS resolution within Docker network:
  • catalog.api:8080
  • basket.api:8080
  • ordering.api:8080

Local Development

Uses localhost with port mapping:
  • localhost:6000 → Catalog
  • localhost:6001 → Basket
  • localhost:6003 → Ordering

NuGet Dependencies

<PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />

Benefits of YARP

  1. Performance: Built on ASP.NET Core for high throughput
  2. Flexibility: Configuration-driven with programmatic customization options
  3. Simplicity: Minimal code required for basic reverse proxy functionality
  4. Integration: Seamless integration with ASP.NET Core middleware
  5. Maintainability: No need for external proxy software (Nginx, Envoy, etc.)

Next Steps

Build docs developers (and LLMs) love