Microservices architecture with Ocelot API gateway in Asp.Net Core

In this tutorial, we are going to cover the implementation of microservices architecture with Ocelot API Gateway using Asp.Net Core 5. We will create a very simple blank solution using visual studio 2019, and then we will implement Microservices architecture, and then we will see how to implement Ocelot API Gateway in Asp.Net Core 5 application.

What we will learn in this tutorial?

What is Microservices Architecture?
What are the advantages of Microservices Architecture?
What are the disadvantages of Microservices Architecture?
What is Monolithic architecture?
What are the disadvantages of Monolithic Architecture?
What is the difference between Microservices Architecture and Monolithic Architecture?
How to create a Microservices Architecture?
What is Ocelot API Gateway?
How to implement Ocelot API Gateway in Microservices Architecture?
How to test Microservices Architecture using Ocelot API Gateway?

What is Microservices Architecture?

Microservices architecture is an architectural development style that is used to build an application as a collection of narrowly focused services developed for a business domain.

What are the advantages of Microservices Architecture?

There are some advantages to using microservices:

It is very easy to build and maintain.
Smaller and faster deployment.
Technology diversity. It means it is very easy to use different technologies.
Independent deployment.
Ease of understanding.
It helps to divide an application into smaller parts.

What are the disadvantages of Microservices Architecture?

There are below some disadvantages of Microservices:

Communication between services is very complex. In case if your application consists of hundreds or thousands of microservices, then the application becomes really complex. Especially when the services talk to each other.
More resources are needed to use multiple services.
Debugging is harder.
Testing is harder.
Cascading of problems to other services.
Handling the messages in the queue is very hard.
Latency: every call to another service adds some latency and the user has to wait longer until the result comes.
Separate deployment of each service.

What is Monolithic architecture?

Monolith architecture is an architectural development style in which all the software components of an application are clubbed within a single autonomous unit.

What are the disadvantages of Monolithic Architecture?

There are below some disadvantages of Microservices:

One failure may cause the whole system to go down.
You need to Deploy an entire system once.
It is not easy to scale up and down based on demand.
Impossible to implement different technologies or programming languages. Because everything is tightly coupled.

What are the differences between Microservices Architecture and Monolith Architecture?

There are below differences between microservices architecture vs monolithic architecture.

Microservices Architecture
Monolith Architecture

In a microservices architecture, we build an application as a collection of narrowly focused services developed for a business domain
All the software components of an application are clubbed within a single autonomous unit.

It is very easy to scale up and down based on demand.
It is not easy to scale up and down based on demand.

Each Microservice has its own database.
It has a shared database.

A failure of one service doesn’t affect the other services.
One failure may cause the whole system to go down.

Smaller and faster deployment. Each service can be deployed individually. 
You need to Deploy an entire system once.

You can use different technologies to build different services for different business needs. Because every service or module is independent of other services or modules.
Impossible to implement different technologies or programming languages. Because everything is tightly coupled.

Communication between services is very complex. In case if your application consists of hundreds or thousands of microservices, then the application becomes really complex. Especially when the services talk to each other.
Secure data processing and transferring are easier at the system level.

You need to test each service individually.
The test is very easy. 

SOA (Service Oriented Architecture) vs Microservices Architecture

How to create a Microservices Architecture?

Now, in this step, we will see how to create a Microservices Architecture using Visual Studio 2019. So, go to vs 2019 and then follow the below steps.

Step # 1: Create a blank solution

So, go to Visual Studio and then click on the “Create a new project” button and then search for a blank solution and then select “Blank Solution” and then click on the Next button as you do see below in the screenshot.

Now, enter the Solution name (E.g. Microservices.WebApi ) and then select Location and then click on the Create button as you do see below in the screenshot.

Step # 2: Create a Product Microservice

Now, right-click on the “Microservices.WebApi” solution name and then add a new folder with the name of Microservices. Now, right-click on the Microservices folder and then select Add and then select New Project… and then select “ASP.NET Core Web Application” and then click on the Next button as you do see below in the screenshot.

Now, enter the name (E.g. Product.Microservice) and then select a location and then click on the Create button as you do see below in the screenshot.

Now, select ASP.NET Core 5.0 and then select ASP.NET Core Web API template and then click on the Create button as you do see below in the screenshot.

Step # 3: Create a Customer Microservice

Now, add another Microservice with the name of Customer.Microservice as we have added Product.Microservice. 

Step # 4: Create an API Gateway

Now, in this step, we will add an API gateway. So, right-click on the solution and then select Add and then select New Project… Then select ASP.NET Core Web Application template and then click on the Next button and then enter the name (E.g. ApiGateway) and then select ASP.NET Core Empty template as you do see below in the screenshot.

Now, you will see the folder structure as you do see below in the screenshot.

Now, before going to configure Ocelot API Gateway in our solution, we will understand what Ocelot API Gateway is. 

What is an Ocelot API Gateway?

Ocelot is an open-source API Gateway. It plays an important role in .NET/Core microservices and service-oriented architecture. It unifies the point of entry for multiple microservices. So, it transforms the incoming HTTP request from the client and then forwards it to a specific microservice.

There are some features of Ocelot:

Routing
Request Aggregation
WebSockets
Authentication
Authorization
Caching
Load Balancing

How to configure Ocelot?

So, in this section, we will configure Ocelot in our solution. So, go to the project folder structure and then go to the ApiGateway project and then install the Ocelot package as you do see below in the screenshot.

=> Install-Package Ocelot

Now, go to startup.cs class and then write the code as you do see below in the file’s line # 21 and line # 38.

Now, go to ApiGateway and then create a new file with the name of configuration.json file to define the Upstream/Downstream routes for the API Gateway which are very helpful for the Ocelot to understand the routes.

Let’s understand the above code:

Line # 4: DownstreamPathTemplate: It will define the route of the actual endpoint of product  Microservice.

Line # 5: DownstreamScheme: It will define the scheme of product microservice.

Line # 6: DownstreamHostAndPorts: It will define the location of product microservice.

Line # 12: UpstreamPathTemplate: Ocelot Api Gateway will be requested by the client using this path.

Line # 13: UpstreamHttpMethod: These are the supported HTTP methods to the API gateway.

Line # 15 to 26: Here in this block of code, we are accepting the id parameter in the route. 

Line # 27 to 38: This block is same as we have discussed above from Line # 3 to Line # 14. But in this block we are dealing with the customer microservice.

Line # 39 to 50: Here in this block we are dealing with the id parameter of customer microservice.

Now, go to Program.cs file of ApiGateway project and then write the code as you do see below file’s line # 25 to line # 28.

How to test Microservices Architecture using Ocelot API Gateway?

Now, in this step, we will test it. By default, our project will run under the ApiGateway port number which we have already defined in the configuration.json file. If you want to see the port number of ApiGateway, then go to ApiGateway project and then go to properties and then open launchSettings.json file. And there you will see the port number as you do see below in the file’s line # 7.

Now, we will run all the APIs in a single go. So, to do this, just go to project folder structure and then right-click on the solution and then click on the properties and then select the Multiple startup projects option and then change the Action of each project as you do see below in the screenshot.

Now, run the project, and then you will see the output as you do see below in the screenshots. As you see, now we can see both the services are accessible via the API gateway.

Product Service Output:

Customer Service Output:

Thank you for reading. If you found this blog helpful then please like and share within your community.

People also reading:

SOA (Service-Oriented Architecture) Vs Microservices Architecture
Microservices Communication using RabbitMQ in Asp.Net Core 5

Flatlogic Admin Templates banner

Leave a Reply

Your email address will not be published. Required fields are marked *