You are on page 1of 3

Lab 2.2 Implementing an API Gateway in ASP.

NET Core with Ocelot


Add docker-compose

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.

Implement the API Gateway Using Ocelot


Before going any further, you should be aware of the terms upstream and downstream. While upstream
refers to the request sent by the client to the API Gateway, downstream is related to the request that the
API Gateway sends to a particular microservice.

Add a new ASP.NET Core Empty project:

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;

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("ocelot.json");

builder.Services.AddOcelot();

var app = builder.Build();

app.UseOcelot().Wait();

app.Run();

Here's a simplified example of ReRoute configuration file Oselot.json:


{
"Routes": [
{
"DownstreamPathTemplate": "/api/Students",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5082
}
],
"UpstreamPathTemplate": "/Students",
"UpstreamHttpMethod": [ "Get", "Post" ]
},
{
"DownstreamPathTemplate": "/api/Courses",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5081
}
],
"UpstreamPathTemplate": "/Courses",
"UpstreamHttpMethod": [ "Get", "Post" ]
},
{
"DownstreamPathTemplate": "/api/Courses/{id}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 5081
}
],
"UpstreamPathTemplate": "/Courses/{id}",
"UpstreamHttpMethod": [ "Get", "Put", "Delete" ]
}
],
"GlobalConfiguration": {}
}

You might also like