编程语言
首页 > 编程语言> > rabbitmq的笔记(三)用Python生产和消费

rabbitmq的笔记(三)用Python生产和消费

作者:互联网

消费者: #!/usr/local/bin/python2.7 # -*- coding: utf-8 -*- import pika   connection = pika.BlockingConnection(pika.ConnectionParameters('172.16.148.79', 5672, '/')) channel = connection.channel()   def callback(channel, method, properties, message): print("get resultc from queue %s" % message)   channel.basic_consume('rhj', callback,)   channel.start_consuming()   生产者: #!/usr/local/bin/python2.7 # -*- coding: utf-8 -*- import pika   connection = pika.BlockingConnection(pika.ConnectionParameters('172.16.148.78', 5672,'/')) channel = connection.channel()   channel.queue_declare(queue='rhj')   channel.basic_publish(exchange='', routing_key='rhj', body='rabbitmq003') connection.close() 先执行消费者,会一直监听队列。等执行生产者,消息马上被消费,显示在消费者那里。 这样的程序适合在新安装的rabbitmq和各种迁移升级里面把读写过程展示出来,可以体现在变更的过程中有没有消息丢失了。 如果遇到集群,要填写所有节点的读写,这一在参数的一栏这样填写。 集群的写法: Parameters = ( pika.ConnectionParameters('39.104.65.201', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1), pika.ConnectionParameters('39.99.35.129', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1), pika.ConnectionParameters('39.104.24.129', 5672,'/',credentials = credentials,connection_attempts=5, retry_delay=1)) connection = pika.BlockingConnection(Parameters)

标签:ConnectionParameters,pika,Python,5672,笔记,connection,rabbitmq,credentials,channel
来源: https://www.cnblogs.com/hodge01/p/15190498.html