RabbitMQ 消息公平分发

本文介绍如何通过配置prefetch_count参数实现RabbitMQ消息的公平分发,避免消费者负载不均的问题。通过代码示例展示如何在多个消费者中均衡分配任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概念:
如果Rabbit只管按顺序把消息发到各个消费者身上,不考虑消费者负载的话,很可能出现,一个机器配置不高的消费者那里堆积了很多消息处理不完,同时配置高的消费者却一直很轻松。为解决此问题,可以在各个消费者端,配置perfetch=1,意思就是告诉RabbitMQ在我这个消费者当前消息还没处理完的时候就不要再给我发新消息了。

消息均发

咱们直接上代码,两个消费者,一个生成者

消息公平均发Send .py
credentials = pika.PlainCredentials('xiaoxia', 'xiaoxia')
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '47.244.*.*', 5672, '/', credentials))
# 声明queue
channel = connection.channel()
# 声明queue
channel.queue_declare(queue='round_robin_queue_mm')
# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
n = 20
sum = 0
counter = 1
while counter <= n:
    channel.basic_publish(exchange='',                 #Producer只能发送到exchange,它是不能直接发送到queue的,发送到默认exchange
                          routing_key='round_robin_queue_mm',         #路由key发送指定队列
                          body='Hello World:'+str(counter)) #发送的消息
    counter += 1
print(" [x] Sent 'Hello World!'")
connection.close()

消息公平分发Received.py
最重要的这个配置:channel.basic_qos(prefetch_count=1)
import pika
import time
credentials = pika.PlainCredentials('xiaoxia', 'xiaoxia')
#建立连接
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '47.244.*.*', 5672, '/', credentials))
channel = connection.channel()
# You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
#如果生产者先运行并创建了队列这里就可以不用声明,但是有可能消费者先运行 下面的basic_consume就会因为没有队列报错。
channel.queue_declare(queue='round_robin_queue_mm')

def callback(ch, method, properties, body):  #定义回调函数用于取出队列中的数据
    print(" [x] Received %r" % body)
    time.sleep(2)
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='round_robin_queue_mm',
                      on_message_callback=callback)          #不确认消息
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()                      #监听数据
消息公平分发消息公平分发Received2.py
最重要的这个配置:channel.basic_qos(prefetch_count=1)

import pika
import time
credentials = pika.PlainCredentials('xiaoxia', 'xiaoxia')
#建立连接
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '47.244.*.*', 5672, '/', credentials))
channel = connection.channel()
# You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.
#如果生产者先运行并创建了队列这里就可以不用声明,但是有可能消费者先运行 下面的basic_consume就会因为没有队列报错。
channel.queue_declare(queue='round_robin_queue_mm')

def callback(ch, method, properties, body):  #定义回调函数用于取出队列中的数据
    print(" [x] Received %r" % body)
    #time.sleep(body.count(b'.'))
    time.sleep(1)
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_qos(prefetch_count=1)
channel.basic_consume(queue='round_robin_queue_mm',
                      on_message_callback=callback)          #不确认消息
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()                      #监听数据


接下来看结果,就一目了然了:
在这里插入图片描述
在这里插入图片描述
这样就实现了实际的效果,也是实际场景中经常用到的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

野火少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值