MICROSERVICES .NET CORE WITH EXAMPLE — Part 6

API gateways with Ocelot

Bingeek
4 min readDec 6, 2019
Source: internet

As we described the solution architect in Part 1, the clients (mobile app, website and CMS) will interact to the server via API Gateway. We are going to check it out shortly

What is API gateway?

Definition by WhatIs.com

An API gateway is programming that sits in front of an application programming interface (API) and acts as a single point of entry for a defined group of microservices. Because a gateway handles protocol translations, this type of front-end programming is especially useful when clients built with microservices make use of multiple, disparate APIs.

To be more visualized, let’s come back to our childhood with Tom & Jerry cartoon, episode “The bodyguard”, we can consider Spike is an API gateway which will grant access for Tom to catch Jerry or not 🙂.

Source: Tom & Jerry cartoon

Following functions should be covered in API gateway

  • Authentication: verify the identity of the client making the request
  • Authorization: verify whether the client is authorized to perform that particular operation or not
  • Caching: cache responses to reduce the number of requests made to the services
  • Rate limiting: limit how many requests per second are allowed from the clients
  • Logging: log what we need for tracing and debugging

Benefit of API gateway

  1. Add an additional layer of security to microservices
  2. Prevent exposing internal context to external clients
  3. Decrease microservice complexity
  4. Support mixing communication protocols
  5. Simpler codes

Building API Gateway with Ocelot

Ocelot is an open source framework used for building .NET core API gateways, the project is aimed at people using .NET / .NET Core to build applications designed with microservices or SOA architectures.

Ocelot provides an easy way to write a mapping file (ocelot.json) that could be used to route incoming HTTP requests to the appropriate downstream services.

What Ocelot offers?

Ocelot is a powerful framework that supports a lot of features, we can list some major features:

  • Routing
  • Request Aggregation
  • Server discovery
  • Authentication
  • Authorization
  • Rate Limiting
  • Caching
  • Load balancing
  • Logging

Let’s start to implement the API gateway with Ocelot

As I mentioned in Part 2, there are many service in KCommerce project, and we’re going to build a gateway to in between. I will apply Ocelot for Catalog service, you can see the full implementation in Github

1. Install Ocelot package in ApiGateways project

2. Add ocelot.json file which will host our Ocelot gateway configuration. We create this file manually and place it the same level path as appsetting.json (you can put it anywhere in project actually)

The basic content of this file is

The most important thing to note here is BaseUrl. Ocelot needs to know the URL it is running under in order to do Header find, replace and for certain administration configurations. When setting this URL, it should be the external URL that clients will see Ocelot running on.

In our case the BaseUrl will be the url of our gateway.

3. Add routing for the gateway

Ocelot takes incoming Http requests and forward them on to a downstream service.

Ocelot describes the routing of one request to another as a ReRoute. In order to get anything working in Ocelot you need to set up a ReRoute in the configuration.

The DownstreamPathTemplate, DownstreamScheme and DownstreamHostAndPorts define the url that a request will be forwarded to.

The UpstreamPathTemplate is the url that Ocelot will use to identify which DownstreamPathTemplate to use for a given request. The UpstreamHttpMethod is used so Ocelot can distinguish between requests with different HTTP verbs to the same URL.

The default ReRouting configuration is case insensitive. We can change it by

"ReRouteIsCaseSensitive": true

There are more configuration here

4. Configure Ocelot in Program.cs

5. Configure Ocelot in Startup.cs

6. Check out the result

Catalog service
API Gateway

This is basic implementation for API gateway with Ocelot. As I mentioned above, there are many features that Ocelot supports, you can reference at this link or wait for my next articles for building KCommerce project (a lot of features will be applied).

Stay tuned for my next articles

Part 7: Service discovery with Consul

--

--