Cluster 模式 - 多节点(集群)多副本模式-同步双写环境部署

集群主机

共计4台虚拟机,2 master, 2 slave。

序号 IP 角色 架构模式
1 192.168.3.73 nameserver、brokerserver Master1
2 192.168.3.74 nameserver、brokerserver Master2
3 192.168.3.75 nameserver、brokerserver Slave1
4 192.168.3.93 nameserver、brokerserver Slave2

下载安装

下载最新安装包: http://rocketmq.apache.org/dowloading/releases/

下载并安装最新稳定版的RocketMQ.

[root@rockketmq ~]# cd /usr/local/src && wget https://mirrors.bfsu.edu.cn/apache/rocketmq/5.0.0/rocketmq-all-5.0.0-bin-release.zip --no-check-certificate
[root@rockketmq local]# unzip rocketmq-all-5.0.0-bin-release.zip -d /usr/local/
[root@rockketmq local]# cd .. && mv rocketmq-all-5.0.0-bin-release/ rocketmq
修改JVM默认配置(物理机器内存为8G)
[root@localhost rocketmq]# vi bin/runserver.sh
JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

[root@localhost rocketmq]# vi bin/runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g"
JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=4g"

用户组和用户

创建专用的数据存储目录

$ mkdir -p /home/rocketmq/data/store
$ mkdir -p /home/rocketmq/logs

所有主机上,创建 rocketmq:rocketmq 用户组和用户,并进行授权:

$ groupadd rocketmq
$ useradd -g rocketmq -M -s /sbin/nologin rocketmq
$ chown -R rocketmq. /home/rocketmq/data/
$ chown -R rocketmq. /home/rocketmq/logs/
$ chown -R rocketmq. /usr/local/rocketmq/

配置集群配置文件

根据我们选择的 “多节点(集群)多副本模式-同步双写” 模式,找到对应的配置文件,比如在 /usr/local/rocketmq/conf/2m-2s-sync/ 目录下,有 broker-a.properties、broker-a-s.properties、broker-b.properties、broker-b-s.properties 四个配置文件:

配置文件与预安装集群的主机对应关系如下:

序号 IP 文件 架构模式
1 192.168.3.73 broker-a.properties Master1
2 192.168.3.74 broker-a-s.properties Master2
3 192.168.3.75 broker-b.properties Slave1
4 192.168.3.93 broker-b-s.properties Slave2

这里务必配置 storePathRootDir 相关参数,实际上启动运行时候没配置会提示错误无法配置开机自启动!

配置 broker-a.properties:
[root@rocketmq ~]# vi /usr/local/rocketmq/conf/2m-2s-sync/broker-a.properties

broker-a.properties 内容:

#所属集群名称
brokerClusterName=RocketMQ-Cluster
#broker名字,同一组的master-slave中,broker名字相同
brokerName=broker-a
#brokerId的ID,0 表示Master,>0 表示Slave
brokerId=0
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=48
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH

#nameServer集群地址,如果是多个,使用分号;分割
namesrvAddr=192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876
brokerIP1=192.168.3.73

#存储路径
storePathRootDir=/home/rocketmq/data/store
#commitLog 存储路径
storePathCommitLog=/home/rocketmq/data/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/home/rocketmq/data/store/consumequeue
#消息索引存储路径
storePathIndex=/home/rocketmq/data/store/index
#checkpoint 文件存储路径
storeCheckpoint=/home/rocketmq/data/store/checkpoint
#abort 文件存储路径
abortFile=/home/rocketmq/data/store/abort
配置 broker-a-s.properties:
[root@rocketmq ~]# vi /usr/local/rocketmq/conf/2m-2s-sync/broker-a-s.properties

broker-a-s.properties 内容:

#所属集群名称
brokerClusterName=RocketMQ-Cluster
#broker名字,同一组的master-slave中,broker名字相同
brokerName=broker-a
#brokerId的ID,0 表示Master,>0 表示Slave
brokerId=1
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=48
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SLAVE
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH

#nameServer集群地址,如果是多个,使用分号;分割
namesrvAddr=192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876
brokerIP1=192.168.3.75

#存储路径
storePathRootDir=/home/rocketmq/data/store
#commitLog 存储路径
storePathCommitLog=/home/rocketmq/data/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/home/rocketmq/data/store/consumequeue
#消息索引存储路径
storePathIndex=/home/rocketmq/data/store/index
#checkpoint 文件存储路径
storeCheckpoint=/home/rocketmq/data/store/checkpoint
#abort 文件存储路径
abortFile=/home/rocketmq/data/store/abort
配置 broker-b.properties:
[root@rocketmq ~]# vi /usr/local/rocketmq/conf/2m-2s-sync/broker-b.properties

broker-b.properties 内容:

#所属集群名称
brokerClusterName=RocketMQ-Cluster
#broker名字,同一组的master-slave中,broker名字相同
brokerName=broker-b
#brokerId的ID,0 表示Master,>0 表示Slave
brokerId=0
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=48
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH

#nameServer集群地址,如果是多个,使用分号;分割
namesrvAddr=192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876
brokerIP1=192.168.3.74

#存储路径
storePathRootDir=/home/rocketmq/data/store
#commitLog 存储路径
storePathCommitLog=/home/rocketmq/data/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/home/rocketmq/data/store/consumequeue
#消息索引存储路径
storePathIndex=/home/rocketmq/data/store/index
#checkpoint 文件存储路径
storeCheckpoint=/home/rocketmq/data/store/checkpoint
#abort 文件存储路径
abortFile=/home/rocketmq/data/store/abort
配置 broker-b-s.properties:
[root@rocketmq ~]# vi /usr/local/rocketmq/conf/2m-2s-sync/broker-b-s.properties

broker-b-s.properties 内容:

#所属集群名称
brokerClusterName=RocketMQ-Cluster
#broker名字,同一组的master-slave中,broker名字相同
brokerName=broker-b
#brokerId的ID,0 表示Master,>0 表示Slave
brokerId=1
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=48
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SLAVE
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH

#nameServer集群地址,如果是多个,使用分号;分割
namesrvAddr=192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876
brokerIP1=192.168.3.93

#存储路径
storePathRootDir=/home/rocketmq/data/store
#commitLog 存储路径
storePathCommitLog=/home/rocketmq/data/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/home/rocketmq/data/store/consumequeue
#消息索引存储路径
storePathIndex=/home/rocketmq/data/store/index
#checkpoint 文件存储路径
storeCheckpoint=/home/rocketmq/data/store/checkpoint
#abort 文件存储路径
abortFile=/home/rocketmq/data/store/abort

Name Server 开机自启动

所有主机上,配置开机启动脚本:

[root@rocketmq ~]# vi /lib/systemd/system/rocketmq-namesrv.service

脚本内容:

[Unit]
Description=RocketMQ 5.x Name Server
After=network.target

[Service]
User=rocketmq
Environment=JAVA_HOME=/usr/local/java
ExecStart=/usr/local/rocketmq/bin/mqnamesrv > /home/rocketmq/logs/namesrv.log 2>&1 &
ExecStop=/usr/local/rocketmq/bin/mqshutdown namesrv
ExecReload=/usr/local/rocketmq/bin/mqshutdown namesrv && /usr/local/rocketmq/bin/mqnamesrv > /home/rocketmq/logs/namesrv.log 2>&1 &
#ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop=/bin/kill -s QUIT $MAINPID
LimitNOFILE=65536
Restart=0

[Install]
WantedBy=multi-user.target

设置随机启动:

[root@rocketmq ~]# systemctl daemon-reload
[root@rocketmq ~]# systemctl disable rocketmq-namesrv.service
[root@rocketmq ~]# systemctl enable rocketmq-namesrv.service
[root@rocketmq ~]# systemctl start rocketmq-namesrv.service
[root@rocketmq ~]# systemctl status rocketmq-namesrv.service
[root@rocketmq ~]# systemctl stop rocketmq-namesrv.service
[root@rocketmq ~]# systemctl restart rocketmq-namesrv.service

Broker 开机自启动

RocketMQ-Master1(主1 - 192.168.3.73)

注意:
首先需将上面配置好的四个配置文件,直接丢到配置文件目录下,比如在 /usr/local/rocketmq/conf/2m-2s-sync/ 目录下。

配置开机启动脚本:
[root@rocketmq ~]# vi /lib/systemd/system/rocketmq-broker.service

脚本内容:

[Unit]
Description=RocketMQ 5.x Broker
After=network.target remote-fs.target nss-lookup.target rocketmq-namesrv.service
Requires=rocketmq-namesrv.service

[Service]
User=rocketmq
Environment='JAVA_HOME=/usr/local/java'
Environment='ROCKETMQ_HOME=/usr/local/rocketmq'
ExecStart=/usr/local/rocketmq/bin/mqbroker autoCreateTopicEnable=true -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 -c /usr/local/rocketmq/conf/2m-2s-sync/broker-a.properties > /home/rocketmq/logs/broker.log 2>&1 &
ExecStop=/usr/local/rocketmq/bin/mqshutdown broker
ExecReload=/usr/local/rocketmq/bin/mqshutdown broker && /usr/local/rocketmq/bin/mqbroker autoCreateTopicEnable=true -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 -c /usr/local/rocketmq/conf/2m-2s-sync/broker-a.properties > /home/rocketmq/logs/broker.log 2>&1 &
LimitNOFILE=65536
Restart=0

[Install]
WantedBy=multi-user.target

设置随机启动:

[root@rocketmq ~]# systemctl daemon-reload
[root@rocketmq ~]# systemctl disable rocketmq-broker.service
[root@rocketmq ~]# systemctl enable rocketmq-broker.service
[root@rocketmq ~]# systemctl start rocketmq-broker.service
[root@rocketmq ~]# systemctl status rocketmq-broker.service
[root@rocketmq ~]# systemctl stop rocketmq-broker.service
[root@rocketmq ~]# systemctl restart rocketmq-broker.service
RocketMQ-Master2(主2 - 192.168.3.74)

注意:
首先需将上面配置好的四个配置文件,直接丢到配置文件目录下,比如在 /usr/local/rocketmq/conf/2m-2s-sync/ 目录下。

配置开机启动脚本:
[root@rocketmq ~]# vi /lib/systemd/system/rocketmq-broker.service

脚本内容:

[Unit]
Description=RocketMQ 5.x Broker
After=network.target remote-fs.target nss-lookup.target rocketmq-namesrv.service
Requires=rocketmq-namesrv.service

[Service]
User=rocketmq
Environment='JAVA_HOME=/usr/local/java'
Environment='ROCKETMQ_HOME=/usr/local/rocketmq'
ExecStart=/usr/local/rocketmq/bin/mqbroker autoCreateTopicEnable=true -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 -c /usr/local/rocketmq/conf/2m-2s-sync/broker-b.properties > /home/rocketmq/logs/broker.log 2>&1 &
ExecStop=/usr/local/rocketmq/bin/mqshutdown broker
ExecReload=/usr/local/rocketmq/bin/mqshutdown broker && /usr/local/rocketmq/bin/mqbroker autoCreateTopicEnable=true -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 -c /usr/local/rocketmq/conf/2m-2s-sync/broker-b.properties > /home/rocketmq/logs/broker.log 2>&1 &
LimitNOFILE=65536
Restart=0

[Install]
WantedBy=multi-user.target

设置随机启动:

[root@rocketmq ~]# systemctl daemon-reload
[root@rocketmq ~]# systemctl disable rocketmq-broker.service
[root@rocketmq ~]# systemctl enable rocketmq-broker.service
[root@rocketmq ~]# systemctl start rocketmq-broker.service
[root@rocketmq ~]# systemctl status rocketmq-broker.service
[root@rocketmq ~]# systemctl stop rocketmq-broker.service
[root@rocketmq ~]# systemctl restart rocketmq-broker.service
RocketMQ-Slave1(从1 - 192.168.3.75)

注意:
首先需将上面配置好的四个配置文件,直接丢到配置文件目录下,比如在 /usr/local/rocketmq/conf/2m-2s-sync/ 目录下。

配置开机启动脚本:
[root@rocketmq ~]# vi /lib/systemd/system/rocketmq-broker.service

脚本内容:

[Unit]
Description=RocketMQ 5.x Broker
After=network.target remote-fs.target nss-lookup.target rocketmq-namesrv.service
Requires=rocketmq-namesrv.service

[Service]
User=rocketmq
Environment='JAVA_HOME=/usr/local/java'
Environment='ROCKETMQ_HOME=/usr/local/rocketmq'
ExecStart=/usr/local/rocketmq/bin/mqbroker autoCreateTopicEnable=true -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 -c /usr/local/rocketmq/conf/2m-2s-sync/broker-a-s.properties > /home/rocketmq/logs/broker.log 2>&1 &
ExecStop=/usr/local/rocketmq/bin/mqshutdown broker
ExecReload=/usr/local/rocketmq/bin/mqshutdown broker && /usr/local/rocketmq/bin/mqbroker autoCreateTopicEnable=true -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 -c /usr/local/rocketmq/conf/2m-2s-sync/broker-a-s.properties > /home/rocketmq/logs/broker.log 2>&1 &
LimitNOFILE=65536
Restart=0

[Install]
WantedBy=multi-user.target

设置随机启动:

[root@rocketmq ~]# systemctl daemon-reload
[root@rocketmq ~]# systemctl disable rocketmq-broker.service
[root@rocketmq ~]# systemctl enable rocketmq-broker.service
[root@rocketmq ~]# systemctl start rocketmq-broker.service
[root@rocketmq ~]# systemctl status rocketmq-broker.service
[root@rocketmq ~]# systemctl stop rocketmq-broker.service
[root@rocketmq ~]# systemctl restart rocketmq-broker.service
RocketMQ-Slave2(从2 - 192.168.3.93)

注意:
首先需将上面配置好的四个配置文件,直接丢到配置文件目录下,比如在 /usr/local/rocketmq/conf/2m-2s-sync/ 目录下。

配置开机启动脚本:
[root@rocketmq ~]# echo '' /lib/systemd/system/rocketmq-broker.service
[root@rocketmq ~]# vi /lib/systemd/system/rocketmq-broker.service

脚本内容:

[Unit]
Description=RocketMQ 5.x Broker
After=network.target remote-fs.target nss-lookup.target rocketmq-namesrv.service
Requires=rocketmq-namesrv.service

[Service]
User=rocketmq
Environment='JAVA_HOME=/usr/local/java'
Environment='ROCKETMQ_HOME=/usr/local/rocketmq'
ExecStart=/usr/local/rocketmq/bin/mqbroker autoCreateTopicEnable=true -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 -c /usr/local/rocketmq/conf/2m-2s-sync/broker-b-s.properties > /home/rocketmq/logs/broker.log 2>&1 &
ExecStop=/usr/local/rocketmq/bin/mqshutdown broker
ExecReload=/usr/local/rocketmq/bin/mqshutdown broker && /usr/local/rocketmq/bin/mqbroker autoCreateTopicEnable=true -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 -c /usr/local/rocketmq/conf/2m-2s-sync/broker-b-s.properties > /home/rocketmq/logs/broker.log 2>&1 &
LimitNOFILE=65536
Restart=0

[Install]
WantedBy=multi-user.target

设置随机启动:

[root@rocketmq ~]# systemctl daemon-reload
[root@rocketmq ~]# systemctl disable rocketmq-broker.service
[root@rocketmq ~]# systemctl enable rocketmq-broker.service
[root@rocketmq ~]# systemctl start rocketmq-broker.service
[root@rocketmq ~]# systemctl status rocketmq-broker.service
[root@rocketmq ~]# systemctl stop rocketmq-broker.service
[root@rocketmq ~]# systemctl restart rocketmq-broker.service
Proxy 开机自启动

所有主机上,配置开机启动脚本:

[root@rocketmq ~]# vi /lib/systemd/system/rocketmq-proxy.service

脚本内容:

[Unit]
Description=RocketMQ 5.x Proxy
After=network.target remote-fs.target nss-lookup.target rocketmq-namesrv.service
Requires=rocketmq-namesrv.service

[Service]
User=rocketmq
Type=forking
Environment='JAVA_HOME=/usr/local/java'
ExecStart=/usr/local/rocketmq/bin/mqproxy -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 > /home/rocketmq/logs/proxy.log 2>&1 &
#ExecReload=/bin/kill -s HUP $MAINPID
#ExecStop=/bin/kill -s QUIT $MAINPID
ExecStop=/usr/local/rocketmq/bin/mqshutdown proxy
ExecReload=/usr/local/rocketmq/bin/mqshutdown proxy && /usr/local/rocketmq/bin/mqproxy -n 192.168.3.73:9876;192.168.3.74:9876;192.168.3.75:9876;192.168.3.93:9876 > /home/rocketmq/logs/proxy.log 2>&1 &
LimitNOFILE=65536
Restart=0

[Install]
WantedBy=multi-user.target

设置随机启动:

[root@rocketmq ~]# systemctl daemon-reload
[root@rocketmq ~]# systemctl disable rocketmq-proxy.service
[root@rocketmq ~]# systemctl enable rocketmq-proxy.service
[root@rocketmq ~]# systemctl start rocketmq-proxy.service
[root@rocketmq ~]# systemctl status rocketmq-proxy.service
[root@rocketmq ~]# systemctl stop rocketmq-proxy.service
[root@rocketmq ~]# systemctl restart rocketmq-proxy.service
作者:Jeebiz  创建时间:2023-01-26 12:57
最后编辑:Jeebiz  更新时间:2024-11-01 10:06