我有一个Rails项目和两个在后台运行的ruby mini守护进程。他们之间最好的沟通方式是什么?
应该可以进行如下通信: Rails ->进程1 ->进程2 -> Rails
有些请求是同步的,另一些则是异步的。
队列(类似于AMQ,或基于自定义Redis )或RPC HTTP调用?
发布于 2011-06-04 04:05:21
我通过RabbitMq + bunny实现了一个系统。
更新:
在阅读了http://blog.brightbox.co.uk/posts/queues-and-callbacks之后,我决定尝试一下RabbitMQ。有两个gem amqp (async,基于eventmachine )或bunny (同步)。amqp很棒,但是如果你在passenger上使用Rails,它可能会做一些奇怪的事情。
系统是这样工作的,守护进程监听队列中的消息:
# The incoming data should be a JSON encoded hash that looks like:
# { "method" => method_to_call, "opts" => [ Array of opts for method ],
# "output" => "a queue where to send the result (optional)" }
# If output is specified it will publish the JSON encoded response there.
def listen_on(queue_name, class)
BUNNY.start
bunny = BUNNY.queue(queue_name)
bunny.subscribe do |msg|
msg = JSON.parse(msg[:payload])
result = class.new.send(msg["method"], *msg["opts"])
if msg["output"]
BUNNY.queue(msg["output"]).publish(result.to_json)
end
end因此,一旦收到消息,它就会调用来自类的方法。需要注意的一件事是,在守护进程中使用bunny作为Rails和amqp是最理想的。但我喜欢使用一个gem pe服务。
发布于 2011-05-24 04:14:06
还要检查一下DRb。
https://stackoverflow.com/questions/6102275
复制相似问题