RocketMQ集群的搭建

 

目录

角色介绍

集群搭建方式

集群特点

集群模式

单Master模式

多Master模式

多Master多Slave模式(异步)

多Master多Slave模式(同步)

双主双从集群搭建

总体架构

集群工作流程

服务器环境(这里由于电脑内存有限,使用两台虚拟机的服务器来搭建,固定服务器的IP,具体搭建过程详见:Linux搭建RocketMQ)

修改服务器Host信息

防火墙配置

Broker配置文件

服务器:192.168.5.101

服务器:192.168.5.101

服务器:192.168.5.102

服务器:192.168.5.102

 修改启动脚本文件

runbroker.sh

runserver.sh

服务启动

启动NameServe集群

启动后通过JPS查看启动进程

查看nameServer日志

 查看broker日志

集群监控平台的搭建

下载并编译打包


角色介绍

  • Producer:消息的发送者
  • Consumer:消息接收者
  • Broker:暂存和传输消息
  • NameServer:管理Broker
  • Topic:区分消息的种类,一个Producer可以发送消息给一个或者多个Topic,一个Consumer可以订阅一个或者多个Topic消息
  • Message Queue:相当于是Topic的分区,用于并行发送和接收消息

集群搭建方式

集群特点

  • NameServer是一个几乎无状态节点,可集群部署,节点之间无任何信息同步
  • Broker分为Master与Slave,一个Master可以对应多个Slave,但是一个Slave只能对应一个Master,Master与Slave的关系通过指定相同的BrokerName,不同的BrokerId来定义,BrokerId为0表示Master,非0表示Slave。每个Broker与NameServer集群中所有节点建立长连接,定时注册Topic信息到所有NameServer。
  • Producer与NameServer集群中的其中一个节点建立长连接,定期从NameServer取Topic路由信息,并向提供Topic服务的Master建立长连接,且定时向Master发送心跳。Producer完全无状态,可集群部署。
  • Consumer与NameServer集群中的其中一个节点建立长连接,定期从NameServer取Topic路由信息,并向提供Topic服务的Master、Slave发送心跳。Consumer即可以订阅消息,也可以从Slave订阅信息,订阅规则由Broker配置决定。

集群模式

单Master模式

风险较高,一旦Broker重启或宕机,整个服务不可用

多Master模式

一个集群无Slave,全是Master

  • 优点:配置简单,单个Master宕机或者重启对应用无影响,即使宕机不可恢复,消息也不会丢失(异步刷盘丢失少量消息,同步刷盘不会丢失)
  • 缺点:单台机器宕机期间,未被消费的消息在机器恢复之前不可订阅,消息实时性会收到影响。

多Master多Slave模式(异步)

每个Master配置一个Slave,有多对Master-Slave,采用异步复制方式,主备有短暂消息延迟

  • 优点:即使磁盘损坏,消息丢失的非常少,且消息实时性不会受影响,同时Master宕机后,消费者仍然可以从Slave消费,而且此过程对应用透明,不需要人工干预,性能同多Master模式几乎一样
  • 缺点:Master宕机,磁盘损坏情况下会丢失少量消息。

多Master多Slave模式(同步)

每个Master配置一个Slave,有多对Master-Slave,采用同步双写模式,即只有主备都写成功,才向应用返回成功,

  • 优点:数据与服务都无单点故障,Master宕机情况下,消息无延迟,服务可用性与数据可用性非常高。
  • 缺点:性能比异步复制模式略低,发送单个消息的RT会略高,主节点宕机后,备机不能自动切换为主机

双主双从集群搭建

总体架构

消息高可用采用2m-2s(同步双写)

 

集群工作流程

  1. 启动NameServer,NameServer起来后监听端口,等待Broker、Producer、Consumer连上来,相当于一个路由控制中心。

  2. Broker启动,跟所有的NameServer保持长连接,定时发送心跳包。心跳包中包含当前Broker信息(IP+端口等)以及存储所有Topic信息。注册成功后,NameServer集群中就有Topic跟Broker的映射关系。

  3. 收发消息前,先创建Topic,创建Topic时需要指定该Topic要存储在哪些Broker上,也可以在发送消息时自动创建Topic。

  4. Producer发送消息,启动时先跟NameServer集群中的其中一台建立长连接,并从NameServer中获取当前发送的Topic存在哪些Broker上,轮询从队列列表中选择一个队列,然后与队列所在的Broker建立长连接从而向Broker发消息。

  5. Consumer跟Producer类似,跟其中一台NameServer建立长连接,获取当前订阅Topic存在哪些Broker上,然后直接跟Broker建立连接通道,开始消费消息。

服务器环境(这里由于电脑内存有限,使用两台虚拟机的服务器来搭建,固定服务器的IP,具体搭建过程详见:Linux固定IP地址Linux搭建JDKLinux搭建RocketMQ

序号Ip角色架构模式
1192.168.5.101NameServer  BrokerServerMaster1  Slave2
2192.168.5.102NameServer BrokerServerMaster2  Slave2

修改服务器Host信息

vim /etc/hosts

配置如下:

# nameserver
192.168.5.101 rocketmq-nameserver1
192.168.5.102 rocketmq-nameserver2
# broker
192.168.5.101 rocketmq-master1
192.168.5.101 rocketmq-slave2
192.168.5.102 rocketmq-master2
192.168.5.102 rocketmq-slave1

配置完成,重启网卡:

systemctl restart network

防火墙配置

由于都是本地虚拟机,仅供学习使用,没有安全性的考虑,所以关闭防火墙

# 关闭防火墙
systemctl stop firewalld.service

# 查看防火墙的状态

firewall-cmd --state

# 禁止firewall开机启动

systemctl disable firewalld.service

Broker配置文件

新建相关目录,Broker日志存储目录,由于主从在一个服务器搭建,所有要建立两个不一样的目录,否则从节点起不来

mkdir -p  {store-a,store-b}

 

服务器:192.168.5.101

vim /usr/local/rocketmq/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.
#所属集群名字
#暴露的外网IP
brokerIP1=192.168.5.101
brokerIP2=192.168.5.101
#所属集群名字
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/rocketmq/store-a
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store-a
#消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq/store-a
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store-a
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store-a
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store-a
#限制的消息大小
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

服务器:192.168.5.101

vi /usr/local/rocketmq/conf/2m-2s-sync/broker-b-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
#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/rocketmq/store-b
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store-b/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq/store-b/consumequeue
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store-b/index
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store-b/checkpoint
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store-b/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

服务器:192.168.5.102

vi /usr/local/rocketmq/conf/2m-2s-sync/broker-b.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
#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/rocketmq/store-b
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store-b/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq/store-b/consumequeue
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store-b/index
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store-b/checkpoint
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store-b/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

服务器:192.168.5.102

vi /usr/local/rocketmq/conf/2m-2s-sync/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
#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-a
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store-a
#消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq/store-a
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store-a
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store-a
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store-a
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVEI
brokerRole=SLAVE
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128

 修改启动脚本文件

  • runbroker.sh

vi /usr/local/rocketmq/bin/runbroker.sh

需要根据内存大小进行适当的对JVM参数进行调

JVM Configuration JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m"

  • runserver.sh

vim /usr/local/rocketmq/bin/runserver.sh 

JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

这两个一定要修改,否则节点起不来,会抛出虚拟机内存不够的异常

服务启动

启动NameServe集群

  • 分别在192.168.5.101和192.168.5.102启动NameServer
nohup sh mqnamesrv &
  • 在192.168.5.101上启动master1和slave2的Broker

master1:

nohup sh mqbroker -c /usr/local/rocketmq/conf/2m-2s-syncbroker-a.properties &

slave2:

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

  • 在192.168.5.102上启动master2和slave2的Broker

master2

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

slave1

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

启动的时候一定不要忘了 -c 才能指定配置文件启动,否则会造成两个broker冲突,起不来,因为storePathRootDir冲突了,导致第二个起不来,启动日志报错

java.lang.RuntimeException: Lock failed,MQ already started
	at org.apache.rocketmq.store.DefaultMessageStore.start(DefaultMessageStore.java:214)
	at org.apache.rocketmq.broker.BrokerController.start(BrokerController.java:827)
	at org.apache.rocketmq.broker.BrokerStartup.start(BrokerStartup.java:64)
	at org.apache.rocketmq.broker.BrokerStartup.main(BrokerStartup.java:58)

启动后通过JPS查看启动进程

[root@rocketmq-nameserver1 rocketmq]# jps
26325 BrokerStartup
26550 NamesrvStartup
26806 Jps
26250 BrokerStartup

查看nameServer日志

tail -500f ~/logs/rocketmqlogs/namesrv.log
Java HotSpot(TM) Server VM warning: Using the DefNew young collector with the CMS collector is deprecated and will likely be removed in a future release
Java HotSpot(TM) Server VM warning: UseCMSCompactAtFullCollection is deprecated and will likely be removed in a future release.
The Name Server boot success. serializeType=JSON

 查看broker日志

tail -500f ~/logs/rocketmqlogs/broker.log
The broker[broker-b, 192.168.5.101:11011] boot success. serializeType=JSON and name server is rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
The broker[broker-a, 192.168.5.101:10911] boot success. serializeType=JSON and name server is rocketmq-nameserver1:9876;rocketmq-nameserver2:9876

集群监控平台的搭建

RocketMQ有一个对其扩展的开源项目incubator-rocketmq-externals,这个项目中有一个子模块叫rocketmq-console,这个便是管理控制台项目了

下载并编译打包(Linux搭建maven

//需安装Git,直接yum -y install git即可
git clone https://github.com/apache/rocketmq-externals
cd rocketmq-console
//需搭建maven客户端
mvn clean package -D maven.test.skip=true

注意:打包前在rocketmq-console中配置namesrv集群地址:(在项目的 rocketmq-externals/rocketmq-console/src/main/resources 目录下的application.properties文件)

rocketmq.config.namesrvAddr=192.168.5.101:9876;192.168.5.102:9876

启动rocketmq-console:

nohup java -jar rocketmq-console-ng-1.0.0.jar &

启动成功后,我们就可以通过浏览器访问http://192.168.5.101:8080进入控制台界面了

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值