使用Python 、 go 语言测试rabbitmq的工作机制

1:在haproxy 和 rabbitmq上安装Python、python2-pip,默认是Python2
yum install -y python python2-pip
 
2:在haproxy 和 rabbitmq上使用pip安装pika库
pip install pika==0.9.8
 
3:在haproxy上写一个Python发送消息代码
vi send.py
-------
#!/usr/bin/env python
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 Uplooking!')
print "Sent 'Hello Uplooking!'"
connection.close()
-------
 
4:在rabbitmq上写一个Python接受消息代码
vi receive.py
-------
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
print ' waiting for messages! press CTRL+C to halt'
def callback(ch, method, properties, body):
print "Received %r" % (body,)
channel.basic_consume(callback,
queue='hello',
no_ack=True)
channel.start_consuming()
-------
 
5:先运行receive.py,在运行send.py
python receive.py
python send.py
原文地址:https://www.cnblogs.com/liu1026/p/7688785.html