温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

RabbitMQ发送端接收端生产者消费者的示例分析

发布时间:2021-12-24 09:46:08 来源:亿速云 阅读:159 作者:小新 栏目:编程语言

这篇文章给大家分享的是有关RabbitMQ发送端接收端生产者消费者的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

rabbit_conn_send_producer.py
import pika

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

# 声明queue
channel.queue_declare(queue='hello')

# 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 发送的消息
print(" [x] Sent 'Hello World!'")
connection.close()

rabbit_conn_recive_consumer.py
# _*_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='hello')#声明queue


def callback(ch, method, properties, body):#处理消息
    print("---->",ch,method,properties)#ch 管道内存对象地址 method:发给queue的信息
    print(" [x] Received %r" % body)


channel.basic_consume(#消费消息
           callback,#如果收到消息,就调用CALLBACK函数来处理消息
           queue='hello',#从哪个队列里收消息
           no_ack=True) #不需要确认消息是否接收

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

rabbit_conn_recive_consumer_no_ack.py
# _*_coding:utf-8_*_
__author__ = 'Alex Li'
import pika,time

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='hello')#声明queue


def callback(ch, method, properties, body):#回调函数
    print("---->",ch,method,properties)#ch 管道内存对象地址 method:发给queue的信息
    time.sleep(5)#模拟消息处理时间
    print(" [x] Received %r" % body)


channel.basic_consume(#消费消息
              callback,#如果收到消息,就调用CALLBACK函数来处理消息
           queue='hello',#从哪个队列里收消息
              #no_ack=True)#no acknowledgement 不确认 开启的话表示不确认消息是否接收 接没接收都不会给服务器端发消息 如果客户端没收到消息就忽略了
              #关闭的话就要服务器就要确认消息是否接收,没有确认到接收消息就会一直保留消息,会自动转到另一个客户端,socket一断,rabbitMQ就将消息转给另一个客户端
              #发送端生产者发送一条消息,被消费者接受者收到了,消费者接收端处理完之后自动给生产者发送端发一个确认,说消息处理完了,然后生产者发送端才会把消息从队列里删除,只要没收到确认就不会删除,如果生产者发送端没收到确认,就会把消息转给另一个消费者接收端
          )

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

感谢各位的阅读!关于“RabbitMQ发送端接收端生产者消费者的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI