问题描述
我正在使用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:
-
为每个请求/响应创建临时队列
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客户端重新获得响应,并使用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.
因此请参考 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!