RabbitMQ 关键字模式(续)用户名与密码 python接口

# 生产者 发送方
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()

相关文章

显卡天梯图2024最新版,显卡是电脑进行图形处理的重要设备,...
初始化电脑时出现问题怎么办,可以使用win系统的安装介质,连...
todesk远程开机怎么设置,两台电脑要在同一局域网内,然后需...
油猴谷歌插件怎么安装,可以通过谷歌应用商店进行安装,需要...
虚拟内存这个名词想必很多人都听说过,我们在使用电脑的时候...