RABBITMQ Introduction
RABBITMQ is an open source implementation of the AMQP (Advanced Message Queue) developed by Erlang, RABBITMQ is a message broker that receives messages from "producers" and delivers messages to "consumers", during which rules can be routed, cached, Persistent messages. "Producer" is also the message sender hereinafter referred to as p, the corresponding "consumer" is a message recipient hereinafter referred to as c,message through the queue from p to c,queue exist in RABBITMQ, can store as much as possible message, Multiple p can send a message to the same queue, and multiple C can receive a message from the same queue
- Internal architecture:
Description
- Message: RabbitMQ The forwarded binary object, including the headers (header), properties (attributes), and data (data), where the part is not necessary. Producer (producer): The producer of the message, responsible for generating the message and sending the message to the switch
Application of Exhange.
- Consumer (consumer): An app that uses queue queues to get messages from Exchange.
- Exchange (Switch): Responsible for receiving messages from the producer and transferring it to the appropriate queue
Queue: A buffer that stores messages sent by Exchange and proactively sends messages to Consumer, or Consumer proactively to get messages. See section 1.4 for a description.
Binding: The relationship between the queue and the switch. Exchange forwards messages based on the properties of the message and the properties of the Binding. An important property of the binding is Binding_key.
Connection (Connect) and channel (channel): Producers and consumers need to establish a TCP connection with RabbitMQ. Some applications require multiple connection, in order to conserve TCP connections, you can use the Channel, which can be considered a lightweight connection to a shared TCP connection. The connection requires user authentication and supports TLS (SSL). The connection requires an explicit shutdown.
Python Operation Rabbitmq
1. Implementing a simple Message queue
A product sends a message to the queue, a client receives a message from the queue and prints
- Send Message Product
Import Pikacredentials= Pika. Plaincredentials ('Alex','alex3714') # Voucher Connection= Pika. Blockingconnection (Pika. Connectionparameters (host='192.168.152.134', port=5672, credentials=credentials)) # define Connection Pool channel=Connection.channel () # Generate connection channel Channel.queue_declare (Queue='Test'#) # declares a queue to send messages to it channel.basic_publish (Exchange="', routing_key='Test', body='Hello world!') # Note When Exchange is not defined, the Routing_key needs to be consistent with the queue's values print ('Send success msg to RABBITMQ') Connection.close () # Close Connection
- Receiving messages, client
Import Pikacredentials= Pika. Plaincredentials ('Alex','alex3714') # Voucher Connection= Pika. Blockingconnection (Pika. Connectionparameters (host='192.168.152.134', port=5672, credentials=credentials)) # Connection Parameters Channel=Connection.channel () # Generate connection channel Channel.queue_declare (Queue='Test') # declares the queue. Consumers also need to declare queues in order to prevent producers from declaring the queue, resulting in an error running. DEF callback (ch, method, properties, body):"""callback function to handle messages taken out of RABBITMQ:p Aram Ch: Channel:p Aram Method: Methods:p Aram Properties: Property:p Aram Body: Content:return: Received information"""Print"[x] Received%r"%body) # Print (ch,method,properties,body)"""<pika.adapters.blocking_connection. BlockingchannelObjectAt0x0000000002f1db70> <basic.deliver (['CONSUMER_TAG=CTAG1.3C1D688587C447E5AC3A72EA99E98CAC','delivery_tag=1','exchange=','Redelivered=false','routing_key=test']) > <BasicProperties> b'Hello world!' """Channel.basic_consume (callback, queue='Test', no_ack=True) # No_ack indicates that an ACK is not required to be sent. The default is False, which indicates an open state. Print ('[*] waiting for messages. To exit Press CTRL + C') channel.start_consuming () # Start listening, receiving messages
Execution effect:
#product端: Send success msg to rabbitmq#client end: [ for messages. To exit Press CTRL +C [x] Received b'Hello world! '
Python Operation Rabbitmq