RabbitMQ Operation Process

Source: Internet
Author: User
Tags rabbitmq

In the initial state, when the producer sends the message

(1) The producer connects to Rabbitmq Broker, establishes a connection (Connection), and opens a channel

(2) The producer declares a switch and sets related properties such as switch type, whether persistent, etc.

(3) Producers declare a queue well set related attributes, such as whether exclusive, persistent, automatic deletion, etc.

(4) The producer binds the exchanger and the queue through a routing key.

(5) Producer sends message to RABBITMQ Broker, which contains routing keys, switches and other information

(6) The corresponding switch locates the matching queue according to the received routing key.

(7) If found, the message sent from the producer is stored in the appropriate queue.

(8) If not found, select discard or fallback to producer based on the attributes of the producer configuration

(9) Close the channel.

(1 0) Close the connection.

The process by which a consumer receives a message:

(1) The consumer connects to RABBITMQ Broker, establishes a connection (Connection), and opens a channel.

(2) The consumer requests the message from the corresponding queue to the RABBITMQ Broker, and the corresponding callback function may be set.
And do some preparatory work.

(3) Wait for the RABBITMQ Broker to respond and deliver the message in the corresponding queue, and the consumer receives the message.

(4) The message received by the consumer acknowledgement (ACK).

(5) RabbitMQ Delete the corresponding confirmed message from the queue.

(6) Close the channel.

(7) Close the connection.

As shown in 2-9, we have introduced two new concepts: Connection and channel. We know whether the production
Or consumers, all need to establish a connection with RABBITMQ Broker, a connection that is a TCP connection,
Connection. Once the TCP connection is established, the client can then create an AMQP channel, each
Each channel will be assigned a unique d. Channels are virtual connections built on connection, RabbitMQ processing
Each AMQP instruction is done through the channel.

We can use connection directly to complete the work of the channel, why do we have to introduce the channel? Just think of it.
A scenario where a number of threads in an application need to consume messages from RABBITMQ, or produce messages, that
It is necessary to build many connection, that is, a lot of TCP connections. For the operating system, however, establishing and
Destroying a TCP connection is a very expensive overhead, and if you are experiencing spikes in usage, performance bottlenecks will also appear. RabbitMQ uses
Similar to NiO ' (non-blocking 1/0), the choice of TCP connectivity multiplexing not only reduces performance overhead but also
Easy to manage.

NIO ', also known as non-blocking UO, contains three core portions of channel (channels), buffer (buffers), and selector (selectors). NIO is based on channel and
Buffer, the data is always read from the channel to the buffer, or from the buffer to the channel. The Selector is used to listen for multiple channels of events (than
such as connection open, data arrival, etc.). Therefore, a single thread can listen to multiple channels of data. There is a well-known reactor model in NIO, and interested readers can
In-depth research.

Each thread holds one channel, so the channel multiplexed the connection TCP connection. At the same time RABBITMQ can ensure
The privacy of each thread, as if it had a separate connection. When the traffic per channel is not very large, multiplexing a single
Connection can effectively conserve TCP connection resources in the event of a performance bottleneck. But when the flow of the channel itself is
Large, this time multiple channels multiplexing a connection will produce a performance bottleneck, which in turn limits the overall flow of traffic.
At this point, you need to open up multiple connection to divide these channels into these connection, as for these related tuning
The strategy needs to be adjusted according to the actual situation of the business itself.

The channel is a very important concept in AMQP, and most operations are carried out at the channel level. In the Code listing
Some clues can be seen in 1-1, such as Chan ne L. Exchangedeclare, channel. Queuedeclare,
Channel. Basicpublish and channel. Basicconsume and other methods. RabbitMQ related APIs vs. amqp
Closely connected, such as the basic.publish command that channel.basicpublish corresponds to AMQP.

RabbitMQ Operation Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.