API Gateway 上的数据聚合

bip*_*pan 12 azure-api-management microservices aws-api-gateway api-gateway ocelot

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

例如,Frontend 调用 API Gateway,API Gateway 调用两个微服务 Customer 和 Order 微服务。客户微服务返回客户详细信息,订单微服务返回客户所有订购的产品。

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

格式 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"
            }
         ]
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

我想要的格式是格式 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"
            }
         ]
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

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

使用业务逻辑在网关上聚合数据是否是一个好习惯。如果不是,这种聚合应该遵循什么做法?

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

Set*_*eth 8

这称为前端的API 组合后端。我会说使用 API Gateway 聚合数据很好,因为它允许您的 API 客户端使用更简单的 API 接口。实际映射将在 API 网关中完成。

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

关于如何在 Azure 中实现这一点的参考是使用 Azure API 管理的 API 聚合