瘦HTTP服务器下的Ruby AMQP
我正在运行一个简单的精简服务器,它将一些消息发布到不同的队列,代码如下所示:瘦HTTP服务器下的Ruby AMQP,ruby,rabbitmq,amqp,thin,Ruby,Rabbitmq,Amqp,Thin,我正在运行一个简单的精简服务器,它将一些消息发布到不同的队列,代码如下所示: require "rubygems" require "thin" require "amqp" require 'msgpack' app = Proc.new do |env| params = Rack::Request.new(env).params command = params['command'].strip rescue "no command" number = params['nu
require "rubygems"
require "thin"
require "amqp"
require 'msgpack'
app = Proc.new do |env|
params = Rack::Request.new(env).params
command = params['command'].strip rescue "no command"
number = params['number'].strip rescue "no number"
p command
p number
AMQP.start do
if command =~ /\A(create|c|r|register)\z/i
MQ.queue("create").publish(number)
elsif m = (/\A(Answer|a)\s?(\d+|\d+-\d+)\z/i.match(command))
MQ.queue("answers").publish({:number => number,:answer => "answer" }.to_msgpack )
end
end
[200, {'Content-Type' => "text/plain"} , command ]
end
Rack::Handler::Thin.run(app, :Port => 4001)
现在,当我运行服务器并执行以下操作时
我总是得到重复的输出,比如:
“没有命令”
“没有号码”
“没有命令”
“没有号码”
第一件事是为什么我会收到重复的请求?这与浏览器有关吗?因为当我使用curl时,我没有相同的行为,第二件事为什么我不能得到参数
任何关于此类服务器最佳实现的提示都将不胜感激
提前感谢。第二个请求来自浏览器,正在查找favicon.ico。您可以通过在处理程序中添加以下代码来检查请求:
params = Rack::Request.new(env).params
p env # add this line to see the request in your console window
或者,您可以使用:
然后在命令行上运行ruby the_server.rb启动http服务器。感谢您的快速响应。你似乎是对的,有没有办法消除这种行为?对于p env的输出,是否有使用Thin和AMQP的最佳优化方法的例子:只是一个旁注:您实际使用的是
rack
,一个用于在不同web服务器上运行web应用程序的抽象层/中间件,Thin
是运行rack应用程序的实际web服务器。在谷歌上搜索“rack amqp”可能会更成功。此外,您还应该看看高级AMQP宝石之一,如Bunny:。希望有帮助。
require "rubygems"
require "amqp"
require "msgpack"
require "sinatra"
get '/:command/:number' do
command = params['command'].strip rescue "no command"
number = params['number'].strip rescue "no number"
p command
p number
AMQP.start do
if command =~ /\A(create|c|r|register)\z/i
MQ.queue("create").publish(number)
elsif m = (/\A(Answer|a)\s?(\d+|\d+-\d+)\z/i.match(command))
MQ.queue("answers").publish({:number => number,:answer => "answer" }.to_msgpack )
nd
end
return command
end