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.
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.
- 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.
- .NET 8
- YARP (Yet Another Reverse Proxy)
- Docker (for containerization)
- Options Pattern for configuration management
- PostgreSQL
- MongoDB
- SQL Server
To run this project, ensure you have:
- .NET 8 SDK installed
- Docker installed (if using containers)
-
Clone the repository:
git clone https://github.com/tasbilek/YarpMicroService.git cd YarpMicroService -
Restore dependencies:
dotnet restore
The gateway will be available at http://localhost:5000.
-
Create Network:
docker network create yarp-network
-
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 .
-
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.
This project uses PostgreSQL, MongoDB, and SQL Server inside separate Docker containers. You need to manually start each database container using Docker commands.
docker run --name postgres -e POSTGRES_PASSWORD=1 -e POSTGRES_DB=shoppingcartsdb -d -p 5432:5432 postgresdocker run -d --name mongodb --network yarp-network -p 27017:27017 -v mongodbdata:/data/db mongoTo 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.
YARP is configured using appsettings.json.
MongoDbSettings is configured in YarpMicroService.Orders.WebAPI/Program.cs with using Options Pattern.
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.internalallows the container to communicate with your local machine.1433is the default port for SQL Server.- Replace
YourDatabasewith your actual database name, and use the appropriatesapassword.
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 inetLook 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.
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;"
}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
localhostwith the MongoDB container ID when running in production. You can find the container ID using the following command:
docker ps- Create a migration to create the database schema from your models:
dotnet ef migrations add InitialCreate- Apply the migration to the database:
dotnet ef database updateContributions are welcome! Please fork the repository, create a new branch, and submit a pull request.
This project is licensed under the MIT License.