RPC模式问题

RPC pattern problems

我已经从官方文档中了解了 RPC 模式,但是示例非常简单。客户端发送带有 reply_to 和 correlation_id 属性的消息,服务器从队列中获取消息并将其重新发送给绑定相同 correlation_id.

的客户端

我的问题是:

1) 如果服务器关闭了,客户端如何得到响应

2) 如何在客户端设置 RPC 超时

3) 如果服务器坏了抛出异常,我们是否应该将这个异常发送给客户端

如有任何答复,我将不胜感激。

  1. 服务器确认 RPC 调用是一种很好的做法。 RabbitMQ docs(Python 部分)中描述的类似内容:

    Although unlikely, it is possible that the RPC server will die just after sending us the answer, but before sending an acknowledgment message for the request. If that happens, the restarted RPC server will process the request again. That's why on the client we must handle the duplicate responses gracefully, and the RPC should ideally be idempotent.

  2. 您可以为消息或整个 queue 设置 TTL:https://www.rabbitmq.com/ttl.html

  3. 视情况而定。如果请求是幂等的,则可以不确认消息 - 另一个工作人员将处理它。但它可能成为毒丸:此类请求会填满您的 RPC queue 并会导致 DoS。因此,您可以手动处理它,例如 retry_count header(如果工作人员 re-queue 消息失败,计数器递减)