API网关(Zuul)合并两个微服务响应

时间:2019-04-23 17:21:45

标签: java spring spring-boot microservices api-gateway

我有两种微服务,即用户微服务和订购微服务。

用户微服务返回用户的详细信息,而订单微服务返回用户的订单详细信息。

http://localhost:8080/microservice1/getuser

{“ id”:“ 100”,“名称”:“ test”}

http://localhost:8081/microservice2/getorders

{“ userid”:“ 100”,“ orders”:{“ orderid”:“ 5001”,“ productname”:“ mobilephone”}}

我正在使用Spring cloud Zuul作为API网关,它将请求路由到每个微服务。

http://localhost:9090/api/microservice1/getuser

http://localhost:9090/api/microservice2/getorders

现在从UI中,我需要调用两个端点

他们是通过哪种方式来合并两个微服务的响应,例如 {“ id”:“ 100”,“名称”:“ test”,“订单”:{“ orderid”:“ 5001”,“产品名称”:“手机”}}

这样,客户端只需呼叫一个端点http://localhost:9090/api/getdetail

我们如何在API网关级别实现这一目标?

1 个答案:

答案 0 :(得分:1)

Zuul不应用于汇总响应,您可以创建业务流程微服务服务,并在内部使用restTemplate根据需要获取所有响应和汇总。

让Zuul保持无状态,网关不应具有任何逻辑或有状态。