微服务之间的通信——请求数据 [英] Communication between microservices - request data

查看:57
本文介绍了微服务之间的通信——请求数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理微服务之间的通信.

I am dealing with communication between microservices.

例如(虚构的例子,只是为了说明):

  • 微服务 A - 存储用户(getUser 等)
  • 微服务 B - 商店订单(createOrder 等)

现在,如果我想从客户端应用程序添加新订单,我需要知道用户地址.所以请求会是这样的:

Now if I want to add new Order from the Client app, I need to know user address. So the request would be like this:

Client -> Microservice B (createOrder for userId 5) -> Microservice A (getUser with id 5)

微服务 B 将使用来自用户微服务的详细信息(地址)创建订单.

The microservice B will create order with details (address) from the User Microservice.

要解决的问题:如何有效地处理微服务 A 和微服务 B 之间的通信,因为我们必须等到响应返回?

PROBLEM TO SOLVE: How effectively deal with communication between microservice A and microservice B, as we have to wait until the response come back?

选项:

我不知道什么对性能会更好.通过 RabbitMQ 或 RestAPI 调用更快吗?微服务架构的最佳解决方案是什么?

I don't know what will be better for the performance. Is call faster via RabbitMQ, or RestAPI? What is the best solution for microservice architecture?

推荐答案

在您的情况下,使用直接 REST 调用应该没问题.

In your case using direct REST calls should be fine.

选项 1 使用 Rest API :

当您需要同步通信时.比如说你的情况.这个选项很合适.

When you need synchronous communication. For example, your case. This option is suitable.

选项 2 使用 AMQP:

当您需要异步通信时.例如,当您的订单服务创建订单时,您可能希望通知产品服务减少产品数量.或者,您可能希望通知用户服务已成功下订单.

When you need asynchronous communication. For example when your order service creates order you may want to notify product service to reduce the product quantity. Or you may want to nofity user service that order for user is successfully placed.

我强烈建议您查看http://microservices.io/patterns/index.html

这篇关于微服务之间的通信——请求数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆