根据https://dzone.com/articles/building-microservices-using一篇文章,有一条声明:
API网关负责请求路由、组合和协议转换。来自客户端的所有请求首先通过API网关。然后,它将请求路由到适当的微服务。 API网关通常通过调用多个微服务并聚合结果来处理请求。
我想知道,基于Zuul的例子,API网关是如何实现这一点的?
假设我们有两个微服务,一个可以检索所有可用的产品名称,另一个可以返回产品的描述。在单块体系结构中,我们只有一个请求来获取所有需要的数据。在微服务架构中,API网关应该组合响应(来自两个微服务)并返回一个响应。
如何实现这一趣味性?对此是否有指导方针或文章?
发布于 2019-11-04 03:02:42
并非所有API网关都支持聚合。这是使用nginx为单个客户端调用聚合两个服务响应的一个例子。
它的副作用是在API网关层引入耦合。
另一种可能的解决方案是使用聚合微服务。此服务的主要职责是提供客户端api。它可以对其他微服务进行多次调用,以便为下面的client.See示例制定响应
@RequestMapping(path = "/product", method = RequestMethod.GET)
public Product getProduct() {
var product = new Product();
String productTitle = informationClient.getProductTitle();
Integer productInventory = inventoryClient.getProductInventories();
if (productTitle != null) {
product.setTitle(productTitle);
} else {
product.setTitle("Error: Fetching Product Title Failed"); //Fallback to error message
}
if (productInventory != null) {
product.setProductInventories(productInventory);
} else {
product.setProductInventories(-1); //Fallback to default error inventory
}
return product;
}完整示例这里
https://stackoverflow.com/questions/58684080
复制相似问题