RabbitMQ使用优先级队列

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import pika

credentials = pika.PlainCredentials("guest", "guest")
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', credentials=credentials))
channel = connection.channel()

channel.queue_declare(queue='test', arguments={"x-max-priority": 10})

channel.basic_publish(
properties=pika.BasicProperties(priority=5),
exchange='',
routing_key='test',
body='test'.encode('utf-8')
)

connection.close()

channel.queue_declare(queue='test', arguments={"x-max-priority": 10}) 在声明队列的时候声明最大优先级.

在发布消息的时候通过properties=pika.BasicProperties(priority=5) 针对不同的消息来设置不同的优先级.

消费的时候,不同优先级,优先级高的先被消费,相同优先级的,会按照FIFO的顺序.


RabbitMQ使用优先级队列
https://kingjem.github.io/2025/07/23/RabbitMQ使用优先级队列/
作者
Ruhai
发布于
2025年7月23日
许可协议