python rabitmq_python使用rabbitMQ介绍四(路由模式)

一、模式介绍

路由模式,与发布-订阅模式一样,消息发送到exchange中,消费者把队列绑定到exchange上。

这种模式在exchange上添加添加了一个路由键(routing-key),生产者发布消息的时候添加路由键(routing-key),消费者绑定队列到交换机时添加键值(routing-key),这样就可以接收到对应的消息。

路由模式的direct exchange。

队列模型:

img?u=aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvNDcxNjc2LzIwMTkwMS80NzE2NzYtMjAxOTAxMTIyMjU1MDgyOTUtODU2OTA2Mjk1LnBuZw==

img?u=aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvNDcxNjc2LzIwMTkwMS80NzE2NzYtMjAxOTAxMTIyMzI4NDkwMTctODk1MjA4MjExLnBuZw==

与发布-订阅模式不同的是,每个消费者队列接收的消息不同,根据消息的routing-key把消息发送到不同的队列中。

当所有的消费队列绑定的routing-key一样时,路由模式行为与发布-订阅模式一样。

二、代码示意

发布者:不再创建队列,发送消息到exchange(交换机)中。exchange_type为direct。

1 importpika2 importsys3

4 connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))5 channel =connection.channel()6

7 channel.exchange_declare(exchange='direct_logs',8 exchange_type='direct')9

10 severity = ['info', 'warning', 'error']11 for i in range(20):12 message = '{} Hello World! {}'.format(i, severity[i % 3])13 channel.basic_publish(exchange='direct_logs',14 routing_key=severity[i % 3],15 body=message)16 print("[x] Sent: {}".format(message))17 connection.close()

每个消费者绑定的队列定义不同的routing-key,接收到不同的消息。

以info为示例:

1 importpika2 importsys3

4 connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))5 channel =connection.channel()6

7 channel.exchange_declare(exchange='direct_logs',8 exchange_type='direct')9

10 result = channel.queue_declare(exclusive=True)11 queue_name =result.method.queue12

13 channel.queue_bind(exchange='direct_logs',14 queue=queue_name,15 routing_key='info')16

17 print('[*] Waiting for logs. To exit press CTRL+C')18

19 defcallback(ch, method, properties, body):20 print("[x] %r:%r" %(method.routing_key, body))21

22 channel.basic_consume(callback,23 queue=queue_name,24 no_ack=True)25

26 channel.start_consuming()

执行结果输出:

发布者:

[x] Sent: 0 Hello World! info

[x] Sent:1Hello World! warning

[x] Sent:2Hello World! error

[x] Sent:3Hello World! info

[x] Sent:4Hello World! warning

[x] Sent:5Hello World! error

[x] Sent:6Hello World! info

[x] Sent:7Hello World! warning

[x] Sent:8Hello World! error

[x] Sent:9Hello World! info

[x] Sent:10Hello World! warning

[x] Sent:11Hello World! error

[x] Sent:12Hello World! info

[x] Sent:13Hello World! warning

[x] Sent:14Hello World! error

[x] Sent:15Hello World! info

[x] Sent:16Hello World! warning

[x] Sent:17Hello World! error

[x] Sent:18Hello World! info

[x] Sent:19 Hello World! warning

Info输出:

[*] Waiting for logs. To exit press CTRL+C

[x]'info':b'0 Hello World! info'[x]'info':b'3 Hello World! info'[x]'info':b'6 Hello World! info'[x]'info':b'9 Hello World! info'[x]'info':b'12 Hello World! info'[x]'info':b'15 Hello World! info'[x]'info':b'18 Hello World! info'

Warning输出:

[*] Waiting for logs. To exit press CTRL+C

[x]'warning':b'1 Hello World! warning'[x]'warning':b'4 Hello World! warning'[x]'warning':b'7 Hello World! warning'[x]'warning':b'10 Hello World! warning'[x]'warning':b'13 Hello World! warning'[x]'warning':b'16 Hello World! warning'[x]'warning':b'19 Hello World! warning'

Error输出:

[*] Waiting for logs. To exit press CTRL+C

[x]'error':b'2 Hello World! error'[x]'error':b'5 Hello World! error'[x]'error':b'8 Hello World! error'[x]'error':b'11 Hello World! error'[x]'error':b'14 Hello World! error'[x]'error':b'17 Hello World! error'

可以看到,不同的消费者收到不同级别的日志信息。

三、队列信息

管理页面,exchange页面,点击“direct_logs”上查看队列情况,可以看到三个不同routing_key的队列

routing key列展示了对应的key。

img?u=aHR0cHM6Ly9pbWcyMDE4LmNuYmxvZ3MuY29tL2Jsb2cvNDcxNjc2LzIwMTkwMS80NzE2NzYtMjAxOTAxMTIyMzM2NDI5OTEtNzMyMTA1MjYyLnBuZw==

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值