相关性和RPC模型中的临时队列-AMQP [英] correlationId and temporary queues in RPC model - AMQP

查看:103
本文介绍了相关性和RPC模型中的临时队列-AMQP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RabbitMQ在AMQP中阅读 RPC模型.本教程将创建一个临时队列以及correlationId.临时队列是唯一的,那么为什么我们需要correlationId?我是JMS的人,在JMS中,我们通过两种方式进行请求/响应:

I was reading RPC-Model in AMQP with RabbitMQ. The tutorial creates a temporary queue and also correlationId. Temporary queues are unique, so why should we need correlationId? I'm a JMS guy, in JMS we do request/response in two ways:

  1. 为每个请求/响应创建临时队列

  1. create temporary queue for each request/response

创建一个响应队列,并使用correlationId和消息选择器.

create one response queue and use correlationId and message selector.

有人可以解释为什么在AMQP RPC模型中我们既需要临时队列又需要correlationId吗?看来AMQP没有类似消息选择器的内容.我说的对吗?

can someone explain why do we need both temporary queue and correlationId in AMQP RPC model? It seems AMQP does not have something like message selector. Am I right?

推荐答案

正确的临时队列对于发出RPC请求的客户端是唯一的.我们可以创建RPC客户端,使其对其发出的每个唯一请求都具有唯一队列,但这效率不高-请参阅CorrelationId

Correct, temporary queues are unique to the client making the RPC request. We could create the RPC client to have a unique queue for each unique request it makes, but that would be inefficient - see the first paragraph of CorrelationId here:

在上述方法中,我们建议为每个RPC请求创建一个回调队列.效率很低,但是幸运的是有更好的方法-让我们为每个客户端创建一个回调队列.

In the method presented above we suggest creating a callback queue for every RPC request. That's pretty inefficient, but fortunately there is a better way - let's create a single callback queue per client.

因此,一种更好的方法是让RPC客户端重新获得响应,并使用correlationId将RPC客户端发出的请求与RPC服务器发回的结果相匹配.

So a better way is to have one queue which the RPC client gets response back on and uses the correlationId to match the request that the RPC client makes to the result that the RPC server sends back.

...在该队列中已收到响应,尚不清楚响应属于哪个请求.那就是使用correlation_id属性的时候.我们将为每个请求将其设置为唯一值.稍后,当我们在回调队列中收到消息时,我们将查看该属性,并基于此属性将响应与请求进行匹配.如果看到未知的related_id值,则可以安全地丢弃该消息-它不属于我们的请求.

...having received a response in that queue it's not clear to which request the response belongs. That's when the correlation_id property is used. We're going to set it to a unique value for every request. Later, when we receive a message in the callback queue we'll look at this property, and based on that we'll be able to match a response with a request. If we see an unknown correlation_id value, we may safely discard the message - it doesn't belong to our requests.

因此请参考 RPC教程的摘要"部分:

So referencing the Summary section of the RPC tutorial:

  • 客户端启动时会创建一个排他和唯一的队列
  • 当它发送RPC请求时,它会设置reply_to为队列名(这样服务器就知道向哪个队列发送响应),并且还设置了correlationIdId,这是每个RPC请求的唯一值
  • 请求已发送到RPC队列
  • RPC工作程序(或服务器)接收请求处理,然后使用reply_to值将响应发送回客户端,还设置了correlationIdId
  • RPC客户端正在等待响应,当它接收到响应时,它将使用correlationId来匹配带有请求的响应
  • when a client starts it creates an exclusive and unique queue
  • when it sends an RPC request it sets the reply_to which is the queue name (so the server knows which queue to send a response to) and also sets a correlationId which is a unique val for each RPC request
  • the request is sent to the RPC queue
  • the RPC worker (or server) receives the request processes it then using the reply_to value sends the response back to the client, it also sets the correlationId
  • the RPC client waits for a response and when it receives one uses the correlationId to MATCH the response with the request

这篇关于相关性和RPC模型中的临时队列-AMQP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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