This is a complete microservices gateway example project based on YARP (Yet Another Reverse Proxy), demonstrating core concepts of modern microservices architecture including API gateway, reverse proxy, load balancing, canary deployment, and more.
- Demonstrate practical application of YARP in .NET 8.0
- Showcase key features of microservices gateway: routing, security, monitoring, traffic management
- Provide runnable complete examples for learning and testing
- Support bilingual documentation (English and Chinese) suitable for international teams
YarpExample/
โโโ ApiGateway/ # YARP-based API Gateway (core component)
โโโ StudentApi/ # Student Information Management API Service
โโโ CourseApi/ # Course Information Management API Service
โโโ Bruno/ # API Test Collection (using Bruno testing tool)
โโโ services-*.bat # Service management scripts
Client Request
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ API Gateway (Port 7000) โ
โ - JWT Authentication & Authorization โ
โ - Routing & Load Balancing โ
โ - Rate Limiting & Caching โ
โ - Distributed Tracing โ
โโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโโผโโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ
โStudent โ โStudent โ โCourse โ
โStable โ โCanary โ โAPI โ
โ(7003) โ โ(7004) โ โ(7001/2) โ
โโโโโโโโโโโ โโโโโโโโโโโ โโโโโโโโโโโ
- .NET 8.0 SDK or higher
- Windows/Linux/macOS operating system
- Recommended: Visual Studio Code or Visual Studio 2022
# Windows
services-start-all.bat
# Or start manually (recommended for development):
# Gateway: cd ApiGateway && dotnet run --launch-profile http
# Course API Instance 1: cd CourseApi && dotnet run --launch-profile http-port-7001
# Course API Instance 2: cd CourseApi && dotnet run --launch-profile http-port-7002
# Student API Stable: cd StudentApi && dotnet run --launch-profile http-7003-stable-version
# Student API Canary: cd StudentApi && dotnet run --launch-profile http-7004-canary-version# Check gateway health status
curl http://localhost:7000/gateway/api/students/healthcheck
# Test Student API
curl http://localhost:7000/gateway/api/students
# Test Course API
curl http://localhost:7000/gateway/api/courses
# Test canary version
curl -H "X-Canary: true" http://localhost:7000/gateway/api/studentsBuilt with YARP, provides the following features:
- Dynamic Routing Configuration: Load routing rules from appsettings.json
- JWT Authentication: Support for Bearer token validation
- Load Balancing: Weighted load balancing (80%/20%) and RoundRobin load balancing
- Rate Limiting: Fixed window rate limiting (4 requests per 12 seconds)
- Output Caching: Header-based response caching
- Health Monitoring: Active monitoring of backend service health
- Distributed Tracing: OpenTelemetry integration
- Canary Deployment: Support for routing to canary version via headers
RESTful API service with features:
- Dual Version Deployment: Stable version (port 7003) and Canary version (port 7004)
- Gateway Protection: Uses
X-Internal-Gateway-Tokento prevent direct access - Version Awareness: Responses include service version and port information
- Health Checks: Independent health check endpoints for load balancing
RESTful API service with features:
- Multi-instance Deployment: Two instances (ports 7001 and 7002) with RoundRobin load balancing
- Dynamic Versioning: Version switching based on
x-api-versionheader - Comprehensive Management: Course creation, querying, enrollment, drop functionality
- Business Validation: Business rule validation for price, credits, capacity, etc.
{
"student-canary-route": {
"ClusterId": "student-canary-cluster",
"Order": 1, // Higher priority
"Headers": [{ "Name": "X-Canary", "Values": ["true"] }]
},
"student-stable-route": {
"ClusterId": "student-stable-cluster",
"Order": 2, // Lower priority (default route)
},
"courseRoute": {
"ClusterId": "course-cluster",
"LoadBalancingPolicy": "RoundRobin"
}
}- Student API: Weighted load balancing (80% stable, 20% canary)
- Course API: RoundRobin load balancing (equal distribution between two instances)
- JWT Tokens: For client authentication
- Internal Gateway Token:
X-Internal-Gateway-Tokenprevents direct backend calls - Rate Limiting: Protects gateway and backend services
The project includes complete Bruno test collections:
# Import the test collections from Bruno/ directory in Bruno# View request headers (for debugging)
GET http://localhost:7000/gateway/api/students/debug/headers
# Health check endpoints
GET http://localhost:7003/api/students/healthcheck
GET http://localhost:7001/api/courses/healthcheck
- OpenTelemetry Tracing: View distributed traces through Jaeger or Zipkin
- Service Port Information: Responses include the port of the service that processed the request
- Version Information: Responses include service version identifiers
# Default route to stable version (80% traffic)
curl http://localhost:7000/gateway/api/students
# Explicit route to canary version (20% traffic)
curl -H "X-Canary: true" http://localhost:7000/gateway/api/students# Old version API
curl http://localhost:7000/gateway/api/courses
# New version API (v2)
curl -H "x-api-version: v2" http://localhost:7000/gateway/api/courses# Make multiple requests to Course API, observe different port responses
for i in {1..5}; do
curl -s http://localhost:7000/gateway/api/courses | grep "port"
done- All backend services validate
X-Internal-Gateway-Token - Token value:
9801919d-b188-42ab-bb2d-7243aba370aa - Only requests coming through the gateway are accepted
# Request with JWT token
curl -H "Authorization: Bearer {jwt-token}" \
-H "X-Internal-Gateway-Token: 9801919d-b188-42ab-bb2d-7243aba370aa" \
http://localhost:7000/gateway/api/students- Maximum 4 requests per 12 seconds
- Exceeding limits returns HTTP 503
- Prevents DDoS attacks and resource exhaustion
โโโ ApiGateway/README.md # Gateway English documentation
โโโ ApiGateway/README_CN.md # Gateway Chinese documentation
โโโ StudentApi/Readme.md # Student API English documentation
โโโ StudentApi/Readme_CN.md # Student API Chinese documentation
โโโ CourseApi/Readme.md # Course API English documentation
โโโ CourseApi/Readme_CN.md # Course API Chinese documentation
โโโ README.md # Main project Chinese documentation
โโโ README_EN.md # Main project English documentation
- YARP (Yet Another Reverse Proxy): Microsoft's official reverse proxy library
- Canary Deployment: Gradually rolling out new versions to reduce risk
- Load Balancing: Distributing traffic across different service instances
- Session Affinity: Routing the same user's requests to the same backend
- Distributed Tracing: End-to-end tracing of cross-service requests
- .NET 8.0: Cross-platform development framework
- OpenTelemetry: Observability standard
- JWT (JSON Web Tokens): Authentication standard
- Bruno: API testing tool
-
Service Startup Failure
# Check port usage netstat -ano | findstr :7000 # Check .NET SDK version dotnet --version
-
Gateway Routing Errors
- Confirm all backend services are started
- Check routing configuration in
appsettings.json - Verify health check endpoints are accessible
-
Authentication Failures
- Confirm JWT token is valid
- Check if
X-Internal-Gateway-Tokenis correct - View detailed error messages in gateway logs
# Gateway logs (debug level)
# Check Logging configuration in ApiGateway/appsettings.json- Swagger/OpenAPI Integration: Automatic API documentation generation
- Prometheus Metrics: Monitoring metrics export
- Configuration Management: Dynamic configuration updates
- Multi-tenancy Support: Tenant-isolated routing and policies
- WebSocket Support: Real-time communication
- Add New Services: Add more microservices following existing patterns
- Identity Provider: Integrate Keycloak or Auth0
- Message Queue: Integrate RabbitMQ or Kafka
- Database Integration: Replace in-memory storage with real database
- Follow .NET coding conventions
- Add XML documentation comments to all public APIs
- Add bilingual comments to important configurations
- All new features require updating relevant README files
- Keep English and Chinese documentation synchronized
- Code examples should be complete and runnable
- New features require corresponding Bruno tests
- Important changes require updating service startup scripts
This project is licensed under the MIT License. See the LICENSE file in the project root for details.
- YARP Team: For providing an excellent reverse proxy library
- .NET Community: For rich ecosystem and tools
- OpenTelemetry Project: For standardizing observability
Important Note: This project is primarily for demonstration and learning purposes. For production use, please perform security hardening and performance optimization based on actual requirements.
For issues or suggestions, please submit feedback through the project's Issue page.