# 生产者 发送方
import pika
cr = pika.PlainCredentials('admin', '.')
# python 链接 RabbitMQ
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.14.87', 5672, '/', cr))
channel = connection.channel()
channel.exchange_declare(exchange='lbw_3', # 指定交换机 名字任意
exchange_type='direct') # 指定类型 direct
# 数据
SB = "SB_123"
channel.basic_publish(exchange='lbw_3',
routing_key='__1', # 关键字模式
body=SB)
print("已发送")
connection.close()
# 消费者 接收方
import pika
cr = pika.PlainCredentials('admin', '.')
# python 链接 RabbitMQ
connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.14.87', 5672, '/', cr))
channel = connection.channel()
channel.exchange_declare(exchange='lbw_3', # 指定交换机
exchange_type='direct') # 指定类型
# 创建队列 生成队列名:exclusive=True
result = channel.queue_declare("", exclusive=True)
# 拿到 队列名
queue_name = result.method.queue
# 将 队列 绑定 交换机
channel.queue_bind(exchange='lbw_3',
queue=queue_name,
routing_key='__1' # 关键字
)
def callback(ch, method, properties, body):
print(body)
# 业务逻辑处理。。。。。。
# 手动应答模式
ch.basic_ack(delivery_tag=method.delivery_tag)
# 监听队列
channel.basic_consume(queue=queue_name,
auto_ack=False,
on_message_callback=callback)
print(1)
channel.start_consuming()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。