Get the App
SLTechnology News&Howtos  ›  Servers  › 

RabbitMQ message Distribution Poll

Shulou Source: shulou.com Published: 2022-06-02 08:09:51 10月05日 Update

We first download pika, as well as rabbitMQ, and ir. RabbitMQ is written in ir.

The process for using message queuing is roughly as follows:

(1) the client connects to the message queuing server and opens a channel.

Channel: message channel. In each connection of the client, multiple channel can be established, and each channel represents a session task.

(2) the client declares an exchange and sets the relevant properties.

Exchange: a message switch that specifies the rules by which messages are routed to which queue.

(3) the client declares a queue and sets the relevant properties.

Queue: message queue carrier, where each message is thrown into one or more queues.

(4) the client uses routing key to establish a binding relationship between exchange and queue.

Routing Key: routing keyword based on which exchange delivers messages.

(5) the client delivers messages to exchange.

Exchange: a message switch that specifies the rules by which messages are routed to which queue.

Next, write a producer:

Import pika

Connection = pika.BlockingConnection (

Pika.ConnectionParameters ('localhost'))

Channel = connection.channel () # first create an instance and create a new channel through socket

# declare queue

Channel.queue_declare (queue='hello') # Note here you need to declare a pipeline or queue, where both the client and the server need to start the message.

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.

Channel.basic_publish (exchange=''

Routing_key='hello',#queue name # routing key, which indicates the queue to which the message is sent. In this case, the message is sent to queue pikamq

Body='Hello wordings') # message content

Print ("[x] Sent 'Hello wordings'") # when the producer has sent the message, he can choose to close the connection

Connection.close ()

Consumers:

Import pika

Connection = pika.BlockingConnection (pika.ConnectionParameters ('localhost'))

Channel = connection.channel ()

Channel.queue_declare (queue='hello')

Def callback (ch, method, properties, body):

Print ('-->', ch,method,properties)

Print ("[x] Received r" body)

Channel.basic_consume (callback

Queue='hello'

# no_ack=True # some code has been added here, but the python system will come with it. After the addition, once the waiting time is too long, the message sent by the producer cannot be transferred to another consumer.

)

Channel.start_consuming ()

Tags: Messages queues customers clients routes producers production switches keys keywords simultaneously multiple attributes consumers rules language services consumption and then between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MariaDB OPPO Reno Shulou Information macOS