I. Introduction of the Model
In the previous example, the message was sent directly to the queue.
Now that the patterns are introduced, messages are sent to exchange, and consumers bind the queues to exchange.
Publish-Subscribe mode is to broadcast the message to each consumer, each consumer receives the message is the same.
A producer, multiple consumers, each consumer has its own queue, the producer does not send the message directly to the queue, but sends to the switch, each queue binds the switch, the producer sends the message through the switch, arrives the queue, realizes a message to be obtained by multiple consumers. It is important to note that if you send a message to an exchange that does not have a queue binding, the message will be lost because Exchange does not have the ability to store messages in RABBITMQ, only the queue has the ability to store messages.
Queue model:
Second, code example
Publisher: No more queues are created and messages are sent to Exchange (switch). Exchange_type for Fanout.
1 #!/usr/bin/env python2 ImportPika3 4Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='localhost'))5Channel =Connection.channel ()6 7Channel.exchange_declare (exchange='logs',8Exchange_type='fanout')9 Ten forIinchRange (20): OneMessage ="Info:hello world! {}". Format (i) AChannel.basic_publish (exchange='logs', -routing_key="', -body=message) the Print("[x] Sent%r"%message) -Connection.close ()
View Code
Subscribed by
Subscribers: Each consumer creates an anonymous queue, binds to the corresponding exchange, and receives the message. Once the consumer's connection port, the corresponding queue is closed, not receiving messages. Use exclusive to control.
1 #!/usr/bin/env python2 ImportPika3 4Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='localhost'))5Channel =Connection.channel ()6 7Channel.exchange_declare (exchange='logs',8Exchange_type='fanout')9 Tenresult = Channel.queue_declare (exclusive=True) OneQueue_name =Result.method.queue A -Channel.queue_bind (exchange='logs', -Queue=queue_name) the - Print('[*] waiting for logs. To exit Press CTRL + C') - - + defCallback (ch, method, properties, body): - Print("[x]%r"%body) + A at Channel.basic_consume (Callback, -Queue=Queue_name, -no_ack=True) - -Channel.start_consuming ()
View Code
Publisher output:
[x] Sent ' Info:hello world! 0 ' [x] Sent ' Info:hello world! 1 ' [x] Sent ' Info:hello world! 2 ' [x] Sent ' Info:hello world! 3 ' [x] Sent ' Info:hello world! 4 ' [x] Sent ' Info:hello world! 5 ' [x] Sent ' Info:hello world! 6 ' [x] Sent ' Info:hello world! 7 ' [x] Sent ' Info:hello world! 8 ' [x] Sent ' Info:hello world! 9 ' [x] Sent ' Info:hello world! [x] Sent ' Info:hello world! One ' [x] Sent ' Info:hello world! [x] Sent ' Info:hello world! [x] Sent ' Info:hello world! [x] Sent ' Info:hello world! [x] Sent ' Info:hello world! [x] Sent ' Info:hello world! [x] Sent ' Info:hello world! [x] Sent ' Info:hello world! 19 '
Subscribers ' output:
[x] B ' Info:hello world! 0 ' [x] b ' Info:hello world! 1 ' [x] b ' Info:hello world! 2 ' [x] b ' Info:hello world! 3 ' [x] b ' Info:hello world! 4 ' [x] b ' Info:hello world! 5 ' [x] b ' Info:hello world! 6 ' [x] b ' Info:hello world! 7 ' [x] b ' Info:hello world! 8 ' [x] b ' Info:hello world! 9 ' [x] b ' Info:hello world! [x] B ' Info:hello world! One ' [x] b ' Info:hello world! [x] B ' Info:hello world! [x] B ' Info:hello world! [x] B ' Info:hello world! [x] B ' Info:hello world! [x] B ' Info:hello world! [x] B ' Info:hello world! [x] B ' Info:hello world! 19 '
You can see that the message received by two subscribers is the same.
The status change for each subscriber does not affect the message received by another subscriber.
Third, queue information
On the Web Administration page, view the logs in Exchange, and you can see two queues.
Python uses RABBITMQ to introduce three (publish subscription mode)