# 发布者classPublisher# In order to publish message we need a exchange name.# Note that RabbitMQ does not care about the payload -# we will be using JSON-encoded stringsdefself.publish(exchange,message={})# grab the fanout exchangex=channel.fanout("blog.#{exchange}")# and simply publish messagex.publish(message.to_json)enddefself.channel@channel||=connection.create_channelend# We are using default settings here# The `Bunny.new(...)` is a place to# put any specific RabbitMQ settings# like host or portdefself.connection@connection||=Bunny.new.tapdo|c|c.startendendend# 消费者# dashboard/app/workers/posts_worker.rbclassPostsWorkerincludeSneakers::Worker# This worker will connect to "dashboard.posts" queue# env is set to nil since by default the actuall queue name would be# "dashboard.posts_development"from_queue"dashboard.posts",env:nil# work method receives message payload in raw format# in our case it is JSON encoded string# which we can pass to RecentPosts service without# changesdefwork(raw_post)RecentPosts.push(raw_post)ack!# we need to let queue know that message was receivedendend然后还有Exchangequeue绑定啊之类的