python rabbitmq是什么
1、当信息在一个队列中变成死信时,可以重新发送到DLX,绑定DLX的队列称为rabbitmq。
也叫死信队列。
2、常用于订单超时自动取消,会议预订提前提醒等等。
实例
代码语言:javascript复制import pika
import json
import time
credentials = pika.PlainCredentials('admin', 'admin') # mq用户名和密码
# 虚拟队列需要指定参数 virtual_host,如果是默认的可以不填。
connection = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1', port=5672, credentials=credentials))
channel = connection.channel()
# 声明消息队列,消息将在这个队列传递,如不存在,则创建
queue_name = "delay_queue_a"
exchange = 'delay_exchange_a'
routing_key = 'delay_routing_key_a'
dead_letter_exchange = 'dead_exchange_a' # 'amq.direct'#'dead_exchange_a'
dead_letter_routing_key = 'dead_letter_routing_key_a' # 'dead_queue_a'#'dead_letter_routing_key_a'
arguments = {
"x-message-ttl": 5000,
'x-dead-letter-exchange': dead_letter_exchange,
'x-dead-letter-routing-key': dead_letter_routing_key
}
channel.confirm_delivery()
channel.exchange_declare(exchange=exchange, durable=True, exchange_type='direct')
result = channel.queue_declare(queue=queue_name, durable=False, arguments=arguments)
channel.queue_bind(exchange=exchange, queue=queue_name, routing_key=routing_key)
for i in range(10):
message = json.dumps({'OrderId': i})
# 向队列插入数值 routing_key是队列名
channel.basic_publish(exchange=exchange, routing_key=routing_key, body=message,
properties=pika.BasicProperties(delivery_mode=2))
print(message)
time.sleep(1.5)
connection.close()
以上就是python rabbitmq的介绍,希望对大家有所帮助。更多Python学习指路:python基础教程
本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。