1、RocketMQ消息队列基础概念
NameServer: 提供轻量级的服务发现和路由。 每个 NameServer 记录完整的路由信息,提供等效的读写服务,并支持快速存储扩展。
Broker: 通过提供轻量级的 Topic 和 Queue 机制来处理消息存储,同时支持推(push)和拉(pull)模式以及主从结构的容错机制。
Producer:生产者,产生消息的实例,拥有相同 Producer Group 的 Producer 组成一个集群。
Consumer:消费者,接收消息进行消费的实例,拥有相同 Consumer Group 的 Consumer 组成一个集群。
每个 Broker 与 NameServer 集群中的所有节 点建立长连接,定时注册 Topic 信息到所有 NameServer 中。
Producer 与 NameServer 集群中的其中一个节点(随机选择)建立长连接,定期从 NameServer 获取 Topic 路由信息,并向提供 Topic 服务的 Broker Master 建立长连接,且定时向 Broker 发送心跳。
Producer 只能将消息发送到 Broker master,但是 Consumer 则不一样,它同时和提供 Topic 服务的 Master 和 Slave 建立长连接,既可以从 Broker Master 订阅消息,也可以从 Broker Slave 订阅消息。
2、2主2从集群搭建
1)集群架构图
2)搭建步骤
基础环境:
两台虚拟机(192.168.2.160,192.168.2.170)内存均是3GB,JDK1.8+;
192.168.2.160机器搭建nameserver及broker-a的主从节点,192.168.2.170机器搭建nameserver及broker-b的主从节点;
rocketmq-console-ng-2.0.0.jar包(rocketmq监控台网站)。
1、下载apache-rocketmq-4.8.0版本(当前最新版),解压至rocketmq-4.8.0中。
2、进入rocketmq-4.8.0文件夹,修改conf/2m-2s-sync文件夹中的配置文件。
broker-a.properties文件内容如下:
# 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=rocketmq-cluster
# 名称可重复 为了便于管理,master和所属的slave采用同一个名称
brokerName=broker-a
# 0 - master | >0 - slave
brokerId=0
# nameServer 服务地址列表
namesrvAddr=192.168.2.160:9876;192.168.2.170:9876
# 在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=false
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=false
#Broker 对外服务的监听端口,
listenPort=10911
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=48
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#存储路径
storePathRootDir=/home/gaochao/rocketmq-4.8.0/data/store/broker
#commitLog 存储路径
storePathCommitLog=/home/gaochao/rocketmq-4.8.0/data/store/broker/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/home/gaochao/rocketmq-4.8.0/data/store/broker/consumequeue
#消息索引存储路径
storePathIndex=/home/gaochao/rocketmq-4.8.0/data/store/broker/index
#checkpoint 文件存储路径
storeCheckpoint=/home/gaochao/rocketmq-4.8.0/data/store/broker/checkpoint
#abort 文件存储路径
abortFile=/home/gaochao/rocketmq-4.8.0/data/store/broker/abort
#限制的消息大小
maxMessageSize=65536
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#发消息线程池数量
sendMessageThreadPoolNums=128
#拉消息线程池数量
pullMessageThreadPoolNums=128
broker-a-s.properties文件内容如下:
# 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=rocketmq-cluster
# 名称可重复 为了便于管理,master和所属的slave采用同一个名称
brokerName=broker-a
# 0 - master | >0 - slave
brokerId=1
# nameServer 服务地址列表
namesrvAddr=192.168.2.160:9876;192.168.2.170:9876
# 在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=false
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=false
#Broker 对外服务的监听端口,
listenPort=10950
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=48
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#存储路径
storePathRootDir=/home/gaochao/rocketmq-4.8.0/data/store/broker-s
#commitLog 存储路径
storePathCommitLog=/home/gaochao/rocketmq-4.8.0/data/store/broker-s/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/home/gaochao/rocketmq-4.8.0/data/store/broker-s/consumequeue
#消息索引存储路径
storePathIndex=/home/gaochao/rocketmq-4.8.0/data/store/broker-s/index
#checkpoint 文件存储路径
storeCheckpoint=/home/gaochao/rocketmq-4.8.0/data/store/broker-s/checkpoint
#abort 文件存储路径
abortFile=/home/gaochao/rocketmq-4.8.0/data/store/broker-s/abort
#限制的消息大小
maxMessageSize=65536
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SLAVE
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#发消息线程池数量
sendMessageThreadPoolNums=128
#拉消息线程池数量
pullMessageThreadPoolNums=128
broker-b.properties与broker-b-s.properties文件的修改可参考如上两配置文件的修改,集群名称不变,机器节点名称注意变化。
3、在上述两节点机器上分别执行如下命令,指定消息队列等数据的存储路径,如果不指定,则默认路径为当前用户的home路径(会自动创建store与log文件夹)
##需要注意的是 存储路径 storeCheckpoint ,abortFile 不需要创建文件夹,会自动创建checkpoint 文件 和 abort 文件,但是需要创建 commitlog ,consumequeue,index 文件夹
mkdir -p /home/gaochao/rocketmq-4.8.0/data/store/broker/commitlog
mkdir -p /home/gaochao/rocketmq-4.8.0/data/store/broker/consumequeue
mkdir -p /home/gaochao/rocketmq-4.8.0/data/store/broker/index
mkdir -p /home/gaochao/rocketmq-4.8.0/data/store/broker/commitlog
mkdir -p /home/gaochao/rocketmq-4.8.0/data/store/broker/consumequeue
mkdir -p /home/gaochao/rocketmq-4.8.0/data/store/broker/index
4、根据实际情况修改namesrv与broker启动需要的jvm内存(默认值较大)
bin路径下runbroker.sh,runserver.sh配置可调整如下:
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m"
5、在两个机器节点上先启动namesrv服务,然后启动broker服务(以broker节点为例)
cd /home/gaochao/rocketmq-4.8.0/bin
nohup sh mqnamesrv &
nohup sh mqbroker -c ../conf/2m-2s-sync/broker-a.properties &
nohup sh mqbroker -c ../conf/2m-2s-sync/broker-a-s.properties &
6、选择其中一节点,如broker-b节点,启动rocketmq监视台(端口默认8080)
java -jar rocketmq-console-ng-2.0.0.jar --rocketmq.config.namesrvAddr='localhost:9876' --server.port=18888 &
效果如下图所示:
至此RocketMQ集群(2主2从)搭建成功!
参考文档:
https://longhujing.github.io/2020/12/24/d9a0e3ef#RocketMQ-Console%E5%AE%89%E8%A3%85
https://cloud.tencent.com/developer/article/1412653