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

How to use RabbitMQ

2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how to use RabbitMQ. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preface

Let's first take a look at the flow process of a message in RabbitMQ.

The main process shown is as follows

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

The producer specifies the RoutingKey when sending the message, and then the message is sent to Exchange

Exchange routes messages to the specified queue according to some column rules

Consumers consume messages from queues

There are mainly four participants in the whole process, message,exchange,queue,consumer. Let's get to know these four participants.

Message

Messages can set some column properties, and the function of each property can be found in the book "in-depth RabbitMQ"

Exchange

Receive the message and forward the message to the bound queue according to the routing key. The common properties are as follows

The type attribute is most commonly used. The type attribute is explained in detail below.

Fanout Exchange

Messages sent to the switch are routed to all queues bound to the switch and can be used for broadcasting

Instead of dealing with routing keys, simply bind the queue to the switch

Fanout switches are the fastest to forward messages.

Direct Exchage

Route messages to queues that match BindingKey and RoutingKey exactly

Topic Exchange

As mentioned earlier, the switch routing rules of type direct exactly match RoutingKey and BindingKey. Topic is similar to direct in that messages are sent to queues that match RoutingKey and BindingKey, except that they can be vaguely matched.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

RoutinKey is a quilt. A string separated by a number (such as com.rabbitmq.client)

BindingKey and RoutingKey are also "." A string separated by a number.

There can be two special strings "*" and "#" in BindKey for fuzzy matching, in which "*" is used to match no more than one word, and "#" is used to match multiple words (including 0 and 1).

If there are now two messages with RoutingKey java.lang and java.util.concurrent, java.lang will be routed to Consumer1 and Consumer2,java.util.concurrent will be routed to Consumer2.

Headers Exchange

A switch of type headers does not rely on the matching rules of the routing key to route the message, but matches according to the headers attribute in the content of the message sent. Headers-type switches are poor in performance, impractical, and basically unusable.

Queue

Common properties of queues are as follows

Common parameters for queues that can be set in arguments are as follows

Rabbitmq-api (use of rabbitmq api)

Chapter_1: get started quickly, handwriting a RabbitMQ producer and consumer

Chapter_2: demonstrates the use of various exchange

Let's review the various exchange machine routing rules mentioned above

Chapter_3: pull messages

There are two ways to get a message

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Pull message (get message)

Push message (consume message)

So should we pull messages or push messages? get is a polling model, while consumer is a push model. The get model results in the overhead of synchronizing communication with the RabbitMQ for each message, which consists of the client application that sends the request frame and the RabbitMQ that sends the reply. So push messages and avoid pulling

Chapter_4: manual ack

There are two ways to confirm a message.

Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community

Automatic confirmation (autoAck=true)

Manual confirmation (autoAck=false)

When consumers consume messages, they can specify the autoAck parameter

String basicConsume (String queue, boolean autoAck, Consumer callback)

AutoAck=false: RabbitMQ waits for the consumer to display a reply confirmation message before removing the message from memory (or disk)

AutoAck=true: RabbitMQ automatically sets the messages sent as acknowledgements and then deletes them from memory (or disk), regardless of whether the consumer actually consumes the messages

The method of manual confirmation is as follows, with 2 parameters

BasicAck (long deliveryTag, boolean multiple)

DeliveryTag: used to identify messages delivered in the channel. When RabbitMQ pushes a message to Consumer, it comes with a deliveryTag so that Consumer can tell RabbitMQ which message has been acknowledged when the message is confirmed.

RabbitMQ guarantees that the deliveryTag of each message is incremented from 1 in each channel.

Multiple=true: message id

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

Internet Technology

Wechat

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

12
Report