How to Build a High‑Availability RocketMQ Cluster on Two Servers
This guide walks through the complete process of setting up a fault‑tolerant RocketMQ cluster on two Linux servers, covering environment preparation, Java installation, NameServer and Broker deployment, configuration of master‑slave modes, startup commands, troubleshooting, port opening, verification, and optional console installation.
Cluster deployment modes
RocketMQ supports several high‑availability deployment patterns. Single‑master mode is not recommended for production. Multi‑master options include:
Multiple masters without slaves : simple configuration and highest throughput, but messages on a failed node are unavailable until recovery.
Multiple masters with asynchronous replication slaves : provides data safety and near‑real‑time delivery; a slave failure causes minimal message loss.
Multiple masters with synchronous double‑write slaves : eliminates single points of failure and guarantees zero message delay, at the cost of roughly 10% lower throughput.
This guide demonstrates the asynchronous multi‑master, multi‑slave (2m‑2s‑async) topology using two servers with IPs 192.168.31.186 and 192.168.31.231.
Installation environment
OS: CentOS 7
RocketMQ version: 4.7.0
Java version: JDK 1.8
Step‑by‑step installation
1. Install Java
Download the JDK tarball (e.g., jdk-8u161-linux-x64.tar.gz).
Create a directory and extract the package:
mkdir /usr/local/java
cd /usr/local/java
tar zxvf jdk-8u161-linux-x64.tar.gzSet environment variables in /etc/profile:
export JAVA_HOME=/usr/local/java/jdk1.8.0_161
export CLASSPATH=.:$JAVA_HOME/lib
export PATH=$PATH:$JAVA_HOME/binApply the changes: source /etc/profile Verify:
java -version2. Install NameServer
Create a data directory: mkdir /data Download RocketMQ binary package:
wget https://mirror.bit.edu.cn/apache/rocketmq/4.7.0/rocketmq-all-4.7.0-bin-release.zipUnzip the package: unzip rocketmq-all-4.7.0-bin-release.zip Start NameServer in background:
nohup sh /data/rocketmq-all-4.7.0-bin-release/bin/mqnamesrv &Confirm it is running with jps (look for NamesrvStartup).
3. Install brokers (2‑master‑2‑slave asynchronous replication)
3.1 Broker configuration files
Example master configuration ( broker-a.properties) on 192.168.31.186:
brokerClusterName=rocketmq-cluster
brokerName=broker-a
brokerId=0 # 0 = master
brokerRole=ASYNC_MASTER
flushDiskType=SYNC_FLUSH
listenPort=10911
namesrvAddr=192.168.31.186:9876;192.168.31.231:9876
defaultTopicQueueNums=8
autoCreateTopicEnable=false
autoCreateSubscriptionGroup=false
brokerIP1=192.168.31.186
storePathRootDir=/data/rocketmq-all-4.7.0-bin-release/data/store
storePathCommitLog=/data/rocketmq-all-4.7.0-bin-release/data/store/commitlog
storePathConsumerQueue=/data/rocketmq-all-4.7.0-bin-release/data/store/consumequeue
storePathIndex=/data/rocketmq-all-4.7.0-bin-release/data/store/index
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000Corresponding slave configuration ( broker-b-s.properties) changes brokerId to 1, sets brokerRole=SLAVE, and uses a separate storage path.
3.2 Start brokers
# On 192.168.31.186
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties &
# On 192.168.31.231
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties &
# Start slaves
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties &
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties &3.3 Common issues
Memory allocation : If the JVM cannot allocate the default 8 GB heap, edit bin/runbroker.sh and reduce the heap size, e.g.:
JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn512m"Port access : Open firewall ports 9876 (NameServer) and 10911, 11011, 10909, 11009 (Brokers).
4. Verification
Check broker logs under /data/rocketmq-all-4.7.0-bin-release/conf/logback_broker.xml for normal startup messages. Use the mqadmin tool to list cluster status:
sh bin/mqadmin clusterList -n "192.168.31.186:9876;192.168.31.231:9876"The command outputs each broker’s ID, address, version, and load metrics.
5. Shutdown procedure
sh bin/mqshutdown broker
sh bin/mqshutdown namesrv6. Optional: Install RocketMQ Console
Install Maven, set MAVEN_HOME and add $MAVEN_HOME/bin to PATH.
Clone the console source:
git clone https://github.com/apache/rocketmq-externals.gitBuild the console:
cd rocketmq-externals/rocketmq-console
mvn clean package -Dmaven.test.skip=trueRun the console:
java -jar target/rocketmq-console-ng-1.0.1.jar --rocketmq.config.namesrvAddr='192.168.31.186:9876;192.168.31.231:9876'Access the UI at http://192.168.31.186:8080 to monitor topics, producers, and consumers.
7. References
RocketMQ Quick Start: http://rocketmq.apache.org/docs/quick-start/
Official documentation (Chinese): https://github.com/apache/rocketmq/tree/master/docs/cn
RocketMQ Console repository: https://github.com/apache/rocketmq-externals/tree/master/rocketmq-console
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Architects Alliance
Discussion and exchange on system, internet, large‑scale distributed, high‑availability, and high‑performance architectures, as well as big data, machine learning, AI, and architecture adjustments with internet technologies. Includes real‑world large‑scale architecture case studies. Open to architects who have ideas and enjoy sharing.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
