RABBITMQ the publication and subscription of Message Queuing is similar to broadcasting, one end sends messages, and multiple clients can receive messages at the same time
Fanout: All queues that are bound to exchange can receive messages
Message Publishing Side
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' 127.0.0.1 ')) Channel = Connection.channel () channel.exchange_declare (exchange= ' Fanout_ Logs ', exchange_type= ' fanout ') message = ' Hello world! ' Channel.basic_publish (exchange= ' fanout_logs ', routing_key= ', body=message) print (' Send message: ', message) Connection.close ()
Message subscribers
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" Import pikaconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' 127.0.0.1 ')) Channel = Connection.channel () channel.exchange_declare (exchange= ' Fanout_ Logs ', exchange_type= ' fanout ') result = Channel.queue_declare (exclusive=true) # does not specify a queue name, RABBITMQ randomly assigns a name # Exclusive=true will automatically delete the queue after the message subscriber using this queue is disconnected queue_name = result.method.queueprint (' Current queue name: ', queue_name) Channel.queue_bind (exchange= ' fanout_logs ', Queue=queue_name) def callback (ch, method, properties, body): print ( CH, method, properties) print (' Received data: ', body) Channel.basic_consume (callback, Queue=queue_name, no_ack= True, ) print (' Start waiting for message ') channel.start_consuming ()
The message Publisher needs to run after the message Subscriber is running, or the message will not receive the message at the subscriber end
Open 3 message subscribers and one message publisher
Message Publishing End Publishing message
3 Message subscribers receive a message at the same time
Direct: The specified queue to receive messages
The queue binding keyword, the message publisher sends a message to the exchange,exchange based on the keyword send a message to the specified queue based on the keyword
Message Publishing Side
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pikaimport sysconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' 127.0.0.1 ')) Channel = Connection.channel () channel.exchange_declare (exchange= ' direct_ Logs ', exchange_type= ' direct ') severity = sys.argv[1] If len (SYS.ARGV) > 1 Else ' info ' # keyword message = ' Hello world! ' Channel.basic_publish (exchange= ' direct_logs ', routing_key=severity, body=message) print (' level [%s] Send data [%s] '% (severity, message)) Connection.close ()
Message subscribers
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pikaimport sysconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' 127.0.0.1 ')) Channel = Connection.channel () channel.exchange_declare (exchange= ' direct_ Logs ', exchange_type= ' direct ') result = Channel.queue_declare (exclusive=true) queue_name = Result.method.queueseverities = sys.argv[1:] # keyword for severity in severities: channel.queue_bind (exchange= ' Direct_logs ', queue=queue_name, routing_key=severity) def callback (ch, method, properties, body): Print (ch, method, properties) Print (' Received message from%s:%s '% (Method.routing_key, body)) Channel.basic_consume (callback, Queue=queue_name, no_ ack=true) print (' Start waiting for message ') channel.start_consuming ()
Open 3 message Subscribers, referring to the keyword info, warning,warning, error,error, info, open a message publisher, reference the keyword wanrning
Only message subscribers that reference the keyword warning receive a message
No message is received without referencing the keyword warning
Topic: Qualifying queue to receive messages
Message Subscribers specify criteria for qualifying queue to be received by message subscribers
#和 * matches one or more arbitrary characters
A single # will match all strings, and a single * cannot match
Message Publishing Side
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pikaimport sysconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' 127.0.0.1 ')) Channel = Connection.channel () channel.exchange_declare (exchange= ' topic_ Logs ', exchange_type= ' topic ') severity = sys.argv[1] If len (SYS.ARGV) > 1 Else ' info ' message = ' Hello world! ' Channel.basic_publish (exchange= ' topic_logs ', routing_key=severity, body=message) print (' Send message [%s] ' to [%s] '% (severity, message)) Connection.close ()
Message subscribers
#-*-Coding:utf-8-*-__author__ = "MuT6 sch01ar" import pikaimport sysconnection = Pika. Blockingconnection (Pika. Connectionparameters (host= ' 127.0.0.1 ')) Channel = Connection.channel () channel.exchange_declare (exchange= ' topic_ Logs ', exchange_type= ' topic ') result = Channel.queue_declare (exclusive=true) queue_name = Result.method.queuekeys = Sys.argv[1:]for key in Keys: channel.queue_bind (exchange= ' topic_logs ', queue=queue_name, routing_key= Key) def callback (ch, method, properties, body): Print ( ch, method, properties) print (' from [%s] ' information [%s] '% (method. Routing_key, body)) Channel.basic_consume (callback, Queue=queue_name, no_ack=true) print (' Start receiving messages ') Channel.start_consuming ()
Open 3 message subscribers and one message publisher
3 message subscribers, one *.exe matches the end of the. exe, one # matches all strings, and one Python.exe matches only the string Python.exe
Message Publishing side reference keyword Qq.exe
Obviously, a message that matches the end of the. exe and matches all the strings will receive a message that matches only the string of Python.exe and does not receive a message
PYTHON-RABBITMQ publication and subscription of Message Queuing