- *(星號)只能代替一個詞。
- (hash) 可以替代零個或多個單詞。
一、創建一個exchange
二、消息產生端
#!/usr/bin/env python import pika import sys import json import datetime def get_message(): # 產生消息入口處 for i in range(100): # 生成100條消息 # 生成三種類型的消息 for str_t in ['quick.orange.rabbit','lazy.orange.elephant','quick.orange.fox','lazy.brown.fox','lazy.pink.rabbit','quick.brown.fox','quick.orange.new.rabbit']: # 生成多種類型的消息 message = json.dumps({'id': "%s-90000%s" % (str_t, i), "amount": 100 * i, "name": "%s" % str_t, "createtime": str(datetime.datetime.now())}) producer(message, str_t) def producer(message, routing_key): # 登陸並創建信道 connection = pika.BlockingConnection( pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672, credentials=pika.PlainCredentials('guest', 'guest'))) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic',durable=True) channel.basic_publish(exchange='topic_logs', routing_key=routing_key, body=message) print(" [x] Sent %r:%r" % (routing_key, message)) connection.close() if __name__ == "__main__": get_message() # 程序執行入口
三、消息接收端【lazy.#】
#!/usr/bin/env python import pika import sys connection = pika.BlockingConnection(pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672, credentials=pika.PlainCredentials('guest', 'guest'))) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic',durable=True) result = channel.queue_declare('', exclusive=True) queue_name = result.method.queue # for binding_key in ['lazy.#','lazy.*']: channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key='lazy.#') print(' [*] Waiting for logs. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body)) channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) channel.start_consuming()
四、消息接收端【.orange.】
#!/usr/bin/env python import pika import sys connection = pika.BlockingConnection(pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672, credentials=pika.PlainCredentials('guest', 'guest'))) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic',durable=True) result = channel.queue_declare('', exclusive=True) queue_name = result.method.queue # for binding_key in ['*.orange.*','*.*.rabbit','lazy.#','lazy.*']: channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key='*.orange.*') print(' [*] Waiting for Orange. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body)) channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) channel.start_consuming()
五、消息接收端【..rabbit】
#!/usr/bin/env python import pika import sys connection = pika.BlockingConnection(pika.ConnectionParameters(virtual_host='/melon_demo', host='82.156.19.94', port=5672, credentials=pika.PlainCredentials('guest', 'guest'))) channel = connection.channel() channel.exchange_declare(exchange='topic_logs', exchange_type='topic',durable=True) result = channel.queue_declare('', exclusive=True) queue_name = result.method.queue # for binding_key in ['*.orange.*','*.*.rabbit','lazy.#','lazy.*']: channel.queue_bind(exchange='topic_logs', queue=queue_name, routing_key='*.*.rabbit') print(' [*] Waiting for *.*.rabbit. To exit press CTRL+C') def callback(ch, method, properties, body): print(" [x] %r:%r" % (method.routing_key, body)) channel.basic_consume(queue=queue_name, on_message_callback=callback, auto_ack=True) channel.start_consuming()
六、運行截圖