What is the API gateway?

An API gateway is an API management tool that sits between the client and a collection of backend services. The client doesn’t need to know the backend service architectures. When a gateway way is used, it becomes the single point of the contract for clients; it receives the API calls and routes each one to the appropriate backend.

An API gateway is suitable for the microservice architecture but not for using sizeable monolithic service architecture. When the backend service can’t be direct access from the outside, this can create additional options for the developers.

What is the Ocelot?

Ocelot is a .NET API Gateway aimed at developers using .NET running a micro-services/service-oriented architecture that needs a unified point of entry into their system. It will work with anything that speaks HTTP and run on any platform ASP.NET Core supports.

Ocelot is a bunch of middleware in a specific order and manipulates the HTTPRequest object into a state specified by this configuration until it reaches a request builder middleware where it creates a HttpRequestMessage object which is used to request downstream services. The middleware that makes the request is the last thing in the Ocelot pipeline, and it doesn’t call the next middleware. The response from the downstream services is retrieved as the requests go back up the Ocelot pipeline. There is a piece of middleware that maps the HttpResponseMessage onto the HttpResponse object, and that is returned to the clients, which are basically it with a bunch of other features.

Fig 1 Ocelot API Gateway architecture applications

How to implement the Ocelot API gateway in the .NET 5 web API application?

Step 1 Create the Web API projects. According to the onion architecture concept, creating the Core and Infra projects in the same solution files.

Fig 2 Web & API project solution exploerers.

Step 2 Create the ORM source code, which uses entity framework 5 (EF5).

Fig 3 Entity framework 5 with the database log mechanism.

Step 3 Create the demo controller file and register the service class files in the Startup.cs file. Remember that the service class file must have the CRUD function source code and the EF disposal mechanism. It can use the generic type method to implement.

Step 4 Create the API gateway project in the same solution file. The Ocelot API gateway project chooses the ASP.NET Core empty template projects without the open API and HTTPS options.

Fig 4 Ocelot API gateway project with the ocelot cache manager Nuget packages.

Note: If you choose the ASP.NET Core Web API template project with the open API option and the HTTPS, the project must remove the open API setting source. It will not work when the project target framework is .NET5.

Step 5 Install the Ocelot packages and the Ocelot.Cache.CacheManager packages.

Note: The related Ocelot package version can use 17.0.1 or older version Ocelot packages on .NET 5 SDK. Version 18 of the related Ocelot package only can use on the .NET 6 SDK or, more lately, the new .NET SDK.

Step 6 Create the ocelot.json files, which define the route information, such as downstream path templates, upstream path templates, and the cache option information.

{
"Routes": [
{
"DownstreamPathTemplate": "/api/Demo",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 48444
}
],
"UpstreamPathTemplate": "/api/v1/Demo",
"UpstreamHttpMethod": [ "GET", "POST", "PUT" ],
"RateLimitOptions": {
"ClientWhitelist": [],
"EnableRateLimiting": true,
"Period": "10s",
"PeriodTimespan": 10,
"Limit": 3
},
"FileCacheOptions": {
"TtlSeconds": 5,
"Region": "democaching"
}
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:24898"
}
}
view raw gistfile1.txt hosted with ❤ by GitHub

Step 7 Register the Ocelot packages in the web API gateway project.

Fig 5 Add the ocelot route information json file in the Ocelot API gateway projects.
Fig 6 Register the ocelot packages to the projects.

Note: You must choose multiple projects to debug models if the developer wants to debug the web API gateway project.

Reference

Leave a Reply