RABBITMQ is a message agent, the core principle: Send messages, receive messages.
RABBITMQ is primarily used for decoupling between components, and the message sender does not need to know the presence of the message consumer, or vice versa.
Unidirectional decoupling bidirectional decoupling (for example, RPC)
For example, a log system that is easy to use RABBITMQ simplifies the workload, one consumer for the normal processing of messages, and another consumer to log messages. As long as the queue of two consumer is specified in the program to be bound to the same exchange in the same way, the remaining message distribution work is done by RABBITMQ.
Please refer to the official website for installation of RABBITMQ.
Let's start with a very simple "Hello World" example of how to use RABBITMQ, and then introduce the basic concepts involved and do a bit more about switches and queues.
I. Examples of HelloWorld
This example is very simple-send a message "Hello World", then get it and output it to the screen.
A total of two programs, a send message called send.py, a message to accept and print the message content is called receive.py.
The diagram is a schematic of the HelloWorld example: Producer send.py (Productor) sends the message ("Hello World") to a queue called "queue" where the consumer receive.py to get the message from the queue. Next look at the code:
send.py (Send message)
#!/usr/bin/env pythonImportPika#The first step is to connect the RABBITMQ serverRabbit_username='XXX'Rabbit_password='XXX'Credentials=Pika. Plaincredentials (Rabbit_username, Rabbit_password) connection= Pika. Blockingconnection (Pika. Connectionparameters (host='x.x.x.x', credentials=credentials))
#Channel is the channels for message reading and writing =Connection.channel ()#The second step is to create a queue that is called the ' queue ' and then send the message to this queueChannel.queue_declare (Queue='Queue')#in the third step, the message can now be sent, but RABBITMQ cannot send the message directly to the queue, to be sent to the switch, which is described later, using the default exchanger (Exchange), which uses an empty string to label
#识, the Routing_key parameter must be specified as the queue name,
channel.basic_publish (Exchange="', Routing_key='Queue', Body='Hello World')Print "send.py:send message ' Hello World ', wait for receive.py deal with the This message"#before exiting the program, ensure that the message has been posted to RABBITMQ by closing the connection Connection.close ()
receive.py (Get Data)
Print '[*] waiting for messages. To exit Press CTRL + C'#!/usr/bin/env pythonImportPika#The first step is to also connect the RABBITMQ serverRabbit_username='XXX'Rabbit_password='XXX'Credentials=Pika. Plaincredentials (Rabbit_username, Rabbit_password) connection= Pika. Blockingconnection (Pika. Connectionparameters (host='x.x.x.x', credentials=credentials)) Channel=Connection.channel ()#to ensure that the queue exists, execute Queue_declare again to create a queue, we can run the command multiple times, but as soon as one queue creates#because the send.py is not guaranteed to execute first or receive.py first, the queue is repeatedly declared to ensure that it existsChannel.queue_declare (queue='Hellolxy')#in the third step, a callback function is defined, and when the message is obtained, the Pika library calls the callback function to process the message, which prints the message content to the screendefCallback (ch, method, properties, body):Print "receive.py:Received Message%r"%(body,)#fourth, tell the RABBBITMQ callback function to receive messages from the queue queuesChannel.basic_consume (callback, queue='Queue', No_ack=True)#Fifth Step, enter an infinite loop to wait for the message data and run the callback functionPrint '[*] waiting for messages. To exit Press CTRL + C'channel.start_consuming ()
Now run the program at the terminal. First, send a message with send.py:
' Hello World ' for receive.py deal with this message
The producer (producer) program send.py stops after each run. Now run receive.py to receive the message:
' Hello World ' [ for messages. To exit Press CTRL + C
it worked! The first message has now been sent through RABBITMQ. But the receive.py program does not exit, it is always ready to get the message, can be interrupted by ctrl-c.
Ii. Basic concepts of RABBITMQ
Summarize the process of sending incoming messages:
After having a perceptual knowledge of RABBITMQ through the above example, we will now introduce the basic concepts in RABBITMQ.
Broker: Message Queuing server entity
message : Each message has a property of a routing key (routing key) . is a simple string.
Connection: The application's network connection to the broker.
Channel: Almost all operations are carried out in the channel, which is the way to read and write messages. The client can establish multiple channel, with each channel representing a session task.
Switch : Receives the message, forwards the message to the bound queue According to the routing key .
binding : A binding is a routing rule that connects switches and queues based on a routing key, so the switch is a routing table made up of bindings.
Example: A message with a routing key of "Key1" to be sent to two queues, QueueA and Queueb. To do this, you create two bindings, one for each switch and one queue for each binding. Both are triggered by the routing key "Key1", in which case the switch copies a message and sends them to two queues respectively.
Queue : The container for the message, and also the end of the message. One message can be put into one or more queues. The message is in the queue, waiting for the consumer to connect to the queue and take it away.
Third, switch
The switch is used to receive messages and forward messages to the bound queue, which is the core of the RABBITMQ.
There are 4 types of switches: Direct,topic,headers and Fanout.
Why not create a switch to handle all types of routing rules? Because the CPU overhead is different for each rule match, the appropriate switch is chosen according to the different requirements.
Example: A switch of type "topic" will match the routing key of the message to a pattern similar to "dog.*". A "direct" type of switch compares the routing key to "dogs". Match-end wildcards consume more CPUs than direct comparisons, so if the flexibility of the "topic" type switch is not used, then the "direct" type switches get higher processing efficiency.
1.
DirectSwitch: Forwards the message to the Routingkey specified queue (exact match, unicast).
The Routingkey exactly matches the queue name, and if a queue is bound to a switch that requires a routing key of "dog", only messages that are tagged as dog are forwarded Routingkey, Dog.puppy are not forwarded, Dog.guard are not forwarded, and so on.
2.
Topic switch:Forwarding messages by rules (most flexible, multicast)
The topic type switch assigns the Routing-key property of the message through pattern matching . Matches a route key with a pattern, at which point the queue needs to be bound to a pattern.
It divides the strings of routing-key and binding-key into words. These words are separated by dots. It also recognizes two wildcard characters: the symbol "#" and the symbol "*". #匹配0个或多个单词, * match not more than a few words.
For example, the binding key:*.stock.# matches routing key:usd.stock and eur.stock.db, but does not match Stock.nana.
For example, "audit.#" can match to "audit.irs.corporate", but "audit.*" matches only to "Audit.irs".
3. Fanout switch: Forwards the message to all bound queues (fastest, broadcast)
The fanout switch does not process the routing key, simply binds the queue to the switch, and each message sent to the switch is forwarded to all the queues that are bound to the switch.
Much like a subnet broadcast, each host in the network receives a copy of the message. Fanout switch Forwarding message is the fastest.
4. Note
- If no queue is bound on the switch, messages sent to the switch are lost.
- A switch can bind multiple queues, and a queue can be bound by multiple switches.
- There are also other types of switch types, such as header, failover, System, and so on, which are not implemented in the current RABBITMQ version.
- Because the switch is a named entity, declaring a switch that already exists, but trying to give different types is causing an error. The client needs to delete the existing switch and then re-declare and give the new type.
- Properties of the switch:
- Persistence: If enabled, the switch will be valid until the server restarts.
- Automatic deletion: If enabled, the switch will delete itself after its bound queue has been deleted.
- Inertia: If the switch is not declared, it will cause an exception when executed to use, and will not be actively declared.
Iv. queues
- Properties of the queue:
- Persistence: If enabled, the queue will be valid until the server service restarts.
- Automatic deletion: If enabled, the queue will automatically delete itself after all consumers have stopped using it.
- Inertia: If no queue is declared, it will cause an exception when executed to use, and will not be actively declared.
- Exclusivity: If enabled, the queue can only be used by consumers who declare it.
For further information, refer to:
AMQP protocol
The Rabbitmq+python classic Bunny and Rabbit den
Official tutorials
Hello World
Work queue
The above two articles are translated into Chinese, the rest are not translated, can refer to the English version
RabbitMQ Tutorials
Python uses the Pika Library with RABBITMQ Summary, multiple notes and examples
RABBITMQ Basic concepts and use