我正在尝试使用rabbitmq构建RPC。
根据通过rabbitmq http://www.rabbitmq.com/tutorials/tutorial-six-ruby.html构建RPC的教程,我们可以为每个客户端使用一个回复队列,并使用correlation_id来映射响应和请求。我对如何使用correlation_id感到困惑?
这是我正在运行的问题,我想从一个客户端同步创建两个rpc调用,使用具有两个不同相关ID的相同回复队列。但是我不知道这是否是正确的用例,因为从我在教程中读到的内容看来,每个客户端都在按顺序进行rpc调用。 (在这种情况下,为什么我们在这里需要correlation_id会变得更加混乱。)
这是代码示例(rpc_server.rb将与教程相同),我想要实现的目标。希望它能让我的问题更清楚。
下面的代码块不起作用,因为当我们在thr1中设置了correlation_id时,corre_id正在被thr2覆盖。
我想知道是否有修改它,让它工作?如果我们试图将@ reply_queue.subscribe块从初始化中移出并传入不同的call_id,它仍然无法正常工作,因为在等待thr1完成时,@ reply-queue似乎将被锁定。
如果问题未清除,请告诉我,请事先感谢您的回复。
#!/usr/bin/env ruby
# encoding: utf-8
require "bunny"
require "thread"
conn = Bunny.new(:automatically_recover => false)
conn.start
ch = conn.create_channel
class FibonacciClient
attr_reader :reply_queue
attr_accessor :response, :call_id
attr_reader :lock, :condition
def initialize(ch, server_queue)
@ch = ch
@x = ch.default_exchange
@server_queue = server_queue
@reply_queue = ch.queue("", :exclusive => true)
@lock = Mutex.new
@condition = ConditionVariable.new
that = self
@reply_queue.subscribe do |delivery_info, properties, payload|
if properties[:correlation_id] == that.call_id
that.response = payload.to_i
that.lock.synchronize{that.condition.signal}
end
end
end
def call(n)
self.call_id = self.generate_uuid
@x.publish(n.to_s,
:routing_key => @server_queue,
:correlation_id => call_id,
:reply_to => @reply_queue.name)
lock.synchronize{condition.wait(lock)}
response
end
protected
def generate_uuid
# very naive but good enough for code
# examples
"#{rand}#{rand}#{rand}"
end
end
client = FibonacciClient.new(ch, "rpc_queue")
thr1 = Thread.new{
response1 = client.call(30)
puts response1
}
thr2 = Thread.new{
response2 = client.call(40)
puts response2
}
ch.close
conn.close
答案 0 :(得分:0)
这里也有同样的问题。
'use strict';
const config = require('./config')
const amqp = require('amqplib').connect('amqp://' + config.username + ':' + config.password + '@ali3')
const co = require('co')
const args = process.argv.slice(2)
if (args.length == 0) {
console.log("Usage: rpc_client.js num");
process.exit(1)
}
function generateUuid() {
return Math.random().toString() +
Math.random().toString() +
Math.random().toString()
}
function* init(){
let conn = yield amqp
let ch = yield conn.createChannel()
let cbQueue = yield ch.assertQueue('', {exclusive: true})
return {"conn": conn, "channel": ch, "cbQueue": cbQueue}
}
function* sender(initConfig, msg, resHandler) {
try {
let ch = initConfig.channel
let conn = initConfig.conn
let cbQueue = initConfig.cbQueue
const corr = generateUuid()
console.log(' [x] [%s] Requesting fib(%d)',corr, msg)
ch.consume(cbQueue.queue, (resultMsg) => {
resHandler(resultMsg, corr, conn)
})
ch.sendToQueue('rpc_queue', new Buffer(msg.toString()), {"correlationId": corr, "replyTo": cbQueue.queue})
}
catch (ex) {
console.warn("ex:", ex)
}
}
function responseHandler(res, corr, conn) {
console.log("corr: %s - %s", corr, res.content.toString());//important debug info
if (res.properties.correlationId == corr)
{
console.log(' [.] Got %s', res.content.toString());
//setTimeout( () => {
// conn.close()
// process.exit(0)
//}, 500);
}
};
function onerror(err) {
console.error(err.stack);
}
co(function*() {
let num = parseInt(args[0])
let initConfig = yield init();
//let initConfig2 = yield init();
yield [
sender(initConfig, num.toString(), responseHandler),
sender(initConfig, (num+3).toString(), responseHandler)
]
}, onerror)
dengwei @ RMBAP:〜/ projects / github / rabbitmq-demo / rpc $ node rpc_client_gen.js 5 [x] [0.64227353665046390.20330130192451180.5467283953912556]要求fib(5) [x] [0.461023105075582860.22911424539051950.9930733679793775]要求fib(8) corr:0.64227353665046390.20330130192451180.5467283953912556 - 5 [。]得到5 corr:0.461023105075582860.22911424539051950.9930733679793775 - 21 [。]得到21
从重要的调试中我们可以看到: 消息已从服务器发送到回调队列,并由客户端消耗,检查uuid是否设置为第一条消息。所以:
if (res.properties.correlationId == corr)
这行代码忽略了结果。
似乎我们只能在发送单个消息之前每次初始化。