Build rocketmq4.0 under Linux 5.2 cluster and set startup and self startup

Posted by saedm on Mon, 14 Feb 2022 11:23:56 +0100

1, Environmental description

    CentOS7;RocketMQ4.5.2;The firewall is closed; Virtual machine address:192.168.0.40,192.168.0.41,192.168.0.42,192.168.0.75;

Because other things have been built on the virtual machine before, the alias will be specified this time, which can be accessed directly through ip and port;

2, Software description

    RocketMQ It was originally Ali's own and later donated to Apache. As of the time I wrote this blog, Apache announced it RocketMQ The version of is 4.5.2,So this installation is 4.5.2 of

Download address: http://rocketmq.apache.org/ , click to download the latest version displayed on the home page. If you need the previous version, click Documentation – "download on the left –" select the version you want to install to install. If you select the Binary version, you can install it directly. Do not select the Source version, it is the Source version, which cannot be used directly and has to be compiled.


3, Stand alone build

I installed the stand-alone version on the 192.168.0.75 virtual machine

1. Upload the installation package to the / root directory of the 75 virtual machine

2. Unzip rocketmq-all-4.5.2-bin-release zip,mv rocketmq-all-4.5.2-bin-release rocketmq

3. Create the directory where RocketMQ stores files, and execute the following commands in sequence:

cd rocketmq
mkdir logs
mkdir store
cd store/
mkdir commitlog
mkdir consumequeue
mkdir index


Folder Description:

logs:storage RocketMQ Log directory`Insert code slice here`
store:storage RocketMQ Data file directory
commitlog:storage RocketMQ Message information
consumequeue,index:Index data for storing messages

4. Modify the configuration file and enter the conf directory of rocketmq


conf directory configuration file description:

2m-2s-async:2 Master 2 slave asynchronous
2m-2s-sync :2 Master 2 slave synchronization
2m-noslave :2 The LORD did not follow

Because we want to build a stand-alone version, we only need to modify the files in one of the folders. We can modify the configuration and implementation of 2m-2s-async.

4.1. Enter the 2m-2s-async directory and modify the first configuration file, broker-a.properties and VIM broker-a.properties

Overwrite all configurations of broker-a.properties with the following configuration:

#Cluster name
brokerClusterName=rocketmq-cluster
#The name of the broker. Note that different configuration files here are filled in differently
brokerName=broker-a
#0 means master, > 0 means slave
brokerId=0
#nameServer address, semicolon split
namesrvAddr=192.168.0.75:9876
#When sending messages, automatically create topics that do not exist in the server. The number of queues created by default
defaultTopicQueueNums=4
#Whether to allow the Broker to automatically create topics. It is recommended to open offline and close online
autoCreateTopicEnable=true
#Whether to allow the Broker to automatically create subscription groups. It is recommended to open offline and close online
autoCreateSubscriptionGroup=true
#Listening port of Broker external service
listenPort=10911
#Delete the file at 4 a.m. by default
deleteWhen=04
#File retention time, 48 hours by default
fileReservedTime=120
#commitLog the default size of each file is 1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue stores 30W files by default, which can be adjusted according to business conditions
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#Detect physical file disk space
diskMaxUsedSpaceRatio=88
#Storage path
storePathRootDir=/root/rocketmq/store
#commitLog storage path
storePathCommitLog=/root/rocketmq/store/commitlog
#Consumption queue storage path
storePathConsumeQueue=/root/rocketmq/store/consumequeue
#Message index storage path
storePathIndex=/root/rocketmq/store/index
#checkpoint file storage path
storeCheckpoint=/root/rocketmq/store/checkpoint
#abort file storage path
abortFile=/root/rocketmq/store/abort
#Limited message size
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Role of Broker
# - ASYNC_MASTER asynchronous replication master
# - SYNC_MASTER synchronous double write master
# - SLAVE
brokerRole=ASYNC_MASTER
# Disc brushing mode
# - ASYNC_FLUSH asynchronous brush disk
# - SYNC_FLUSH synchronous brush disc
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#Number of message sending thread pools
#sendMessageTreadPoolNums=128
#Number of pull message thread pools
#pullMessageTreadPoolNums=128

4.2. Enter the conf directory and replace ${user.home} in all xml to ensure that the log path is correct

sed -i 's#${user.home}#/root/rocketmq#g' *.xml

Note: sed -i plays the role of batch replacement here,

sed -i 's#Original string#New character#g 'replaced file

5. RocketMQ has high requirements for memory, at least 1G. If there is too little memory, it will affect the operation efficiency and execution performance of RocketMQ. We need to modify runbroker. Com under the bin directory SH and runserver SH file

runbroker.sh

Before modification:
JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
 
After modification:
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn1g"

runserver.sh

Before modification:
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
 
After modification:
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

6. When starting RocketMQ, please start namesrv before starting broker

Start namesrv, nohup sh mqnamesrv&

Restart the broker, nohup sh mqbroker - C / root / rocketmq / conf / 2m-2s-async / broker-a.properties > / dev / null 2 > & 1&

Enter jps to view the process. You can see that NamesrvStartup and BrokerStartup processes represent the start

4, RocketMQ console installation

Here, we directly put the source code of RocketMQ control code into IDEA and run it. Later, we can package it into jar package and run it on the virtual machine. The RocketMQ console is provided by some RocketMQ enthusiasts.

Download and import the RocketMQ connect console into our local idea, and then modify the application Properties to configure the namesrv address of RocketMQ, as shown in the following figure:


After modification, find the startup class. After startup, you can view the following figure. After RocketMQ receives the message, you can view the message on the console

5, Cluster construction

1. Construction instructions

I have prepared four virtual machines: 192.168.0.40192.168.0.41192.168.0.42192.168.0.75

It is planned to build a two master and two slave asynchronous disk brushing synchronous replication cluster, with 40 and 41 as the master and 42 and 75 as the slave

The message will be written to the disk by the client asynchronously. If the message is received from the disk, the message will be written to the disk directly. If the message is written to the disk asynchronously, the message will be written to the disk from the client. If the message is received later, it will be written to the disk directly. Synchronous disk brushing frequently interacts with the disk, and the efficiency is not as high as that of asynchronous disk brushing.

Replication: broker s are divided into master and slave. After the master receives the message, it is copied to the slave node and then fed back to the client. It is synchronous replication, otherwise it is asynchronous replication.

The common mode in work is the combination of asynchronous disk brushing and synchronous replication to ensure that there is no single point.

2. Environmental preparation

RocketMQ has just been installed on 75. Now we want to use it as a slave node. If 75 is running, stop RocketMQ on 75 first. When starting, start namesrv and then start broker. When stopping, stop broker and then namesrv.

Enter the bin folder of rocketmq in 75 and execute the following command:

sh mqshutdown broker
sh mqshutdown namesrv

3. Extract the rocketmq compressed files from the four machines together, and finally store the extracted files in the / root/rocketmq directory.

Perform the following operations in the rocketmq directory of the four machines:

mkdir logs
mkdir store
cd store/
mkdir commitlog
mkdir consumequeue
mkdir index

Enter the rocketmq/conf/2m-2s-sync directory of cluster 40, configure the corresponding configuration file, and modify the broker-a.properties configuration file as follows:

#Cluster name
brokerClusterName=rocketmq-cluster
#The name of the broker. Note that different configuration files here are filled in differently
brokerName=broker-a
#0 means master, > 0 means slave
brokerId=0
#nameServer address, semicolon split
namesrvAddr=192.168.0.40:9876;192.168.0.41:9876;192.168.0.42:9876;192.168.0.75:9876
#When sending messages, automatically create topics that do not exist in the server. The number of queues created by default
defaultTopicQueueNums=4
#Whether to allow the Broker to automatically create topics. It is recommended to open offline and close online
autoCreateTopicEnable=true
#Whether to allow the Broker to automatically create subscription groups. It is recommended to open offline and close online
autoCreateSubscriptionGroup=true
#Listening port of Broker external service
listenPort=10911
#Delete the file at 4 a.m. by default
deleteWhen=04
#File retention time, 48 hours by default
fileReservedTime=120
#commitLog the default size of each file is 1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue stores 30W files by default, which can be adjusted according to business conditions
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#Detect physical file disk space
diskMaxUsedSpaceRatio=88
#Storage path
storePathRootDir=/root/rocketmq/store
#commitLog storage path
storePathCommitLog=/root/rocketmq/store/commitlog
#Consumption queue storage path
storePathConsumeQueue=/root/rocketmq/store/consumequeue
#Message index storage path
storePathIndex=/root/rocketmq/store/index
#checkpoint file storage path
storeCheckpoint=/root/rocketmq/store/checkpoint
#abort file storage path
abortFile=/root/rocketmq/store/abort
#Limited message size
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Role of Broker
# - ASYNC_MASTER asynchronous replication master
# - SYNC_MASTER synchronous double write master
# - SLAVE
brokerRole=SYNC_MASTER
# Disc brushing mode
# - ASYNC_FLUSH asynchronous brush disk
# - SYNC_FLUSH synchronous brush disc
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#Number of message sending thread pools
#sendMessageTreadPoolNums=128
#Number of pull message thread pools
#pullMessageTreadPoolNums=128

Modify the broker-b.properties file as follows:

#Cluster name
brokerClusterName=rocketmq-cluster
#The name of the broker. Note that different configuration files here are filled in differently
brokerName=broker-b
#0 means master, > 0 means slave
brokerId=0
#nameServer address, semicolon split
namesrvAddr=192.168.0.40:9876;192.168.0.41:9876;192.168.0.42:9876;192.168.0.75:9876
#When sending messages, automatically create topics that do not exist in the server. The number of queues created by default
defaultTopicQueueNums=4
#Whether to allow the Broker to automatically create topics. It is recommended to open offline and close online
autoCreateTopicEnable=true
#Whether to allow the Broker to automatically create subscription groups. It is recommended to open offline and close online
autoCreateSubscriptionGroup=true
#Listening port of Broker external service
listenPort=10911
#Delete the file at 4 a.m. by default
deleteWhen=04
#File retention time, 48 hours by default
fileReservedTime=120
#commitLog the default size of each file is 1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue stores 30W files by default, which can be adjusted according to business conditions
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#Detect physical file disk space
diskMaxUsedSpaceRatio=88
#Storage path
storePathRootDir=/root/rocketmq/store
#commitLog storage path
storePathCommitLog=/root/rocketmq/store/commitlog
#Consumption queue storage path
storePathConsumeQueue=/root/rocketmq/store/consumequeue
#Message index storage path
storePathIndex=/root/rocketmq/store/index
#checkpoint file storage path
storeCheckpoint=/root/rocketmq/store/checkpoint
#abort file storage path
abortFile=/root/rocketmq/store/abort
#Limited message size
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Role of Broker
# - ASYNC_MASTER asynchronous replication master
# - SYNC_MASTER synchronous double write master
# - SLAVE
brokerRole=SYNC_MASTER
# Disc brushing mode
# - ASYNC_FLUSH asynchronous brush disk
# - SYNC_FLUSH synchronous brush disc
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#Number of message sending thread pools
#sendMessageTreadPoolNums=128
#Number of pull message thread pools
#pullMessageTreadPoolNums=128

Modify the broker-a-s.properties file as follows:

#Cluster name
brokerClusterName=rocketmq-cluster
#The name of the broker. Note that different configuration files here are filled in differently
brokerName=broker-a
#0 means master, > 0 means slave
brokerId=1
#nameServer address, semicolon split
namesrvAddr=192.168.0.40:9876;192.168.0.41:9876;192.168.0.42:9876;192.168.0.75:9876
#When sending messages, automatically create topics that do not exist in the server. The number of queues created by default
defaultTopicQueueNums=4
#Whether to allow the Broker to automatically create topics. It is recommended to open offline and close online
autoCreateTopicEnable=true
#Whether to allow the Broker to automatically create subscription groups. It is recommended to open offline and close online
autoCreateSubscriptionGroup=true
#Listening port of Broker external service
listenPort=10911
#Delete the file at 4 a.m. by default
deleteWhen=04
#File retention time, 48 hours by default
fileReservedTime=120
#commitLog the default size of each file is 1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue stores 30W files by default, which can be adjusted according to business conditions
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#Detect physical file disk space
diskMaxUsedSpaceRatio=88
#Storage path
storePathRootDir=/root/rocketmq/store
#commitLog storage path
storePathCommitLog=/root/rocketmq/store/commitlog
#Consumption queue storage path
storePathConsumeQueue=/root/rocketmq/store/consumequeue
#Message index storage path
storePathIndex=/root/rocketmq/store/index
#checkpoint file storage path
storeCheckpoint=/root/rocketmq/store/checkpoint
#abort file storage path
abortFile=/root/rocketmq/store/abort
#Limited message size
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Role of Broker
# - ASYNC_MASTER asynchronous replication master
# - SYNC_MASTER synchronous double write master
# - SLAVE
brokerRole=SLAVE
# Disc brushing mode
# - ASYNC_FLUSH asynchronous brush disk
# - SYNC_FLUSH synchronous brush disc
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#Number of message sending thread pools
#sendMessageTreadPoolNums=128
#Number of pull message thread pools
#pullMessageTreadPoolNums=128

Modify the broker-b-s.properties file as follows:

#Cluster name
brokerClusterName=rocketmq-cluster
#The name of the broker. Note that different configuration files here are filled in differently
brokerName=broker-b
#0 means master, > 0 means slave
brokerId=1
#nameServer address, semicolon split
namesrvAddr=192.168.0.40:9876;192.168.0.41:9876;192.168.0.42:9876;192.168.0.75:9876
#When sending messages, automatically create topics that do not exist in the server. The number of queues created by default
defaultTopicQueueNums=4
#Whether to allow the Broker to automatically create topics. It is recommended to open offline and close online
autoCreateTopicEnable=true
#Whether to allow the Broker to automatically create subscription groups. It is recommended to open offline and close online
autoCreateSubscriptionGroup=true
#Listening port of Broker external service
listenPort=10911
#Delete the file at 4 a.m. by default
deleteWhen=04
#File retention time, 48 hours by default
fileReservedTime=120
#commitLog the default size of each file is 1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue stores 30W files by default, which can be adjusted according to business conditions
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#Detect physical file disk space
diskMaxUsedSpaceRatio=88
#Storage path
storePathRootDir=/root/rocketmq/store
#commitLog storage path
storePathCommitLog=/root/rocketmq/store/commitlog
#Consumption queue storage path
storePathConsumeQueue=/root/rocketmq/store/consumequeue
#Message index storage path
storePathIndex=/root/rocketmq/store/index
#checkpoint file storage path
storeCheckpoint=/root/rocketmq/store/checkpoint
#abort file storage path
abortFile=/root/rocketmq/store/abort
#Limited message size
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Role of Broker
# - ASYNC_MASTER asynchronous replication master
# - SYNC_MASTER synchronous double write master
# - SLAVE
brokerRole=SLAVE
# Disc brushing mode
# - ASYNC_FLUSH asynchronous brush disk
# - SYNC_FLUSH synchronous brush disc
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#Number of message sending thread pools
#sendMessageTreadPoolNums=128
#Number of pull message thread pools
#pullMessageTreadPoolNums=128

Copy the node information to each machine, and execute the following command on machine 40:

scp broker-*.properties 192.168.0.41:/root/rocketmq/conf/2m-2s-sync
scp broker-*.properties 192.168.0.42:/root/rocketmq/conf/2m-2s-sync
scp broker-*.properties 192.168.0.75:/root/rocketmq/conf/2m-2s-sync


Enter the conf directory of each machine, replace the log file path, and execute the following command:

sed -i 's#${user.home}#/root/rocketmq#g' *.xml

Enter the bin directory of each machine and change runbroker SH and runserver SH file:

runbroker.sh

Before modification:
JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
 
After modification:
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn1g"

runserver.sh

Before modification:
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
 
After modification:
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

4. Start cluster

Start namesrv on each server first

nohup sh mqnamesrv &

Restart the broker,

40. The server executes the following startup command:

nohup sh mqbroker -c /root/rocketmq/conf/2m-2s-sync/broker-a.properties > /dev/null 2>&1 &

41. The server executes the following startup command:

nohup sh mqbroker -c /root/rocketmq/conf/2m-2s-sync/broker-b.properties > /dev/null 2>&1 &

42. The server executes the following startup command:

nohup sh mqbroker -c /root/rocketmq/conf/2m-2s-sync/broker-a-s.properties > /dev/null 2>&1 &

75 the server executes the following startup command:

nohup sh mqbroker -c /root/rocketmq/conf/2m-2s-sync/broker-b-s.properties > /dev/null 2>&1 &

Enter jps to view the process. If you see NamesrvStartup and BrokerStartup on each machine, it means that the startup is successful.


5. Viewing clusters from the console

In the project application The namesrv address of properties is modified as follows:

rocketmq.config.namesrvAddr=192.168.0.40:9876;192.168.0.41:9876;192.168.0.42:9876;192.168.0.75:9876

6. Start the console service and access the console as follows:

If you want to stop the service here, you can stop the broker first:

sh mqshutdown broker

Close namesrv again:

sh mqshutdown namesrv

7. Problem attention

When you use the console project to view the cluster, you may report: remotingconnectexception: connect to < 172.17.0.1:10909 > failed after startup,

If your version of rocketmq is less than 3.5.8, you need to use the project application. In the console Rocketmq in properties config. Set isvipchannel to false, as shown in the following figure:

If your rocketmq version is greater than 3.5.8, the application of the console project Rocketmq in properties config. Isvipchannel does not need to set any value. Its default value is true, but you need to add brokerIP1 = ip of your machine in the cluster configuration file of the corresponding machine. It is said that it is caused by multiple network cards of your virtual machine. After specifying, there will be no whole problem.

For example: 41 machine, when I start the broker, I use the conf/2m-2s-sync/broker-b.properties file according to the above command. Then I can add brokerIP1 = the ip of your machine in broker-b.properties, as shown in the following figure


6, Use script to set startup and self startup

1. Write management script command, VI / etc / init d/rocketmq

#!/bin/sh
#
# rocketmq - this script starts and stops the rocketmq daemon
#
# chkconfig: - 85 15
 
ROCKETMQ_HOME=/root/rocketmq
ROCKETMQ_BIN=${ROCKETMQ_HOME}/bin
ADDR=192.168.0.75:9876
LOG_DIR=${ROCKETMQ_HOME}/logs
 
start() {
if [ ! -d ${LOG_DIR} ];then
mkdir ${LOG_DIR}
fi
cd ${ROCKETMQ_HOME}
nohup sh bin/mqnamesrv &
echo -n "The Name Server boot success..."
nohup sh bin/mqbroker -c ${ROCKETMQ_HOME}/conf/2m-2s-sync/broker-b-s.properties > /dev/null 2>&1 &
echo -n "The broker[%s, ${ADDR}] boot success..."
}
stop() {
cd ${ROCKETMQ_HOME}
sh bin/mqshutdown broker
sleep 1
sh bin/mqshutdown namesrv
}
restart() {
stop
sleep 5
start
}
 
 
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 2
esac

be careful:

(1.1) Note: copying this script to Linux may lose the top part. I don't know why. If you open Linux through xshell and copy these contents to the file, you will lose part. After copying, check whether you lose some contents. Otherwise, even if you register as a service, it won't work.

(1.2)ROCKETMQ_ The address of home and the configuration file for starting the broker with nohup should be written correctly

2. Add rocketmq service as startup service

chmod +x /etc/init.d/rocketmq
 
chkconfig --add rocketmq

Then run chkconfig --list to check whether the following contents appear. If they appear, it means that the joining is successful

Note: if 0-6 in the figure below is off, it means that the machine cannot be started automatically. Then run chkconfig rocketmq on, and then run chkconfig --list. Only after 2-5 are on.

3. Use the service command to manage rocketmq

Start: service rocketmq start
 close: service rocketmq stop
 Restart: service rocketmq restart

Topics: Java Linux Operation & Maintenance CentOS