Build Microservices with Ocelot API Gateway in .NET Core

Jaykrishnareddy
Mar 26 · 4 min read
Microservices with Ocelet API Gateway

Today we are going to learn about Microservice architecture and its implementation using Ocelot API Gateway in ASp.Net Core — 3.1. We all know the benefits of using Microservices for large-scale applications.

Microservice Architecture — Overview

There are two possible ways to build and structure the applications:

  1. Monolith Architecture
  2. Microservices Architecture

Monolith Architecture

Monolith is like a big container, wherein all the software components of an app are assembled and tightly coupled; i.e each component fully depends on the other. There are advantages and disadvantages to Monolith. Compared to Microservices the advantages are less in Monolith Architecture.

Disadvantages of Monolith:

Slow Development

If we modify any module or any component we need to redeploy the whole application instead of updating part of it. It consumes more time in slow development.

Unreliable

If one service goes down then the entire application stops working because all the services of the application are connected to each other.

Large and Complex Applications

For large-scale applications, it is difficult to maintain because they are dependent on each other.

It consumes more memory where each component will access the whole data and it makes more memory consumption and also rebuilds the application. In addition to that, we need to change the whole application and it makes the process a bit difficult.

Microservices Architecture

Microservices Architecture refers to a technique that gives modern developers a way to design highly scalable, flexible applications by decomposing the application into discrete services that implement specific business functions. These services, often referred to as “Loosely Coupled,” can be built, deployed, and scaled independently.

Setting up the Project

Create a Blank Solution

Create a New Solution Folder with the name Microservices

Right Click on Microservices Folder

Click on Add

Click on New Project

Create a Customer Microservice

Enter the project name

Choose API as a template and we are going with .Net Core 3.1 Version.

Click on Create

Create the Product Microservice

Follow the same process as we did for Customer Microservice.

Create the API Gateway

Choose Empty as a template with the same .Net Core 3.1 Version.

Folder Structure

Configuring the Ocelot API Gateway

This is how the Ocelot API Gateway Works in our project.

To know about the Ocelot and its features go through this link Ocelot API Gateway

Install the package under the Gateway.WebAPI

Install-Package Ocelot

Add Configuration settings in Startup. cs

Startup.cs

Create configuration.Json File under the Gateway.WebAPI to define the Routes which are necessary for Microservices.

configuration.Json

DownstreampathTemplate — Defines the route of the actual endpoint of Microservice

DownstreamScheme — scheme of Microservice, HTTPS

DownstreamHostsandPorts — Host and Port of Microservice will define here.

UpstreampathTemplate — The path at which the client will request the Ocelot API Gateway

UpstreamHttpmethod — The Supported HTTP Methods to the API Gateway. Based on the incoming method, Ocelot sends a similar HTTP method request to microservices as well.

Let’s test the application and this will run under the Gateway.WebAPI Port number which we already defined in the configuration.json file

launchsettings.json (Gateway API)

Before testing the application make sure to run multiple projects in a single go.

Right Click on solution

Click on properties

Run the Project to check the results.

Output

Product Microservice — endpoint

Customer Microservice — endpoint

Note

I also implemented Swagger in this to check the result of individual Microservices.

If you found this article helps you, please give it a 👏🏻

Keep Learning!!!

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To stay up to date on other topics, follow us on LinkedIn. https://www.linkedin.com/company/nerdfortech

Jaykrishnareddy

Written by

Programmer 💻 | Biker 🏍️ | Blogger 👨‍💻 | Indian

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To stay up to date on other topics, follow us on LinkedIn. https://www.linkedin.com/company/nerdfortech

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store