Rabbitmq消息发送接收Demo

Rabbitmq生产者Demo

# encoding: utf-8
import pika, sys

rabbitmq_user = "michael"
rabbitmq_passwd = "michael"
rabbitmq_host = "192.168.2.188"

'''创建连接'''
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_passwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(
    host=rabbitmq_host, credentials=credentials))
channel = connection.channel()



'''遍历消息以及routingkey'''
channel.basic_publish(exchange='test',  #设置交换机
                          routing_key="test", #设置routingkey
                          body=str(sys.argv[1]), #设置发送消息
                          properties=pika.BasicProperties(
                              delivery_mode=2,  # 持久化消息
                          ))

'''打印发送消息'''
print "Sent Message:%s" % (sys.argv[1])

'''关闭连接'''
connection.close()

Rabbitmq消费者Demo

# encoding: utf-8
import pika

rabbitmq_user = "michael"
rabbitmq_passwd = "michael"
rabbitmq_host = "192.168.2.188"

'''创建连接'''
credentials = pika.PlainCredentials(rabbitmq_user, rabbitmq_passwd)
connection = pika.BlockingConnection(pika.ConnectionParameters(
    host=rabbitmq_host, credentials=credentials))
channel = connection.channel()

print ' [*] Waiting for messages. To exit press CTRL+C'

'''回调和ack'''
def callback(ch, method, properties, body):
    print "%s - %s - %s - %s" % (ch, method, properties, body)
    ch.basic_ack(delivery_tag=method.delivery_tag)

'''设置qos,防止大量unack'''
channel.basic_qos(prefetch_count=1)

channel.basic_consume(callback,
                      queue='test')
'''启动消费者'''
channel.start_consuming()

你可能感兴趣的:(Rabbitmq)