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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

默执_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值