RabbitMQ中的常见错误

时间:2018-09-13 10:44:27

标签: ruby-on-rails ruby-on-rails-3 heroku rabbitmq bunny

我有两个应用程序 S-APP1 S-APP2 ,并且刚开始使用带有客户端Bunny gem的RabbitMQ,所以当我单击时(这可能是更多单击一分钟),然后按 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插件。

publisher.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

这是我的消费部分

consumer.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

0 个答案:

没有答案