API网关中的数据聚合 - Zuul

Sau*_*abh 7 netflix-zuul spring-cloud-netflix

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

Catalog - All products 
DeviceInfo - a particular product detail 
Inventory - product stock
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

在产品详细信息页面中,我需要拨两个电话 -

http://localhost:8080/device/deviceInfo/ - for product details
http://localhost:8080/device/stock/ - for stock details
Run Code Online (Sandbox Code Playgroud)

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

Arm*_*yan 2

您可以使用聚合器微服务模式,但不能在 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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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