系列十五(实战)、搭建RocketMQ集群(双主双从)

一、概览

二、集群特点

2.1、NameServer

        NameServer是一个几乎无状态节点,可集群部署,节点之间无任何信息同步。

2.2、Broker

        Broker部署相对复杂,Broker分为Master与Slave,一个Master可以对应多个Slave,但是一个Slave只能对应一个Master,Master与Slave的对应关系通过指定相同的BrokerName,不同的BrokerId来定义

2.3、Producer与NameServer

        Producer与NameServer集群中的其中一个节点(随机选择)建立长连接,定期从NameServer取Topic路由信息,并向提供Topic服务的Master建立长连接,且定时向Master发送心跳。Producer完全无状态,可集群部署。

2.4、Consumer与NameServer

        Consumer与NameServer集群中的其中一个节点(随机选择)建立长连接,定期从NameServer取Topic路由信息,并向提供Topic服务的Master、Slave建立长连接,且定时向Master、Slave发送心跳。Consumer既可以从Master订阅消息,也可以从Slave订阅消息,订阅规则由Broker配置决定。 

三、集群搭建

3.1、服务器配置(两台服务器都要配置)

3.1.1、服务器环境

序号主机角色架构模式
1centos720230613nameserver、brokerserverMaster1、Slave2
2centos720230711nameserver、brokerserverMaster2、Slave1

3.1.2、hosts配置 

1、编辑hosts文件
vim /etc/hosts

2、配置信息:
# nameserver
192.168.173.200 rocketmq-nameserver1
192.168.173.198 rocketmq-nameserver2

# broker
192.168.173.200 rocketmq-master1
192.168.173.200 rocketmq-slave2
192.168.173.198 rocketmq-master2
192.168.173.198 rocketmq-slave1

 

 3.1.3、重启网卡

systemctl restart network

3.1.4、防火墙配置

场景:测试环境,比较简单粗暴,直接关闭防火墙即可
# 查看防火墙的状态
firewall-cmd --state 
# 关闭防火墙
systemctl stop firewalld.service 
# 禁止firewall开机启动
systemctl disable firewalld.service

场景:生产环境,为了安全,只开放特定的端口,RocketMQ默认使用3个端口:9876 、10911 、11011 。如果防火墙没有关闭的话,那么防火墙就必须开放这些端口:
nameserver:默认使用 9876 端口
master:默认使用 10911 端口
slave:默认使用 11011 端口
执行如下命令:
# 开放nameserver默认端口
firewall-cmd --remove-port=9876/tcp --permanent
# 开放master默认端口
firewall-cmd --remove-port=10911/tcp --permanent
# 开放slave默认端口 (当前集群模式可不开启)
firewall-cmd --remove-port=11011/tcp --permanent 
# 重启防火墙
firewall-cmd --reload

 3.1.5、环境变量配置

# 编辑profile文件
vim /etc/profile

# 配置
# RocketMQ Config
ROCKETMQ_HOME=/usr/local/myRocketMQ
PATH=$PATH:$ROCKETMQ_HOME/bin
export ROCKETMQ_HOME PATH

# 生效配置
source /etc/profile

3.1.6、创建消息存储路径

mkdir /usr/local/myRocketMQ/store
mkdir /usr/local/myRocketMQ/store/commitlog
mkdir /usr/local/myRocketMQ/store/consumequeue
mkdir /usr/local/myRocketMQ/store/index

3.2、broker配置

3.2.1、master1配置

# 服务器:【centos720230613】
vim /usr/local/myRocketMQ/conf/2m-2s-sync/broker-a.properties

# 配置信息如下:
# 所属集群名字
brokerClusterName=rocketmq-cluster
# broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-a
# 0 表示 Master,>0 表示 Slave
brokerId=0
# nameServer地址,分号分割
namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
# 在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
# 是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
# 是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
# Broker 对外服务的监听端口
listenPort=10911
# 删除文件时间点,默认凌晨 4点
deleteWhen=04
# 文件保留时间,默认 48 小时
fileReservedTime=120
# commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
# ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
# destroyMapedFileIntervalForcibly=120000
# redeleteHangedFileInterval=120000
# 检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
# 存储路径
storePathRootDir=/usr/local/myRocketMQ/store
# commitLog 存储路径
storePathCommitLog=/usr/local/myRocketMQ/store/commitlog
# 消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/myRocketMQ/store/consumequeue
# 消息索引存储路径
storePathIndex=/usr/local/myRocketMQ/store/index
# checkpoint 文件存储路径
storeCheckpoint=/usr/local/myRocketMQ/store/checkpoint
# abort 文件存储路径
abortFile=/usr/local/myRocketMQ/store/abort
# 限制的消息大小
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Broker 的角色
# - ASYNC_MASTER 异步复制Master
# - SYNC_MASTER 同步双写Master
# - SLAVE
brokerRole=SYNC_MASTER
# 刷盘方式
# - ASYNC_FLUSH 异步刷盘
# - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
# checkTransactionMessageEnable=false
# 发消息线程池数量
# sendMessageThreadPoolNums=128
# 拉消息线程池数量
# pullMessageThreadPoolNums=128

3.2.2、slave2配置

# 服务器:【centos720230613】
vim /usr/local/myRocketMQ//conf/2m-2s-sync/broker-b-s.properties

# 配置信息如下:
# 所属集群名字
brokerClusterName=rocketmq-cluster
# broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-b
# 0 表示 Master,>0 表示 Slave
brokerId=1
# nameServer地址,分号分割
namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
# 在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
# 是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
# 是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
# Broker 对外服务的监听端口
listenPort=11011
# 删除文件时间点,默认凌晨 4点
deleteWhen=04
# 文件保留时间,默认 48 小时
fileReservedTime=120
# commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
# ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
# destroyMapedFileIntervalForcibly=120000
# redeleteHangedFileInterval=120000
# 检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
# 存储路径
storePathRootDir=/usr/local/myRocketMQ/store
# commitLog 存储路径
storePathCommitLog=/usr/local/myRocketMQ/store/commitlog
# 消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/myRocketMQ/store/consumequeue
# 消息索引存储路径
storePathIndex=/usr/local/myRocketMQ/store/index
# checkpoint 文件存储路径
storeCheckpoint=/usr/local/myRocketMQ/store/checkpoint
# abort 文件存储路径
abortFile=/usr/local/myRocketMQ/store/abort
# 限制的消息大小
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Broker 的角色
# - ASYNC_MASTER 异步复制Master
# - SYNC_MASTER 同步双写Master
# - SLAVE
brokerRole=SLAVE
# 刷盘方式
# - ASYNC_FLUSH 异步刷盘
# - SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
# checkTransactionMessageEnable=false
# 发消息线程池数量
# sendMessageThreadPoolNums=128
# 拉消息线程池数量
# pullMessageThreadPoolNums=128

3.2.3、master2配置

# 服务器:【centos720230711】
vim /usr/local/myRocketMQ/conf/2m-2s-sync/broker-b.properties

# 配置信息如下:
# 所属集群名字
brokerClusterName=rocketmq-cluster
# broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-b
# 0 表示 Master,>0 表示 Slave
brokerId=0
# nameServer地址,分号分割
namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
# 在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
# 是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
# 是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
# Broker 对外服务的监听端口
listenPort=10911
# 删除文件时间点,默认凌晨 4点
deleteWhen=04
# 文件保留时间,默认 48 小时
fileReservedTime=120
# commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
# ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
# destroyMapedFileIntervalForcibly=120000
# redeleteHangedFileInterval=120000
# 检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
# 存储路径
storePathRootDir=/usr/local/myRocketMQ/store
# commitLog 存储路径
storePathCommitLog=/usr/local/myRocketMQ/store/commitlog
# 消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/myRocketMQ/store/consumequeue
# 消息索引存储路径
storePathIndex=/usr/local/myRocketMQ/store/index
# checkpoint 文件存储路径
storeCheckpoint=/usr/local/myRocketMQ/store/checkpoint
# abort 文件存储路径
abortFile=/usr/local/myRocketMQ/store/abort
# 限制的消息大小
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Broker 的角色
# - ASYNC_MASTER 异步复制Master
# - SYNC_MASTER 同步双写Master
# - SLAVE
brokerRole=SYNC_MASTER
# 刷盘方式
# - ASYNC_FLUSH 异步刷盘
# - SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
# checkTransactionMessageEnable=false
# 发消息线程池数量
# sendMessageThreadPoolNums=128
# 拉消息线程池数量
# pullMessageThreadPoolNums=128


3.2.4、slave1配置

# 服务器:【centos720230711】
vim /usr/local/myRocketMQ/conf/2m-2s-sync/broker-a-s.properties

# 配置信息如下:
# 所属集群名字
brokerClusterName=rocketmq-cluster
# broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-a
# 0 表示 Master,>0 表示 Slave
brokerId=1
# nameServer地址,分号分割
namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
# 在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
# 是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
# 是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
# Broker 对外服务的监听端口
listenPort=11011
# 删除文件时间点,默认凌晨 4点
deleteWhen=04
# 文件保留时间,默认 48 小时
fileReservedTime=120
# commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
# ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
# destroyMapedFileIntervalForcibly=120000
# redeleteHangedFileInterval=120000
# 检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
# 存储路径
storePathRootDir=/usr/local/rocketmq/store
# commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store/commitlog
# 消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq/store/consumequeue
# 消息索引存储路径
storePathIndex=/usr/local/rocketmq/store/index
# checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store/checkpoint
# abort 文件存储路径
abortFile=/usr/local/rocketmq/store/abort
# 限制的消息大小
maxMessageSize=65536
# flushCommitLogLeastPages=4
# flushConsumeQueueLeastPages=2
# flushCommitLogThoroughInterval=10000
# flushConsumeQueueThoroughInterval=60000
# Broker 的角色
# - ASYNC_MASTER 异步复制Master
# - SYNC_MASTER 同步双写Master
# - SLAVE
brokerRole=SLAVE
# 刷盘方式
# - ASYNC_FLUSH 异步刷盘
# - SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
# checkTransactionMessageEnable=false
# 发消息线程池数量
# sendMessageThreadPoolNums=128
# 拉消息线程池数量
# pullMessageThreadPoolNums=128

3.3、修改启动脚本(两台服务器都要修改)

3.3.1、修改runbroker.sh

# 编辑runbroker.sh
vim /usr/local/myRocketMQ/bin/runbroker.sh

# 配置信息如下:需要根据内存大小进行适当的对JVM参数进行调整
# 开发环境配置 JVM Configuration
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

3.3.2、修改runserver.sh

# 编辑runserver.sh
vim /usr/local/myRocketMQ/bin/runserver.sh

# 配置信息如下:需要根据内存大小进行适当的对JVM参数进行调整
# 开发环境配置 JVM Configuration
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

3.4、服务启动

3.4.1、启动NameServer集群

# 分别在【centos720230613】、【centos720230711】启动NameServer
nohup sh mqnamesrv &

3.4.2、启动Broker集群

# 【centos720230613】:启动master1和slave2
1、master1
nohup sh mqbroker -c /usr/local/myRocketMQ/conf/2m-2s-sync/broker-a.properties &

2、slave2
nohup sh mqbroker -c /usr/local/myRocketMQ/conf/2m-2s-sync/broker-b-s.properties &


# 【centos720230711】:启动master2和slave1
1、master2
nohup sh mqbroker -c /usr/local/myRocketMQ/conf/2m-2s-sync/broker-b.properties &

2、slave1
nohup sh mqbroker -c /usr/local/myRocketMQ/conf/2m-2s-sync/broker-a-s.properties &

3.4.3、查看进程状态

3.4.4、查看日志

# 查看nameServer日志
tail -500f ~/logs/rocketmqlogs/namesrv.log
# 查看broker日志
tail -500f ~/logs/rocketmqlogs/broker.log

【centos720230613】/namesrv.log

【centos720230613】/broker.log

【centos720230711】/namesrv.log

【centos720230711】/broker.log

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值