Understand the important concept and installation of rabbitmq
An introduction to rabbitmq
This part refers to Chapter 1 and Chapter 2 of “rabbitmq practical guide”.
1.1 introduction to rabbitmq
Rabbitmq is a message middleware that uses Erlang language to implement AMQP (Advanced message queuing protocol). It originated from the financial system and is used to store and forward messages in the distributed system.
With the development of rabbitmq, it is recognized by more and more people, which is inseparable from its outstanding performance in ease of use, scalability, reliability and high availability. The specific characteristics of rabbitmq can be summarized as follows:
- Reliability:Rabbitmq uses some mechanisms to ensure the reliability of messages, such as persistence, transmission confirmation and release confirmation.
- Flexible routing:The message is routed through the switch before it enters the queue. For typical routing functions, rabbitmq has provided some built-in switches. For more complex routing functions, multiple switches can be bound together, or plug-in mechanism can be used to implement their own switches. This will be introduced in detail later when we introduce the core concept of rabbitmq.
- Scalability:Multiple rabbitmq nodes can form a cluster, or dynamically expand the nodes in the cluster according to the actual business situation.
- High availability:The queue can be mirrored on the machines in the cluster, so that the queue can still be used when some nodes have problems.
- Support multiple protocols:Rabbitmq supports not only AMQP protocol, but also stomp, mqtt and other message middleware protocols.
- Easy to use management interface:Rabbitmq provides an easy-to-use user interface that enables users to monitor and manage messages, nodes in the cluster, etc. It will be introduced when installing rabbitmq that rabbitmq will bring its own management interface.
- Plug in mechanism:Rabbitmq provides many plug-ins to extend in many ways. Of course, you can also write your own plug-ins. It seems that this is a bit similar to Dubbo’s SPI mechanism.
1.2 core concepts of rabbitmq
Rabbitmq is a producer and consumer model, which is mainly responsible for receiving, storing and forwarding messages. Think of the process of message delivery as: when you send a package to the post office, the post office will store it temporarily and finally send it to the recipient through the postman. Rabbitmq is like a system composed of post office, mailbox and postman. In terms of computer terminology, rabbitmq model is more like a switch model.
Let’s take a look at Figure 1, the overall model architecture of rabbitmq.
Now I’ll introduce some of the concepts in the figure above.
1.2.1 producer and consumer
- Producer (producer): the party that produces the message (the mail sender)
- Consumer (consumer): the party consuming the message (mail recipient)
The message generally consists of two partsMessage header(or label) andMessage body。 The message body can also be called payload. The message body is opaque, while the message header consists of a series of optional attributes, including routing key, priority, delivery mode, etc. After the producer hands over the message to rabbitmq, rabbitmq will send the message to the interested consumers (consumers) according to the message header.
In rabbitmq, messages are not delivered directly to theQueue (message queue)In the middle, we have to go through the middleExchange (exchange)This layer,Exchange (exchange)Will assign our messages to the correspondingQueue (message queue)In the middle.
Exchange (exchange)It is used to receive messages sent by the producer and route them to the queue in the server. If not, it may be returned to the serverProducer (producer)It might be thrown away. Here you can think of the switch in rabbitmq as a simple entity.
There are four types of exchange in rabbitmq, and different types correspond to different routing policies：Direct (default)，fanout, topic, andheadersDifferent types of exchange forward messages with different strategies. This meeting will introduceExchange typesI introduced it when I was young.
The schematic diagram of exchange is as follows:
When a producer sends a message to a switch, it usually specifies aRoutingkey, which specifies the routing rules for this message, and thisRoutingkey needs to be used in combination with switch type and bindingkey to be effective。
RabbitmqBinding (binding)WillExchange (exchange)AndQueue (message queue)When binding, you usually specify oneBindingkey (to be built)In this way, rabbitmq knows how to route messages to the queue correctly, as shown in the figure below. A binding is a routing rule that connects the switch and message queue based on the routing key, so the switch can be understood as a routing table composed of binding. The binding between exchange and queue can be many to many.
When a producer sends a message to a switch, it needs a routingkey. When the bindingkey matches the routingkey, the message will be routed to the corresponding queue. When binding multiple queues to the same switch, these bindings allow the use of the same bindingkey. Bindingkey does not work in all cases. It depends on the switch type. For example, fanout switches will ignore it and route messages to all queues bound to the switch.
Queue (message queue)Used to save messages until they are sent to consumers. It is the container and destination of the message. A message can be put into one or more queues. The message is always in the queue, waiting for the consumer to connect to the queue and pick it up.
RabbitMQMessages can only be stored inqueueThis is not the same as that in ChinaKafkaThis kind of message middleware is the opposite. Kafka stores messages inTopicThis logic level, and the corresponding queue logic is only the displacement identifier in the actual storage file of topic. The producer of rabbitmq produces messages and finally delivers them to the queue. Consumers can get messages from the queue and consume them.
Multiple consumers can subscribe to the same queueIn this case, the messages in the queue will be evenly allocated (round robin, that is, polling) to multiple consumers for processing, instead of each consumer receiving all the messages and processing them, so as to avoid repeated consumption of messages.
RabbitMQIt does not support the broadcast consumption at the queue level. If there is a demand for broadcast consumption, it needs to carry out secondary development on it, which will be very troublesome. It is not recommended to do so.
1.2.4 broker (service node of message middleware)
For rabbitmq, a rabbitmq broker can be simply regarded as a rabbitmq service node or a rabbitmq service instance. In most cases, a rabbitmq broker can also be regarded as a rabbitmq server.
The following figure shows the whole process of producers storing messages into rabbitmq broker and consumers consuming data from the broker.
In this way, we have finished introducing some basic concepts about rabbitmq in Figure 1. Let’s introduce them againExchange types 。
1.2.5 exchange types
The exchange types commonly used by rabbitmq arefanout、direct、topic、headersThese four types (AMQP specification also mentions two exchange types: system and custom, which are not described here).
The routing rule of fanout type exchange is very simple. It routes all messages sent to the exchange to all queues bound to it without any judgment. Therefore, fanout type is the fastest among all switch types. The fanout type is often used to broadcast messages.
The direct type of exchange routing rule is also very simple. It routes messages to the queues whose bindingkey and routingkey exactly match.
For example, if the routing key is set to “warning” when sending a message, the message will be routed to queue1 and Queue2. If the routing key is set to “info” or “debug” when sending a message, the message will only be routed to Queue2. If the message is sent with another routing key, the message will not be routed to both queues.
Direct type is often used to deal with priority tasks. According to the priority of the task, messages are sent to the corresponding queue, so that more resources can be assigned to deal with the high priority queue.
As mentioned earlier, the routing rules of direct type switches are completely matched with bindingkey and routingkey, but this strict matching method can not meet the needs of actual business in many cases. The topic type switch extends the matching rules. Similar to the direct type switch, it routes messages to the queues where bindingkey and routingkey match. However, the matching rules here are different. It stipulates that
- Routingkey is a string separated by dot “.” (each independent string separated by dot “.” is called a word)“ com.rabbitmq.client ”、“ java.util.concurrent ”、“ com.hidden.client ”;
- Bindingkey and routingkey are also character strings separated by dot “.”;
- There are two special strings in bindingkey“”And “#”, which are used for fuzzy matching“”It is used to match one word, and “#” is used to match multiple words (which can be zero).
The above figure is an example
- The routing key is“ com.rabbitmq.client ”The message will be routed to both queuel and Queue2 at the same time;
- The routing key is“ com.hidden.client ”The message will only be routed to Queue2;
- The routing key is“ com.hidden.demo ”The message will only be routed to Queue2;
- The routing key is“ java.rabbitmq.demo ”The message will only be routed to the queue;
- The routing key is“ java.util.concurrent ”The message will be discarded or returned to the producer (the mandatory parameter needs to be set) because it does not match any routing key.
④ Headers (not recommended)
The switches of headers type do not rely on the matching rules of routing keys to route messages, but match them according to the headers attribute in the message content. When binding the queue and switch, a set of key value pairs is set. When sending a message to the switch, rabbitmq will get the headers (also in the form of a key value pair) of the message. Compare whether the key value pairs match the key value pairs specified when binding the queue and switch. If they match, the message will be routed to the queue, otherwise it will not be routed to the queue. The performance of the header type switch will be very poor, and it is not practical, and it will not exist.
II. Installation of rabbitmq
It’s very convenient to install through docker. Just a few commands are needed. I’ll just talk about the general installation method here.
As mentioned earlier, rabbitmq is written in Erlang language. For this reason, you need to install Erlang before installing rabbitmq.
Note: when installing rabbitmq, you need to pay attention to the version relationship between rabbitmq and Erlang. If you don’t pay attention, it will cause errors. The corresponding relationship between them is as follows:
2.1 installing Erlang
1 download Erlang installation package
Download it from the official website and upload it to Linux, or download the corresponding version directly by using the following command.
[[email protected] local]#wget http://erlang.org/download/otp_src_19.3.tar.gz
Download from Erlang official website:http://www.erlang.org/downloads
2 decompress Erlang installation package
[[email protected] local]#tar -xvzf otp_src_19.3.tar.gz
3 delete Erlang installation package
[[email protected] local]#rm -rf otp_src_19.3.tar.gz
4. Install Erlang’s dependent tools
[[email protected] local]#yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC-devel
5. Enter the Erlang installation package and unzip the file to configure the installation environment of Erlang
Create a new folder
[[email protected] local]# mkdir erlang
Configure the installation environment of Erlang
[[email protected] otp_src_19.3]# ./configure --prefix=/usr/local/erlang --without-javac
6 compilation and installation
[[email protected] otp_src_19.3]# make && make install
7 verify that Erlang is installed successfully
[[email protected] otp_src_19.3]# ./bin/erl
Run the following statement to output “Hello world”
io:format("hello world~n", ).
It’s done. Our Erlang has been installed.
8 configure Erlang environment variables
[[email protected] etc]# vim profile
Append the following environment variables to the end of the file
#erlang ERL_HOME=/usr/local/erlang PATH=$ERL_HOME/bin:$PATH export ERL_HOME PATH
Run the following command to change the configuration file
[[email protected] etc]# source /etc/profile
Enter ERL to see if the Erlang environment variable is configured correctly
[[email protected] etc]# erl
2.2 installing rabbitmq
1. Download rpm
Or download it directly from the official website
2. Install rpm
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
The following is followed:
yum install rabbitmq-server-3.6.8-1.el7.noarch.rpm
You need to enter “Y” to continue the installation.
3. Open the web management plug-in
rabbitmq-plugins enable rabbitmq_management
4. Set the startup time
chkconfig rabbitmq-server on
4. Start the service
service rabbitmq-server start
5. View service status
service rabbitmq-server status
6. Access rabbitmq console
Browser access:Http: / / your IP address:15672/
The default user name and password are: Guest / guest; however, it should be noted that guest users are only allowed to access from localhost. The official website documents are described as follows:
“guest” user can only connect via localhost
Resolve remote access password error of rabbitmq
Create a new user and authorize it
[[email protected] rabbitmq]# rabbitmqctl add_user root root Creating user "root" ... [[email protected] rabbitmq]# rabbitmqctl set_user_tags root administrator Setting tags for user "root" to [administrator] ... [[email protected] rabbitmq]# [[email protected] rabbitmq]# rabbitmqctl set_permissions -p / root ".*" ".*" ".*" Setting permissions for user "root" in vhost "/" ...
Visit again:Http: / / your IP address: 15672 /, enter the user name and password: root