API网关中的数据聚合 - Zuul

时间:2016-08-17 10:05:18

标签: netflix-zuul spring-cloud-netflix

我正在寻找能够在API网关中提供各种数据聚合的解决方案。我使用spring cloud netflix zuul作为API网关。我使用spring boot创建了3个微服务 -

Catalog - All products 
DeviceInfo - a particular product detail 
Inventory - product stock

这是Zuul配置 -

zuul.routes.deviceInfo.path=/device/deviceInfo/**
zuul.routes.deviceInfo.url=http://localhost:9002/getDeviceInfo

zuul.routes.catalog.path=/device/all/**
zuul.routes.catalog.url=http://localhost:9001/getProductCatalog

zuul.routes.inventory.path=/device/stock/**
zuul.routes.inventory.url=http://localhost:9003/getInventory

ribbon.eureka.enabled=false

server.port=8080

在产品详细信息页面中,我需要进行两次调用 -

http://localhost:8080/device/deviceInfo/ - for product details
http://localhost:8080/device/stock/ - for stock details

有没有办法对API网关进行一次调用,这将结合上述两个调用的结果?两个调用都给出了JSON作为回应。

2 个答案:

答案 0 :(得分:1)

您可以使用聚合器 - 微服务模式,但不能使用ZUUL。让Zuul成为无国界的门户。 聚合器微服务应该有像这样的客户端代码

public String getProductTitle() {
    String response = null;
    try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
      HttpGet httpGet = new HttpGet("http://localhost:51515/information");
      try (CloseableHttpResponse httpResponse = httpClient.execute(httpGet)) {
        response = EntityUtils.toString(httpResponse.getEntity());
      }
    } catch (IOException e) {
      LOGGER.error("Exception caught.", e);
    }
    return response;
  }
}

请查看https://github.com/iluwatar/java-design-patterns/tree/master/aggregator-microservices

答案 1 :(得分:0)

我认为最初的问题是关于 here 中记录的 API 组合模式。 API 组合是微服务生态系统中常用的模式,其中来自客户端的单个调用可能会导致对许多微服务的调用并返回合并的结果。在 API Gateway 中执行此操作是一种相当普遍的做法,并且还记录在 here 中。

我自己前段时间用zuul寻找过这个解决方案,但没有找到。也许从那时起事情就发生了变化。