This project demonstrates the implementation of an API Gateway using Ocelot in .NET 8. It leverages both InMemory DB and SQL Server for storage, and includes a Dockerfile for containerization.
This project uses Ocelot as an API Gateway to route API requests to various microservices. It demonstrates how to set up an API Gateway in a microservices architecture, implementing routing, load balancing, and API aggregation.
For full documentation, visit: Ocelot Documentation.
- Routing: Routes requests to backend services based on predefined routes in the
ocelot.jsonconfiguration file. - Load Balancing: Routes traffic to multiple instances of backend services if needed.
- Aggregation: Aggregates multiple responses into a single response.
The project also uses an InMemory Database for temporary data storage and SQL Server for persistent data storage.
- Ocelot API Gateway routing and request aggregation
- In-memory data storage for fast temporary data operations
- SQL Server for persistent data storage
- Docker support for easy containerization and deployment
- Scalable architecture with microservices communication via the API Gateway
- .NET 8
- Ocelot API Gateway
- InMemory Database (for temporary data storage)
- SQL Server (for persistent storage)
- Docker (for containerization)
To run this project, ensure you have the following installed on your machine:
- .NET 8 SDK
- Docker
- SQL Server (for persistent database) or use SQL Server Docker container
-
Clone the repository:
git clone https://github.com/tasbilek/OcelotMicroService.git cd OcelotMicroService -
Restore the dependencies:
dotnet restore
-
Update the connection strings in the
appsettings.jsonfor SQL Server configuration. -
Run the application:
dotnet run
The API Gateway will be available locally at http://localhost:5000.
This project includes a Dockerfile for containerization. You can build and run the project using Docker.
-
Build the Docker image:
docker build -t todos-img -f OcelotMicroService.Todos.WebAPI/Dockerfile . docker build -t categories-img -f OcelotMicroService.Categories.WebAPI/Dockerfile .
-
Run the Docker container:
docker run -d --name todos-api -p 5001:8080 todos-img docker run -d --name categories-api -p 5002:8080 categories-img
-
If you want, you can dockerize the
Ocelot.Gatewayproject; otherwise, you need to run it manually. -
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
For temporary data storage, the project uses InMemory DB. You can use this for development or testing purposes when you don't need persistent storage.
For persistent data storage, SQL Server is used. 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.
- 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 updateThis project is licensed under the MIT License.