rabbitmq_消息持久化_消息公平分发_消息广播
admin
2023-07-12 06:04:52
0

生产者_procudure_send_消息持久化
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
   
'localhost'))  # rabbit默认端口5672 建立一个基本的 socket连接
channel = connection.channel()  # 声明一个管道 在管道里面发消息

#
声明queue
channel.queue_declare(queue='hello',durable=True)#durable=True队列持久化

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
                     
routing_key='hello'# queue名字
                     
body='Hello World!'# body 发送的消息
                     
properties=pika.BasicProperties(delivery_mode=2),#消息持久化
                         
)
print(" [x] Sent 'Hello World!'")
connection.close()

 

消费者_consumer_recive_消息公平分发
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
   
'localhost')) #rabbit默认端口5672 建立一个基本的 socket连接
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.
channel.queue_declare(queue='hello1')#声明queue 消费持久化的消息


def callback(ch, method, properties, body):#处理消息
   
print("---->",ch,method,properties)#ch 管道内存对象地址 method:发给queue的信息
   
print(" [x] Received %r" % body)
    ch.basic_ack(
delivery_tag=method.delivery_tag)# 手动确认消息是否接收

channel.basic_qos(prefetch_count=1)#根据权重发消息 一对一发
channel.basic_consume(#消费消息
          
callback,#如果收到消息,就调用CALLBACK函数来处理消息
          
queue='hello1',#从哪个队列里收消息
           #no_ack=True,
           
)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
#启动 开始收消息 一直收,没有就卡主

 

生产者_send_广播模式
import pika
import sys

connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.exchange_declare(
exchange='logging',exchange_type='fanout')

messages =
' '.join(sys.argv[1:]) or "info: Hello World!"
#messages="info: Hello World!"
channel.basic_publish(exchange='yyyy',
                     
routing_key='',
                      
body=messages)
print(" [x] Sent %r" % messages)
connection.close()

 

消费者_recive_广播模式
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()

channel.exchange_declare(
exchange='yyyy',
                        
exchange_type='fanout')

result = channel.queue_declare(
exclusive=True# 不指定queue名字,rabbit会随机分配一个名字,exclusive=True会在使用此queue的消费者断开后,自动将queue删除
queue_name = result.method.queue # 随机分配的queue
print("random queue name:",queue_name)

channel.queue_bind(
exchange='yyyy',
                  
queue=queue_name)#绑定转发器

print(' [*] Waiting for logs. To exit press CTRL+C')


def callback(ch, method, properties, body):
   
print(" [x] %r" % body)


channel.basic_consume(callback,
                     
queue=queue_name,
                     
no_ack=True)

channel.start_consuming()

相关内容

热门资讯

OpenAI,正式组建机器人事... 人工智能(AI)领域巨头OpenAI发布公告,宣布大力扩张内部机器人事业部,正式全面切入硬件赛道,实...
星火空间完成近亿元Pre-A轮... 据星火空间消息,6月1日,合肥星火空间科技有限公司完成近亿元Pre-A轮融资。本轮融资由云泽资本和轨...
刚刚,宇树IPO闪电过会!王兴... 智东西 作者 | 许丽思 编辑 | 漠影 智东西6月1日报道,刚刚,宇树通过上交所上市委会议审议。 ...
京东工业发起百川计划 携手上游... 京东工业大模型生态发布会6月1日在北京举行,京东工业携手合作伙伴正式开启“百川计划”,从数据、模型、...
强脑科技预计今年机械手销量大涨... IT之家 6 月 2 日消息,据彭博社 2 日(今天)报道,强脑科技预计,随着中国人形机器人产业快速...
一图看懂差距!iPhone 1... 快科技6月2日消息,iPhone 18 Pro不同版本电池容量不同的相关话题冲上社交平台热搜榜,引发...
iPhone 18 Pro 或... 据科技狐,近日,知名爆料人 Sonny Dickson 分享了 iPhone 18 Pro 全套机模...
武契奇:不排除卸任总统后担任总... 塞尔维亚总统武契奇近期密集释放政坛人事与大选相关信号,明确无意在 2027 年总统任期届满后谋求连任...
6月新机夯到拉盘点,告诉你哪台... 现在这形势,手机升价是不可能躲得过的了,而且涨价期至少持续两年。那既然内存涨价躲不过,就只能选升级大...
伊朗公开已故最高领袖哈梅内伊安... 新华社德黑兰6月2日电 据伊朗伊斯兰共和国通讯社2日报道,根据伊朗已故最高领袖阿里·哈梅内伊生前遗愿...