Introduction
MQ is all called the message queue, and Message Queuing (MQ) is an application-to-application communication method. Applications communicate by reading and writing messages to and from the queue (data for the application), without requiring a dedicated connection to link them. Message passing refers to the process of communicating between programs by sending data in a message, rather than by directly invoking each other, and directly invoking techniques such as remote procedure calls. Queuing refers to an application communicating through a queue. The use of queues removes the requirement that both the receiving and sending applications execute concurrently. Some of the more mature MQ products are IBM WEBSPHERE MQ and so on.
MQ Features
MQ is a typical representative of the consumer-producer model, where one end writes messages to the message queue, while the other end reads or subscribes to messages in the queue. MQ is similar to JMS, but the difference is that JMS is a standard and API definition for the Sun Java Messaging Middleware Service, and MQ follows the specific implementations and products of the AMQP protocol.
Usage Scenarios
In the project, some operations without immediate return and time-consuming are extracted and processed asynchronously, which greatly saves the request response time of the server and improves the throughput of the system.
Meaning
RABBITMQ is a popular open source Message Queuing system, developed in Erlang language. RABBITMQ is a reusable enterprise messaging system that is done on an AMQP basis. He follows the Mozilla Public License open source Agreement,
RABIITMQ Working principle Diagram
Concept:
? Brocker: Message Queuing server entity.
? Exchange: A message switch that specifies what rules the message is routed to and to which queue.
? Queue: Message Queuing, each message will be put into one or more queues.
? Binding: Bind, which is the role of the exchange and queue according to the routing rules binding together.
? Routing key: The routing keyword, exchange messages are delivered based on this keyword.
? Vhost: Virtual host, a broker can open multiple Vhost, as a separate user-free permissions.
? Producer: The message producer is the program that delivers the message.
? Consumer: The message consumer is the program that receives the message.
? Channel: The message channels, in each connection of the client, multiple channels can be established, each channel represents a session task.
The use of Message Queuing is probably as follows:
(1) The client connects to the Message Queuing server and opens a channel.
(2) The client declares an exchange and sets the related properties.
(3) The client declares a queue and sets the related properties.
(4) The client uses routing key to establish a good binding relationship between Exchange and queue.
(5) Clients post messages to exchange.
When Exchange receives a message, it routes messages to one or more queues based on the key of the message and the binding that has been set.
There are several types of exchange, which are called direct switches that are delivered entirely according to key, for example, when the routing key is set to "ABC", the client submits a message that only the key "ABC" is set to be posted to the queue. When the key is matched with a pattern, it is called a topic switch, and the symbol "#" matches one or more words, and the symbol "" matches exactly one word. For example "abc.#" matches "Abc.def.ghi", "ABC." Matches only "Abc.def". There is also a need for a key, called the fanout Switch, which takes broadcast mode, when a message comes in, it is posted to all queues that are bound to the switch.
RABBITMQ supports the persistence of messages, that is, data is written on disk, and for data security reasons, I think most users will choose to persist. Message Queuing persistence consists of 3 parts:
(1) Exchange persistence, specifying durable + 1 at the time of declaration
(2) Queue persistence, specify durable when declaring = 1
(3) Message persistence, specifying Delivery_mode = 2 (1 non-persistent) on delivery
If both Exchange and queue are persisted, then the binding between them is persistent. If there is a persistence between Exchange and queue, a non-persisted, binding is not allowed.
RABBITMQ cluster nodes include memory nodes, disk nodes. So the lab environment prepares three servers, one using disk mode and two using memory mode.
RABBITMQ Cluster Specific configuration:
Host name |
IP Address |
Use |
Operating System |
Mq01 |
192.168.195.147 |
Disk node |
CentOS7 |
Mq02 |
192.168.195.160 |
Memory nodes |
CentOS7 |
Mq03 |
192.168.195.159 |
Memory nodes |
CentOS7 |
Note: There are three servers connected to the Internet and installing packages. In addition, the RABBITMQ cluster node must be in the same network segment.
RABBITMQ installation and deployment,
First we need to configure three nodes of the Hosts file, the following content is added to three servers respectively.
Vim/etc/hosts
192.168.195.147 mq01//Note do not bring "."
192.168.195.160 mq02
192.168.195.159 mq03
and modify the host name of the three node servers separately
Vim/etc/hostname
Mq01.localdomain//The other two units are mq02,mq03
Restart the host for the host name to take effect
Init 6
Shut down the experimental host's firewall and SELinux function (otherwise it will affect the experimental effect) after rebooting
Service Firewalld Stop
Setenforce 0
Three nodes install RABBITMQ software with Yum
Yum install-y epel-release//installation Epel source
Yum install-y rabbitmq-server//install RABBITMQ software
Ln-s/usr/lib/rabbitmq/bin/*/usr/bin///Create a link that enables the system to recognize RABBITMQ commands
Rabbitmq-plugins list//view plugin installation status
Rabbitmq-plugins enable rabbitmq_management//enabling Rabbitmq_management Services
Systemctl start Rabbitmq-server.service//Open RABBITMQ Service
Netstat-ntap | grep 5672//with three ports open instructions OK
Where 15672,25672 are the RABBITMQ management ports,
5672 is the port of communication with producers and consumers
Rabbitmqctl cluster_status//Check three cluster status, currently independent of each other, did not form a cluster.
RABBITMQ clusters are dependent on Erlang's clusters to work, so you must first build a clustered environment of Erlang, in the Erlang cluster, each node is implemented by a Magic.cookie, this cookie is stored in the/var/lib/ In Rabbitmq/.erlang.cookie, the file is 400 permissions. Therefore, it is necessary to ensure that each node cookie is consistent, otherwise the nodes cannot communicate.
Now three nodes of the RABBITMQ service are open, However, the values in the. erlang.cookie file for each node are different, so we need to stop the node's RABBITMQ service, copy the. Erlang.cookie value on the disk node and save it to the. erlang.cookie file on two memory nodes, and start the R of the two memory nodes again ABBITMQ services and add them to a cluster.
Service Rabbitmq-server stop//stops three servers
Vi/var/lib/rabbitmq/.erlang.cookie//Three values are configured to replicate the contents of the disk nodes to the other two
Disk Node Host MQ01
Memory Node mq02
Memory Node mq03
Open three-node RABBITMQ service
Service Rabbitmq-server Start
Connect the memory node mq02,mq03 to the disk node mq01
On the Memory node mq02,mq03, first stop the RABBITMQ application, then call the cluster command, compose the cluster, and finally start the RABBITMQ app
Rabbitmqctl Stop_app
Rabbitmqctl join_cluster--ram [email protected]//Join to disk node--ram add as Memory node at least one disk node exists in the cluster
Rabbitmqctl Start_app
Rabbitmqctl cluster_status//Verify cluster status
Open a browser into web page management
Input: http://192.168.195.147:15672/
Default User name: Guest Password: Guest
Add Policy
Name: Policy names
Pattern: Matching Rules
Definition; use all in Ha-mode mode, which is to synchronize all matching queues.
Priority: Precedence, default is 0, the higher the value, the greater the priority.
Finally click on the Add Policy button so that you have one more strategy
The following simple increase in Message Queuing
Name: Queue names
Durability: Whether the queue is persisted (durable is supported)
Auto Delete: Auto Delete
Arguments: type of policy used
Last click on the Add Queue button, the added queues will appear
Create a new BB queue. Arguments items are not filled in.
View Queue messages
Create a new message in the AA and BB queues
You can see the message queue you just added
The management of the RABBITMQ Service Web page is described here.
RabbitMQ (Message Queuing) cluster configuration and Usage Chapter