python rabbitmq(三) 发布订阅之"direct"

#2direct 实现根据关键字发布消息
#       在发送消息的时候使用routing_key参数指定关键字,rabbitmq的exchange会判断routing_key的值,然后只将消息转发至匹配的队列,
#       注意,此时需要订阅者先创建队列配置参数为exchange的type=direct,然后定义routing_key即可

mq_direct_producer.py

import pika


credient = pika.PlainCredentials(username='admin',password='admin')
connection = pika.BlockingConnection(pika.ConnectionParameters(host='0.0.0.0',port=5672,virtual_host='/',credentials=credient))

channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange',exchange_type='direct',durable=True)

for i in range(10):
    message = f'time {i}'
    if i % 2 ==0:
        channel.basic_publish(exchange='direct_exchange',routing_key='direct',body=message,properties=pika.BasicProperties(
                                  delivery_mode=2,))  # 确保消息是持久的,设置消息持久化,将要发送的消息的属性标记为2,表示该消息要持久化
    else:
        channel.basic_publish(exchange='direct_exchange',routing_key='notdirect',body=message,properties=pika.BasicProperties(delivery_mode=2,))
    print(message)

connection.close()

mq_direct_consumer1.py

import pika


credient = pika.PlainCredentials(username='admin',password='admin')
connection = pika.BlockingConnection(pika.ConnectionParameters(host='0.0.0.0',port=5672,credentials=credient))

channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange',exchange_type='direct',durable=True)
channel.queue_declare(queue='direct_queue1',durable=True,auto_delete=True)
channel.queue_bind(exchange='direct_exchange',queue='direct_queue1',routing_key='direct') # routing_ke队列指定以什么规则绑定交换和队列

def callback(channel,method,properity,body):
    channel.basic_ack(delivery_tag=method.delivery_tag)
    print(body.decode())

channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_message_callback=callback,queue='direct_queue1')
channel.start_consuming()

mq_direct_consumer2.py

import pika
import time


credient = pika.PlainCredentials(username='admin',password='admin')
connection = pika.BlockingConnection(pika.ConnectionParameters(host='0.0.0.0',port=5672,virtual_host='/',credentials=credient))

channel = connection.channel()
channel.exchange_declare(exchange='direct_exchange',exchange_type='direct',durable=True)
channel.queue_declare(queue='direct_queue1',durable=True,auto_delete=True)
channel.queue_bind(exchange='direct_exchange',queue='direct_queue1',routing_key='notdirect')

def callback(channel,method,properity,body):
    time.sleep(2)
    channel.basic_ack(delivery_tag=method.delivery_tag)
    print(body.decode())
channel.basic_qos(prefetch_count=1)
channel.basic_consume(on_message_callback=callback,queue='direct_queue1')
channel.start_consuming()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值