之前写过一篇roketmq的单机使用:springboot整合rocketMq示例
在4.5 版本开始,rocketMq新支持了基于raft协议dledger集群,这个集群类似之前搭建过的vault集群,采用选举模式,最少三台服务器组成一主两从的集群,其中主挂了从会自动选举一个变成主,这是一个比较完美的集群方案。
服务器以及环境信息
服务器:ubuntu20.04 分别为 192.168.0.120 192.168.0.117 192.168.0.118 三台
jdk: openjdk-8-jdk
下载并解压安装包
以4.9.4为例(最新的5.0.0没有成功,启动脚本会报错)
在192.168.0.120的某个文件夹下执行如下命令,我所在的文件夹是/home/zhaohy/profiles
wget https://archive.apache.org/dist/rocketmq/4.9.4/rocketmq-all-4.9.4-bin-release.zip
unzip rocketmq-all-4.9.4-bin-release.zip
mv rocketmq-all-4.9.4-bin-release rocketmq-4.9.4
修改启动内存
真实服务器上可以不修改(内存够用的话),默认一个server占4g,一个broker占8g,一般家用电脑扛不住,修改两个文件
1.将bin文件夹下的runbroker.sh的JAVA_OPT标识内存的地方改成
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m"
2.将runserver.sh的JAVA_OPT标识内存的地方改成
JAVA_OPT="${JAVA_OPT} -server -Xms128m -Xmx256m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
修改conf/dledger文件夹下的三个配置文件
broker-n0.conf:
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
brokerClusterName = RaftCluster
brokerName=RaftNode00
listenPort=30911
namesrvAddr=192.168.0.120:9876;192.168.0.117:9876;192.168.0.118:9876
storePathRootDir=/home/zhaohy/profiles/rocketmq-4.9.4/data/broker0/store
storePathCommitLog=/home/zhaohy/profiles/rocketmq-4.9.4/data/broker0/commitlog
enableDLegerCommitLog=true
dLegerGroup=RaftNode00
dLegerPeers=n0-192.168.0.120:40911;n1-192.168.0.117:40912;n2-192.168.0.118:40913
## must be unique
dLegerSelfId=n0
sendMessageThreadPoolNums=16
#开启异步刷盘
flushDiskType=ASYNC_FLUSH
brokerRole = ASYNC_MASTER
#线上关闭自动创建topic
autoCreateTopicEnable=true
#开启临时存储池-异步刷盘建议开启
TransientStorePoolEnable=true
autoCreateSubscriptionGroup=true
#同步刷盘建议使用重入锁-异步建议关闭
useReentrantLockWhenPutMessage=false
#发送消息的最大线程数,默认1,同步刷盘建议适当增大,建议配置成CPU核数
sendMessageThreadPoolNums=8
#关闭堆内存数据传输-建议关闭,可以提高拉消息效率
transferMsgByHeap=false
#开启消息轨迹,建议再集群中新增加个节点,并仅再新增节点上开启消息轨迹
traceTopicEnable=true
#开启从Slave读数据功能
slaveReadEnable=true
#启用ACL访问控制(不添加ACL访问控制,可直接跳过ACL访问控制的配置)
aclEnable=false
brokerIP1=192.168.0.120
deleteWhen = 04
fileReservedTime = 48
broker-n1.conf:
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
brokerClusterName = RaftCluster
brokerName=RaftNode00
listenPort=30921
namesrvAddr=192.168.0.120:9876;192.168.0.117:9876;192.168.0.118:9876
storePathRootDir=/home/zhaohy/profiles/rocketmq-4.9.4/data/broker1/store
storePathCommitLog=/home/zhaohy/profiles/rocketmq-4.9.4/data/broker1/commitlog
enableDLegerCommitLog=true
dLegerGroup=RaftNode00
dLegerPeers=n0-192.168.0.120:40911;n1-192.168.0.117:40912;n2-192.168.0.118:40913
## must be unique
dLegerSelfId=n1
sendMessageThreadPoolNums=16
#开启异步刷盘
flushDiskType=ASYNC_FLUSH
brokerRole = ASYNC_MASTER
#线上关闭自动创建topic
autoCreateTopicEnable=true
#开启临时存储池-异步刷盘建议开启
TransientStorePoolEnable=true
autoCreateSubscriptionGroup=true
#同步刷盘建议使用重入锁-异步建议关闭
useReentrantLockWhenPutMessage=false
#发送消息的最大线程数,默认1,同步刷盘建议适当增大,建议配置成CPU核数
sendMessageThreadPoolNums=8
#关闭堆内存数据传输-建议关闭,可以提高拉消息效率
transferMsgByHeap=false
#开启消息轨迹,建议再集群中新增加个节点,并仅再新增节点上开启消息轨迹
traceTopicEnable=true
#开启从Slave读数据功能
slaveReadEnable=true
#启用ACL访问控制(不添加ACL访问控制,可直接跳过ACL访问控制的配置)
aclEnable=false
brokerIP1=192.168.0.117
deleteWhen = 04
fileReservedTime = 48
broker-n2.conf:
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
brokerClusterName = RaftCluster
brokerName=RaftNode00
listenPort=30931
namesrvAddr=192.168.0.120:9876;192.168.0.117:9876;192.168.0.118:9876
storePathRootDir=/home/zhaohy/profiles/rocketmq-4.9.4/data/broker2/store
storePathCommitLog=/home/zhaohy/profiles/rocketmq-4.9.4/data/broker2/commitlog
enableDLegerCommitLog=true
dLegerGroup=RaftNode00
dLegerPeers=n0-192.168.0.120:40911;n1-192.168.0.117:40912;n2-192.168.0.118:40913
## must be unique
dLegerSelfId=n2
sendMessageThreadPoolNums=16
#开启异步刷盘
flushDiskType=ASYNC_FLUSH
brokerRole = ASYNC_MASTER
#线上关闭自动创建topic
autoCreateTopicEnable=true
#开启临时存储池-异步刷盘建议开启
TransientStorePoolEnable=true
autoCreateSubscriptionGroup=true
#同步刷盘建议使用重入锁-异步建议关闭
useReentrantLockWhenPutMessage=false
#发送消息的最大线程数,默认1,同步刷盘建议适当增大,建议配置成CPU核数
sendMessageThreadPoolNums=8
#关闭堆内存数据传输-建议关闭,可以提高拉消息效率
transferMsgByHeap=false
#开启消息轨迹,建议再集群中新增加个节点,并仅再新增节点上开启消息轨迹
traceTopicEnable=true
#开启从Slave读数据功能
slaveReadEnable=true
#启用ACL访问控制(不添加ACL访问控制,可直接跳过ACL访问控制的配置)
aclEnable=false
brokerIP1=192.168.0.118
deleteWhen = 04
fileReservedTime = 48
新建数据存储目录
可以看到上面的配置文件里,是有配置storePathRootDir和storePathCommitLog,要保证这两个路径的文件夹存在
cd /home/zhaohy/profiles/rocketmq-4.9.4
mkdir -p data/broker0/store
mkdir -p data/broker0/logs
mkdir -p data/broker1/store
mkdir -p data/broker1/logs
mkdir -p data/broker2/store
mkdir -p data/broker2/logs
把配置好的rocketmq-4.9.4文件夹复制到其他两台服务器上
cd /home/zhaohy/profiles/
scp -r rocketmq-4.9.4/ 192.168.0.117:/home/zhaohy/profiles/
scp -r rocketmq-4.9.4/ 192.168.0.118:/home/zhaohy/profiles/
分别在三台服务器上启动mqnamesrv和mqbroker
192.168.0.120:
cd /home/zhaohy/profiles/rocketmq-4.9.4
nohup sh bin/mqnamesrv &
nohup sh bin/mqbroker -c conf/dledger/broker-n0.conf > data/broker0/logs/broker.log 2>&1 &
192.168.0.117:
cd /home/zhaohy/profiles/rocketmq-4.9.4
nohup sh bin/mqnamesrv &
nohup sh bin/mqbroker -c conf/dledger/broker-n1.conf > data/broker1/logs/broker.log 2>&1 &
192.168.0.118:
cd /home/zhaohy/profiles/rocketmq-4.9.4
nohup sh bin/mqnamesrv &
nohup sh bin/mqbroker -c conf/dledger/broker-n2.conf > data/broker2/logs/broker.log 2>&1 &
查看集群列表
在任一一台服务器上执行
cd /home/zhaohy/profiles/rocketmq-4.9.4
./bin/mqadmin clusterList -n 192.168.0.120:9876
结果如下:
zhaohy@zhaohy-VirtualBox:~/profiles/rocketmq-4.9.4$ ./bin/mqadmin clusterList -n 192.168.0.120:9876
#Cluster Name #Broker Name #BID #Addr #Version #InTPS(LOAD) #OutTPS(LOAD) #PCWait(ms) #Hour #SPACE
RaftCluster RaftNode00 0 192.168.0.120:30911 V4_9_4 0.00(0,0ms) 0.00(0,0ms) 0 464262.37 0.9400
RaftCluster RaftNode00 2 192.168.0.117:30921 V4_9_4 0.00(0,0ms) 0.00(0,0ms) 0 464262.37 0.4200
RaftCluster RaftNode00 3 192.168.0.118:30931 V4_9_4 0.00(0,0ms) 0.00(0,0ms) 0 464262.37 0.4000
可以看到集群搭建成功。
启动rocketmq客户端web应用截图如下:
地址配置:
rocketmq.config.namesrvAddr= 192.168.0.117:9876;192.168.0.118:9876;192.168.0.120:9876

可以看到一主两从已经搭建成功
主的挂掉,从的会自动变成主的,挂掉的再重新启动起来就自动作为从的加入这个集群
操作命令总结
# 启动mqnamesrv
nohup sh bin/mqnamesrv &
# 启动mqbroker
nohup sh bin/mqbroker -c conf/dledger/broker-n0.conf > data/broker0/logs/broker.log 2>&1 &
# 查看集群列表
./bin/mqadmin clusterList -n 192.168.0.120:9876
# 停止mqbroker
./bin/mqshutdown broker
# 停止mqnamesrv
./bin/mqshutdown namesrv
设置开机启动脚本
以192.168.0.120为列,其他两台服务器省略
在/home/zhaohy/profiles/rocketmq-4.9.4文件夹下新建mqnamesrv.service和mqbroker.service
mqnamesrv.service
[Unit]
Description=mqnamesrv
After=syslog.target
[Service]
#ExecStart=mkdir /home/zhaohy/Desktop/test
ExecStart=nohup sh /home/zhaohy/profiles/rocketmq-4.9.4/bin/mqnamesrv &
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
mqbroker.service:
[Unit]
Description=mqbroker
After=syslog.target
[Service]
#ExecStart=mkdir /home/zhaohy/Desktop/test
ExecStart=nohup sh /home/zhaohy/profiles/rocketmq-4.9.4/bin/mqbroker -c /home/zhaohy/profiles/rocketmq-4.9.4/conf/dledger/broker-n0.conf > /home/zhaohy/profiles/rocketmq-4.9.4/data/broker0/logs/broker.log 2>&1 &
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
systemctl设置开机启动
sudo systemctl enable /home/zhaohy/profiles/rocketmq-4.9.4/mqnamesrv.service
sudo systemctl enable /home/zhaohy/profiles/rocketmq-4.9.4/mqbroker.service
关于systemctl开机启动更多可见之前记录的一篇:ubuntu20-04利用systemctl命令实现开机自启服务
完结撒花~
参考:https://blog.csdn.net/L2111533547/article/details/125564421
网友评论