Rabbit 持久话模式
-
生产者模式
- 在我们创建队列的时候,把队列变成可持久化的队列
- 在我们插入数据的时候,把数据变成持久话数据
from pika import BlockingConnection, ConnectionParameters, BasicProperties connection = BlockingConnection(ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='hello2', durable=True) # durable:把数据变成可持久话 channel.basic_publish( exchange='', body='Hello World!', routing_key='hello2', # 队列名称 properties=BasicProperties( delivery_mode=2, # 让里面的消息变得可持久话 ) ) connection.close()
-
消费者模式
from pika import BlockingConnection, ConnectionParameters connection = BlockingConnection(ConnectionParameters(host='localhost')) channel = connection.channel() channel.queue_declare(queue='hello2', durable=True) def callback(ch, method, properties, body): print('消息的内容是{}'.format(body)) channel.basic_consume( queue='hello2', on_message_callback=callback, auto_ack=True ) channel.start_consuming()