Building Simple API Gateways with Ocelot and ASP.net Core

In the previous article, we've discovered what is an API gateway and the cool things that it brings. This article will demonstrate how to build a simple API gateway that routes incoming HTTP requests to the appropriate downstream services.

Articles in the Series

This article belongs to a series of articles that explains the importance of API gateways and how to build them using ASP.net Core. If you're interested to learn more about API gateways, it might be a good idea to spend some time reading the articles listed below.

Introducing Ocelot

Download POC from Github

Ocelot is an open source framework used for building .NET core API gateways, the project is aimed at people using .NET / .NET Core to build applications designed with microservices or SOA architectures. Ocelot provides an easy way to write a mapping file (ocelot.json) that could be used to route incoming HTTP requests to the appropriate downstream services.

See: GITHUB | Official Website

Battle Plan

In this guide, we'll build an API gateway for an e-commerce website. The e-commerce API will contain three sub-domains that we'll call "Authentication", "Catalog" and "Ledger" contexts. The gateway we'll build in this article will behave like a reverse proxy that routes incoming HTTP requests to the following downstream services:

  • Authentication Service - Bounded context in-charge of managing all authentication related things like end-users and access to system.
  • Catalog Service - Bounded context in-charge of managing all offered products.
  • Ledger Service - Bounded context in-charge of managing all monetary-product transactions in the system.

Setup

If you want to skip the setup phase, you can download a bunch of bash scripts that I wrote to automate the generation of the four ASP.net core projects. Once you've downloaded the scripts, place them in a folder to group the output of the automation process and execute the script named 100_build-api-gateway.sh.

If you are a newbie in ASP.net Core or just keen to learn how the setup works, you can follow the steps below:

  • Generate 4 ASP.net Core Projects

  • Generate Solution and Attach Projects

  • Install Ocelot Nuget Package

  • Generate POCOs and Controllers

    You can navigate to a sample project from GITHUB and copy the following models and controllers to your solutions:
    • User Model

      ./Authentication/Models/User.cs
      Github Link
    • User Controller

      ./Authentication/Controllers/UserController.cs
      Github Link
    • Product Model

      ./Catalog/Models/Product.cs
      Github Link
    • Project Controller

      ./Catalog/Controllers/ProductController.cs
      Github Link
    • Transaction Model

      ./Ledger/Models/Transaction.cs
      Github Link
    • Transaction Contorller

      ./Ledger/Controllers/TransactionController.cs
      Github Link
  • Configure Service Ports via launchSettings.json

    In order to route HTTP requests properly, you have to ensure that each service will be able to run on the following local ports:
  • Start and test individual bounded context services

    Test the solutions by running each of them and issuing a GET request against the following URLS: NOTE: You can configure a solution to run multiple services at the same time by following this awesome guide:

Configuring Ocelot

Now lets focus on the most important detail which is configuring Ocelot. The first thing that we have to do is configure the ocelot.json file and add the code below. Note that the BaseUrl is required by Ocelot for it to be aware of the URL it is running in order to perform a header find & replace for certain administrative configurations.

Navigate to the Program.cs file of the Gateway project and add the following configurations on the web host builder method. The code below will setup all the middlewares required by ocelot to function.

Setting up your Routes

Now that everything we need in the gateway side is configured, its time to setup routes using the ocelot.json file. Update your ocelot.json file by updating the re-routes section. The code above setups up the mappings dictionary of (see below) your application gateway to the downstream services.

  • Gateway to Authentication
  • Gateway to Catalog
  • Gateway to Ledger

Testing the routes out

To test the routes, start all projects and navigate to each link listed below.

Conclusion

In this article, we've learned how to perform basic re-routing of HTTP requests from a gateway to downstream services using Ocelot. Please do note that the article only explained basic re-routing and the power of API gateways starts to shine with request aggregation which is the subject of the next article.

Related Articles


Get Some Cool Stuff From Amazon!


Comments

  1. That is very interesting; you are a very skilled blogger. I have shared your website in my social networks! A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article.
    Best Quoting Software

    ReplyDelete
  2. Thanks, this is generally helpful.
    Still, I followed step-by-step your method in this
    dot net online training
    dot net online course

    ReplyDelete
  3. toptan iç giyim tercih etmenizin sebebi kaliteyi ucuza satın alabilmektir. Ürünler yine orjinaldir ve size sorun yaşatmaz. Yine de bilinen tekstil markalarını tercih etmelisiniz.

    Digitürk başvuru güncel adresine hoşgeldiniz. Hemen başvuru yaparsanız anında kurulum yapmaktayız.

    tutku iç giyim Türkiye'nin önde gelen iç giyim markalarından birisi olmasının yanı sıra en çok satan markalardan birisidir. Ürünleri hem çok kalitelidir hem de pamuk kullanımı daha fazladır.

    nbb sütyen hem kaliteli hem de uygun fiyatlı sütyenler üretmektedir. Sütyene ek olarak sütyen takımı ve jartiyer gibi ürünleri de mevcuttur. Özellikle Avrupa ve Orta Doğu'da çokça tercih edilmektedir.

    yeni inci sütyen kaliteyi ucuz olarak sizlere ulaştırmaktadır. Çok çeşitli sütyen varyantları mevcuttur. iç giyime damga vuran markalardan biridir ve genellikle Avrupa'da ismi sıklıkla duyulur.

    iç giyim ürünlerine her zaman dikkat etmemiz gerekmektedir. Üretimde kullanılan malzemelerin kullanım oranları, kumaşın esnekliği, çekmezlik testi gibi birçok unsuru aynı anda değerlendirerek seçim yapmalıyız.

    iç giyim bayanların erkeklere göre daha dikkatli oldukları bir alandır. Erkeklere göre daha özenli ve daha seçici davranırlar. Biliyorlar ki iç giyimde kullandıkları şeyler kafalarındaki ve ruhlarındaki özellikleri dışa vururlar.

    ReplyDelete
  4. Ah, there is a good discussion about this paragraph on this website at this place, I read all that, so now I am commenting here too... Qualified travelers can get a Turkey visa by filling an online fill visa application to Turkey can do.

    ReplyDelete
  5. I think you should make a video how to build it and share such video on youtube. If you use this site https://soclikes.com you can promote your video faster

    ReplyDelete
  6. Very informative. I advice you to post this article as a video-review on Youtube so that many interested people can see it. And you can always use the services of https://viplikes.net/buy-youtube-subscribers to increase their number.

    ReplyDelete
  7. You made some good points there. I searched on google about this topic and found that most of the people will believe your blog… Eligible citizens can apply for a kenya e-visa through a simple online application form to obtain an approved e-Visa electronically linked to their passport.

    ReplyDelete

Post a Comment

Popular posts from this blog

API Gateway: Response Aggregation with Ocelot and ASP.net Core

API Gateway in a Nutshell

Security: HTTP headers that expose web application / server vulnerabilities