Professional Documents
Culture Documents
6 2.2.DEMO Implementing An API Gateway in ASP - NET Core With Ocelot
6 2.2.DEMO Implementing An API Gateway in ASP - NET Core With Ocelot
Replace the contents of docker-compose .yml and docker-compose.override.yml with the following code.
The new code is highlighted.
docker-compose .yml
version: '3.4'
services:
cursesapi:
image: ${DOCKER_REGISTRY-}cursesapi
build:
context: .
dockerfile: CursesAPI/Dockerfile
depends_on:
- sqldata
studentsapi:
image: ${DOCKER_REGISTRY-}studentsapi
build:
context: .
dockerfile: StudentsAPI/Dockerfile
depends_on:
- sqldata
sqldata:
image: mcr.microsoft.com/mssql/server:2019-latest
docker-compose.override.yml
version: '3.4'
services:
cursesapi:
container_name: cursesapi
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "5081:80"
studentsapi:
container_name: studentsapi
environment:
- ASPNETCORE_ENVIRONMENT=Development
ports:
- "5082:80"
sqldata:
container_name: sqldata
environment:
SA_PASSWORD: "My!P@ssw0rd1"
ACCEPT_EULA: "Y"
ports:
- "1433:1433"
API Gateway
Introducing Ocelot
We are going to use Ocelot API Gateway. It is a lightweight, open-source, scalable, and fast API Gateway
based on .NET Core and specially designed for microservices architecture. Basically, it is a set of middleware
designed to work with ASP.NET Core.
It has several features such as routing, caching, security, rate limiting, etc.
Install Ocelot and its dependencies in your ASP.NET Core project with Ocelot's NuGet package, from Visual
Studio.
The important point here for Ocelot is the Oselot.json file that you must provide to the builder through the
AddJsonFile() method in Program.cs:
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("ocelot.json");
builder.Services.AddOcelot();
app.UseOcelot().Wait();
app.Run();