API网关上的数据汇总 [英] Aggregation of data on API Gateway

查看:117
本文介绍了API网关上的数据汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究微服务架构,我想聚合来自两个微服务的数据.

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

例如,前端调用API网关,而API网关调用两个微服务客户"和订购"微服务.客户微服务返回客户详细信息,而订单微服务返回客户所订购的所有产品.

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.

这是API网关使用Ocelot或Azure API Management从两个微服务聚合后返回的格式.

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

格式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"
            }
         ]
      }
   ]
}

我想要的格式是格式2.

The Format that I want is format 2.

格式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"
            }
         ]
      }
   ]
}

使用Ocelot可以实现第二种格式,但是数据的合并基于Gateway上的ID,并且需要进行一些处理.

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

使用业务逻辑在Gateway上聚合数据是一种好习惯.如果不是,应该遵循哪种做法进行此类汇总?

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?

如果您可以提供一些参考来使用Azure API管理实现此聚合,那就太好了.

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

推荐答案

这称为API组成后端后端.我想说使用API​​网关聚合数据是可以的,因为它允许您的API客户端使用更简单的API接口.实际的映射将在API网关中完成.

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.

但是,当您在一个Web API中将多个微服务链接在一起时,一项服务的失败将导致整个Web API失败.另一种(也是更复杂的)解决方案是创建一个专用的微服务,该服务将来自其他微服务的数据集聚合在一起,并公开一个显示已连接数据的API.请参阅- CQRS 模式.

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.

关于如何在Azure中实施此操作的参考是 API使用Azure API管理进行聚合.

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

这篇关于API网关上的数据汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆