Microservices With Ocelot API Gateway In ASP.NET Core

Introduction

 
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 benifits 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 each other. There are advantages and disadvantages in Monolith. Compared to Microservices the advantages are less in Monolith Architecture.
 
Microservices With Ocelot API Gateway In ASP.NET Core
 

Disadvantage

 
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 for maintenance 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 diffcult.
 

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.
 
The following picture is from Microsoft Docs which shows the microservices architecture style. 
 
Microservices With Ocelot API Gateway In ASP.NET Core
 

Microservices Creation

 
Create a Blank Solution
 
Microservices With Ocelot API Gateway In ASP.NET Core
 
Create a New Solution Folder with the name as Microservices
 
     ->  Right Click on Microservices Folder
     ->  Click on Add
     ->  Click on New Project
 
Create a Customer Microservice
 
Microservices With Ocelot API Gateway In ASP.NET Core
     
-> Enter the project name 
-> Choose API as 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 template with the same .Net Core 3.1 Version.
 
Folder Structure
 
Microservices With Ocelot API Gateway In ASP.NET Core
 
Configuring the Ocelot API Gateway
 
This is how the Ocelot API Gateway Works in our project.
 
Microservices With Ocelot API Gateway In ASP.NET Core
 
To know about the Ocelot and its features go through this link Ocelot API Gateway
 
Install the package under the Gateway.WebAPI
  1. Install-Package Ocelot  
Add Configuration settings in Startup.cs
 
Startup.cs 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Builder;  
  6. using Microsoft.AspNetCore.Hosting;  
  7. using Microsoft.AspNetCore.Http;  
  8. using Microsoft.Extensions.DependencyInjection;  
  9. using Microsoft.Extensions.Hosting;  
  10. using Ocelot.DependencyInjection;  
  11. using Ocelot.Middleware;  
  12.   
  13. namespace Gateway.WebApi  
  14. {  
  15.     public class Startup  
  16.     {  
  17.         // This method gets called by the runtime. Use this method to add services to the container.  
  18.         // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940  
  19. public void ConfigureServices(IServiceCollection services)  
  20. {  
  21.     services.AddOcelot();  
  22. }  
  23.   
  24.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  25. public async void Configure(IApplicationBuilder app, IWebHostEnvironment env)  
  26. {  
  27.     if (env.IsDevelopment())  
  28.     {  
  29.         app.UseDeveloperExceptionPage();  
  30.     }  
  31.     app.UseRouting();  
  32.     app.UseEndpoints(endpoints =>  
  33.     {  
  34.         endpoints.MapControllers();  
  35.     });  
  36.     await app.UseOcelot();  
  37. }  
  38.     }  
  39. }  
Create configuration.Json File under the Gateway.WebAPI to define the Routes which are necessary for Microservices.
 
configuration.Json
  1. {  
  2.   "Routes": [  
  3.     {  
  4.       "DownstreamPathTemplate""/api/product",  
  5.       "DownstreamScheme""https",  
  6.       "DownstreamHostAndPorts": [  
  7.         {  
  8.           "Host""localhost",  
  9.           "Port": 44337  
  10.         }  
  11.       ],  
  12.       "UpstreamPathTemplate""/gateway/product",  
  13.       "UpstreamHttpMethod": [ "POST""PUT""GET" ]  
  14.     },  
  15.     {  
  16.       "DownstreamPathTemplate""/api/product/{id}",  
  17.       "DownstreamScheme""https",  
  18.       "DownstreamHostAndPorts": [  
  19.         {  
  20.           "Host""localhost",  
  21.           "Port": 44337  
  22.         }  
  23.       ],  
  24.       "UpstreamPathTemplate""/gateway/product/{id}",  
  25.       "UpstreamHttpMethod": [ "GET""DELETE" ]  
  26.     },  
  27.     {  
  28.       "DownstreamPathTemplate""/api/customer",  
  29.       "DownstreamScheme""https",  
  30.       "DownstreamHostAndPorts": [  
  31.         {  
  32.           "Host""localhost",  
  33.           "Port": 44373  
  34.         }  
  35.       ],  
  36.       "UpstreamPathTemplate""/gateway/customer",  
  37.       "UpstreamHttpMethod": [ "POST""PUT""GET" ]  
  38.     },  
  39.     {  
  40.       "DownstreamPathTemplate""/api/customer/{id}",  
  41.       "DownstreamScheme""https",  
  42.       "DownstreamHostAndPorts": [  
  43.         {  
  44.           "Host""localhost",  
  45.           "Port": 44373  
  46.         }  
  47.       ],  
  48.       "UpstreamPathTemplate""/gateway/customer/{id}",  
  49.       "UpstreamHttpMethod": [ "GET""DELETE" ]  
  50.     }  
  51.   ],  
  52.   "GlobalConfiguration": {  
  53.     "BaseUrl""http://localhost:44382"  
  54.   }  
  55.   
  56. }  
DownstreampathTemplate -  Defines the route of 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)
  1. {  
  2.   "iisSettings": {  
  3.     "windowsAuthentication"false,   
  4.     "anonymousAuthentication"true,   
  5.     "iisExpress": {  
  6.       "applicationUrl""http://localhost:51733",  
  7.       "sslPort": 44382  
  8.     }  
  9.   },  
  10.   "profiles": {  
  11.     "IIS Express": {  
  12.       "commandName""IISExpress",  
  13.       "launchBrowser"true,  
  14.       "environmentVariables": {  
  15.         "ASPNETCORE_ENVIRONMENT""Development"  
  16.       }  
  17.     },  
  18.     "Gateway.WebApi": {  
  19.       "commandName""Project",  
  20.       "launchBrowser"true,  
  21.       "applicationUrl""https://localhost:5001;http://localhost:5000",  
  22.       "environmentVariables": {  
  23.         "ASPNETCORE_ENVIRONMENT""Development"  
  24.       }  
  25.     }  
  26.   }  
  27. }  
Before testing the application make sure to run mutiple projects in a single go.
 
 -> Right Click on solution
-> Click on properties
 
Microservices With Ocelot API Gateway In ASP.NET Core
 
Run the Project to check the results.
 
Output
 
Product Microservice - end point 
 
Microservices With Ocelot API Gateway In ASP.NET Core
Customer Microservice - end point  
 
Microservices With Ocelot API Gateway In ASP.NET Core
Note 
I also implemented Swagger in this to check the result of individual Microservices.
I hope this article helps you!
 
Keep Learning!!!


Recommended Ebook

Dockerizing ASP.NET Core and Blazor Applications on Mac

Download Now!
Similar Articles