RabbitMQ Getting Started Guide

1 MQ Introduction

Message middleware efficient use reliable messaging platform-independent mechanism for data exchange and to integrate the data communication system based on distributed. By providing messaging and message queuing model, it can extend communication between processes in a distributed environment. For messaging middleware, common roles will have roughly Producer (producer), Consumer (consumer).

Common messaging middleware products:

1). ActiveMQ

Apache ActiveMQ is produced, most popular, strong ability to open source message bus. ActiveMQ is a fully supported JMS1.1 specification and J2EE 1.4 JMS Provider implementation. We introduce the use ActiveMQ in this course.

2). RabbitMQ

Leading AMQP protocol implementation, support a variety of scenarios. Internal MySQL Cluster Taobao have to use it to communicate,

Communication component OpenStack open source cloud platform, was first used in the financial industry.

3). ZeroMQ

Fastest in the history of the message queue system

4). Kafka

A subproject of Apache. Features: high throughput, on a common server may be reached 10W / s

The throughput rate; fully distributed system. Suitable for handling massive amounts of data

2 MQ role

1) Decoupling: middleware producers just to send a message, consumers can get messages from the queue, which can consume in order to achieve the decoupling of business.

2) redundant storage: In some cases, the data processing process will fail. Messaging middleware data can be persistent until they have been fully processed, by this way to avoid the risk of data loss. Before the message is deleted from the message middleware, you need treatment system clearly indicates that the message has been processed to ensure that your data is safely stored until you are finished using.

3) Recoverability: when a portion of the system component failure, does not affect the entire system. Messaging middleware reduces the degree of inter-draft process, so even if a process of processing the message hang up, added messaging middleware messages can still be processed after the system recovery.

4) order to ensure that: In most usage scenarios, the order data processing is very important, most messaging middleware to support the order of a certain degree.

5) Buffer: any significant system, there will need different processing time element. Messaging middleware through a buffer layer to help perform the task most efficiently, written messaging middleware processing will be as fast as possible.

6) asynchronous communication: in many cases do not want to use does not need to immediately process the message. Messaging middleware provides asynchronous processing mechanism that allows applications to put some message into the messaging middleware, but does not deal with it immediately when needed and then slowly after treatment.

3 RabbitMQ installation and startup

3.1 Installation depends environment

rpm -ivh erlang-20.3.8.6-1.el6.x86_64.rpm

yum -y install epel-release

yum -y install socat

3.2 Installation rabbitMQ

rpm -ivh rabbitmq-server-3.7.7-1.el6.noarch.rpm

3.3 Add User

Management interface can only be accessed in the native Linux system by default, if you want other hosts can access, you need to configure:

rabbitmq-plugins enable rabbitmq_management

Add user access:

rabbitmqctl add_user admin admin

3.4 RabbitMQ start / stop

Start: service rabbitmq-server start

Stop: service rabbitmq-server stop

View status: service rabbitmq-server status

4 Rabbit MQ management interface access

4.1 Overview Summary

This section is a summary of the main display MQ information, such as the number of messages, Connection, Channel, Exchange, Queue, Consumer's.

RabbitMQ Guide

 

4.2 Exchange Switch

The main display section is currently under virtual host switch, you can also add a new switch here, and configuration rules attribute corresponding exchanger.

RabbitMQ Guide

 

4.3 Queues Queue

The columns show the message queue, each queue which has summary information, may be added to a queue in this section

Queue

RabbitMQ Guide

 

4.4 Admin System Management

This column shows the user management information, including a list of the user's display, add users, add information to web hosting,

RabbitMQ Guide

 

5 RabbitMQ related concepts

5.1 of producers and consumers

5.1.1 Producers

Producer: the producer, is one of the delivery of the message.

Producers create a message, and then posted to the RabbitMQ. Message generally comprise two parts: message body and the label (Label). Message body may also be referred to as payload, in practical application, the message body is typically a data structure with business logic, such as a JSON string. Of course, the message body may be further serialize operations. Tag message used to describe this message, such as a name and a switch routing keys. The news producers referred RabbitMQ, will send a message to consumers under the label after RabbitMQ interest (Consumer).

5.1.2 Consumers

Consumer: Consumer is the party that receives the message.

Consumers connect to RabbitMQ server, and subscribe to the queue. When consumer spending a message, but the message body consumes messages (payload). In the process of message routing, the label will discard the message, the message is stored in the queue of the message body only, consumers will only consume the body of the message, the message also does not know who the producer is, of course, consumers do not need to know.

5.2 queue

Queue: Queue is RabbitMQ internal object, for storing messages.

RabbitMQ Guide

 

5.3 switches, routing key bindings

5.3.1 Switch

Exchange: switch. In the figure above we do can be understood as the producer delivers messages to the queue, in fact this does not happen in the RabbitMQ. The truth is, the producer sends a message to the Exchange (switch), the switch routes the message to one or more queues. If the route is less than might be returned to the producer, and perhaps discarded. Here it can RabbitMQ exchange is seen as a simple entity.

RabbitMQ Guide

 

RabbitMQ exchange has four types, four types are fanout, direct, topic, headers, different types have different routing strategies.

5.3.2 routing keys

RoutingKey: routing keys.

When the producers message to the switch, usually assigns a RoutingKey, it is used to specify the message routing rules, and this RoutingKey need to exchange types and key bindings (BindingKey) used in combination in order to ultimately take effect.

In the case of a fixed type and binding key switch (bindingKey), when the producer may send a message to the switch, designated by the message flow where RoutingKey determined.

5.3.3 Binding

Binding: binding. RabbitMQ bound by the exchanger associated with the queue, you will typically specify a binding key (bindingKey) when bound, so RabbitMQ know how to route messages to the correct queue.

5.4 exchanger type

1) .fanout: it will all be sent to the message routing switch all bound to the switch queue.

2) .direct: This type of switch is very simple routing rules, it will route the message to those

BindingKey and RoutingKey exact match queue.

3) .topic: earlier mentioned direct type of switch routing rules are exact match BindingKey and RoutingKey, but this strict matching mode can not meet the actual needs of the business in many cases. topic type switch in the matching rule extended, it is similar to direct exchanger type, but also route messages to and RoutingKey BindingKey match queue, but here somewhat different matching rules, it conventions:

RoutingKey a split dot string, such as ".": Com.itcast.client, com.itheima.exam.

BindingKey and RoutingKey the same number of points also. "" String segmentation.

BindingKey may be present in two special character string "*" and "#", for fuzzy matching, wherein

"#" Is used to match a word, "*" is used to match a plurality of single (which may be zero).

4) headers:. This type of switch is not dependent on the matching rule to route message routing keys, but according to the match in the content transmission message headers property.

Message sent by the producer 6

6.1 queue bindings

6.1.1 Creating queue

Create a queue, specify the queue name in RabbitMQ background management interface.

RabbitMQ Guide

 

6.1.2 Creating exchangers Exchange

RabbitMQ is created in the admin interface in a switch, specify the name of the switch, and the switch type specified.

RabbitMQ Guide

 

6.1.3 bound to the queue and the switch

In the list for the switch corresponding to switch into the binding interface, specifies the name of the queue Queue, designated RoutingKey, to bind the queue through which the exchanger Exchange RoutingKey.

RabbitMQ Guide

 

Thereafter, when sending a message, specifying the Exchange, and RoutingKey, the message will be routed to the queue in the queue.

RabbitMQ Guide

 

6.2 Message transmission logic code

6.2.1 introduced dependence

RabbitMQ Guide

 

6.2.2  Send Message

RabbitMQ Guide

 

RabbitMQ Guide

 

6.3  to send a message monitoring platform

RabbitMQ Guide

 

 

7 consumer acceptance message

7.1  introduced dependence

RabbitMQ Guide

 

7.2  receiving a message

RabbitMQ Guide

 

RabbitMQ Guide

 

7.3  Result Output

RabbitMQ Guide

 

among them:

consumerTag: message consumer label

properties: the header information of the data message content

envelope: the packet of the message body, which contains the specified when sending the message exchange, routingKey information.

Published 682 original articles · won praise 1391 · Views 1.71 million +

Guess you like

Origin blog.csdn.net/itcast_cn/article/details/104865695