<> One ,RocketMQ introduce

<>1.1. brief introduction

RocketMQ It's distributed , Message middleware based on queue model , It has the following characteristics :
It can guarantee strict message order
Provide rich message pull mode
Efficient subscriber level scalability
Real time message subscription mechanism
100 million level information accumulation capacity

Reasons for selection :

* Emphasize that there is no single point in the cluster , Scalable , High availability at any point , Horizontal scalability .
* Massive message accumulation capability , After accumulating news , Write low latency .
* Support tens of thousands of queues
* Message failure retrial mechanism
* Message can be inquired
* Open source community active
* Maturity ( After the double 11 test )
<>1.2. Key concepts

<>1.2.1. Themes and labels

theme Tpoic: First level message type , Title of the book
label Tags: Second level message type , Catalogue of books , Can be based on Tag Do message filtering
for example :
theme :
Order transaction
label :
Order transaction - establish Order transaction - payment Order transaction - complete
<>1.2.2. Send and subscribe groups

production team : For sending messages .
Consumer group : Subscription processing for messages .

Production group and consumption group , Convenient expansion and contraction machine , Increase or decrease processing capacity , The name of the cluster group , A member used in marking purposes . Only one member of each cluster will be randomly distributed .

<> Two ,RocketMQ Cluster mode

Several recommended Broker Cluster deployment mode , there Slave Not writable , But readable , be similar to Mysql Main standby mode .

<>2.1. single Master

This method is risky , once Broker On restart or downtime , Will cause the entire service to be unavailable , Online environment is not recommended .

<>2.2. many Master pattern

     A cluster has no Slave, All of them Master, for example 2 individual Master perhaps 3 individual Master
     advantage : Simple configuration , single Master Downtime or restart maintenance has no impact on the application , When the disk is configured as RAID10 Time , Even if the machine is down and cannot be recovered , By and RAID10
The disk is very reliable , The news won't be lost ( A small number of messages are lost in asynchronous disk flushing , Do not lose one synchronous disk ). Highest performance .
     shortcoming : During downtime of single machine , Messages that are not consumed on this machine cannot be subscribed to until the machine is restored , The real-time performance of the message will be affected .
### Start first NameServer
### In the machine A, Start the first one Master
### In the machine B, Start the second one Master

<>2.3. many Master many Slave pattern , Asynchronous replication

     each Master Configure one Slave, How many pairs Master-Slave,HA Adopt asynchronous replication mode , There is a short message delay between the master and the standby , millisecond .
     advantage : Even if the disk is damaged , Very little is lost , And the real-time performance of the message will not be affected , because Master After downtime , Consumers can still get Slave
consumption , This process is transparent to the application . No manual intervention is required . Same performance Master The pattern is almost the same .
     shortcoming :Master Downtime , Disk damage , A small amount of messages will be lost .
### Start first NameServer
### In the machine A, Start the first one Master
### In the machine B, Start the second one Master
### In the machine C, Start the first one Slave
### In the machine D, Start the second one Slave

<>2.4. many Master many Slave pattern , Synchronous double writing

     each Master Configure one Slave, How many pairs Master-Slave,HA Synchronous double write mode is adopted , Both master and slave are successfully written , Return success to app .
     advantage : There is no single point in data and services ,Master In case of downtime , Message without delay , Both service availability and data availability are very high
     shortcoming : Performance is slightly lower than asynchronous replication mode , About low 10% about , Sending a single message RT It will be slightly higher . After the current main outage , The standby machine cannot automatically switch to the host computer , Automatic switching will be supported in the future .
### Start first NameServer
### In the machine A, Start the first one Master
### In the machine B, Start the second one Master
### In the machine C, Start the first one Slave
### In the machine D, Start the second one Slave
above Broker And Slave Pairing is done by specifying the same brokerName Parameter to pair ,Master Of BrokerId It has to be 0,Slave Of
BrokerId It has to be big and big 0 The number of . Another one Master You can mount multiple Slave, same Master Many under Slave By specifying different
BrokerId To distinguish .

<> Three ,RocketMQ deploy 【Master-Slave mode 】

<>3.1. Server environment

Serial number IP user name password role pattern
1 192.168.11.128 root *** nameServer1,brokerServer1 Master1
2 192.168.11.129 root *** nameServer2,brokerServer2 Master2
<>3.2. Hosts Add information

IP NAME
192.168.11.128 rocketmq-nameserver1
192.168.11.128 rocketmq-master1
192.168.11.129 rocketmq-nameserver2
192.168.11.129 rocketmq-master1-slave vi /etc/hosts
<>3.3. Upload and decompress 【 Two machines 】
# upload apache-rocketmq.tar.gz File to /usr/local tar -zxvf apache-rocketmq.tar.gz -C
/usr/localln -s apache-rocketmq rocketmq ll /usr/local
<>3.4. Create storage path 【 Two machines 】
mkdir /usr/local/rocketmq/store mkdir /usr/local/rocketmq/store/commitlog mkdir
/usr/local/rocketmq/store/consumequeuemkdir /usr/local/rocketmq/store/index
<>3.5. RocketMQ configuration file 【 Two machines 】
vim /usr/local/rocketmq/conf/2m-2s-async/broker-a.properties vim
/usr/local/rocketmq/conf/2m-2s-async /broker-a-s.properties # Cluster name
brokerClusterName=rocketmq-cluster #broker name , Note that different configuration files are filled in differently brokerName=
broker-a|broker-b #0 express Master,>0 express Slave brokerId=0 #nameServer address , Semicolon segmentation
namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
# When sending a message , Automatically create a server that does not exist topic, Default number of queues created defaultTopicQueueNums=4 # Is it allowed Broker
Automatic creation Topic, Suggest to open offline , Online shutdown autoCreateTopicEnable=true # Is it allowed Broker
Automatically create subscription groups , Suggest to open offline , Online shutdown autoCreateSubscriptionGroup=true #Broker External service listening port
listenPort=10911 # Delete file time point , Default to early morning 4 spot deleteWhen=04 # File retention time , default 48 hour
fileReservedTime=120 #commitLog Default size per file 1G mapedFileSizeCommitLog=1073741824
#ConsumeQueue Each file is saved by default 30W strip , Adjust according to business situation mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000 #redeleteHangedFileInterval=120000
# Detect physical file disk space diskMaxUsedSpaceRatio=88 # Storage path storePathRootDir=
/usr/local/rocketmq/store#commitLog Storage path storePathCommitLog=
/usr/local/rocketmq/store/commitlog# Consumption queue storage path storage path storePathConsumeQueue=
/usr/local/rocketmq/store/consumequeue# Message index storage path storePathIndex=
/usr/local/rocketmq/store/index#checkpoint File storage path storeCheckpoint=
/usr/local/rocketmq/store/checkpoint#abort File storage path abortFile=
/usr/local/rocketmq/store/abort# Limited message size maxMessageSize=65536
#flushCommitLogLeastPages=4 #flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000 #flushConsumeQueueThoroughInterval=60000
#Broker The role of #- ASYNC_MASTER Asynchronous replication Master #- SYNC_MASTER Synchronous double writing Master #- SLAVE
brokerRole=ASYNC_MASTER # Brush disk mode #- ASYNC_FLUSH Asynchronous disk brushing #- SYNC_FLUSH Synchronous brush disk
flushDiskType=ASYNC_FLUSH #checkTransactionMessageEnable=false # Number of message sending thread pools
#sendMessageThreadPoolNums=128 # Number of pull message thread pools #pullMessageThreadPoolNums=128
<>3.6. Modify log configuration file 【 Two machines 】
mkdir -p /usr/local/rocketmq/logs cd /usr/local/rocketmq/conf && sed -i 's#
${user.home}#/usr/local/rocketmq#g' *.xml
<>3.7. Modify startup script parameters 【 Two machines 】
vim /usr/local/rocketmq/bin/runbroker.sh
#==============================================================================
# development environment JVM Configuration
#==============================================================================
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:PermSize=128m
-XX:MaxPermSize=320m" vim /usr/local/rocketmq/bin/runserver.sh JAVA_OPT="
${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:PermSize=128m
-XX:MaxPermSize=320m"
<>3.8. start-up NameServer【 Two machines 】
cd /usr/local/rocketmq/bin nohup sh mqnamesrv &
<>3.9. start-up BrokerServer A【192.168.11.128】
cd /usr/local/rocketmq/bin nohup sh mqbroker -c
/usr/local/rocketmq/conf/2m-noslave/broker-a.properties>/dev/null 2>&1 & netstat
-ntlp jpstail -f -n 500 /usr/local/rocketmq/logs/rocketmqlogs/broker.log tail
-f -n 500 /usr/local/rocketmq/logs/rocketmqlogs/namesrv.log
<>3.10. start-up BrokerServer B【192.168.11.129】
cd /usr/local/rocketmq/bin nohup sh mqbroker -c
/usr/local/rocketmq/conf/2m-noslave/broker-a-s.properties>/dev/null 2>&1 &
netstat -ntlp jps tail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/broker.logtail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/namesrv.log
<>3.11. RocketMQ Console

stay tomcat Deployment in rocketmq-console.war

<>3.12. Data cleaning
cd /usr/local/rocketmq/bin sh mqshutdown broker sh mqshutdown namesrv -- Wait to stop rm
-rf /usr/local/rocketmq/storemkdir /usr/local/rocketmq/store mkdir
/usr/local/rocketmq/store/commitlogmkdir /usr/local/rocketmq/store/consumequeue
mkdir /usr/local/rocketmq/store/index -- Restart according to the above steps NameServer And BrokerServer
<> Four , Precautions for use

<>4.1. Consumption retrial mechanism

How many retries Interval time between retries
1 10 second
2 30 second
3 1 minute
4 2 minute
5 3 minute
6 4 minute
7 5 minute
8 6 minute
9 7 minute
10 8 minute
11 9 minute
12 10 minute
13 20 minute
14 30 minute
15 1 hour
16 2 hour
<>4.2. Idempotent processing on the consumer side

RocketMQ Duplicate messages cannot be avoided , If the business is very sensitive to consumption duplication , Be sure to focus on the business situation , There are several ways to remove the weight :

* The unique key of the message , It can be msgId, It can also be a unique identification field in the message content , For example, orders Id etc . It is recommended to use the unique identification field in the message content to remove duplicate .
* Using state machine of business situation to remove duplication .RocketMQ Of Consumer All from Broker Pull news to consume , But in order to receive messages in real time , RocketMQ
Using long polling , It can ensure the real-time performance of the message Push In the same way .’
<> Five , Multi master and multi slave mode

* The multi master and multi slave mode can be divided into two modes , The first is asynchronous replication , The second is synchronous double writing , Let's consider one of them for the moment , be careful :*(RocketMQ
Each cluster environment configuration corresponds to a different directory )
Double main mode , The folder is configured as : conf/2m-noslave/
Multi master and multi slave mode ( Asynchronous replication ), The folder is configured as : conf/2m-2s-async/
Multi master and multi slave mode ( Synchronous double writing ), The folder is configured as : conf/2m-2s-sync/
* The cluster planning is as follows :【 Four machines 】
IP NAME
192.168.11.121 rocketmq-nameserver1,rocketmq-master1
192.168.11.122 rocketmq-nameserver2,rocketmq-master2
192.168.11.123 rocketmq-nameserver3,rocketmq-master1-slave
192.168.11.124 rocketmq-nameserver4,rocketmq-master2-slave
* add to hosts information vi /etc/hosts
* Unzip upload 【113,114 Two machines 】 upload alibaba-rocketmq-3.2.6.tar.gz To document /usr/local tar -zxvf
alibaba-rocketmq-3.2.6.tar.gz -C /usr/localmv alibaba-rocketmq
alibaba-rocketmq-3.2.6ln -s alibaba-rocketmq-3.2.6 rocketmq ll /usr/local
* Create storage directory 【113,114 Two machines 】 mkdir /usr/local/rocketmq/store mkdir
/usr/local/rocketmq/store/commitlogmkdir /usr/local/rocketmq/store/consumequeue
mkdir /usr/local/rocketmq/store/index
* RocketMQ configuration file 【 Four machines 】
vim /usr/local/rocketmq/conf/2m-2s-async/broker-a.properties vim
/usr/local/rocketmq/conf/2m-2s-async/broker-b.properties vim
/usr/local/rocketmq/conf/2m-2s-async/broker-a-s.properties vim
/usr/local/rocketmq/conf/2m-2s-async/broker-b-s.properties
broker-a.properties,broker-b.properties The configuration is as follows :
# Cluster name brokerClusterName=rocketmq-cluster #broker name , Note that different configuration files are filled in differently
brokerName=broker-a|broker-b #0 express Master,>0 express Slave brokerId=0
#nameServer address , Semicolon segmentation namesrvAddr=rocketmq-nameserver1:9876;
rocketmq-nameserver2:9876;rocketmq-nameserver3:9876;rocketmq-nameserver4:9876
# When sending a message , Automatically create a server that does not exist topic, Default number of queues created defaultTopicQueueNums=4 # Is it allowed Broker
Automatic creation Topic, Suggest to open offline , Online shutdown autoCreateTopicEnable=true # Is it allowed Broker
Automatically create subscription groups , Suggest to open offline , Online shutdown autoCreateSubscriptionGroup=true #Broker External service listening port
listenPort=10911 # Delete file time point , Default to early morning 4 spot deleteWhen=04 # File retention time , default 48 hour
fileReservedTime=120 #commitLog Default size per file 1G mapedFileSizeCommitLog=1073741824
#ConsumeQueue Each file is saved by default 30W strip , Adjust according to business situation mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000 #redeleteHangedFileInterval=120000
# Detect physical file disk space diskMaxUsedSpaceRatio=88 # Storage path storePathRootDir=
/usr/local/rocketmq/store#commitLog Storage path storePathCommitLog=
/usr/local/rocketmq/store/commitlog# Consumption queue storage path storage path storePathConsumeQueue=
/usr/local/rocketmq/store/consumequeue# Message index storage path storePathIndex=
/usr/local/rocketmq/store/index#checkpoint File storage path storeCheckpoint=
/usr/local/rocketmq/store/checkpoint#abort File storage path abortFile=
/usr/local/rocketmq/store/abort# Limited message size maxMessageSize=65536
#flushCommitLogLeastPages=4 #flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000 #flushConsumeQueueThoroughInterval=60000
#Broker The role of #- ASYNC_MASTER Asynchronous replication Master #- SYNC_MASTER Synchronous double writing Master #- SLAVE
brokerRole=ASYNC_MASTER # Brush disk mode #- ASYNC_FLUSH Asynchronous disk brushing #- SYNC_FLUSH Synchronous brush disk
flushDiskType=ASYNC_FLUSH #checkTransactionMessageEnable=false # Number of message sending thread pools
#sendMessageThreadPoolNums=128 # Number of pull message thread pools #pullMessageThreadPoolNums=128
broker-a-s.properties,broker-b-s.properties The configuration is as follows :
# Cluster name brokerClusterName=rocketmq-cluster
#broker name , Note that different configuration files are filled in differently , And Master adopt brokerName To match brokerName=broker-a|broker-b
#0 express Master,>0 express Slave brokerId=1 #nameServer address , Semicolon segmentation namesrvAddr=
rocketmq-nameserver1:9876;rocketmq-nameserver2:9876;rocketmq-nameserver3:9876;
rocketmq-nameserver4:9876# When sending a message , Automatically create a server that does not exist topic, Default number of queues created defaultTopicQueueNums
=4 # Is it allowed Broker Automatic creation Topic, Suggest to open offline , Online shutdown autoCreateTopicEnable=true # Is it allowed Broker
Automatically create subscription groups , Suggest to open offline , Online shutdown autoCreateSubscriptionGroup=true #Broker External service listening port
listenPort=10911 # Delete file time point , Default to early morning 4 spot deleteWhen=04 # File retention time , default 48 hour
fileReservedTime=120 #commitLog Default size per file 1G mapedFileSizeCommitLog=1073741824
#ConsumeQueue Each file is saved by default 30W strip , Adjust according to business situation mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000 #redeleteHangedFileInterval=120000
# Detect physical file disk space diskMaxUsedSpaceRatio=88 # Storage path storePathRootDir=
/usr/local/rocketmq/store#commitLog Storage path storePathCommitLog=
/usr/local/rocketmq/store/commitlog# Consumption queue storage path storage path storePathConsumeQueue=
/usr/local/rocketmq/store/consumequeue# Message index storage path storePathIndex=
/usr/local/rocketmq/store/index#checkpoint File storage path storeCheckpoint=
/usr/local/rocketmq/store/checkpoint#abort File storage path abortFile=
/usr/local/rocketmq/store/abort# Limited message size maxMessageSize=65536
#flushCommitLogLeastPages=4 #flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000 #flushConsumeQueueThoroughInterval=60000
#Broker The role of #- ASYNC_MASTER Asynchronous replication Master #- SYNC_MASTER Synchronous double writing Master #- SLAVE
brokerRole=SLAVE # Brush disk mode #- ASYNC_FLUSH Asynchronous disk brushing #- SYNC_FLUSH Synchronous brush disk flushDiskType=
ASYNC_FLUSH#checkTransactionMessageEnable=false # Number of message sending thread pools
#sendMessageThreadPoolNums=128 # Number of pull message thread pools #pullMessageThreadPoolNums=128
* Modify log configuration file 【113,114 Two machines 】 mkdir -p /usr/local/rocketmq/logs cd
/usr/local/rocketmq/conf&& sed -i 's#${user.home}#/usr/local/rocketmq#g' *.xml
* Modify script startup parameters 【113,114 Two machines 】 vim /usr/local/rocketmq/bin/runbroker.sh
#==============================================================================
# development environment JVM Configuration
#==============================================================================
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:PermSize=128m
-XX:MaxPermSize=320m" vim /usr/local/rocketmq/bin/runserver.sh JAVA_OPT="
${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:PermSize=128m
-XX:MaxPermSize=320m"
* start-up NameServer【 Four machines 】 cd /usr/local/rocketmq/bin nohup sh mqnamesrv &
* start-up Master1:BrokerServerA【192.168.11.121】 cd /usr/local/rocketmq/bin nohup sh
mqbroker -c /usr/local/rocketmq/conf/2m-2s-async/broker-a.properties>/dev/null 2
>&1 & netstat -ntlp jps tail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/broker.logtail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/namesrv.log
* start-up Master2:BrokerServerB【192.168.11.122】 cd /usr/local/rocketmq/bin nohup sh
mqbroker -c /usr/local/rocketmq/conf/2m-2s-async/broker-b.properties>/dev/null 2
>&1 & netstat -ntlp jps tail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/broker.logtail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/namesrv.log
* start-up Master1-Slave:BrokerServerC【192.168.11.123】 cd /usr/local/rocketmq/bin
nohup sh mqbroker -c /usr/local/rocketmq/conf/2m-2s-async/broker-a-s.properties
>/dev/null 2>&1 & netstat -ntlp jps tail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/broker.logtail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/namesrv.log
* start-up Master2-Slave:BrokerServerD【192.168.11.124】 cd /usr/local/rocketmq/bin
nohup sh mqbroker -c /usr/local/rocketmq/conf/2m-2s-async/broker-b-s.properties
>/dev/null 2>&1 & netstat -ntlp jps tail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/broker.logtail -f -n 500
/usr/local/rocketmq/logs/rocketmqlogs/namesrv.log
* Service stopped ( Shut down first 4 individual BrokerServer, Close again 4 individual NameServer): cd /usr/local/rocketmq/bin sh
mqshutdown broker sh mqshutdown namesrv

Technology