RabbitMQ管理8

2022-04-23 19:01:13 浏览数 (1)

ruby连接RabbitMQ

ruby连接RabbitMQ

生产脚本

代码语言:javascript复制
[root@h102 ruby]# cat p.rb 
#!/usr/bin/env ruby
## encoding: utf-8

require "bunny"
conn = Bunny.new
conn.start
conn = Bunny.new(:hostname => "localhost")
conn.start
ch   = conn.create_channel
q    = ch.queue("ruby_test_q")
ch.default_exchange.publish("I am a handsome guy!", :routing_key => q.name)
puts " [x] Sent 'Done!'"
conn.close
[root@h102 ruby]# 

Tip: 要连接远程的服务器只用修改下面的代码就可以了,相关的配置可以参考 bunny的API文档

代码语言:javascript复制
conn = Bunny.new(:host => "192.168.1.20",:user => "test", :password => "test")

消费脚本

代码语言:javascript复制
[root@h102 ruby]# cat c.rb 
#!/usr/bin/env ruby
## encoding: utf-8

require "bunny"

conn = Bunny.new
conn.start

ch   = conn.create_channel
q    = ch.queue("ruby_test_q")
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
[root@h102 ruby]# 

0 人点赞