Mike Liu c5a4068e84
docs: Translation for zh (#1805)
* add state and callback pattern

* add command and template-method pattern

* add iterator pattern

* add bridege and DI pattern

* fix issue #1600

* add converter,proxy,visitor pattern

* add caching,composite,delegation,dirty-flag,interpreter patterns

* add dao and producer-consumer

* add dto and provate class data pattern

* fix #1646 png path problems

* fix #1646 composite png path case problem

* add abstract document pattern and version-number pattern

* add ambassador pattern

* add acyclic-visitor and api-gateway pattern

* add abstract-factory pattern

* add active-object pattern

* add aggregator-microservices and arrange-act-assert pattern

* update async-method-invocation pattern

* add balking and business-delegate pattern

* add bytecode and circuit-break pattern

* update arrange/act/assert pattern problems

* add csch pattern

* add language code, correct pic path

* #1805 update permalink

Co-authored-by: Subhrodip Mohanta <subhrodipmohanta@gmail.com>
Co-authored-by: Mike <admin@xiaod.info>
Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
2021-08-01 20:25:54 +05:30
..
2021-08-01 20:25:54 +05:30

layout, title, folder, permalink, categories, language, tags
layout title folder permalink categories language tags
pattern Aggregator Microservices aggregator-microservices /patterns/aggregator-microservices/ Architectural zh
Cloud distributed
Decoupling
Microservices

意图

用户对聚合器服务进行一次调用,然后聚合器将调用每个相关的微服务。

解释

真实世界例子

我们的网络市场需要有关产品及其当前库存的信息。 它调用聚合服务,聚合服务依次调用产品信息微服务和产品库存微服务,返回组合信息。

通俗地说

聚合器微服务从各种微服务中收集数据,并返回一个聚合数据以进行处理。

Stack Overflow上说

聚合器微服务调用多个服务以实现应用程序所需的功能。

程序示例

让我们从数据模型开始。 这是我们的产品

public class Product {
  private String title;
  private int productInventories;
  // getters and setters ->
  ...
}

接下来,我们将介绍我们的聚合器微服务。 它包含用于调用相应微服务的客户端ProductInformationClient ProductInventoryClient

@RestController
public class Aggregator {

  @Resource
  private ProductInformationClient informationClient;

  @Resource
  private ProductInventoryClient inventoryClient;

  @RequestMapping(path = "/product", method = RequestMethod.GET)
  public Product getProduct() {

    var product = new Product();
    var productTitle = informationClient.getProductTitle();
    var productInventory = inventoryClient.getProductInventories();

    //Fallback to error message
    product.setTitle(requireNonNullElse(productTitle, "Error: Fetching Product Title Failed"));

    //Fallback to default error inventory
    product.setProductInventories(requireNonNullElse(productInventory, -1));

    return product;
  }
}

这是产品信息微服务的精华实现。 库存微服务类似,它只返回库存计数。

@RestController
public class InformationController {
  @RequestMapping(value = "/information", method = RequestMethod.GET)
  public String getProductTitle() {
    return "The Product Title.";
  }
}

Now calling our Aggregator REST API returns the product information.

现在调用我们的聚合器 REST API会返回产品信息。

curl http://localhost:50004/product
{"title":"The Product Title.","productInventories":5}

类图

alt text

适用性

当需要各种微服务的统一API时无论客户端设备如何都可以使用Aggregator微服务模式。

鸣谢