A the RabbitMQ is introduced
Learn more about Redis, installation, and messaging
zhuanlan.zhihu.com/p/369888458
zhuanlan.zhihu.com/p/355340010
This section refers to chapters 1 and 2 of the RabbitMQ Field Guide.
1.1 introduction of the RabbitMQ
RabbitMQ is a messaging middleware that uses Erlang to implement AMQP(Advanced Message Queuing Protocol). It originated in financial systems and is used to store and forward messages in distributed systems.
RabbitMQ has grown in popularity due to its excellence in ease of use, scalability, reliability and high availability. The specific features of RabbitMQ can be summarized as follows:
- Reliability: RabbitMQ uses mechanisms to ensure the reliability of messages such as persistence, transport confirmation, and release confirmation.
- Flexible routing: Messages are routed through the exchange before they are queued. For typical routing, RabbitMQ already provides some built-in switches. For more complex routing functions, multiple switches can be bound together, or you can implement your own switch through a plug-in mechanism. This will be covered later when we cover the core concepts of RabbitMQ.
- Scalability: Multiple RabbitMQ nodes can form a cluster or dynamically expand the cluster based on service requirements.
- High availability: Queues can be mirrored on machines in a cluster, making them available even if some nodes fail.
- Supports multiple protocols: RabbitMQ supports multiple messaging middleware protocols, such as STOMP and MQTT, in addition to AMQP.
- Multi-language clients: RabbitMQ supports almost all common languages, such as Java, Python, Ruby, PHP, C#, JavaScript, etc.
- Easy-to-use management interface: RabbitMQ provides an easy-to-use user interface that allows users to monitor and manage messages, nodes in a cluster, etc. Install RabbitMQ and the admin interface will come with RabbitMQ.
- Plugin mechanisms: RabbitMQ provides many plug-ins to extend in many ways, but you can also write your own. This feels a bit like Dubbo’s SPI mechanism.
1.2 RabbitMQ Core Concepts
RabbitMQ is essentially a producer and consumer model, responsible for receiving, storing, and forwarding messages. Think of it as sending a package to the post office, which stores it and eventually sends it to the postman. RabbitMQ is like a system of post office, mailbox and postman. In computer terms, the RabbitMQ model is more like a switch model.
Let’s look at Figure 1 — the overall model architecture for RabbitMQ.
Below I will introduce some of the concepts in the image above.
1.2.1 Producers and consumers
- Producer: the party that produces messages
- Consumer: the party that consumes the message (the person to whom the message is sent)
A message typically consists of two parts: a header (or Label) and a body. The body of the message, also called payLoad, is opaque, and the header is made up of a set of optional attributes. These attributes include routing-key, priority (priority over other messages), delivery-mode (indicating that the message may require persistent storage), and so on. After the producer sends the message to RabbitMQ, RabbitMQ sends the message to the interested Consumer based on the message header.
1.2.2 Exchange
In RabbitMQ, messages are not sent directly to a Queue, they have to pass through the Exchange layer, which allocates our messages to the corresponding Queue.
Exchanges are used to receive messages sent by producers and route them to queues on the server. If they are not routed, they may be returned to the Producer or discarded. Here you can think of the switch in RabbitMQ as a simple entity.
RabbitMQ has four types of exchanges, each of which has a different routing policy: Direct (default), Fanout, Topic, and headers. This will be covered when we introduce Exchange Types.
An Exchange is shown as follows:
When a producer sends a message to an exchange, it typically specifies a RoutingKey that specifies the routing rules for the message, and this RoutingKey needs to be used in conjunction with the exchange type and the BindingKey to take effect.
RabbitMQ uses Binding to associate exchanges with queues, A BindingKey is usually specified during binding so RabbitMQ knows how to route messages to queues correctly, as shown below. A binding is a routing rule that connects a switch to a message queue based on a routing key, so a switch can be thought of as a routing table made up of bindings. Exchange and Queue bindings can be many-to-many.
Binding schematic diagram:
When a producer sends a message to the exchange, a RoutingKey is required, and when the BindingKey matches the RoutingKey, the message is routed to the corresponding queue. When binding multiple queues to the same exchange, these bindings allow the same BindingKey to be used. BindingKey does not work in all cases. It depends on the type of switch, such as fanout, and instead routes messages to all queues bound to that switch.
1.2.3 Queue
A Queue is used to hold 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 remains in the queue, waiting for the consumer to connect to the queue to pick it up.
RabbitMQ messages can only be stored in queues, as opposed to messaging middleware such as Kafka. Kafka stores messages at the logical level of the topic (topic), and the corresponding queue logic is just a displacement identifier in the actual storage file of the topic. The RabbitMQ producer produces the message and ultimately delivers it to the queue, from which the consumer can retrieve and consume the message.
Multiple consumers can subscribe to the same queue, and messages in the queue are round-robin for processing among multiple consumers, rather than each consumer receiving all messages and processing them, thus avoiding repeated consumption of messages.
RabbitMQ does not support queue-level broadcast consumption, so secondary development on top of broadcast consumption is cumbersome and not recommended.
1.2.4 Broker (Message-oriented middleware service Node)
For RabbitMQ, a RabbitMQ Broker can simply be regarded as a RabbitMQ service node, or RabbitMQ service instance. In most cases a RabbitMQ Broker can also be thought of as a RabbitMQ server.
The following figure shows the process by which a producer stores a message into a RabbitMQ Broker and a consumer consumes data from the Broker.
This completes some of the basic concepts of RabbitMQ shown in Figure 1, with a brief introduction to Exchange Types.
1.2.5 Exchange Types
The main Exchange types for RabbitMQ are FANout, Direct, Topic, and headers. The AMQP specification also mentions system and custom Exchange types.
(1) the fanout
The Exchange routing rule of the FANout type is very simple. It routes all messages sent to the Exchange to all queues bound to it without any judgment, so the Fanout type is the fastest of all switch types. The fanout type is often used to broadcast messages.
(2) direct
Exchange routing rules of type Direct are also simple, routing messages to queues where bindingKeys match routingkeys exactly.
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 messages, messages will only be routed to Queue2. If messages are sent using other routing keys, they are not routed to either queue.
The direct type is commonly used to process tasks with a higher priority. The message is sent to the corresponding queue according to the priority of the task. In this way, more resources can be assigned to the queue with a higher priority.
(3) the topic
As mentioned above, the routing rules of the direct type switch match BindingKey and RoutingKey exactly. However, such strict matching method cannot meet the requirements of actual business in many cases. Topic exchanges extend the matching rules. Similar to direct exchanges, they route messages to queues that match BindingKey and RoutingKey, but the matching rules are a little different:
- The RoutingKey is a dot “. Delimited string (dot ‘. Each separate string is called a word), such as “com.rabbitmq.client”, “java.util.concurrent”, “com.hidden. Client”;
- BindingKey is also a dot like RoutingKey. Delimited strings;
- There can be two special strings in a BindingKey.“And” # “for fuzzy matching, where”“Is used to match one word, and” # “is used to match multiple words (which can be zero).
The above picture shows an example:
- The messages whose routing key is com.rabbitmq.client are routed to both Queuel and Queue2.
- Messages with a routing key of “com.hidden.client” are routed to Queue2 only.
- The messages whose routing key is com.hidden.demo will only be routed to Queue2.
- The messages whose routing key is java.rabbitmq.demo are routed to Queuel only.
- Messages with a routing key of “java.util.concurrent” will be discarded or returned to the producer (the mandatory parameter is required) because it does not match any routing key.
④ Headers (not recommended)
A HEADERS exchange does not rely on the matching rules of the routing key to route a message, but matches it based on the HEADERS attribute in the content of the sent message. When sending a message to the exchange, RabbitMQ will fetch the headers of the message and compare the matching keys to the queue and the exchange binding. If they match, the message will be routed to the queue. Otherwise, the queue will not be routed to. Headers switches tend to be poor in performance and impractical, and you’ll almost never see them.
2 install the RabbitMq
Docker is very easy to install, just need a few commands, I’m just talking about the general installation method.
As mentioned earlier RabbitMQ is written in Erlang and for this reason Erlang is required before RabbitMQ can be installed.
Note: When installing RabbitMQ, pay attention to the version relationship between RabbitMQ and Erlang. If you do not pay attention to the version relationship, it will cause an error.
2.1 install Erlang
1 Download the Erlang installation package
Download it from the official website and upload it to Linux or use the following command to download the corresponding version.
[root @ SnailClimb local] # wget HTTP: / / http://erlang.org/download/otp_src_19.3.tar.gzCopy the code
Download Erlang from www.erlang.org/downloads
2 Decompress the Erlang installation package
[root @ SnailClimb local] # tar - XVZF otp_src_19. 3. The tar. GzCopy the code
3 Delete the Erlang installation package
[root @ SnailClimb local] # rm - rf otp_src_19. 3. The tar. GzCopy the code
4 Install dependency tools for Erlang
[root@SnailClimb local]#yum -y install make gcc gcc-c++ kernel-devel m4 ncurses-devel openssl-devel unixODBC-devel
Copy the code
5 Access the decompressed Erlang installation package and configure the Erlang installation environment
Create a new folder
[root@SnailClimb local]# mkdir erlang
Copy the code
Configure the installation environment for Erlang
[root@SnailClimb otp_src_19.3]#
./configure --prefix=/usr/local/erlang --without-javac
Copy the code
6 Compilation and Installation
[root@SnailClimb otp_src_19.3Copy the code
7 Verify that Erlang is installed successfully
[root @ SnailClimb otp_src_19. 3] #. / bin/erlCopy the code
Run the following statement to print “Hello world”
io:format("hello world~n", []).
Copy the code
That’s it. Our Erlang is now installed.
8 Configure the Erlang environment variables
[root@SnailClimb etc]# vim profile
Copy the code
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
Copy the code
Run the following command to make the profile take effect
[root@SnailClimb etc]# source /etc/profile
Copy the code
Type ERl to see if the Erlang environment variable is configured correctly
[root@SnailClimb etc]# erl
Copy the code
2.2 install the RabbitMQ
1. Download the RPM
Wget HTTP: / / https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.8/rabbitmq-server-3.6.8-1.el7.noarch.rpmCopy the code
Or download it directly from the official website
www.rabbitmq.com/install-rpm…
2. Install the RPM
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
Copy the code
Then execute:
Yum install the rabbitmq server - 3.6.8-1. El7. Noarch. RPMCopy the code
You need to type “Y” midway to continue installation.
3 Enable the Web management plug-in
rabbitmq-plugins enable rabbitmq_management
Copy the code
4 Set startup
chkconfig rabbitmq-server on
Copy the code
4. Start the service
service rabbitmq-server start
Copy the code
5. Check the service status
service rabbitmq-server status
Copy the code
6. Access the RabbitMQ console
Visit http:// your IP address :15672/
Default user name and password: guest/guest; Note, however, that the Guestuest user is only allowed to access from localhost. The official documentation is as follows:
Guest user can only connect via localhostCopy the code
The remote access password of RabbitMQ is incorrect
Create and authorize a user
[root@SnailClimb rabbitmq]# rabbitmqctl add_user root root Creating user "root" ... [root@SnailClimb rabbitmq]# rabbitmqctl set_user_tags root administrator Setting tags for user "root" to [administrator] . [root@SnailClimb rabbitmq]# [root@SnailClimb rabbitmq]# rabbitmqctl set_permissions -p / root ".*" ".*" ".*" Setting permissions for user "root" in vhost "/" ...Copy the code
Visit http://your IP address :15672/ and enter the user name and password: root root
By Snailclimb Link: RabbitMQ Primer Source: Gitee
Technology Github learning address: github.com/codeGoogler…
Programming books for programmers: github.com/codeGoogler…
On how to learn Java, on the one hand, we need to continue to learn, to learn the basic knowledge of solid, on the other hand, we should also realize that Java learning can not only rely on theory, more rely on practical operation, so we should practice more projects, learning in practice is the best way to learn. Many people don’t know how to learn at first. Here I have compiled some important technical articles into the Github open source project, hoping to give you some help. The project is: JavaCodeHub
In addition, I also sorted out some programming books for programmers and put them on Github. The projects are: ProgramBooks, if necessary, you can get them by yourself. Address: github.com/codeGoogler…
If Github access is too slow? I also put ProgramBooks on the code cloud
Finally, we will continue to use our official account “Terminal R&d Department” to recommend a high-quality technology-related article every day, mainly sharing Java related technology and interview skills. Our goal is to know what it is, why, lay a solid foundation, and do everything well! The main technology of the public super worth our attention.