我正在寻找能够在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作为回应。
答案 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)