rabbitmq中python和ruby通信

      前面一章已经介绍了rabbitmq在python中传递消息,那么作为比较强大的rabbitmq中间件,他如何实现在ruby上传递消息以及和python中间传递消息呢?

      1.安装rabbtmq的服务器,(前一篇已经介绍过了)

      2. gem install bunny   --安装bunny的gem包,这样就可以在客户端用ruby访问了

      我们写一个send.rb

   require "bunny"     --引入bunny
   conn = Bunny.new(:hostname => "localhost")
   conn.start                        --链接服务器
   ch = conn.create_channel          --创建通道
   
   q    = ch.queue("hello")         --创建队列hello
   ch.default_exchange.publish("Hello World!", :routing_key => q.name)
   puts " [x] Sent 'Hello World!'"
conn.close

然后在写一个 receive.rb
   require "bunny"
   conn = Bunny.new
   conn.start
   ch   = conn.create_channel
   q    = ch.queue("hello")
   puts " [*] Waiting for messages in #{q.name}. To exit press CTRL+C"
   q.subscribe(:block => true) do |delivery_info, properties, body|
      puts " [x] Received #{body}"

      # cancel the consumer to exit
      delivery_info.consumer.cancel
   end
 好了,运行下就好了!

        
        
$ ruby send.rb
" [x] Sent 'Hello World!'"

$ ruby receive.rb
 
”[x] Received Hello World!"

这是ruby和ruby之间通信,那么我们在加入python的代码

send.py

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
        host='localhost'))
channel = connection.channel()

channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print " [x] Sent 'Hello World!'"
connection.close()
我们在运行下
python send.py
" [x] Sent 'Hello World!'"
ruby receive.rb
" [x] Receive 'Hello World!'"









 







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值