Understanding amqp, the protocol used by rabbitmq -- Reference

Source: Internet
Author: User
Tags grails rabbitmq

Rabbitmq is a lightweight, reliable, scalable and portable Message Broker. but unlike publish Message Brokers familiar to Java developers, It's not based on JMS. instead, your applications communicate with it via a platform-neutral, wire-level protocol: the Advanced Message Queuing protocol (amqp ). fortunately there's already a Java client library and springsource is working on first class spring and grails integration-so don't worry about having to do low-level stuff to use rabbitmq. you can even find amqp client libraries that expose a JMS interface. but amqp is sufficiently different in operation from JMS that it might cause headaches for Java developers that are used to the JMS model.

In order to cancel the transition, I'll be looking in this post at the basic concepts that underpin amqp along with three common usage scenarios. by the end, you will hopefully have a good enough understanding to configure rabbitmq and use it via the APIS provided by spring and grails.

Exchanges, queues, and bindings

Like any messaging system, amqp is a message protocol that deals with publishers and consumers. the publishers produce the messages, the consumers pick them up and process them. it's the job of the Message Broker (such as rabbitmq) to ensure that the messages from a publisher go to the right consumers. in order to do that, the broker uses two key components: exchanges and queues. the following dimo-shows how they connect a publisher to a consumer:

As you can see, the setup is pretty straightforward. A publisher sends messages to a named exchange and a consumer pulls messages from a queue (or the queue pushes them to the consumer depending on the configuration ). of course, the connections have to be made in the first place, so how do publishers and consumers discover each other? Via the name of the exchange. usually, either the publisher or consumer creates the exchange with a given name and then makes that name public. how that publication happens depends on the circumstances, but one might put it in public API documentation or send it to known clients.

How are the messages routed from the exchange to the queue? Good question. first, the queue has to be attached to the given exchange. typically, a consumer creates a queue and attaches it to an exchange at the same time. second, messages passed ed by the Exchange have to be matched to the queue-a process called "binding ".

To understand binding, it's useful to understand the structure of an amqp message:

The headers and properties of the message are basically key/value pairs. the difference between them is that headers are defined by the amqp specification whereas properties can contain arbitrary, application-specific information. the actual message content is just a sequence of bytes, so if you want to pass text around in your messages, then you shoshould standardise on an encoding. UTF-8 is a good bet. you can specify a content type and encoding in the message headers if you want, but that's apparently not special common.

What does this have to do with binding? One of the standard headers is calledRouting-KeyAnd it is this that the broker uses to match messages to queues. Each queue specifies a "binding key" and if that key matches the value ofRouting-KeyHeader, the queue has es the message.

Things are slightly complicated by the concept of exchange types. The amqp spec. defines the following four types:

Exchange type Behaviour
Direct The binding key must match the routing key exactly-No wildcard support.
Topic Same as direct, but Wildcards are allowed in the binding key. '# 'matches zero or more dot-delimited words and' * 'matches exactly one such word.
Fanout The routing and binding keys are ignored-all published messages go to all bound queues.
Headers  

UpdateI corrected the information on wildcards, which work on the basis of dot-delimited words or terms.

For example, say a publisher sends a message with a routing key of "NYSE" to a topic exchange called "stocks ". if a consumer creates a queue attached to "stocks" with a binding key of "#", "*", or "NYSE ", then that consumer will get the message because all three binding keys match "NYSE ". however, if the message is published to a direct exchange, then the consumer will not get the message if the binding key is "#" or "*" since those characters are treated as literals, not wildcards. interestingly ,"#. # "will also match" NYSE "despite the routing key not having a dot.

Now consider a message with a routing key of "NYSE. Tech. MSFT". What binding keys will match it given that the message is going to a topic exchange?

Binding key Match?
NYSE. Tech. MSFT Yes
# Yes
NYSE .# Yes
*.* No
NYSE .* No
NYSE. Tech .* Yes
NYSE. *. MSFT Yes

That's really all there is to it. flexibility is provided by support for multiple consumers per queue and multiple queues per exchange. in fact, a single queue can even be bound to multiple exchanges. now let's look at some of those scenarios.

RPC

An amqp broker can act as an RPC mechanic between a client and a service. The general setup is like this, using a direct exchange:

The general sequence goes:

    1. Client sends message to the queue, specifying: (a) a routing key that matches the Service; and (B) the name of a queue to pick the response up from.
    2. Exchange passes the message to the Service's Queue ("ops_q" in this case ).
    3. The queue pushes the message to the Service, which then does some work and sends a response message back to the exchange, specifying a routing_key that matches the reply queue.
    4. The client picks the response message off the reply queue.

From the perspective of the client, the call cocould either be blocking or non-blocking. How easy it is to do one or the other, though, depends on the client library in use.

The key to the RPC scenario is making sure that the client and service are using the same exchange for the initial request and that the client knows what to specify for the routing key.

As for the reply queue, it's typically created by the client, which then populatesReply_toHeader appropriately. Also, although you can use a different exchange for the replies compared to the requests, it's much more common to use the same exchange for both requests and replies.

Pub (Lish)/SUB (scribe)

JMS has the concept of topic queues that ensure that messages from a publisher go to all subscribers. You can easily achieve the same behaviour in amqp by binding multiple queues to an exchange like so:

Even better, the queues can filter which messages they receive via the binding key. if a consumer wants to receive all messages, then it can specify a binding key of "#"-the "match any number of words" wildcard. rather confusingly for your average developer, "*" matches zero or one (Dot-delimited) words as mentioned earlier.

Work Distribution

Imagine you have an application that has a bunch of jobs that need executing. with amqp, you can hook up multiple consumers such that each job goes to one, and only one, of those consumers. the publisher doesn' t care which consumer does the work, just that the work is done. this is work distribution.

Grouping it is pretty straightforward, as shown in this digoal:

So you have one queue bound to the exchange with multiple consumers sharing that queue. This setup guarantees that only one consumer processes a given message, no matter how many consumers there are.

Those are the three main usage patterns for amqp brokers. although I have described each individually, it's fairly common to combine them. for example, you cocould have multiple services sharing the same Queue (work distribution) in the RPC pattern. it's really up to you to decide how to configure the exchanges and queues, And now you shoshould have a good enough understanding to work out the appropriate setup for your situation.

 

If you want to go further into amqp, then check out the specification itself, the section on general architecture. And to get started with rabbitmq, just go to its website.

Reference from:

Http://spring.io/blog/2010/06/14/understanding-amqp-the-protocol-used-by-rabbitmq/

Understanding amqp, the protocol used by rabbitmq -- Reference

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.