0

我有两个应用程序 sap1 和 sap2,我一直在使用 rabbitMQ(bunny gem)进行消息传递,所以就像从 sap1 开始一样,我将创建消息并发布

sap1(发布者应用)

宝石文件

ruby 1.9.3
gem 'bunny', '~> 1.7', '>= 1.7.1'

发布.rb

def publish
    # Start a communication session with RabbitMQ
    connection = Bunny.new(:host => "123123", :vhost => "1231", :user => "123", :password => "Z7tN")
    connection.start

    # open a channel
    channel = connection.create_channel

    # declare a queue
    queue  = channel.queue("order-test", :durable => true)

    # publish a message to the default exchange which then gets routed to this queue
    values = { mac_id: 14123 }
    queue.publish(values.to_json)
    connection.close
end

sap2(消费者应用程序)

宝石文件

ruby 2
gem 'bunny'
gem 'sneakers'

配置/初始化程序/sneakers.rb

require 'sneakers'
Sneakers.configure :connection => Bunny.new(:host => "123123", :vhost => "1231", :user => "123", :password => "Z7tN"), :timeout_job_after => 360

耙文件

require 'sneakers/tasks'

应用程序/工人/mac_worker.rb

class MacWorker
  include Sneakers::Worker
  from_queue "order-test"

  def work(params)
     # Calling the function
  end
end

并在控制台中听这样做

 bundle exec rake sneakers:run

这是可行的,有时我也会遇到连接问题,在消费者应用程序中我不想使用运动鞋 gem 所以我想用兔子从队列中读取消息并将其放入 sidekiq,以及如何在 sidekiq 中自动收听?

4

0 回答 0