Implementing API Gateway in .NET Core with Ocelot

Implementing API Gateway in .NET Core with Ocelot

Microservices Architecture 

The adoption of microservices architecture, which was developed to solve the monolithic architecture limitations, is a big trend in the software industry. 

Many of today's applications are designed as small independent services that communicate with each other. Every one of these services has a separate responsibility from the others. 

Modern cloud-native applications are built as microservices. 

No alt text provided for this image

 

Why use an API Gateway? 

A microservices based application may have many microservices, from 10 to 100 to 1000, each having their own entry point. 

By using an API Gateway, we can provide one unified entry point to the whole system. 

This way, a client needs only to communicate with the API Gateway, without knowing the entry points for each microservice it needs to call. 

Benefits of using an API Gateway? 

We can implement some cross-cutting concerns at the API Gateway level, without the need to implement and manage them at each microservice separately. 

Some of those are: 

  • Rate limiting 
  • Authentication 
  • Authorization 
  • Response caching 
  • Request aggregation 
  • Logging 
  • Performance metrics 

For additional information of Ocelots capabilities, visit GitHub - ThreeMammals/Ocelot: .NET core API Gateway

API Gateway with Ocelot 

Ocelot is an open-source, scalable and fast API Gateway designed for .NET Core in combination with a microservices architecture. 

Below, I will briefly explain how we can use it in a project. 

Step 1) Creating the project structure 

Here, we will create two separate projects. One will be our API Gateway and the second will be our microservice which will be behind the API Gateway. For the microservice project we can use the default template. 

No alt text provided for this image

For additional details of the code in every file, visit the GitHub repository specified at the end of this article. 

Step 2) Install Ocelot package 

We need to install Ocelot package using Install-Package Ocelot.  

Step 3) Add Configuration file 

Once the installation is complete, we need to add an ocelot.json file, which is the configuration file of the API Gateway. The file should have the following structure:  

No alt text provided for this image

Additionally, caching and rate limiting for the specific route have also been set. 

With this configuration, we can call the service by making a HTTP GET request on the following URL: https://localhost:5021/api/weather 

No alt text provided for this image

Perfect. We have successfully managed to get data back from the microservice by making a request to our API Gateway. 

A full, more detailed implementation can be found on my GitHub repository on the following link:

DimitarIliev/API-Gateway-with-Ocelot: Example of buliding an API Gateway for ASP.NET Core using Ocelot (github.com) 

Hi Dimitar, Thanks for sharing the article. Please can I also suggest you to provide Ocelot hosting model in Azure.My API is hosted on Azure PaaS . Can I host Ocelot in Azure Web Apps itself and protect it with Azure app gateway at front end. Ocelot and Api would be internal and protected by Identity server also. Thanks, Ajay

Like
Reply

To view or add a comment, sign in

More articles by Dimitar Iliev

Others also viewed

Explore content categories