Aggregation of data on API Gateway

ε祈祈猫儿з 提交于 2021-02-07 12:35:14

问题


I am working on microservice architecture and I want to aggregate data from two microservices.

For example, Frontend calls the API Gateway and API Gateway calls two microservices Customer and Order microservices. Customer microservice returns customer details and Order microservice returns all ordered products by customer.

This is the format returned by API Gateway after aggregation from two microservice using Ocelot or Azure API Management.

Format 1

{ 
   "Customers":[ 
      { 
         "customerId":1001,
         "customerName":"Tom"
      },
      { 
         "customerId":1002,
         "customerName":"Jerry"
      }
   ],
   "Orders":[ 
      { 
         "CustomerId":1001,
         "Orders":[ 
            { 
               "ProductId":"PRO1",
               "ProductName":"Books"
            },
            { 
               "ProductId":"PRO2",
               "ProductName":"Pens"
            }
         ]
      },
      { 
         "CustomerId":1002,
         "Orders":[ 
            { 
               "ProductId":"PRO3",
               "ProductName":"Pencils"
            },
            { 
               "ProductId":"PRO4",
               "ProductName":"Toys"
            }
         ]
      }
   ]
}

The Format that I want is format 2.

Format 2

{
   "OrderDetails":[
      {
         "customerId":1001,
         "customerName":"Tom",
         "Orders":[
            {
               "ProductId":"PRO1",
               "ProductName":"Books"
            },
            {
               "ProductId":"PRO2",
               "ProductName":"Pens"
            }
         ]
      },
      {
         "customerId":1002,
         "customerName":"Jerry",
         "Orders":[
            {
               "ProductId":"PRO3",
               "ProductName":"Pencils"
            },
            {
               "ProductId":"PRO4",
               "ProductName":"Toys"
            }
         ]
      }
   ]
}

The second format is achievable using Ocelot but the merging of data is based on the ids on Gateway and it requires some processing.

Is it a good practice to aggregate data on Gateway using business logic. If not what practices should be followed for this kind of aggregation?

It would be nice if you can provide some references for achieving this aggregation using the Azure API Management.


回答1:


This is known as API composition or Backend for Frontend. I would say it is fine to aggregate data using the API Gateway because it allows your API clients to use a simpler API interface. The actual mapping would be done in the API Gateway.

However, when you chain multiple microservices together in one Web API, the failure of one service will cause the entire Web API to fail. An alternative (and more complex) solution is to create a dedicated microservice that aggregates the datasets from the other microservices and expose a single API that shows the joined data. See - CQRS pattern.

A reference for how you might implement this in Azure is API Aggregation Using Azure API Management.



来源:https://stackoverflow.com/questions/58316573/aggregation-of-data-on-api-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!