RabbitMQ 路由绑定与广播

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')

转载于:https://my.oschina.net/zhenruyan/blog/3097266

你可能感兴趣的:(RabbitMQ 路由绑定与广播)