Use of RocketMQ message middleware

Use of RocketMQ message middleware

Why choose RocketMQ?

According to our research, the ActiveMQ IO module reaches a bottleneck as the number of queues and virtual topics in use increases. We tried our best to solve this problem through throttling, circuit breaker or downgrade, but it didn't work well. So we started looking at Kafka, a popular messaging solution at the time. Unfortunately, Kafka did not meet our requirements, especially in terms of low latency and high reliability, see here for details.

In this case, we decided to invent a new messaging engine to handle a wider set of use cases, from traditional publish/subscribe scenarios to high-volume real-time zero-loss-tolerant transaction systems. We believe this solution could be beneficial, so we would like to open source it to the community. Today, more than 100 companies use the open source version of RocketMQ in their operations.

Message queue is a "first in, first out" data structure

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-PU64BFpk-1667211673180) (D:/PRD/information/document/img/queue1.png)]

Its application scenarios mainly include the following three aspects:

1. Application decoupling

The more coupled a system is, the less fault tolerant it is. Take e-commerce applications as an example. After a user creates an order, if the inventory system, logistics system, and payment system are coupled and called, and any subsystem fails or is temporarily unavailable due to upgrades or other reasons, it will cause abnormal ordering operations and affect the user's use. experience.

Please add image description

Using message queues to decouple will improve the coupling of the system. For example, if the logistics system fails, it will take several minutes to repair it. During this time, the data to be processed by the logistics system is cached in the message queue, and the user's order operation is completed normally. When the logistics system responds, it can supplement the order messages stored in the message queue. The terminal system will not be aware that the logistics system has failed for a few minutes.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-S7a9idq0-1667211673183) (D:/PRD/information/documentation/img/decoupling2.png)]

2. Traffic peak clipping

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-x5OVh5X4-1667211673184) (D:/PRD/information/document/img/mq-5.png)]

If an application system encounters a sudden surge in system request traffic, it may overwhelm the system. With the message queue, a large number of requests can be cached and processed over a long period of time, which can greatly improve the stability of the system and user experience.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-iweWk9sm-1667211673185) (D:/PRD/information/document/img/mq-6.png)]

Generally speaking, in order to ensure the stability of the system, if the system load exceeds the threshold, user requests will be blocked, which will affect the user experience. If the message queue is used to cache the request, wait for the system to complete the processing and notify the user that the order is completed. In this way You can’t always place an order and have a better experience.

For economic reasons:

If the QPS of the business system during normal periods is 1,000 and the peak traffic is 10,000, it is obviously not cost-effective to configure a high-performance server to cope with the traffic peak. In this case, message queues can be used to reduce the peak traffic.

3. Data distribution

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-09q0TBBT-1667211673186) (D:/PRD/information/document/img/mq-1.png)]

Message queues allow data to flow between multiple systems. The data producer does not need to care about who uses the data. It only needs to send the data to the message queue, and the data user can obtain the data directly in the message queue.

[The external link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-nN4Nn9br-1667211673187) (D:/PRD/information/document/img/mq-2.png)]

4. Advantages and Disadvantages of MQ

Advantages: decoupling, peak clipping, data distribution

Disadvantages include the following:

  • Reduced system availability

    The more external dependencies introduced into the system, the worse the stability of the system. Once MQ goes down, it will have an impact on the business.

    How to ensure the high availability of MQ?

  • Increased system complexity

    The addition of MQ has greatly increased the complexity of the system. In the past, synchronous remote calls were made between systems, but now asynchronous calls are made through MQ.

    How to ensure that messages are not consumed repeatedly? How to deal with message loss? So ensure the order of message delivery?

  • Consistency issue

    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 it successfully, system D fails to process it.

    How to ensure the consistency of message data processing?

5. What are the differences between RocketMQ vs. ActiveMQ vs. Kafka
Messaging products Client SDK protocols and specifications Ordering Information scheduled message Bulk messages Broadcast message Message filter Server triggered redelivery Message storage Message tracing Message priority High availability and failover Message tracking Configuration Management and operational tools
ActiveMQ Java, .NET, C++, etc. Push model, supports OpenWire, STOMP, AMQP, MQTT, JMS Exclusive Consumer or Exclusive Queues can guarantee ordering supported not support supported supported not support Supports very fast persistence using JDBC and high-performance logging, such as levelDB, kahaDB supported supported Supported, depends on storage, requires ZooKeeper server if using levelDB not support The default configuration is low-level, users need to optimize configuration parameters supported
kafka Java、Scala etc. Pull model, supports TCP Ensure ordering of messages within a partition not support Supported, with async producers not support Yes, messages can be filtered using Kafka Streams not support High performance file storage Supported offset indications not support Supported, requires ZooKeeper server not support Kafka uses a key-value pair format for configuration. These values ​​can be provided from a file or programmatically. Support, use terminal commands to expose core indicators
RocketMQ Java, C++, Go Pull model, support TCP, JMS, OpenMessaging Ensures strict ordering of messages and scales out gracefully supported Supported, with synchronous mode to avoid message loss supported Supported SQL92-based attribute filter expressions supported High-performance and low-latency file storage Supported timestamp and offset representations not support Supported master-slave models, no additional kit required supported Ready to use out of the box, users only need to pay attention to a few configurations Supported, rich web and terminal commands to expose core metrics

Official website address: https://rocketmq.apache.org/

1. Download the RocketMQ installation package

**Requirements:** Preparation

Software and hardware requirements

  • Linux64-bit system
  • JDK1.8(64-bit)
  • Source code installation requires Maven 3.2.x.
  • **Note:** Integrating RocketMQ requires open ports: 9876, 10912, 10911, 10909. If these ports are not opened, the project will not be accessible.

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-5PU3mwwb-1667211673188) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221025085826308.png)]

We download version 4.9.3 here

Material: RockerMQ material download (already configured, just follow the steps below)

In order to facilitate everyone to learn quickly, the materials are prepared here, you can also follow the document to download from the official website

2. Configure using Linux

2.1 Use the command mkdir to create the /usr/local/recketMQ directory

Use the command cd to jump to the /usr/local/recketMQ directory

2.2 Use Xftp to open Xshell for the currently logged in user to perform file operations

Jump to the /usr/local/recketMQ directory for file transfer

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-gSU0fKYL-1667211673189) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221029171601892.png)]

2.3 Use unzip rocketmq-all-4.9.3-source-release.zip to decompress the file

You need to download the unzip decompression tool first, command: yum install unzip

2.4 Use cd to jump to the /usr/local/recketMQ/rocketmq-4.9.3 directory

Use ll to view file details in the current directory

2.5. Modify the initial memory

The default virtual machine memory of RocketMQ is large. Starting the Broker or NameServer may fail due to insufficient memory. Therefore, you need to edit the following two configuration files to modify the JVM memory size.

2.5.1. Modify runserver.sh

Use the vim command to open the bin/runserver.sh file. The first 71 and 76 lines have not been modified, but are modified as follows:

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-sMYpA42f-1667211673190) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20220825194822525.png)]

2.5.2 Modify runbroker.sh

Use the vim command to open the bin/runbroker.sh file. The first 85 lines have not been modified and are modified as follows:

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-ZI8jSdDK-1667211673191) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221025154050923.png)]

3. Start RocketMQ service

nohup means running the command without hanging up. When the account is logged out or the terminal is closed, the program is still running.

3.1 Start NameServer
nohup sh bin/mqnamesrv & 

tail -f ~/logs/rocketmqlogs/namesrv.log 

Started nameserver successfully

3.2 Start broker
nohup sh bin/mqbroker -n 192.168.230.128:9876  &

配置broker文件之后使用:nohup sh bin/mqbroker -n 192.168.230.128:9876 -c /usr/local/recketMQ/rocketmq-4.9.3/conf/broker.conf &

tail -f ~/logs/rocketmqlogs/broker.log 

Failed to start broker,

Insert image description here

Right-click the nohup.out file and open it with EditPlus editor to view errors.

As shown below, the memory is not configured successfully, resulting in insufficient memory. Need to reconfigure

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-i1EkbsDz-1667211673192) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221025153302236.png)]

Just reconfigure it

Started broker successfully

3.3 Send/receive message test

Send a message

export NAMESRV_ADDR=192.168.230.128:9876 

sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer

receive messages

sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer 

3.4 Shut down the server

Whether to shut down the name server or broker, use the bin/mqshutdown command.

[root@mqOS rocketmq]# 关闭broker服务命令: sh bin/mqshutdown broker 

The mqbroker(1740) is running..	. 

Send shutdown request to mqbroker(1740) OK 

[root@mqOS rocketmq]# 关闭nameserver服务命令: sh bin/mqshutdown namesrv 

The mqnamesrv(1692) is running... 

Send shutdown request to mqnamesrv(1692) OK 

[2]+ 退出 143 nohup sh bin/mqbroker -n localhost:9876 

4. Console installation and startup

RocketMQ has a visual dashboard through which a lot of data can be viewed intuitively.

**If you don’t want to download from the official website, you can directly use the console provided by the material (already configured)** Just follow the steps below.

4.1 Installation
4.1.1 Download

Download address: https://github.com/apache/rocketmq-dashboard

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-SrccosQn-1667211673193) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221025160927143.png)]

4.1.2 Modify configuration

Modify its application.properties configuration file in src/main/resources.

The original port number was 8080, which was changed to an uncommon one.

Specify the name server address of RocketMQ

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-E0dMXTXl-1667211673194) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221031150825807.png)]

4.1.3 Upload rocketmq-dashboard-master to the /usr/local/recketMQ directory

Use the cd command to jump to the /usr/local/recketMQ directory

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-7h60Wxgu-1667211673195) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221029173635009.png)]

4.1.4 Use unzip to decompress rocketmq-dashboard-master

4.1.5 Use the cd command to jump to the /usr/local/recketMQ/rocketmq-dashboard-master directory

4.2 Startup
4.2.1 Front-end packaging

Annotation packaging plug-in frontend-maven-plugin

<!--            <plugin>-->
<!--                <groupId>com.github.eirslett</groupId>-->
<!--                <artifactId>frontend-maven-plugin</artifactId>-->
<!--                <version>1.11.3</version>-->
<!--	        </plugin>-->
4.2.2 Backend packaging

Run the maven packaging command in the rocketmq-dashboard-master directory. (Note: The maven warehouse must be prepared in the virtual machine)

Maven installation URL

Excuting an order:

chmod a+x /usr/local/maven/apache-maven-3.6.0/bin/mvn
mvn clean package -Dmaven.test.skip=true

**Error report:** Most of the others are version issues, just match the version.

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-BmcRJQDq-1667211673195) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20220810163446732.png)]

**Solution:** Just comment out the frontend-maven-plugin plug-in.

<!--            <plugin>-->
<!--                <groupId>com.github.eirslett</groupId>-->
<!--                <artifactId>frontend-maven-plugin</artifactId>-->
<!--                <version>1.11.3</version>-->
<!--            </plugin>-->
4.2.2 Startup

Use the cd command to jump to the /usr/local/recketMQ/rocketmq-dashboard-master/target directory

Excuting an order:

nohup java -jar rocketmq-dashboard-1.0.1-SNAPSHOT.jar &

4.2.3 Access

Use http://IP address:port
[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-kpBnWehM-1667211673196) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221031173214440.png)]

At this point, our RocketMQ integration is successful

5. RocketMQ use

5.1 Open the project in the material

Insert image description here

Click to start the project

5.2 Open the http://localhost:8380/doc.html interface document
1. General news
  1. Perform data testing (normal messages)

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-pKZvlUmB-1667211673197) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221031173849911.png)]

  1. Control layer code

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-xBNkp3iq-1667211673198) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221031173817310.png)]

  1. Test Results

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-ishLsjLp-1667211673199) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221031174135268.png)]

2. Delayed message
  1. Perform data testing (delayed messages)

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-z1AC8IrX-1667211673199) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221031174428528.png)]

  1. Control layer code

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-xtTMWqd9-1667211673199) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221031174517264.png)]

  1. Test Results

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-gsRihc5y-1667211673200) (C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\ image-20221031174504570.png)]

6. Customized delay time level

Add the following lines to the property configuration file broker.conf on the server side (rocketmq-broker side):

messageDelayLevel=1s 5s 10s 30s 1m 2m 3m 4m 5m 6m 7m 8m 9m 10m 20m 30m 1h 2h
describes the corresponding mapping relationship between each level and the delay time.

  1. This configuration item configures the delay time of each level starting from level 1. The delay time of this specified level can be modified;

  2. Time units support: s, m, h, d, representing seconds, minutes, hours, and days respectively;

  3. The default value is the one declared above and can be adjusted manually;

  4. The default value is sufficient and it is not recommended to modify this value.

At this point, our study of RockerMQ is completed.

Guess you like

Origin blog.csdn.net/ITKidKid/article/details/127620821