RabbitMQ messaging middleware core concepts and operational mechanism

First, the concept

RabbitMQ is an open source of the message queue service provided by a software LShift the AMQP (Advanced the Message Queuing Protocol Advanced Message Queuing Protocol) open source implementation of
a robust and high performance can be written in a well-known Erlang stretchability
with high stability and reliability

1, the core concepts:

  • Message(Message) :
    message that is anonymous message header and a message body
    the message body and the message header is opaque by a series of optional attributes
    These attributes include routing-Key (key specified route message transmission destination) and priority (priority relative to other messages) and Delivery-MODE (the message whether persistent storage) attributes

  • Publisher(Publisher) :
    Producers message client application that is a release of information to the switch

  • Exchange(Switch) :
    for messages received and sent by the producer to route the messages in the message queue server
    Exchange There are four types: Direct (default) and fanout and topic and and headers
    policies for different types of messages are forwarded Exchange the difference between the
    direct model can achieve point to point and fanout and topic and publish and subscribe model can be realized headers

  • Queue(Message queue) :
    used to hold messages until the transmission to the consumer
    it is a message of the container is the end of the message may be a message into one or more queues
    messages waits consumer connects to the queue in the queue and which takes go

  • Binding(Binding) :
    used to switch between the message queue and the associated
    routing rule is based on binding to a switch and routing message queue bond connecting the exchanger can thus be appreciated that the routing tables to a binding consisting of
    Exchange queue and binding can be many- a switch can bind multiple queues and a queue and can be bound to multiple switches
    messages sent regardless of the switch to which to bind the queue so long as the message can reach

  • Connection(Network connection) :
    in order to operate the message queue connection established

  • Channel(Channel) :
    to create and destroy a TCP connection is very resource for the operating system and therefore is introduced the concept of channels to multiplex a TCP connection
    channels are multiplexed connections a separate bidirectional data flow path is established in the real TCP connection in the plurality of virtual connections AMQP command is sent through a channel
    regardless of the subscription queue or publish messages received message channels these operations are accomplished by

  • Consumer(Consumers) :
    message consumers represent a client application to get the message from the message queue

  • Virtual Host(Virtual Host) :
    represents the number of switches and message queues related objects
    divided on the RabbitMQ server multiple virtual hosts virtual host is sharing the same stand-alone server domain authentication and encryption environment
    for each Virtual Host (Virtual Host ) which is essentially a mini version of RabbitMQ server each exchange has its own queue and permissions mechanism to bind
    virtual host (virtual host) is the basis for the concept of AMQP must be specified when connecting
    RabbitMQ is used by default virtual host (virtual host) is /
    can specify, for example their / abc / employ / department, etc.

  • Broker(Message broker) :
    represents exactly the message queue server entity

schematic diagram:

Here Insert Picture Description
(Picture does not look good will look at it)


2, operating mechanism:

AMQP message routing:

RabbitMQ messaging service specification using AMQP message routing
AMQP routing process messages and Java developers are familiar with JMS there are some differences
AMQP added to Exchange (the switch) and the Binding (binding) the role of
the producer to post messages to the Exchange message on the message queue and final consumer receives the Binding determined exchanger which should be sent to the queue

Exchange exchanger type:

Differ depending on the type of distribution policy when you distribute news Exchange currently four types: direct , fanout , Topic , headers
because the headers match that header AMQP message instead of routing and direct-key mode switch exactly, but much worse performance It is rarely used and therefore not much into details


Direct Mode:

Here Insert Picture Description
Key message routing (routing key) and if the Binding bindingkey same switch will send a message to the queue corresponding
routing key and the queue name must match exactly If a queue is bound to routing keys as in claim switch "dog" is only forward routing key labeled "dog" is news, not forward "dog.a" does not deliver "dog.b" tag information
it is exact match unicast mode is point to point communication model


Fanout Mode:

Here Insert Picture Description
Each hair switch types to fanout messages are assigned to all queues bound up
fanout switch does not process routing keys simply bound the queues to be sent to each switch of the switch will be forwarded to the message the exchange is bound to all the queues
which is broadcast mode speed forwarding of messages is the fastest mode is JMS publish-subscribe model to achieve the
hosts in the subnet broadcast much like each subnet have received a copy of the message


Topic mode:

Here Insert Picture Description
by topic switch pattern to match the routing key property allocation message routing key and a pattern matching at this time queue need to bind to a pattern
string routing keys and will cut into the key bindings between words those words a point spaced from
and identifies wildcard: the symbol * and # symbols
#representatives matches zero or more words * representative of matching a word
Note: a word level matching


Second, the test

First installation of Docker container RabbitMQ

# 拉取带管理界面的镜像
docker pull docker.mirrors.ustc.edu.cn/library/rabbitmq:3-management

NOTE: The next version of the band management is managed interface

# 启动容器并开放5672和15672端口
docker run -d -p 5672:5672 -p 15672:15672 --name myrabbitmq 479479d8e188 

5672Port and RabbitMQ client communications 15672is port management interface

Browser access ip: 15672 to enter the management interface default account password are guest

Management Interface:
Here Insert Picture Description

Then create three switch (exchange) for testing

Named exchange.direct , exchange.fanout and exchange.topic
are used for direct type, fanout type and topic type exchanger
Here Insert Picture Description
Here Insert Picture Description
addition was complete

Add four message queue for testing (Queue)

Namely: zjitc , zjitc.news , zjitc.emps , ciczjitc.news
Here Insert Picture Description

To be able to work must be added after the completion of binding (binding) and the switch

Binding direct type of Exchange:
Here Insert Picture Description
Here Insert Picture Description
Binding completed

Then bind fanout type of Exchange:
Here Insert Picture Description

Finally, to bind the topic type of Exchange:
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Binding completed

Then you can send a test message

Direct test switch

Send a message:
Here Insert Picture Description

After the transmission is completed there will be tips:
Here Insert Picture Description

Since the direct exchange is fully matched so that only the full name of the queue to receive the message: Here Insert Picture Description
successfully received the message:
Here Insert Picture Description


Test fanout switch

Send a message:
Here Insert Picture Description
After sending is complete there will be prompt:
Here Insert Picture Description

All have received the message queue: Here Insert Picture Description
successfully received the message:
Here Insert Picture Description


Test topic Switch

Test 1--

Send a message:
Here Insert Picture Description
After sending is complete there will be prompt:
Here Insert Picture Description
Here Insert Picture Description
because the front and rear so all can match the queue have received the message:
Here Insert Picture Description

Test 2 -

Send message:
Here Insert Picture Description
only match to the queue as a suffix news:
Here Insert Picture Description
at the end of the queue news received message:
Here Insert Picture Description


Published 174 original articles · won praise 5 · Views 240,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/105205789