Skip to content

tasbilek/YarpMicroService

Repository files navigation

YARP MicroService

This project implements an API Gateway using YARP (Yet Another Reverse Proxy) in .NET 8 to manage routing, load balancing, and reverse proxying for microservices.

Table of Contents

Overview

YARP is a highly customizable reverse proxy built on .NET 8, making it a great choice for handling API Gateway responsibilities in a microservices architecture. This project enables seamless routing between different microservices using a flexible configuration.

Features

  • Dynamic Routing: Routes requests to backend microservices based on configuration.
  • Load Balancing: Distributes requests across multiple instances of services.
  • Reverse Proxy: Acts as an intermediary between clients and services.
  • Middleware Support: Custom middleware for authentication, logging, etc.
  • Docker Support: Easily containerized for deployment.
  • Options Pattern: Implements the Options Pattern for configuration.

Tech Stack

  • .NET 8
  • YARP (Yet Another Reverse Proxy)
  • Docker (for containerization)
  • Options Pattern for configuration management

Databases

  • PostgreSQL
  • MongoDB
  • SQL Server

Setup

Prerequisites

To run this project, ensure you have:

  • .NET 8 SDK installed
  • Docker installed (if using containers)

Running Locally

  1. Clone the repository:

    git clone https://github.com/tasbilek/YarpMicroService.git
    cd YarpMicroService
  2. Restore dependencies:

    dotnet restore

The gateway will be available at http://localhost:5000.

Docker Setup

  1. Create Network:

    docker network create yarp-network
  2. Build the Docker image:

    docker network create yarp-network
    docker build -t products-img -f YarpMicroService.Products.WebAPI/Dockerfile .
    docker build -t orders-img -f YarpMicroService.Orders.WebAPI/Dockerfile .
    docker build -t carts-img -f YarpMicroService.ShoppingCarts.WebAPI/Dockerfile .
    docker build -t gateway-img -f YarpMicroService.YARP.Gateway/Dockerfile .
  3. Run the container:

    docker run -d --name products-api --network yarp-network -p 5001:8080 -e ASPNETCORE_URLS=http://+:8080 products-img
    docker run -d --name orders-api -p --network yarp-network 5002:8080 -e ASPNETCORE_URLS=http://+:8080 orders-img
    docker run -d --name carts-api -p --network yarp-network 5003:8080 -e ASPNETCORE_URLS=http://+:8080 carts-img
    docker run -d --name yarp-gateway -p --network yarp-network 5000:8080 -e ASPNETCORE_URLS=http://+:8080 gateway-img

The API Gateway should now be running at http://localhost:5000.

Running Databases with Docker

This project uses PostgreSQL, MongoDB, and SQL Server inside separate Docker containers. You need to manually start each database container using Docker commands.

Running PostgreSQL

docker run --name postgres -e POSTGRES_PASSWORD=1 -e POSTGRES_DB=shoppingcartsdb -d -p 5432:5432 postgres

Running MongoDB

docker run -d --name mongodb --network yarp-network -p 27017:27017 -v mongodbdata:/data/db mongo

Running SQL Server

To run SQL Server in Docker (if not already running locally), you can find it this address: https://hub.docker.com/r/microsoft/azure-sql-edge

Ensure that your application connection strings are correctly configured to connect to these database instances.

Configuration

YARP is configured using appsettings.json. MongoDbSettings is configured in YarpMicroService.Orders.WebAPI/Program.cs with using Options Pattern.


SQL Server

Ensure that the SQL Server container or your local SQL Server instance is running. You can configure the connection strings in the appsettings.json file for SQL Server.

If you are using a SQL Server container, the connection string should be configured as follows:

"ConnectionStrings": {
  "DefaultConnection": "Server=host.docker.internal,1433;Database=YourDatabase;User Id=sa;Password=yourPassword123!"
}
  • host.docker.internal allows the container to communicate with your local machine.
  • 1433 is the default port for SQL Server.
  • Replace YourDatabase with your actual database name, and use the appropriate sa password.

On Mac (and also on some Linux environments), host.docker.internal might not work as expected to refer to the host machine. In such cases, you can use your local machine's IP address instead.

To find your local machine's IP address on Mac, you can use the following command in the terminal:

ifconfig | grep inet

Look for the IP address associated with your network interface (typically en0 for Wi-Fi or en1 for Ethernet). Use this IP address in place of host.docker.internal in your connection string.

For example:

"ConnectionStrings": {
  "DefaultConnection": "Server=192.168.x.x,1433;Database=YourDatabase;User Id=sa;Password=yourPassword123!"
}

Where 192.168.x.x is the IP address of your local machine.

This approach should work in environments where host.docker.internal is not resolving correctly.

If you're running SQL Server locally, you can adjust the connection string to point to your local instance.

PostgreSQL

Ensure that the PostgreSQL container or your local PostgreSQL instance is running. You can configure the connection strings in the appsettings.json file for PostgreSQL.

"ConnectionStrings": {
  "PostgreSql":"Host=host.docker.internal;Port=5432;Database=shoppingcartsdb;Username=postgres;Password=1;"
}

MongoDB

Ensure that the MongoDB container or your local MongoDB instance is running. You can configure the connection strings in the appsettings.json file for MongoDB.

"MongoDbSettings":{
   "ConnectionString":"mongodb://localhost:27017",
   "DatabaseName":"OrdersDb"
}
  • Ensure that you replace localhost with the MongoDB container ID when running in production. You can find the container ID using the following command:
docker ps

CodeFirst with Dotnet CLI

  1. Create a migration to create the database schema from your models:
dotnet ef migrations add InitialCreate
  1. Apply the migration to the database:
dotnet ef database update

Contributing

Contributions are welcome! Please fork the repository, create a new branch, and submit a pull request.

License

This project is licensed under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published