Article directory
1. mall open source project
1.1 Source
mall learning tutorial , comprehensive analysis of architecture, business and technical points. The mall project ( 50k+star ) is an e-commerce system that is implemented using current mainstream technologies. Covers SpringBoot 2.3.0, MyBatis 3.4.6, Elasticsearch 7.6.2, RabbitMQ 3.7.15, Redis 5.0, MongoDB 4.2.5, Mysql5.7 and other technologies, using Docker container deployment.
Project github address: github.com
1.2 Project transfer
You can transfer projects on github to gitee for easy cloning to idea.
Specific steps are as follows:
1.3 Project cloning
Since github is deployed abroad, although idea also supports pulling from github, the cloning speed is too slow, so it is recommended to clone the project to idea after importing gitee as mentioned above.
The specific cloning steps are too simple and routine. Readers can complete it by themselves, or search on Baidu~
2. RabbitMQ message middleware
RabbitMQ 消息的传递
: It is a model consisting of "producer->switch->queue->consumer", but the point-to-point mode and work queue mode can be understood as an anonymous switch for delivery queue.
2.1 Introduction to rabbit
RabbitMQ official website : official website
RabbitMQ is a message broker - a messaging system intermediary. It can provide your application with a universal platform for sending and receiving messages , and ensure the security of messages during transmission.
RabbitMQ is an open source message middleware developed using Erlang language and implementing AMQP (Advanced Message Queuing Protocol). First of all, you need to know some of the characteristics of RabbitMQ as follows:
- reliability. Supporting persistence, transmission confirmation, release confirmation, etc. ensures the reliability of MQ.
- Flexible distribution message strategy. This should be a major feature of RabbitMQ. The Exchange (switch) routes the message before it enters MQ. Distribution message strategies include: simple mode, work queue mode, publish and subscribe mode, routing mode, and wildcard mode.
- Supports clustering. Multiple RabbitMQ servers can form a cluster to form a logical Broker.
- Multiple protocols. RabbitMQ supports multiple message queue protocols, such as STOMP, MQTT, etc.
- Supports multiple language clients. RabbitMQ supports almost all commonly used programming languages, including Java, .NET, Ruby, etc.
- Visual management interface. RabbitMQ provides an easy-to-use user interface that allows users to monitor and manage message brokers.
- Plug-in mechanism. RabbitMQ provides many plug-ins, which can be extended through plug-ins, or you can write your own plug-ins.
Rabbit development document address: [java client development document] ( Java client guide · RabbitMQ in Chinese (mr-ping.com) )
2.2 Usage process of distributed back-end projects
The figure below is a basic usage process summarized by myself based on project experience and Baidu search information.
The usage flow chart of rabbitmq is as follows:
2.3 Usage scenarios of distributed backend projects
The picture below is a basic usage scenario summarized by myself based on project experience and Baidu search information.
The usage scenario diagram of rabbitmq is as follows:
3. Install RabbitMQ (Win10)
Since rabbitmq was operated on Linux in the past (usually Docker is used to pull down a RabbitMQ image, eliminating the need for environment deployment), now I will interpret the source code and try to operate it under win10 . You need to have an environment first, download it first Erlang
, and then download it RabbitMQ服务端
.
p.s. and are closely related, so when choosing a RabbitMQ version you need to make sure it is compatible with the specific Erlang version you have installed RabbitMQ
.Erlang
( Erlang/OTP 26.0.2
The compatible version is RabbitMQ 3.8.x 或者 更高
)
❌I was dumbfounded after writing this. I should have known better than to choose such a high-end version.
Experience : After installing the two versions, I forgot to check that spring-boot-starter-amqp
the version in 2.1.3
the project was used as the RabbitMQ client library. As a result, the version was incompatible, resulting in the failure to connect. There were a lot of questions on Baidu on the Internet. I carefully checked and selected them, and they were all correct. . Later, I could only locate the problem as version incompatibility, and then I settled for the next best thing and upgraded spring-boot-starter-amqp
the version to 2.3.x
solve the problem perfectly. What I didn’t expect was...
Alibaba Cloud warehouse is so confusing, it doesn’t even have one. . .
Could not find artifact org.springframework.boot:spring-boot-starter-amqp:pom:2.3.3 in alimaven
Then Baidu looked for many tutorials, saying that they changed the scope and changed the image. I tried them all and found that this one could not be found. All other dependencies also reported errors. It should be a dependency conflict in the warehouse or something, so I won’t go into details. . . .
✒️It’s better to follow the project and lower Erlang
the and versions.RabbitMQ服务端
The version of spring-boot-starter-amqp is 2.1.3, which is compatible with RabbitMQ 3.7.x version
Download the RabbitMQ server here: rabbitmq-server-3.7.3.exe
.
The corresponding Erlang
version is here 19.3 - 20.x
( remember to uninstall it cleanly and the version must be compatible, otherwise when installing the plug-in, an incompatible version error will be reported )
The screenshot below will not be updated. It is not necessary. It is just that the version number is different and the operation is almost the same. (In the end, it was connected. It was indeed a version incompatibility problem. I really went through all the pitfalls...)
3.1 Install erLang language and configure environment variables
1. First go to the official website to download, Erlang official website: official website (download win10 version)
2. After the download is completed otp_win64_26.0.exe
, double-click to install and keep clicking next
(you can switch the installation path). After the installation is complete, configure the environment variables.
3. After the environment variables are configured, use the cmd command erl -version
to verify whether the input is successful.
A successful screenshot looks like the one below:
3.2 Install RabbitMQ server
1. github上
Download window版
the server installation package from RabbitMQ.
Download address: Release RabbitMQ 3.8.32 · rabbitmq/rabbitmq-server (github.com) )
2. Download as rabbitmq_server-3.8.32.exe
, then double-click to install, and click Next to install (you can switch the installation path). After the installation is completed, find the installation directory, open the \rabbitmq\rabbitmq_server-3.8.32\sbin
cmd command, and enter rabbitmq-plugins enable rabbitmq_management
the command to install the plug-in on the management page.
3. Double-click rabbitmq-server.bat
the startup script, and then open the service management to see RabbitMQs
whether it is running. (After downgrading the version, it is not registered with the service. You need to start the script. Do not close the command line window so that the service can run on it.)
3.3 Test the installation effect
1. Open the browser and enter http://localhost:15672
The default account password is: guest/guest
, successful effect:
2. The installation is successful. The effect after logging in is as shown below.
4. Source code analysis
Looking at the source code, I summarized the basic steps when looking at a new project. First, look at integration and configuration, then analyze it from a business perspective, and combine the integrated framework and components to explore the system architecture in turn.
**Analysis content:** Most of the analysis content below is explained in the figure, and there will not be too much explanation outside.
4.1 Integration and configuration
Take the source code analysis directly and only analyze the parts related to rabbitmq. Readers of other parts are asked to read the source code analysis by themselves.
**Project Startup:**Only need to start mall-tiny-rabbit
the module part.
**Required for startup:** Start the RabbitMQ service (win10 version was installed earlier)
4.1.1 Import dependencies
Import pom文件
rabbitmq's related dependencies spring-boot-starter-amqp
(rabbitmq is integrated into springboot).
4.1.2 Add configuration
application.yml
Add rabbitmq related configuration in .
4.1.3 Create users, passwords, and bind roles (at the command line)
RabbitMQ's user management includes adding users, deleting users, viewing user lists, and changing user passwords.
1. View existing users and user roles:
rabbitmqctl.bat list_users
2. Add a new user:
// rabbitmqctl.bat add_user username password
rabbitmqctl.bat add_user mall mall
3. Delete a user:
// 注意观察查询、添加、删除都是只换前缀,更新亦然,就不列举了
rabbitmqctl.bat delete_user mall
4. Bind the role:
rabbitmqctl.bat set_user_tags mall administrator
4.1.4 Create users, passwords, and bind roles (under the web interface management tool)
1. Introduce the web interface management tool page
2. Add users and assign roles
3. Since the yml file is configured with virtual-host: /mall
, add Virtual Hosts
(can be understood as a database, similar to mysql, oracle, etc.)
4.1.5 Expansion
RabbitMQ user roles can be divided into five categories : super administrator, monitor, policy maker, ordinary manager and others.
(1) Super administrator (administrator)
You can log in to the management console (when the management plugin is enabled), view all information, and operate users and policies.
(2) Monitoring
You can log in to the management console (when the management plugin is enabled) and view relevant information about the rabbitmq node (number of processes, memory usage, disk usage, etc.)
(3) Policymaker
You can log in to the management console (when the management plugin is enabled) and manage the policy.
(4) Ordinary managers (management)
You can only log in to the management console (when the management plugin is enabled), you cannot see node information, and you cannot manage policies.
(5) Others
Unable to log in to the management console, usually ordinary producers and consumers.
4.2 Explain in simple terms
4.2.1 Introduction to six message modes
Official website of six message modes : RabbitMQ Tutorials — RabbitMQ
Simple Work Queue
(Simple work queue): This is also commonly referred to as the point-to-point mode . One message is consumed by one consumer. (When there are multiple consumers, the polling mechanism is used by default to distribute messages to consumers).Work Queues
(Work queue): Also called fair queue, a message queue model in which those who can do more work. The queue must receive a manual ack from the consumer before it can continue to send messages to the consumer.Publish/Subscribe
(Publish-subscribe model): A message is consumed by multiple consumers.Routing
(Routing mode): Receive messages selectively.Topics
(Topic mode): Selectively receive messages based on certain rules.RPC
Mode: The publisher publishes messages and waits for the results through RPC. (There are few usage scenarios and are not included in the source code. If you want to know more, you can go to the official website to learn more)
psPublisher Confirms
: There is a message confirmation mechanism at the end of the official website . Refers to how producers send reliable messages.
4.2.2 Simple work queue mode
A message is consumed by one consumer (when there are multiple consumers, the rotation mechanism is used by default to distribute the message to the consumer)
1、 配置类
Configure the Spring configuration class of RabbitMQ SimpleRabbitConfig
, which creates a message queue object, an object to send messages ( SimpleSender
), and an object to receive messages ( SimpleReceiver
). These objects will be managed and injected by the Spring framework.
2、消息发送者
3、消息消费者
4、简单工作队列模式的控制层接口
5、swagger发送效果
If swagger cannot be started, please refer to my previous blog post: mall: redis project source code analysis_Yi~Successful Blog-CSDN Blog
6、rabbitmq服务端的界面显示效果
7、idea控制台打印的日志信息
4.2.3 Work queue mode
Fair queue , a message queue model in which those who can do more work can do more work. The queue must receive a manual ack from the consumer before it can continue to send messages to the consumer. (Simulated consumption)
1、 定义了一些消息队列以及相应的消息接收器和发送器
2、消息发送者
3、消息消费者
4、工作队列模式的控制层接口
5、idea控制台打印的日志信息
4.2.4 Publish-subscribe model
A message is consumed by multiple consumers.
1、配置类
2、消息发送者
3、消息消费者
4、发布/订阅模式的控制层接口
5、rabbitmq服务端的界面显示效果
6、idea控制台打印的日志信息
4.2.5 Routing mode
Receive messages selectively.
1、配置类
By configuring different binding keys, different queues can be bound to the same switch to implement message routing and distribution.
2、消息发送者
3、消息消费者
4、路由模式的控制层接口
5、idea控制台打印的日志信息
4.2.5 Theme mode
Selectively receive messages based on certain rules.
1、配置类
2、消息发送者
3、消息消费者
4、路由模式的控制层接口
5、idea控制台打印的日志信息
5. Summary
In this article, I first obtained the requirements from the actual project, so as to learn the RabbitMQ message queue, combined with the source code to learn, and learned mall
RabbitMQ from the open source project. I feel that I have gained a lot. I hope this article will be helpful to you.
In the future, I will also combine this framework to learn other technology stacks.
盈若安好,便是晴天