栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

mq 的 direct 模式(python)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

mq 的 direct 模式(python)

mq 的direct 模式,在生产者中,绑定队列和路由key 和 发布的路由key 一定要一致。消费者和生产者的路由key 一致,当然一个队列也可以绑定多个key.

生产者
python 代码

import pika                                                                                      
from pika.exchange_type import ExchangeType                                                      
                                                                                                 
con = pika.BlockingConnection(                                                                   
    pika.ConnectionParameters(host="localhost")                                                  
)                                                                                                
# 创建通道                                                                                           
channel = con.channel()                                                                          
# 声明队列                                                                                           
result = channel.queue_declare(queue='sss')                                                      
queue_name = result.method.queue                                                                 
# 声明交换机和交换机类型                                                                                    
channel.exchange_declare(exchange="direct_logs",exchange_type=ExchangeType.direct)               
# 绑定交换机和队列                                                                                       
routing_key = "info"                                                                             
channel.queue_bind(queue_name,"direct_logs",routing_key=routing_key)                             
message = "oh my gold !"                                                                         
channel.basic_publish(exchange="direct_logs",routing_key=routing_key,body=message)               
                                                                                                 
con.close()                                                                                      

消费者

import pika

con = pika.BlockingConnection(
    pika.ConnectionParameters(host="localhost") # 连接参数处,可以什么都不传,因为默认的值,默认的端口5672,默认的交换机,默认的虚拟机/,默认的用户名和默认的密码
)

channel = con.channel()

channel.exchange_declare(exchange="direct_logs", exchange_type="direct")
result = channel.queue_declare(queue="sss")
queue_name = result.method.queue

# 绑定交换机和路由
channel.queue_bind(exchange="direct_logs", queue=queue_name, routing_key="info")


def callback(ch, method, properties, body):
    print("[x] %r:%r file_name = %s" % (method.routing_key, body,"----consumer_01"))


channel.basic_consume(queue=queue_name,on_message_callback=callback,auto_ack=True)

channel.start_consuming()
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/708784.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号