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: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
Service Configuration
The API Gateway is configured inProgram.cs:
Supported Microservices
The API Gateway routes requests to three core microservices:| Service | Path Prefix | Backend Address (Docker) | Backend Address (Local) |
|---|---|---|---|
| Catalog | /catalog-service/* | http://catalog.api:8080 | http://localhost:6000 |
| Basket | /basket-service/* | http://basket.api:8080 | http://localhost:6001 |
| Ordering | /ordering-service/* | http://ordering.api:8080 | http://localhost:6003 |
Request Flow Example
- Client Request:
GET http://gateway:8080/catalog-service/api/v1/products - Route Matching: Gateway matches the
/catalog-service/{**catch-all}pattern - Path Transformation: Strips
/catalog-serviceprefix →/api/v1/products - Forwarding: Sends request to
http://catalog.api:8080/api/v1/products - Response: Gateway returns the catalog service response to the client
Deployment
Docker Deployment
The API Gateway runs in a Docker container:8080: HTTP endpoint8081: HTTPS endpoint (optional)
Local Development
For local development, useappsettings.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:8080basket.api:8080ordering.api:8080
Local Development
Uses localhost with port mapping:localhost:6000→ Cataloglocalhost:6001→ Basketlocalhost:6003→ Ordering
NuGet Dependencies
Benefits of YARP
- Performance: Built on ASP.NET Core for high throughput
- Flexibility: Configuration-driven with programmatic customization options
- Simplicity: Minimal code required for basic reverse proxy functionality
- Integration: Seamless integration with ASP.NET Core middleware
- Maintainability: No need for external proxy software (Nginx, Envoy, etc.)
Next Steps
- YARP Configuration - Detailed configuration reference
- Rate Limiting - Request throttling configuration
- Routing - Route matching and path transformation
