Project Making with API Gateway and Microservice in Asp.Net

Hi, We'll write two microservices in this post and managing with api gateway

What is Api Gateway?

An API gateway is an API management tool that sits between a client and a collection of backend services

How to work?

An Api gateway routes requests from client to available microservices

A Sample Project Making

We will write two microservices in the visual studio environment and route them with the api gateway.

Let's open as blank project and add services folder and ApiGateway as .net 5.0 api

Let's add two project named UserService and BookService to the services folder. Each other will be entirely independent these services which designed as Domain Driven Design

Folder Strcuture of Project is ready. Let's add BookServiceand BookService. BookService will return book info, add get action to Book Controller and the parametre of their is isbn as string. with Book?isbn="asd" url will return book info without database, using static list

Let's create folders named services to Book.Domain and Book.Infastructure, add IBookService and BookService to these folders

DTOS/BookDTO.cs

IBookService

BookService :

Let's make dependecy injection

BookController:

Let's set the Book.API layer a startup as project then when we test with a url like api/book?isbn=test, we can sucessfully see the data in response. Thus for now we completed the book service

User service will be like Book service. The parameter wil be username.

Bu bölüm kitap servisinin neredeyse aynısı olduğu için sadece UserController ve IUserService’i göstereceğim

UserController:

IUserService:

In this section we added UserService, IUserService, UserController same as BookService. when we test with a url like api/user?username=test, we can sucessfully see the data in response. Let's go gateway create.

Api Gateway

First of all, let's talk about why we need api gateway. The two services we do will work on different ports, maybe on different machines. We will can request to these services at client, connecting different port on every request will be hard api management at client, instead having all our services at a single point will facilitate api management.

Let's add Ocelot package to layer of APIGateway on nuget.
offical documention

after installing package let's add apiSettings.json to root directory, will get definition of these services.
We are editing our json file as follows.

There is array object named routes. We added our services to these array, wrote https ports of services to port section. Downstream is the information of the service we are connecting to, and Upstream is the port we call over the gateway, that is, we have determined which address to trigger on the port we actually use. Downstream, bağlandığımız servisin bilgileri, Upstream ise gateway üzerinden çağırdığımız yani aslında kullandığımız port üzerinde hangi adresle tetikleneceğini belirledik.

Let's entire solution properties, these services and gateway set as start

Let's run our project and check the response with the urls we have determined.

You can access the project files from the link below.

Project url : https://github.com/okankrdg/ApiGatewayExample

You may also like...

2 Responses

  1. abdullah says:

    Hocam Selamlar,
    Book servisinde okan kitap aldı diye bir tabloda kitap ve kullanıcı Id lerini tutuyor diyelim.
    Ve işlemler binlerce satırı buldu.
    Book ve User ayrı Db’lerde diye düşünelim.
    Şimdi datayı çoklayarak Book’ta user tablosu oluşturabilirim. Ya da sadece Id lerini tutarak BFF tarzı bir işlem yapabilirim.
    Şimdi BFF yaptığımızı düşünelim Apigetaway’de yalnız yüz bin satır kitap ve yüz bin satır user datasını nasıl birleştirebiliriz.
    Teşekkürler

    • Merhaba,
      apiSetting.json dosyasında Routes gibi Aggregates adında bir dizi tanımlamalısınız,
      ör: "Aggregates": [
      {
      "RouteKeys": [
      "user",
      "book"
      ],
      "Aggregator": "UserBookAggregator",
      "UpstreamPathTemplate": "/bookwithuser/{id}"
      }
      ]
      , daha sonra yazılan UserBookAggregator’ü IDefinedAggregator’dan miras alarak yeni bir response dönderebilirsiniz. Daha fazla detay için resmi doküman da yardımcı olabilir

Leave a Reply

Your email address will not be published.

English