0%

Apache RocketMQ 消息队列部署

Apache RocketMQ 消息队列部署

Apache RocketMQ是一个分布式、队列模型的消息中间件,具有低延迟、高性能和高可靠、万亿级容量和灵活的可扩展性。核心组件由四部分组成:Name Servers,Brokers,Producer 和 Consumer;它们中的每一个都可以水平扩展,而没有单一的故障节点。
详细组件介绍请参考github: https://github.com/apache/rocketmq/blob/master/docs/cn/concept.md
对应的rockermq的特性介绍: https://github.com/apache/rocketmq/blob/master/docs/cn/features.md

  • NameServer:是一个几乎无状态的节点,可集群部署,节点之间无任何信息同步
  • Broker:部署相对复杂,Broker分为Master与Slave,一个Master可以对应多个Slaver,但是一个Slaver只能对应一个Master,Master与Slaver的对应关系通过指定相同的BrokerName,不同的BrokerId来定义,BrokerId为0表示Master,非0表示Slaver。Master可以部署多个。每个Broker与NameServer集群中的所有节点建立长连接,定时注册Topic信息到所有的NameServer

Apache RocketMQ安装部署

打开对应的版本地址, 如下的是github的版本地址
https://github.com/apache/rocketmq/releases
如下是本次部署的架构规划图,本次部署选择的版本是4.7.1,选择长期支持版本
rockermqjiagou

部署方式

RocketMQ有三种部署方式:

  • 2m-2s-async。多主多从异步复制
  • 2m-2s-sync。多主多从同步复制
  • 2m-noslave。多主无从

生产broker-master和broker-slaver尽量分开.
本文介绍2m-2s-async多主多从异步复制集群安装方式,集群包含三台节点 配置均为2c4g,各节点Broker分布情况:
| 节点 |IP | 主服务 |从服务 |
| —- | —-| —- |—- |
| node1 | 192.168.1.10| broker-a |broker-b-s |
| node2 | 192.168.1.11| broker-b |broker-c-s |
| node3 | 192.168.1.12| broker-c |broker-a-s |

修改启动脚本内存参数

每个节点均需要执行如下操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
wget https://github.com/apache/rocketmq/archive/rocketmq-all-4.7.1.tar.gz
tar xf rocketmq-all-4.7.1.tar.gz
ln -sv rocketmq-all-4.7.1-bin-release rocketmq
vim /opt/rocketmq/bin/runbroker.sh
# 修改 根据实际所需进行修改, 一般是内存的50%, 最多不能超过75%
#JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m"
# 注释 debug模式
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"


vim /opt/rocketmq/bin/runserver.sh
# 注释
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
#JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m"

修改配置文件

192.168.1.10配置文件

broker.conf配置文件详解

1
2
3
4
5
6
7
8
cat broker.conf
brokerClusterName = DefaultCluster
brokerName = broker-a
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH

2m-2s-async/broker-a.properties 配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
cat 2m-2s-async/broker-a.properties 
#集群名
brokerClusterName=Data-Cluster
#broker名字
brokerName=broker-a
#0 表示 Master, >0 表示 Slave
brokerId=0
#nameServer地址,分号分割
namesrvAddr=18.16.200.248:9876;18.16.200.230:9876;18.16.200.239:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=3
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10911
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
#fileReservedTime=3600
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#文件磁盘最大利用率
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/data/rocketmq/store-a
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/commitlog-a
#消费队列存储路径存储路径
storePathConsumeQueue=/data/rocketmq/consumequeue-a
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH

2m-2s-async/broker-b-s.properties 配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
cat 2m-2s-async/broker-b-s.properties 
#集群名
brokerClusterName=Data-Cluster
#broker名字
brokerName=broker-b
#0 表示 Master, >0 表示 Slave
brokerId=1
#nameServer地址,分号分割
namesrvAddr=18.16.200.248:9876;18.16.200.230:9876;18.16.200.239:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=3
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
#autoCreateTopicEnable=ture
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
#autoCreateSubscriptionGroup=ture
#Broker 对外服务的监听端口
listenPort=20912
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
#fileReservedTime=3600
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#磁盘最大利用率
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/data/rocketmq/store-b-s
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/commitlog-b-s
#消费队列存储路径存储路径
storePathConsumeQueue=/data/rocketmq/consumequeue-b-s
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
#brokerRole=ASYNC_MASTER
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH

192.168.1.11配置文件

broker.conf配置文件详解

1
2
3
4
5
6
7
8
cat broker.conf
brokerClusterName = DefaultCluster
brokerName = broker-b
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH

2m-2s-async/broker-b.properties 配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
cat 2m-2s-async/broker-b.properties 
#集群名
brokerClusterName=Data-Cluster
#broker名字
brokerName=broker-b
#0 表示 Master, >0 表示 Slave
brokerId=0
#nameServer地址,分号分割
namesrvAddr=18.16.200.228:9876;18.16.200.238:9876;18.16.200.211:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=3
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10911
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
#fileReservedTime=3600
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#文件磁盘最大利用率
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/data/rocketmq/store-b
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/commitlog-b
#消费队列存储路径存储路径
storePathConsumeQueue=/data/rocketmq/consumequeue-b
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH

2m-2s-async/broker-c-s.properties 配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
cat 2m-2s-async/broker-c-s.properties 
#集群名
brokerClusterName=Data-Cluster
#broker名字
brokerName=broker-c
#0 表示 Master, >0 表示 Slave
brokerId=1
#nameServer地址,分号分割
namesrvAddr=18.16.200.228:9876;18.16.200.238:9876;18.16.200.211:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=3
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=ture
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=ture
#Broker 对外服务的监听端口
listenPort=20912
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
#fileReservedTime=3600
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#磁盘最大利用率
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/data/rocketmq/store-c-s
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/commitlog-c-s
#消费队列存储路径存储路径
storePathConsumeQueue=/data/rocketmq/consumequeue-c-s
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SLAVE
#brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH

192.168.1.12配置文件

broker.conf配置文件详解

1
2
3
4
5
6
7
8
cat broker.conf
brokerClusterName = DefaultCluster
brokerName = broker-c
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH

2m-2s-async/broker-c.properties 配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
cat 2m-2s-async/broker-c.properties 
#集群名
brokerClusterName=Data-Cluster
#broker名字
brokerName=broker-c
#0 表示 Master, >0 表示 Slave
brokerId=0
#nameServer地址,分号分割
namesrvAddr=18.16.200.228:9876;18.16.200.238:9876;18.16.200.211:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=3
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
#listenPort=10911
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
#fileReservedTime=3600
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#文件磁盘最大利用率
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/data/rocketmq/store-c
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/commitlog-c
#消费队列存储路径存储路径
storePathConsumeQueue=/data/rocketmq/consumequeue-c
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH

2m-2s-async/broker-a-s.properties 配置文件详解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
cat 2m-2s-async/broker-a-s.properties 
#集群名
brokerClusterName=Data-Cluster
#broker名字
brokerName=broker-a
#0 表示 Master, >0 表示 Slave
brokerId=1
#nameServer地址,分号分割
namesrvAddr=18.16.200.228:9876;18.16.200.238:9876;18.16.200.211:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=3
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=ture
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=ture
#Broker 对外服务的监听端口
listenPort=20912
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
#fileReservedTime=3600
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#磁盘最大利用率
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/data/rocketmq/store-a-s
#commitLog 存储路径
storePathCommitLog=/data/rocketmq/commitlog-a-s
#消费队列存储路径存储路径
storePathConsumeQueue=/data/rocketmq/consumequeue-a-s
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SLAVE
#brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH

添加环境变量

1
2
export ROCKETMQ_HOME=/opt/rocketmq
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk1.8.0_271/bin:/usr/local/jdk1.8.0_271/jre/bin:/bin:/root/bin:/bin:/opt/rocketmq/bin

启动命令

启动nameserver

三台机器分别运行这个命令

1
nohup sh mqnamesrv > /data/logs/rocketmq/mqnamesrv.log 2>&1 &

启动broker的master配置

192.168.1.10上执行

1
2
3
nohup sh mqbroker -c /opt/rocketmq/conf/2m-2s-async/broker-a.properties>/data/logs/rocketmq/mqbroker-a.log 2>&1 &
nohup sh mqbroker -c /opt/rocketmq/conf/2m-2s-async/broker-b-s.properties >>/data/logs/rocketmq/mqbroker-b-s.log 2>&1 &

192.168.1.11上执行

1
2
nohup sh mqbroker -c /opt/rocketmq/conf/2m-2s-async/broker-b.properties > /data/logs/rocketmq/mqbroker-b.log 2>&1 &
nohup sh mqbroker -c /opt/rocketmq/conf/2m-2s-async/broker-c-s.properties > /data/logs/rocketmq/mqbroker-c-s.log 2>&1 &

192.168.1.12上执行

1
2
nohup sh mqbroker -c /opt/rocketmq/conf/2m-2s-async/broker-c.properties >> /data/logs/rocketmq/mqbroker-c.log 2>&1 &
nohup sh mqbroker -c /opt/rocketmq/conf/2m-2s-async/broker-a-s.properties >> /data/logs/rocketmq/mqbroker-a-s.log 2>&1 &