Giter Site home page Giter Site logo

matjazbravc / aspnetcore-api-gateway-demo Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 4.0 29 KB

POC guide how we build an simple ASP.NET Core 2.2 API Gateway application with Ocelot.

C# 83.43% HTML 16.57%
aspnetcore aspnet-web-api aspnetcorewebapi gateway-api ocelot ocelotsample gateway dotnetcore best-practices csharp

aspnetcore-api-gateway-demo's Introduction

ASP.NET Core 2.2 Web API Gateway Demo

In this POC guide, we'll build an simple API gateway application. The API will contain three sub-domains that we'll call "Company", "Department" and "Employee" contexts. The gateway will behave like a reverse proxy that routes incoming HTTP requests to the mentioned downstream services.

Introducing Ocelot

Ocelot is an open source framework used for building .NET core API gateways, the project is aimed at people using .NET Core to build applications designed with microservices or SOA architectures. It 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.

Read more about Ocelot here.

Getting Started

Test the APIs by running each of them and issuing a GET request against the following urls:

Note: You can configure a solution to run multiple APIs at the same time.

Configuring Ocelot

The first thing that we have to do is configure the ocelot.json file and add the code below.

{
  "ReRoutes": [
    {
      "DownstreamPathTemplate": "/api/company/",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51262
        }
      ],
      "UpstreamPathTemplate": "/api/company/",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/company/getbyid/{id}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51262
        }
      ],
      "UpstreamPathTemplate": "/api/company/getbyid/{id}",
      "UpstreamHttpMethod": [ "GET" ],
      "Key": "company-profile"
    },
    {
      "DownstreamPathTemplate": "/api/employee/",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51261
        }
      ],
      "UpstreamPathTemplate": "/api/employee/",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/employee/getbycompanyid/{id}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51261
        }
      ],
      "UpstreamPathTemplate": "/api/employee/getbycompanyid/{id}",
      "UpstreamHttpMethod": [ "GET" ],
      "Key": "company-employees"
    },
    {
      "DownstreamPathTemplate": "/api/department/",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51260
        }
      ],
      "UpstreamPathTemplate": "/api/department/",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/department/getbyid/{id}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51260
        }
      ],
      "UpstreamPathTemplate": "/api/department/getbyid/{id}",
      "UpstreamHttpMethod": [ "GET" ],
      "Key": "company-departments"
    }
  ],
  "Aggregates": [
    {
      "ReRouteKeys": [
        "company-employees",
        "company-profile"
      ],
      "UpstreamPathTemplate": "/api/company-employees/{id}"
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:51263"
  }
}

The code above setups up the mappings dictionary of (see below) your application gateway to the downstream services.

  • Gateway to Company API
  • Gateway to Department API
  • Gateway to Employee API

Testing the routes

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

Basic Response Aggregation

Response aggregation is a technique used for merging response from multiple downstream services into a single object. API gateways achieve this by accepting a single request from clients and issuing multiple parallelized requests to downstream services, once all downstream services responds, API gateways perform merging of data into single object and serves it to the clients. This technique results to the following benefits:

  • Cross continental communication become faster.
  • Client only needs to know a single schema.
  • Fewer HTTP transactions between client and server

Company-Profile Endpoint Mapping

    // Company Routes
    {
      "DownstreamPathTemplate": "/api/company/",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51262
        }
      ],
      "UpstreamPathTemplate": "/api/company/",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/company/getbyid/{id}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51262
        }
      ],
      "UpstreamPathTemplate": "/api/company/getbyid/{id}",
      "UpstreamHttpMethod": [ "GET" ],
      "Key": "company-profile"
    },

Company-Employees Endpoint Mapping

    // Employee Routes
    {
      "DownstreamPathTemplate": "/api/employee/",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51261
        }
      ],
      "UpstreamPathTemplate": "/api/employee/",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/employee/getbycompanyid/{id}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 51261
        }
      ],
      "UpstreamPathTemplate": "/api/employee/getbycompanyid/{id}",
      "UpstreamHttpMethod": [ "GET" ],
      "Key": "company-employees"
    },

Aggregated Route Configuration

    "Aggregates": [
    {
      "ReRouteKeys": [
        "company-employees",
        "company-profile"
      ],
      "UpstreamPathTemplate": "/api/company-employees/{id}"
    }
  ],

Test routes

You can test the output of the individual endpoint re-routes and aggregated route using the links below:

Company-Profile http://localhost:51263/api/company/getbyid/1

Company-Employees http://localhost:51263/api/employee/getbycompanyid/1

Company-Profile + Company-Employees http://localhost:51263/api/company-employees/1

Aggregated Response

{
  "company-profile": {
    "companyId": 1,
    "name": "Company One",
    "employees": []
  },
  "company-employees": [
    {
      "employeeId": 1,
      "firstName": "John",
      "lastName": "Whyne",
      "birthDate": "1991-08-07T00:00:00",
      "companyId": 1
    },
    {
      "employeeId": 2,
      "firstName": "Mathias",
      "lastName": "Gernold",
      "birthDate": "1997-10-12T00:00:00",
      "companyId": 1
    },
    {
      "employeeId": 3,
      "firstName": "Julia",
      "lastName": "Reynolds",
      "birthDate": "1955-12-16T00:00:00",
      "companyId": 1
    }
  ]
}

Ocelot offers a simple yet powerful mechanism to aggregate multiple downstream endpoints. This feature eases the life of front-end developers that previously have to deal with multiple endpoints.

Prerequisites:

Tags & Technologies

Enjoy!

Licence

Licenced under MIT. Contact me on LinkedIn.

aspnetcore-api-gateway-demo's People

Contributors

matjazbravc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.