Ocelot with .Net Core 5.0
Tags: microservices, ocelot, csharp, dotnet
Published: 2020-11-30 13:44:06

Hello everybody. The world is struggling with the Covid 19 and the coronavirus is increasing day by day. I just want to say "Do not go anywhere stay at home and protect yourself".  

So I have been working from home since March. Since I am always at home, I try to spend my time very efficiently. These days, I make more time to read and write code. This is my first post on this website so and I'm excited. I want to share my experience. In this post, I'm going to explain how to implement Ocelot on .Net Core Web API.

What is API Gateway?

An API Gateway is basically an interface. The API Gateway receives are calls and redirects to services. 

What is Ocelot?

Ocelot is an API Gateway for the .Net Platform. Ocelot’s primary functionality is to take incoming HTTP requests and forward them to a downstream service I use for the microservice project. Here is the official page Ocelot https://ocelot.readthedocs.io/

Ocelot is a very powerful framework and here are the Ocelot features.

- Routing
- WebSockets
- Authentication
- Authorization
- Rate Limiting
- Caching
- Retry policies/QoS 
- Load Balancing
- Logging/Tracing/Correlation etc.. For more feature lists you should visit the official website.

I designed a new microservice project with .Net Core 5.0. If you ask why did you prefer the newest framework? Only one reason. The reason is Swagger installed by default. At the bottom of the picture, I draw my project design.

![Untitled Document](https://dev-to-uploads.s3.amazonaws.com/i/0gpw9obdpookrnj136ec.png)

Firstly I created a Visual Studio blank application. This application includes 3 different projects in the blank application. Here is the folder structure.

![folder_structure](https://dev-to-uploads.s3.amazonaws.com/i/gevo18lcxrodab22ectr.png)

If you develop with visual studio code you should visit Dotnet commands from [Commands](https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet)
 
Now I'm gonna explain how to configure Ocelot with the .Net Core project. Here is the hero :) NuGet package is Ocelot.

![ocelot-nuget](https://dev-to-uploads.s3.amazonaws.com/i/wl38idbb5epaj133k71g.png)

Install the NuGet package on the API Gateway project then create a new JSON file and called ocelot.json here is the configuration.

```
{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/catalog",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7001
        }
      ],
      "UpstreamPathTemplate": "/catalog-service/catalog"
    },
    {
      "DownstreamPathTemplate": "/api/customer",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7002
        }
      ],
      "UpstreamPathTemplate": "/customer-service/customer",
      "RateLimitOptions": {
        "ClientWhitelist": [],
        "EnableRateLimiting": true,
        "Period": "1s",
        "PeriodTimespan": 1,
        "Limit": 1
      }
    },
    {
      "DownstreamPathTemplate": "/api/customer/{id}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 7002
        }
      ],
      "UpstreamPathTemplate": "/customer-service/customer/{id}"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:7000"
  }
}
```
I defined the default port in Program.cs every project.

* API Gateway runs on http://localhost:7000
* Catalog API runs on http://localhost:7001
* Customer API runs on http://localhost:7002

Configure the Startup file.

![Screen Shot 2020-11-30 at 3.59.55 PM](https://dev-to-uploads.s3.amazonaws.com/i/vqzoikv6h0i3kysutfcz.png)

Every first request coming to UpstreamPathTemplate after that goes to DownstreamPathTemplate. After all the configurations start all projects without debugging mode and open the Postman application.

CatalogController.cs

![Screen Shot 2020-11-30 at 4.02.35 PM](https://dev-to-uploads.s3.amazonaws.com/i/gpm7emekv9e2gykotzcc.png)
 
UpstreamPathTemplate is /customer-service/customer
DownstreamPathTemplate is /api/customer

![postman](https://dev-to-uploads.s3.amazonaws.com/i/r0x97sjw0dn7jfq50fci.png)

If you request with a {id} parameter 

UpstreamPathTemplate is /customer-service/customer/{id}
DownstreamPathTemplate is /api/customer/{id}

![request-param](https://dev-to-uploads.s3.amazonaws.com/i/5ekupztv479qq3hxv584.png)

Another important feature is Rate Limiting. Rate limiting is a strategy for limiting network traffic. It puts a cap on how often someone can repeat an action within a certain timeframe – for instance, trying to log in to an account. Rate limiting can help stop certain kinds of malicious bot activity. It can also reduce strain on web servers. However, rate limiting is not a complete solution for managing bot activity. [Cloudflare](https://www.cloudflare.com/learning/bots/what-is-rate-limiting/)

```
"RateLimitOptions": {
        "ClientWhitelist": [],
        "EnableRateLimiting": true,
        "Period": "1s",
        "PeriodTimespan": 1,
        "Limit": 1
      }
 ```

I configure the /customer-service/customer endpoint. If you request in 1s multiple times I'll not reach the endpoint.

![rate-limit](https://dev-to-uploads.s3.amazonaws.com/i/9ls30wjfpkhu8duivpy4.png)
 

Thank you for your time :) 👋🏻

Source Code: [Github](https://github.com/bburaksseyhan/coffeemarket.git)

Reference:
[Header Image](https://www.houstonchronicle.com/opinion/outlook/article/Texas-has-60-ocelots-left-Let-s-save-them-13869144.php)