首页 日常 数据库 仓库 关于我 关于本站

RabbitMQ 路由绑定与广播

2018-11-27-23:11

rabbitMQ 实现组播与广播

rabbitmq 分路由和队列

路由分三个模式

topic 模糊匹配 * 匹配一个字符 # 匹配多个字符

fanout 广播

direct 全匹配

生产者代码

# !/usr/bin/env python
import pika
import time
credentials = pika.PlainCredentials('guest','guest')



if __name__ == '__main__':
    # 声明queue

        connection = pika.BlockingConnection(pika.ConnectionParameters(
        '127.0.0.1', 5672, '/', credentials))
        # channel.exchange_declare()
        for a in range(1,1000000):
            channel = connection.channel()
            channel.queue_declare(queue="chat."+str(a),durable=False)
            channel.basic_publish(exchange='amq.topic',
                                  routing_key="chat.*",
                                  body='Hello World!')
            channel.queue_bind(exchange='amq.topic',
                               queue="chat."+str(a),
                               routing_key="chat.*")
            channel.basic_publish(exchange='amq.topic',
                                  routing_key="chat.*",
                                  body='Hello World!')
            channel.close()
            time.sleep(1)
            print(" [x] Sent 'Hello World!'")

        connection.close()

消费者代码

# _*_coding:utf-8_*_
import pika
import time
credentials = pika.PlainCredentials('guest','guest')
connection = pika.BlockingConnection(pika.ConnectionParameters(
    '127.0.0.1',5672,'/',credentials))


if __name__ == '__main__':
    while True:
        channel = connection.channel()
        channel.exchange_declare(exchange='topic_logs',type='topic')
        method_frame, header_frame, body = channel.consume("chat")
        if method_frame:
            print(method_frame, header_frame, body)
            channel.basic_ack(method_frame.delivery_tag)
        else:
            time.sleep(1)
            print('No message returned')

更多文章


决定了 博客要增加rss和同步推送到微信公众号

愿意透露姓名的网友:admin

今天20号了 依旧没更新。。。

愿意透露姓名的网友:admin

明天三月十五号 我还是没更新博客啊

愿意透露姓名的网友:admin

此时一位单身狗路过

愿意透露姓名的网友:渣哥牛逼

谁能想到它会死在头七呢 -----------郭德纲

愿意透露姓名的网友:admin

友情链接