Introduction to RabbitMQ Message Queue
Introduction to RabbitMQ Message Queue
Table of Contents of Series Articles
Tip: Here you can add the directory of all the articles in the series, the directory needs to be added manually by yourself
Introduction to [MQ](/search?q=RabbitMQ) Message Queue
Tip: After writing the article, the table of contents can be automatically generated, how to generate can refer to the right side of the help file
Article directory
- Table of Contents of Series Articles
- Preface
- 1. Introduction to RabbitMQ
-
- [message queue](/search?q=message queue)
- information
- queue
- RabbitMQ
- 2. Advantages of using Rabbit MQ
-
- Decoupled applications
- Asynchronous processing
- Traffic peak clipping
- High reliability
- 3. Disadvantages of using Rabbit MQ
- 4. Application scenarios of RabbitMQ
-
- flash sale activity
- message distribution
- data synchronization
- Asynchronous processing
- 5. How RabbitMQ works
- 6. Use of RabbitMQ
-
- Install Erlang environment
- Install RabbitMQ
- Dockers install RabbitMQ
- Account management
- Summarize
Preface
Tip: Here you can add the approximate content to be recorded in this article:
In today's distributed systems and microservice architectures, message queues play a vital role. They provide a reliable asynchronous communication mechanism for applications, allowing efficient messaging and coordination between components. Among the many message queue technologies, RabbitMQ is undoubtedly a leader that has attracted much attention and is widely used.
RabbitMQ is an open source message queue server , which is implemented based on AMQP (Advanced Message Queuing Protocol) protocol. By using RabbitMQ, developers can easily implement functions such as messaging between applications, asynchronous processing, and traffic peaking. It is highly reliable, flexible and scalable, capable of handling large amounts of message concurrency, and supports multiple programming languages and clients.
In this blog, I will take you deep into the world of RabbitMQ. We'll explore the core concepts of RabbitMQ, how it works, and some of its important features. I will also share some practical usage scenarios and best practices to help you better understand how to apply RabbitMQ to your projects.
Whether you are an experienced developer or just starting out with message queues, I believe this blog can provide you with valuable information and inspiration. Let us explore the charm of RabbitMQ together, master its powerful functions, and take a solid step towards building efficient and reliable applications!
Tip: Below is the body of this post, with the following examples for reference
1. Introduction to RabbitMQ
message queue
MQ is the abbreviation of Message Queue, which is also the message queue. The message queue is a container used to store messages during the message delivery process. Applied to asynchronous communication,
information
A message is a unit of data passed between two computers. Messages can be simple strings or complex objects.
queue
Queue is a concept in data structure. The data in the queue has the characteristics of first in, first out and last in, last out.
RabbitMQ
RabbitMQ is an open source message queue server implemented based on the AMQP (Advanced Message Queuing Protocol) protocol. It provides a reliable message delivery mechanism to ensure that messages are not lost.
RabbitMQ is highly flexible and supports multiple messaging modes, such as point-to-point, publish/subscribe, etc., to meet different application scenarios. At the same time, it also supports multiple programming languages and clients, and provides a wide range of client libraries to facilitate development by developers.
RabbitMQ scales easily when handling higher message throughput and more connections. It provides rich documentation and resources to facilitate users to learn and solve problems, and has an active community that can provide technical support and communication.
2. Advantages of using Rabbit MQ
Decoupled applications
By using message queues, different components of an application can communicate loosely coupled without direct dependencies on each other. This helps improve application flexibility and maintainability.
As an example: consider an order processing system for an e-commerce website. When a user places an order on the website, the order system needs to send the order information to the inventory system to check the inventory of the goods, and at the same time send the order information to the payment system to process the payment. If the order system communicates directly with the inventory system and payment system, then there will be a tight coupling between them.
Using RabbitMQ, the order system can send order messages to a queue, and the inventory system and payment system can receive order messages from the queue as consumers and process them. In this way, the order system no longer communicates directly with the inventory system and payment system, but is decoupled through message queues.
The benefit of this decoupling is that when the inventory system or payment system requires upgrades or maintenance, the order system is not affected. They can continue to send order messages to the queue, and the inventory system and payment system can process these messages at a later time. In addition, if there is a sudden increase in order volume, the queue can act as a buffer to ensure that the inventory system and payment system are not overwhelmed by the sudden high traffic.
Asynchronous processing
RabbitMQ allows applications to process asynchronously, thereby improving the system's responsiveness and concurrent processing capabilities. The sender can send a message to the queue and return immediately, while the receiver can process the message at a later time.
Traffic peak clipping
In situations of high concurrency or burst traffic, RabbitMQ can act as a buffer to absorb and process large amounts of messages. It helps the system withstand momentary peak loads and avoid application crashes or performance degradation.
High reliability
RabbitMQ provides a reliable message delivery mechanism to ensure that messages are not lost. Even in the event of a sender or receiver failure, messages can be stored in the queue and reprocessed after the system recovers.
3. Disadvantages of using Rabbit MQ
- Reduced system availability. The more external dependencies a system introduces, the worse the system stability. Once MQ goes down, it will have an impact on the business.
- The complexity of the system has increased. The addition of MQ has greatly increased the complexity of the system. In the past, there were synchronous remote calls between systems, but now asynchronous calls are made through MQ.
- Consistency problem: After system A processes the business, it sends message data to systems B, C, and D through MQ. If system B and system C process successfully but system D fails, it will cause inconsistency in data processing.
4. Application scenarios of RabbitMQ
flash sale activity
For flash sale activities such as grabbing red envelopes and grabbing tickets, a large number of requests will come in an instant. If processed directly, a lot of resources will be wasted. We can first put the request in MQ, and the flash sale requested first will succeed, and the flash sale requested later will fail. .
message distribution
For example, if an e-commerce website wants to push promotional information, this business takes a lot of time, but does not require high timeliness. MQ can be used for message distribution.
data synchronization
If we need to save data outside the database, it will take some time to synchronize the data to cache (such as Redis) and search engines (such as Elasticsearch). At this time, the database data can be sent to MQ as a message and synchronized to the cache and search engine.
Asynchronous processing
In the e-commerce system, after the order is completed, the subsystem needs to be notified in time (delivery by the purchase, sale and inventory system, user service points, sending text messages) for the next step. In order to ensure the high performance of the order system, the order results should be returned directly, and then MQ can notify the subsystem to perform other non-real-time business operations. This can ensure the efficiency and timeliness of core business.
5. How RabbitMQ works
- Producer: The producer sends messages to the RabbitMQ queue. Producers can be applications or services that send messages to a RabbitMQ queue for subsequent processing.
- Connection: TCP connection established between producer/consumer and RabbitMQ server.
- Channel: It is a virtual connection in TCP. For example: Connection is equivalent to a cable, and Channel is equivalent to an independent optical fiber bundle. Multiple channels can be created in a TCP connection to increase connection efficiency. Whether publishing messages, receiving messages, or subscribing to queues, all are done through channels.
- Broker: Message queue server entity. i.e. RabbitMQ server
- Virtual host: virtual host. Designed for multi-tenancy and security reasons, the basic components of AMQP are divided into a virtual group. Each vhost is essentially a mini version of RabbitMQ server, with its own queue, switch, binding and permission mechanism. When multiple different users use the same RabbitMQ server, multiple virtual hosts can be divided. The default virtual host path of RabbitMQ is /.
- Exchange: An exchange is a component in RabbitMQ that is used to send messages from producers to queues. The switch can send messages to different queues based on their content or attributes.
- Queue: RabbitMQ queue is a container that stores messages. The queue can be consumed by multiple consumers at the same time, and messages are stored in the queue according to the first-in-first-out (FIFO) principle.
- Binding: Binding is a configuration in RabbitMQ that is used to associate switches and queues. Through binding, a producer can send messages to a specific exchange, and the exchange can forward the message to a specified queue.
- Consumer: The consumer obtains messages from the RabbitMQ queue and processes them. Consumers can be applications or services that obtain messages from the queue and perform subsequent processing or storage.
When a producer sends a message to RabbitMQ, it sends the message to the specified exchange. The switch forwards the message to the corresponding queue according to the binding rules. The consumer then gets the message from the queue and processes it. The entire process is asynchronous, and producers and consumers do not need to wait for each other to complete operations, but can continue their work.
6. Use of RabbitMQ
Because RabbitMQ is written in Erlang, you need to install Erlanghuanjing to use RabbitMQ.
Install Erlang environment
1. Install the dependencies required by Erlang
yum install -y epel-release
2. Add repository entry
wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
rpm -Uvh erlang-solutions-1.0-1.noarch.rpm
3. Install Erlang
yum install -y erlang
4. Check whether Erlang is installed successfully
erl -version
Install RabbitMQ
1. In order for the outside world to access the RabbitMQ service normally, first close the firewall.
# Shut down the running firewall
systemctl stop firewalld.service
# Disable the firewall from starting up
systemctl disable firewalld.service
2.RabbitMQ is accessed through the host name, and the host name must be added to the server.
# Modify the file
vim /etc/sysconfig/network
# Add the following
NETWORKING=yes
HOSTNAME=itbaizhan
# Modify the file
vim /etc/hosts
# Add the following
Server ip zhangsan
3. Download the RabbitMQ compressed package in Windos (you can find it online) and use the rz command to upload it from Windos to Linux (virtual machine).
4. Install RabbitMQ
# Unpack RabbitMQ
tar xf [rabbitmq](/search?q=rabbitmq)-server-generic-unix-3.9.13.tar.xz
# Rename:
mv rabbitmq_server-3.9.13 rabbitmq
# Move the folder:
mv rabbitmq /usr/local/
5. Configure environment variables
# Edit the /etc/profile file
vim /etc/profile
# Add the following
export PATH=$PATH:/usr/local/rabbitmq/sbin
# Run the file to make the changes take effect
source /etc/profile
6. Open the management console plug-in
rabbitmq-plugins enable rabbitmq_management
7. Running in the background
# Create the configuration folder
mkdir -p /usr/local/rabbitmq/etc/rabbitmq
# Create the configuration file
vim /usr/local/rabbitmq/etc/rabbitmq/rabbitmq.conf
# Add the following
loopback_users=none
# Restart RabbitMQ
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
8. Access RabbitMQ through the management console, path: http://ipaddress: 15672, username: guest, password: guest
9. At this time, you will be prompted that the guest account is only allowed to be used locally. We can configure to allow remote access using guest
# Create the configuration folder
mkdir -p /usr/local/rabbitmq/etc/rabbitmq
# Create the configuration file
vim /usr/local/rabbitmq/etc/rabbitmq/rabbitmq.conf
# Add the following
loopback_users=none
# Restart RabbitMQ
rabbitmqctl stop_app
rabbitmqctl reset
rabbitmqctl start_app
Dockers install RabbitMQ
1. Install docker in the virtual machine
# Install Docker
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
# Start docker
systemctl start docker
2. Pull the image
docker pull rabbitmq
3. Start the RabbitMQ container
docker run -d --hostname zhangsan --name rabbit -p 15672:15672 -p 5672:5672 rabbitmq
4. Open the management console plug-in
# Query rabbitmq container IDs
docker ps
# Enter the container
docker exec -it container id /bin/bash
# Enable the console plugin
rabbitmq-plugins enable rabbitmq_management
# Exit the container
ctrl+p+q
5. Access rabbitmq through the control console, path: http://ip address: 15672, username: guest, password: guest
6. Close the RabbitMQ container
docker stop rabbit
Account management
The guest account is only allowed to be used locally by default. We can create a new account to access RabbitMQ remotely.
1. Create an account
# Create account
rabbitmqctl add_user username password
2. Grant the administrator role to the user
rabbitmqctl set_user_tags username administrator
3. Authorize users
# "/" for virtual machine
# itbaizhan for username
# ". *" ". *" ". *" for full permissions
rabbitmqctl set_permissions -p "/" itbaizhan ". *" ". *" ". *"
Summarize
Hint: Summarize the article here:
RabbitMQ is a powerful and flexible message queue solution that helps applications achieve high-performance, reliable asynchronous communication and data processing.