每个Master配置一个Slave,有多对Master-Slave,HA采用异步复制方式,主备有短暂消息延迟(毫秒级),这种模式的优缺点如下:
优点:即使磁盘损坏,消息丢失的非常少,且消息实时性不会受影响,同时Master宕机后,消费者仍然可以从Slave消费,而且此过程对应用透明,不需要人工干预,性能同多Master模式几乎一样;
缺点:Master宕机,磁盘损坏情况下会丢失少量消息。
一、下载 RockerMQ安装包
下载二进制编译包:
https://archive.apache.org/dist/rocketmq/4.4.0/rocketmq-all-4.4.0-bin-release.zip
解压
yum install -y zip unzip
unzip rocketmq-all-4.4.0-bin-release.zip
默认启动需要8G内寸机器,如果机器内存小,需要设置启动jvm参数
vim bin/runserver.sh
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m"
vim bin/runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn1g"
三、机器 IP 信息
NodeA:
192.168.56.101
NodeB:
192.168.56.109
四、编辑配置文件cd /root/rocketmq-all-4.4.0-bin-release/conf/2m-2s-async
broker-a.properties
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
listenPort=10911
namesrvAddr=192.168.56.101:9876;192.168.56.109:9876
brokerIP1=192.168.56.101
brokerIP2=192.168.56.101
autoCreateSubscriptionGroup=true
autoCreateTopicEnable=true
storePathRootDir=/home/rocketmq/rocketmq/data
storePathCommitLog=/home/rocketmq/rocketmq/data/commitlog
storePathConsumeQueue=/home/rocketmq/rocketmq/data/consumequeue
storePathIndex=/home/rocketmq/rocketmq/data/index
storeCheckpoint=/home/rocketmq/rocketmq/data/checkpoint
abortFile=/home/rocketmq/rocketmq/data/abort
broker-a-s.properties
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=15911
namesrvAddr=192.168.56.101:9876;192.168.56.109:9876
brokerIP1=192.168.56.109
autoCreateSubscriptionGroup=true
autoCreateTopicEnable=true
storePathRootDir=/home/rocketmq/slave/rocketmq/data
storePathCommitLog=/home/rocketmq/slave/rocketmq/data/commitlog
storePathConsumeQueue=/home/rocketmq/slave/rocketmq/data/consumequeue
storePathIndex=/home/rocketmq/slave/rocketmq/data/index
storeCheckpoint=/home/rocketmq/slave/rocketmq/data/checkpoint
abortFile=/home/rocketmq/slave/rocketmq/data/abort
broker-b.properties
brokerClusterName=DefaultCluster
brokerName=broker-b
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
listenPort=10911
namesrvAddr=192.168.56.101:9876;192.168.56.109:9876
brokerIP1=192.168.56.109
brokerIP2=192.168.56.109
autoCreateSubscriptionGroup=true
autoCreateTopicEnable=true
storePathRootDir=/home/rocketmq/rocketmq/data
storePathCommitLog=/home/rocketmq/rocketmq/data/commitlog
storePathConsumeQueue=/home/rocketmq/rocketmq/data/consumequeue
storePathIndex=/home/rocketmq/rocketmq/data/index
storeCheckpoint=/home/rocketmq/rocketmq/data/checkpoint
abortFile=/home/rocketmq/rocketmq/data/abort
broker-b-s.properties
brokerClusterName=DefaultCluster
brokerName=broker-b
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=15911
namesrvAddr=192.168.56.101:9876;192.168.56.109:9876
brokerIP1=192.168.56.101
autoCreateSubscriptionGroup=true
autoCreateTopicEnable=true
storePathRootDir=/home/rocketmq/slave/rocketmq/data
storePathCommitLog=/home/rocketmq/slave/rocketmq/data/commitlog
storePathConsumeQueue=/home/rocketmq/slave/rocketmq/data/consumequeue
storePathIndex=/home/rocketmq/slave/rocketmq/data/index
storeCheckpoint=/home/rocketmq/slave/rocketmq/data/checkpoint
abortFile=/home/rocketmq/slave/rocketmq/data/abort
五、启动
注意:一个节点放一主一从
A节点:A master B slave
B节点:B maset A slave
nohup sh bin/mqnamesrv > boot.log 2>&1 &
nohup sh bin/mqbroker -c ./conf/2m-2s-async/broker-a.properties >> boot.log 2>&1 &
nohup sh bin/mqbroker -c ./conf/2m-2s-async/broker-b.properties >> boot.log 2>&1 &
nohup sh bin/mqbroker -c ./conf/2m-2s-async/broker-b-s.properties >> boot.log 2>&1 &
nohup sh bin/mqbroker -c ./conf/2m-2s-async/broker-a-s.properties >> boot.log 2>&1 &
sh bin/mqshutdown broker
sh bin/mqshutdown namesrv
tail -f ~/logs/rocketmqlogs/namesrv.log
八、集群状态查看bin/mqadmin clusterList -n localhost:9876
九、测试消息发送export NAMESRV_ADDR=localhost:9876
消息生产sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
消息消费sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
十、简单运维脚本startNodeA.sh
nohup sh bin/mqnamesrv > boot.log 2>&1 &
sleep 30s
nohup sh bin/mqbroker -c ./conf/2m-2s-async/broker-a.properties >> boot.log 2>&1 &
sleep 60s
nohup sh bin/mqbroker -c ./conf/2m-2s-async/broker-b-s.properties >> boot.log 2>&1 &
startNodeB.sh
nohup sh bin/mqnamesrv > boot.log 2>&1 &
sleep 30s
nohup sh bin/mqbroker -c ./conf/2m-2s-async/broker-b.properties >> boot.log 2>&1 &
sleep 60s
nohup sh bin/mqbroker -c ./conf/2m-2s-async/broker-a-s.properties >> boot.log 2>&1 &
shutdownBroker.sh
sh bin/mqshutdown broker
shutdownNamesrv.sh
sh bin/mqshutdown namesrv
clusterInfo.sh
sh ./bin/mqadmin clusterList -n localhost:9876
添加脚本可运行权限
chmod +x ./*.sh
注意:作者踩过的坑:brokerIP1 当前broker监听的IP
brokerIP2 存在broker主从时,在broker主节点上配置了brokerIP2的话,broker从节点会连接主节点配置的brokerIP2来同步
主节点:
brokerIP1 配置当前节点所在节点ip地址
brokerIP2 配置当前节点所在节点ip地址,即从节点需要从该节点同步数据
从节点:
brokerIP1 配置当前节点所在节点ip地址,即从节点所在ip
多nameser 中间分号分隔
namesrvAddr=10.10.10.1:9876;10.10.10.2:9876