AMQP Protocol Learning

Source: Internet
Author: User

Refer to this: http://kb.cnblogs.com/page/73759/

Well written.

The AMQP protocol is a binary protocol that provides asynchronous, secure, and efficient interaction between client applications and messaging middleware. Overall, the AMQP protocol can be divided into three tiers:

This layered architecture is similar to the OSI network protocol, which replaces the implementation of each layer without affecting interaction with other layers . AMQP defines the appropriate server-side domain model for standardizing server behavior (AMQP server-side can be called Broker). Here the model layer determines the behavior generated by these basic domain models, which are represented in AMQP with "command" , which in the following article focuses on analyzing these domain models. The session layer defines the communication between the client and the broker (both sides of the communication are a peer, which can be called partner), providing a guarantee for the reliable transmission of the command . The transport layer focuses on data transfer and interacts with the session, accepting data from the upper layer, assembling it into a binary stream, transmitting it to receiver, and then parsing the data for delivery to the session layer. The session layer needs the transport layer to complete the network abnormal Situation report, the order sends the command and so on work.

The above is a rough description of the AMQP protocol. The domain model provided by AMQP is understood below with our requirements for the messaging service. The main function of message middleware is message routing (Routing) and caching (buffering). two domain models that provide similar functionality in AMQP : Exchange and Message queue.

The messages sent by the Exchange Receive message producer (message Producer) send messages to the message queue according to different routing algorithms. The message queue caches these messages when the message is not consumed normally, and the specific cache policy is determined by the implementation, and when the connection between the message queue and the messaging consumer (messages consumer) is unobstructed, the message The queue has the responsibility to forward the message to consumer .

(Note: The code example can look at the example of the http://www.cnblogs.com/charlesblc/p/5516585.html at the back of the PYAMQP)

You can also look at http://www.cnblogs.com/charlesblc/p/6286900.html, which is ready to do an in-depth study of AMQP models.

The message is the basic unit manipulated in the current model, generated by producer and consumed by the broker by consumer. Its basic structure has two parts: header and body. The header is a collection of various properties that are added by producer, which have control over whether the message can be cached, which queue is received, what the priority is, and so on. The body is the data that really needs to be transmitted, which is a binary stream of data that is not visible to the broker and should not be affected during transmission.

There will be more than one message in a broker Queue,exchange how do you know which message queue to send messages to? This is the role of the binding in the show. The creation of the message queue is controlled by client application and needs to be determined after the message queue is created to receive and save the results of which exchange route. A binding is a domain model that is used to correlate exchange with message queue. The Client application controls the association of exchange with a specific message queue and binds the condition of which message the queue accepts to exchange, which is also called the binding key or criteria .

After associating with multiple message queues,there is a routing table in exchange that stores the restrictions on the messages required for each message queue. Exchange checks the header and body information of each message it receives to determine which queue the message is routed to. The header of the message should have a property called Routing Key, which is generated by the message sender and provides the standard for Exchange to route this message. Exchange has a different Exchange Type depending on the routing algorithm. For example, if you have direct, you need a binding key equal to routing key,and a binding key that conforms to a schema relationship with routing key, as well as some of the attributes contained in the message. Some of the underlying routing algorithms are provided by AMQP, andclient application can customize a variety of its own extended routing algorithms . Then the process of a message is similar to this:

Here's a term you need to introduce: Virtual Host. a virtual host can hold some Exchange and message queue. It is a virtual concept, and a VM can be a server or a cluster of multiple servers . Under synchronous extensions, the Exchange and message queue deployments can also be on one or more servers.

the creator and consumer of the message may be the same application . The entire AMQP defines the interaction between the client application and the broker. After a rough introduction of the AMQP domain model, you can focus on how the client is connected to the broker.

In AMQP, the Client application wants to communicate with the broker and needs to establish a connection with the broker, which is actually associated with virtual host, in other words, The connection is built between the client and virtual host. multiple channel runs can be run concurrently on a connection, each channel performs communication with the broker , and the session we provide above is attached to the channel . (Note: See the previous PYAMQP code can also be seen, after the establishment of connection is necessary to establish the channel)

The session here can be defined in many ways , either as a command distribution mechanism provided by AMQP, or as a macro-differentiated interface to the domain model. Normal understanding is what we usually call the interactive context, the main role is to reliably pass each command on the network. In the design of AMQP, it should be a reference to the various designs of TCP to ensure this reliability.

At the session level, a unique identifier (which can be a UUID) is assigned to each command that the upper layer needs to interact with, so that the command can be verified and retransmitted during the transfer. The command sender also needs to record each sent command to replay Buffer in order to receive feedback from the receiving party, to ensure that the command is explicitly received by the receiver or has executed the command.

The sender is re-routed again for a command that does not receive feedback for timeouts . If the receiving party has explicitly returned the message and wants to inform the command sender but the message is lost in the middle or other issues have not been received by the sender, then the sender will have an impact on the receiver , in order to reduce this effect, the command receiver set a  The filter idempotency Barrier to intercept the command that has been received. For this retransmission and confirmation mechanism, you can refer to the related design of TCP.

The above outlines the domain model of AMQP and the confirmation and retransmission model in the connection mechanism, and does not involve the verbose binary specification of AMQP.

AMQP Protocol Learning

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.