Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

RabbitMQ message Distribution Poll

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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 ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report