In this article, we are going to implement API Gateway for our microservices to define a unified URL with that all client applications can consume it instead of consuming individual URLs of the microservices.Part-4 Asynchronous Data Communication Between Microservices Using RabbitMQ Message Broker With MassTransit[.NET6 Microservice Series]
Need Of API GateWay?:
Consider our microservice where we have two microservice applications. The 'Manufacture.API' application runs at 'https://localhost:7132' and The 'SalesBusiness.API' runs at 'https://localhost:7456' so from this we can understand we have 2 different domains bound to our each microservices.
So in the real-world application, we may deal with 10plus microservices base on our business. So handling or managing or consuming 10plus different domains by a client application is very hard.
So to solve this problem API Gateway comes into the picture. So API Gateway is also a simple API project with its own domain. But we will register our microservice URL with our API Gateway URL with appropriate Paths. So now will expose only API Gateway URL to the clients. So when client calls Gateway API endpoint behind scenes API Gateway invokes appropriate microservice URL based on URLs registered with, on receiving the response from microservices, API Gateway delivers the same response to the clients.
Ocelot API Gateway:
Ocelot is an open-source API Gateway built on top of .NET Core. It can be used to unify all microservices endpoints.
Create A .NET6 API Project To Setup API Gateway:
Let's create one more .NET6 API project to use it as API Gateway for our microservice application.
To create .NET6 application recommended IDE's are Visual Studio 2022', 'Visual Studio Code'. In this demo, I'm using .NET CLI and Visual Studio Code editor.
To create .NET6 application recommended IDE's are Visual Studio 2022', 'Visual Studio Code'. In this demo, I'm using .NET CLI and Visual Studio Code editor.
CLI command
dotnet new webapi -o Your_Project_Name
dotnet new webapi -o Your_Project_Name
Install Ocelot NuGet Package:
Let's install the Ocelot NuGet Package.
CLI command
dotnet add package Ocelot --version 18.0.0
dotnet add package Ocelot --version 18.0.0
Package Manager
Install-Package Ocelot -Version 18.0.0
Install-Package Ocelot -Version 18.0.0
Ocelot.json File:
Now we have to create file like 'Ocelot.json'. In this file, we will map our microservice URLs with our ocelot application URLs
Ocelot.json:
{ "Routes": [ { "DownstreamPathTemplate": "/products", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7109 } ], "UpstreamPathTemplate": "/manufacture/products", "UpstreamHttpMethod": ["Get", "Post"] }, { "DownstreamPathTemplate": "/orders", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7013 } ], "UpstreamPathTemplate": "/sales/orders", "UpstreamHttpMethod": ["Get", "Post"] }, { "DownstreamPathTemplate": "/products/{id}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7109 } ], "UpstreamPathTemplate": "/manufacture/products/{id}", "UpstreamHttpMethod": ["Get", "Post"] }, { "DownstreamPathTemplate": "/orders/{id}", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7013 } ], "UpstreamPathTemplate": "/sales/orders/{id}", "UpstreamHttpMethod": ["Get", "Post"] } ], "GlobalConfiguration": {} }
- The 'Routes' property is an array type where we are going to add our URL mappings.
- The 'DownstreamPathTeplate' is our microservice endpoint.
- The 'DownstreamSchem' is our microservice is 'HTTPS' or 'HTTP'.
- The 'DownstreamHostAndPorts' where we have to define our microservice HostName and PortNumber.
- The 'UpStreamPathTemplate' is ocelot URL which is an alia URL for our 'DownStreamPathTemlate'.
- The 'UpstreamHttpMethod' specifies the supported methods.
Integrate Ocelot Pipeline:
Now we have to integrate the Ocelot Pipeline. So replace code inside of the 'Program.cs' file with the below code.
Program.cs:
using Ocelot.DependencyInjection; using Ocelot.Middleware; var builder = WebApplication.CreateBuilder(args); builder.Configuration.AddJsonFile("ocelot.json"); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddOcelot(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseOcelot().Wait(); app.UseAuthorization(); app.MapControllers(); app.Run();
- (Line 1&2) Added required namespaces of the 'Ocelot' package.
- (Line: 6) Load the 'Ocelot.json' file.
- (Line: 14) Registered 'AddOcelot' service.
- (Line: 27) Registered the 'Ocelot' middleware.
Test Mapped API Gateway Endpoints:
Our 'Manufacture.API' microservice product endpoint looks like below:
Now let's consume the 'Products' endpoint from our API Gateway endpoint.
Our 'SalesBusiness.API' microservice orders endpoint looks like below:
Now let's consume the 'Orders' endpoint from our API Gateway endpoint.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Support Me!
Buy Me A Coffee
PayPal Me
Wrapping Up:
Hopefully, I think this article delivered some useful information Microservice flow in .NET6 applications. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Refer:
Part-1 Introduction On Microservice And SQL Database Design For Microservices [.NET 6 MicorService Series]Part-2 Create A Microservice For The Products Endpoint[.NET6 Microservice Series]
Part-3 Create A Microservice For The Orders Endpoint[.NET6 Microservice Series]
Part-4 Asynchronous Data Communication Between Microservices Using RabbitMQ Message Broker With MassTransit[.NET6 Microservice Series]
Git Resource
Part-3 Create A Microservice For The Orders Endpoint[.NET6 Microservice Series]
Part-4 Asynchronous Data Communication Between Microservices Using RabbitMQ Message Broker With MassTransit[.NET6 Microservice Series]
Git Resource
Comments
Post a Comment