我有两个应用程序S-APP1和S-APP2,刚刚开始使用 rabbitMQ 和客户端 bunny gem,所以当我点击(这可能是一分钟内点击更多)来自S-APP1的按钮时,我会调用publisher.rb将消息发布到队列中并从S-APP2 - consumer.rb中读取并放入 sidekiq 进行处理。
今天我刚刚看到这个链接 https://www.cloudamqp.com/blog/2018-01-19-part4-rabbitmq-13-common-errors.html 并阅读了这篇文章,对来自该链接“重用连接”的这些行有点困惑”和“不要使用太多的连接或通道”,我做对了这件事吗?我正在使用 heroku 来运行它并使用 CloudAMQP 插件。
发布者.rb (S-APP1)
def publish
connection = Bunny.new( :host => ENV["AMQP_HOST"],
:vhost => ENV["AMQP_VHOST"],
:user => ENV["AMQP_USER"],
:password => ENV["AMQP_PASSWORD"])
connection.start # start a communication session with the amqp server
channel = connection.channel()
channel.queue('order-queue', durable: true)
exchange = channel.default_exchange
message = { order_id: '1542' }
# publish a message to the queue
exchange.publish(message.to_json, routing_key: 'order-queue')
puts " [x] Sent #{message}"
connection.close()
end
这是我的消费者部分
消费者.rb (S-APP2)
connection = Bunny.new( :host => ENV["AMQP_HOST"],
:vhost => ENV["AMQP_VHOST"],
:user => ENV["AMQP_USER"],
:password => ENV["AMQP_PASSWORD"])
connection.start # start a communication session with the amqp server
channel = connection.channel()
queue = channel.queue('order-queue', durable: true)
puts ' [*] Waiting for messages. To exit press CTRL+C'
queue.subscribe(manual_ack: true, block: true) do |delivery_info, properties, payload|
puts " [x] Received #{payload}"
puts " [x] Done"
channel.ack(delivery_info.delivery_tag, false)
# Call worker to do refresh
callSidekiqWorker.perform_async(payload)
end