Featured

Microservice Architecture In ASP.NET 6 With API Gateway

Last modified: April 17, 2022

There are two main ways of building a software

  • Monolith
  • Microservice
Monolith

Monolith

Microservice

Microservice

Microservice vs Monolith Architecture
Monolith Microservice
All the logic including business in one application Each Microservice has it own responsibility
Single Database Each Database per Microservice
One programming lanaguage Each Microservice can have its own programming lanaguage
Usually deploy to a server Each Microservice can be deploy to individual server

1. Create Microservice application and API Getaway

Microservice

Microservice

Microservice

Microservice

Remove old code from WebAPI Microservice

Repeat above steps to create Customer and Product Microservices projects

Now we have Microservice

We are going to install Ocelot to allow us to setup API Gateway

"Ocelot is a .NET API Gateway. This project is aimed at people using .NET running a micro services / service oriented architecture that need a unified point of entry into their system. However it will work with anything that speaks HTTP and run on any platform that ASP.NET Core supports." source: https://github.com/ThreeMammals/Ocelot

Microservice

We need to run all three Web APIs to test the application to do that, right the solution and select as below. Microservice

Create ocelot.json file in API Getaway Project and copy the following code. You can find Web API port from launchSettings.json file

  • DownstreamPathTemplate for Web API endpoint
  • UpstreamPathTemplate for Getaway API endpoint
{ "Routes": [ { "DownstreamPathTemplate": "/product", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7105 } ], "UpstreamPathTemplate": "/gateway/product", "UpstreamHttpMethod": [ "GET" ] }, { "DownstreamPathTemplate": "/customer", "DownstreamScheme": "https", "DownstreamHostAndPorts": [ { "Host": "localhost", "Port": 7242 } ], "UpstreamPathTemplate": "/gateway/customer", "UpstreamHttpMethod": [ "GET" ] } ] }

Microservice

Microservice

Test application

Microservice

Microservice