Skip to content

cgonzalezvera/YarpMicroservices

Repository files navigation

🚀 Microservicios con YARP + Docker Compose

Sistema de microservicios usando YARP como API Gateway.

📋 Requisitos

  • .NET 8.0 SDK
  • Docker
  • Docker Compose

🏗️ Arquitectura

Esquema conceptual de como se integra YARP con una Web API C# y Kestrel:

┌─────────────────────────────────────────┐
│         Cliente (Browser/App)           │
└──────────────┬──────────────────────────┘
               │ HTTP/HTTPS Request
               ▼
┌─────────────────────────────────────────┐
│    YARP Reverse Proxy (ASP.NET Core)    │
│  ┌───────────────────────────────────┐  │
│  │   Middleware Pipeline (YARP)      │  │
│  │  - Routing                        │  │
│  │  - Load Balancing                 │  │
│  │  - Transformations                │  │
│  └───────────────────────────────────┘  │
│              Kestrel Server             │
└──────────────┬──────────────────────────┘
               │ Proxy Request
               ▼
┌─────────────────────────────────────────┐
│         Microservicios Backend          │
│  ┌──────────┐ ┌──────────┐ ┌─────────┐  │
│  │ API 1    │ │ API 2    │ │ API 3   │  │
│  │ (Kestrel)│ │ (Kestrel)│ │(Kestrel)│  │
│  └──────────┘ └──────────┘ └─────────┘  │
└─────────────────────────────────────────┘

Implementacion especifica

Cliente → API Gateway (YARP) → Orders API → Products API

┌─────────────────────────────────────────┐
│           Cliente HTTP                  │
│     (Browser, Postman, cURL)            │
└──────────────┬──────────────────────────┘
               │ http://localhost:8000
               ▼
┌─────────────────────────────────────────┐
│      YARP API Gateway (Port 8000)       │
│  ┌───────────────────────────────────┐  │
│  │  Routing Rules:                   │  │
│  │  - /orders → orders-api:8080      │  │
│  │  - /products → products-api:8081  │  │
│  └───────────────────────────────────┘  │
└──────────┬──────────────┬───────────────┘
           │              │
     ┌─────▼──────┐  ┌────▼──────┐
     │ Orders API │  │ Products  │
     │ Port: 8080 │  │    API    │
     │            │  │ Port: 8081│
     └────────────┘  └───────────┘

🔧 Construcción y Ejecución

Opción 1: Con Docker Compose (Recomendado)

# Construir imágenes
docker-compose build

# Iniciar servicios
docker-compose up -d

# Ver logs
docker-compose logs -f

# Detener servicios
docker-compose down

Opción 2: Desarrollo local (sin Docker)

# Terminal 1 - Orders API
cd src/Orders.Api
dotnet run --urls "http://localhost:8080"

# Terminal 2 - Products API
cd src/Products.Api
dotnet run --urls "http://localhost:8081"

# Terminal 3 - API Gateway
cd src/ApiGateway
dotnet run --urls "http://localhost:8000"

🧪 Probar los Servicios

# Info del gateway
curl http://localhost:8000

# Health check
curl http://localhost:8000/health

Orders API (a través del Gateway)

# Obtener todas las órdenes
curl http://localhost:8000/orders

# Obtener orden por ID
curl http://localhost:8000/orders/1

# Crear nueva orden
curl -X POST http://localhost:8000/orders \
  -H "Content-Type: application/json" \
  -d '{
    "customerName": "Carlos Ruiz",
    "totalAmount": 499.99,
    "status": "Pending",
    "items": [
      {
        "productId": 101,
        "productName": "Laptop",
        "quantity": 1,
        "price": 499.99
      }
    ]
  }'

# Actualizar orden
curl -X PUT http://localhost:8000/orders/1 \
  -H "Content-Type: application/json" \
  -d '{
    "customerName": "Juan Pérez Updated",
    "totalAmount": 399.99,
    "status": "Completed",
    "items": []
  }'

# Eliminar orden
curl -X DELETE http://localhost:8000/orders/1

Products API (a través del Gateway)

# Obtener todos los productos
curl http://localhost:8000/products

# Obtener producto por ID
curl http://localhost:8000/products/101

# Buscar por categoría
curl http://localhost:8000/products/category/Electronics

# Crear nuevo producto
curl -X POST http://localhost:8000/products \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Mechanical Keyboard",
    "description": "RGB backlit keyboard",
    "price": 129.99,
    "stock": 30,
    "category": "Accessories"
  }'

# Actualizar producto
curl -X PUT http://localhost:8000/products/101 \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Laptop Dell XPS 15 Updated",
    "description": "Updated description",
    "price": 1199.99,
    "stock": 10,
    "category": "Electronics"
  }'

# Eliminar producto
curl -X DELETE http://localhost:8000/products/101

About

an examplo of how to use YARP library as Reverse Proxy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors