rebbitMQwindows RabbitMQ installation and use python in use (installation and simple tutorial)

1 Introduction

RabbitMQ (Rabbit Message Queue) is a popular open source message queuing system, developed using erlang language.
 
1.1 Description Keywords:
Broker: Message Queue server entity.
Exchange: Message switch that specifies what the message according to the rules, which are routed to a queue.
Queue: vector message queue, each message will be put into one or more queues.
Binding: binding, its role is to exchange routing and queue bind in accordance with the rules.
Routing Key: routing keywords, exchange of messages delivered in accordance with this keyword.
vhost: web hosting, where a broker can offer more vhost, as a different user privilege separation.
producer: news producer, is the program delivered the message.
consumer: consumer news, is that the program accepts messages.
channel: message channels, each client's connection can be established multiple channel, each channel representing a session task.
 
1.2 Message Queue Operation Mechanism:
(1) Message Queuing clients connect to the server to open a channel.
(2) a declaration client exchange, and provision of the relevant property.
(3) The client declares a queue, and set the associated property.
(4) The client uses routing key, establish a good relationship between the exchange and the binding queue.
(5) the delivery message to the client exchange.
After (6) exchange receives the message, according to the message and key binding has been set, a message will be delivered to one or more queue.
Note: After declaring a queue, if it persisted, the next time you do not need to be declared, because the queue is already in the rabbitMQ! ! !
         For example, the following examples are for the first time declared a queue! ! !
 
1.3exchange type:
1.Direct switch
Features: key basis for delivery
Such as setting up routing key for the "hello", then the message is submitted by the client when binding only set the key to "hello" will be delivered to the queue.
2.Topic switch
Features: After key delivery pattern matching, the symbol "#" matches one or more words, the symbol "*" matches a word
For example, "abc. #" Matching "abc.def.ghi", "abc. *" Matches only "abc.def".
3.Fanout switch
Features: no key, take the broadcast mode, when a message comes in, delivered to all queues bound with the switch
 
2. Build Environment
2.1 rabbitmq installed in the windows environment, the tutorial below:
2.2 Installation pika module
python use rabbitmq service, you can use ready-made library pika, txAMQP or py-amqplib, here select the pika.
Use pip commands directly from the command line:
pip install pika

 

3. Sample Test

Examples of message content is transmitted from the send.py to rabbitmq, receive.py send.py receiving information transmitted from rabbitmq.

P represents Produce, meaning producers, may also be referred to as the sender, examples showed send.py;

C represents the consumer, the consumer's meaning, also known as the receiver, examples showed receive.py;

Red indicates that the queue intermediate meaning example showed hello queue.

send.py

Copy the code
!. 1 # / usr / bin / the env to python3 
 2 # - * - Coding: UTF-. 8 - * - 
 . 3 
 . 4 Import the PIKA 
 . 5 Import Random 
 . 6 
 . 7 # new connection, rabbitmq installed locally, hostname is' localhost ' 
 . 8 hostname =' 192.168.1.133 ' 
 . 9 pika.ConnectionParameters Parameters = (hostname) 
10 = Connection pika.BlockingConnection (Parameters) 
. 11 
12 is to create a channel # 
13 is connection.channel channel = () 
14 # declare a queue, a producer and consumer must declare the same queue, is used to prevent the case linked to a party, the other can operate normally 
15 channel.queue_declare (queue = 'Hello') 
16 
. 17 the random.randint Number = (. 1, 1000) 
18 is body = 'Hello World:% S '% Number 
. 19 # switch; queue name, indicate the queue to which the message is sent; message content 
20 # routing_key only needs to be specified when using anonymous switch, which indicates to the transmission queue
21 channel.basic_publish (exchange =' ', routing_key =' hello ',body=body)
22 print " [x] Sent %s" % body
23 connection.close()
Copy the code

receive.py

Copy the code
!. 1 # / usr / bin / the env to python3 
 2 # - * - Coding: UTF-. 8 - * - 
 . 3 
 . 4 Import the PIKA 
 . 5 
 . 6 hostname = '192.168.1.133' 
 . 7 pika.ConnectionParameters Parameters = (hostname) 
 . 8 = Connection the PIKA. the BlockingConnection (Parameters) 
 . 9 
10 create channels # 
. 11 = channel connection.channel () 
12 is channel.queue_declare (Queue = 'Hello') 
13 is 
14 
15 the callback DEF (CH, Method, Properties, body): 
16 Print "[X] Received R & lt% "% (body,) 
. 17 
18 is used rabbitmq # tell callback to receive information 
. 19 channel.basic_consume (callback, queue = 'Hello', NO_ACK = True) 
20 is 
21 is the start # receiving information, and enters the blocking state, there queue information will be called callback processing, exit press ctrl + c
22 print ' [*] Waiting for messages. To exit press CTRL+C'
23 channel.start_consuming()
Copy the code

Let's run send.py send a message:

Run receive.py received message:

1 Introduction

RabbitMQ (Rabbit Message Queue) is a popular open source message queuing system, developed using erlang language.
 
1.1 Description Keywords:
Broker: Message Queue server entity.
Exchange: Message switch that specifies what the message according to the rules, which are routed to a queue.
Queue: vector message queue, each message will be put into one or more queues.
Binding: binding, its role is to exchange routing and queue bind in accordance with the rules.
Routing Key: routing keywords, exchange of messages delivered in accordance with this keyword.
vhost: web hosting, where a broker can offer more vhost, as a different user privilege separation.
producer: news producer, is the program delivered the message.
consumer: consumer news, is that the program accepts messages.
channel: message channels, each client's connection can be established multiple channel, each channel representing a session task.
 
1.2 Message Queue Operation Mechanism:
(1) Message Queuing clients connect to the server to open a channel.
(2) a declaration client exchange, and provision of the relevant property.
(3) The client declares a queue, and set the associated property.
(4) The client uses routing key, establish a good relationship between the exchange and the binding queue.
(5) the delivery message to the client exchange.
After (6) exchange receives the message, according to the message and key binding has been set, a message will be delivered to one or more queue.
Note: After declaring a queue, if it persisted, the next time you do not need to be declared, because the queue is already in the rabbitMQ! ! !
         For example, the following examples are for the first time declared a queue! ! !
 
1.3exchange type:
1.Direct switch
Features: key basis for delivery
Such as setting up routing key for the "hello", then the message is submitted by the client when binding only set the key to "hello" will be delivered to the queue.
2.Topic switch
Features: After key delivery pattern matching, the symbol "#" matches one or more words, the symbol "*" matches a word
For example, "abc. #" Matching "abc.def.ghi", "abc. *" Matches only "abc.def".
3.Fanout switch
Features: no key, take the broadcast mode, when a message comes in, delivered to all queues bound with the switch
 
2. Build Environment
2.1 rabbitmq installed in the windows environment, the tutorial below:
2.2 Installation pika module
python use rabbitmq service, you can use ready-made library pika, txAMQP or py-amqplib, here select the pika.
Use pip commands directly from the command line:
pip install pika

 

3. Sample Test

Examples of message content is transmitted from the send.py to rabbitmq, receive.py send.py receiving information transmitted from rabbitmq.

P represents Produce, meaning producers, may also be referred to as the sender, examples showed send.py;

C represents the consumer, the consumer's meaning, also known as the receiver, examples showed receive.py;

Red indicates that the queue intermediate meaning example showed hello queue.

send.py

Copy the code
!. 1 # / usr / bin / the env to python3 
 2 # - * - Coding: UTF-. 8 - * - 
 . 3 
 . 4 Import the PIKA 
 . 5 Import Random 
 . 6 
 . 7 # new connection, rabbitmq installed locally, hostname is' localhost ' 
 . 8 hostname =' 192.168.1.133 ' 
 . 9 pika.ConnectionParameters Parameters = (hostname) 
10 = Connection pika.BlockingConnection (Parameters) 
. 11 
12 is to create a channel # 
13 is connection.channel channel = () 
14 # declare a queue, a producer and consumer must declare the same queue, is used to prevent the case linked to a party, the other can operate normally 
15 channel.queue_declare (queue = 'Hello') 
16 
. 17 the random.randint Number = (. 1, 1000) 
18 is body = 'Hello World:% S '% Number 
. 19 # switch; queue name, indicate the queue to which the message is sent; message content 
20 # routing_key only needs to be specified when using anonymous switch, which indicates to the transmission queue
21 channel.basic_publish(exchange='', routing_key='hello', body=body)
22 print " [x] Sent %s" % body
23 connection.close()
Copy the code

receive.py

Copy the code
!. 1 # / usr / bin / the env to python3 
 2 # - * - Coding: UTF-. 8 - * - 
 . 3 
 . 4 Import the PIKA 
 . 5 
 . 6 hostname = '192.168.1.133' 
 . 7 pika.ConnectionParameters Parameters = (hostname) 
 . 8 = Connection the PIKA. the BlockingConnection (Parameters) 
 . 9 
10 create channels # 
. 11 = channel connection.channel () 
12 is channel.queue_declare (Queue = 'Hello') 
13 is 
14 
15 the callback DEF (CH, Method, Properties, body): 
16 Print "[X] Received R & lt% "% (body,) 
. 17 
18 is used rabbitmq # tell callback to receive information 
. 19 channel.basic_consume (callback, queue = 'Hello', NO_ACK = True) 
20 is 
21 is the start # receiving information, and enters the blocking state, there queue information will be called callback processing, exit press ctrl + c
22 print ' [*] Waiting for messages. To exit press CTRL+C'
23 channel.start_consuming()
Copy the code

Let's run send.py send a message:

Run receive.py received message:

Guess you like

Origin www.cnblogs.com/lice-blog/p/11617813.html