DEV Community

VzlDev
VzlDev

Posted on

Create a API Gateway using Ocelot

Hello guys!
Today I challenged myself to learn a little bit about Api Gateway, so I created one in my project using Ocelot.

What is an API Gateway?

An API gateway is a server that acts as an intermediary between clients and backend services. It functions as a reverse proxy to accept all application programming interface (API) calls, aggregate the various services required to fulfill them, and return the appropriate result.

What is Ocelot?

Ocelot is an open-source API Gateway built on the .NET platform. It is designed to manage and secure the flow of traffic between clients and backend services, particularly in microservices architectures. Ocelot provides a range of features to facilitate the handling of API requests and responses.

How to create an API Gateway using Ocelot step by step?

Step 1

First of all, you need to set up 2 different API's, in my project I created an UsersAPI and an EventsAPI
I have the following endpoints for the 2 controllers:

Step 2

Next you'll need to create a new api for the gateway, and do not add any controller.

Step 3

Add the Ocelot package to your gateway api.
Image description

Step 4

Create a file named ocelot.json in your gateway api.
Here's mine:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/users/{everything}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7239
        }
      ],
      "UpstreamPathTemplate": "/api/users/{everything}",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ]
    },
    {
      "DownstreamPathTemplate": "/events/{everything}",
      "DownstreamScheme": "https",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7235
        }
      ],
      "UpstreamPathTemplate": "/api/events/{everything}",
      "UpstreamHttpMethod": [ "GET", "POST", "PUT", "DELETE" ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "https://localhost:7174"
  }
}

Enter fullscreen mode Exit fullscreen mode
  • In the GlobalConfiguration section, the BaseUrl is the URL of the API Gateway. When running the API Gateway it will run on this base URL and the users will interact with this URL.
  • The UpStreamPathTemplate section represents the API Gateway endpoint, this will be the public endpoint for the users to ping.
  • The DownStreamTemplate section represents the APIs endpoints.

By the above configuration, when you hit the endpoint http://localhost:7174/api/events, it will be redirected to API endpoint http://localhost:7235/Events

Step 5

In your program.cs file, add the following:

builder.Configuration.AddJsonFile("ocelot.json", optional: false, reloadOnChange: true);

builder.Services.AddOcelot(builder.Configuration);

app.UseOcelot();
Enter fullscreen mode Exit fullscreen mode

Step 6

Try yourself and enjoy!

And that's it guys, a simple use case of using an api gateway with ocelot. I hope you liked it, stay tuned for more!

Top comments (0)