一、集群搭建可选方式

RocketMQ的物理部署结构图如下:

rocky8 配置vlan rocket r9_rocky8 配置vlan

Producer和Consumer对应的是我们的应用程序,多个NameServer实例组成集群,但相互独立,没有信息交换,所以对于NameServer来说部署两个或两个以上即可保证高可用,对于Broker来说,我们可以选择以下几种集群部署方式:

1.单Master模式

这种方式风险较大,一旦Broker重启或者宕机时,会导致整个服务不可用。不建议线上环境使用,可以用于本地测试。

2.多Master模式

一个集群无Slave,全是Master,例如2个Master或者3个Master,这种模式的优缺点如下:

优点:配置简单,单个Master宕机或重启维护对应用无影响,在磁盘配置为RAID10时,即使机器宕机不可恢复情况下,由于RAID10磁盘非常可靠,消息也不会丢(异步刷盘丢失少量消息,同步刷盘一条不丢),性能最高;

缺点:单台机器宕机期间,这台机器上未被消费的消息在机器恢复之前不可订阅,消息实时性会受到影响。

3.多Master多Slave模式-异步复制

每个Master配置一个Slave,有多对Master-Slave,HA采用异步复制方式,主备有短暂消息延迟(毫秒级),这种模式的优缺点如下:

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

缺点:Master宕机,磁盘损坏情况下会丢失少量消息。

4.多Master多Slave模式-同步双写

每个Master配置一个Slave,有多对Master-Slave,HA采用同步双写方式,即只有主备都写成功,才向应用返回成功,这种模式的优缺点如下:

优点:数据与服务都无单点故障,Master宕机情况下,消息无延迟,服务可用性与数据可用性都非常高;

缺点:性能比异步复制模式略低(大约低10%左右),发送单个消息的RT会略高,且目前版本在主节点宕机后,备机不能自动切换为主机。

本篇文章介绍如何用两台服务器搭建双Nameserver、双主Broker、双从Broker、无单点故障的高可用RocketMQ集群,两台服务器IP分别为:192.168.31.186和192.168.31.231。

在conf目录下提供了几种集群方式配置文件的示例,2m-noslave=双master模式;2m-2s-sync=双master双slave同步双写模式;2m-2s-async=双master双slave异步复制模式。

5. 2m-2s-async模式

首先启动2台服务器的NameServer

5.1 编辑broker配置文件

在192.168.31.186机器上的Master Broker的配置文件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
#broker名字,注意此处不同的配置文件填写的不一样  例如:在a.properties 文件中写 broker-a  在b.properties 文件中写 broker-b
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=0
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
#Broker 的角色,ASYNC_MASTER=异步复制Master,SYNC_MASTER=同步双写Master,SLAVE=slave节点
brokerRole=ASYNC_MASTER
#刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘 
flushDiskType=SYNC_FLUSH
#Broker 对外服务的监听端口
listenPort=10911
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.31.186:9876;192.168.31.231:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=8
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubscriptionGroup=true
#设置BrokerIP
brokerIP1=192.168.31.186
#存储路径
storePathRootDir=/data/rocketmq-all-4.7.0-bin-release/data/store-a
#commitLog 存储路径
storePathCommitLog=/data/rocketmq-all-4.7.0-bin-release/data/store-a/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/data/rocketmq-all-4.7.0-bin-release/data/store-a/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq-all-4.7.0-bin-release/data/store-a/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq-all-4.7.0-bin-release/data/store-a/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq-all-4.7.0-bin-release/data/store-a/abort
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000

在192.168.31.186机器上的Slave Broker的配置文件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名字,注意此处不同的配置文件填写的不一样  例如:在a.properties 文件中写 broker-a  在b.properties 文件中写 broker-b
brokerName=broker-b
#0 表示 Master,>0 表示 Slave
brokerId=1
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
#Broker 的角色,ASYNC_MASTER=异步复制Master,SYNC_MASTER=同步双写Master,SLAVE=slave节点
brokerRole=SLAVE
#刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘 
flushDiskType=SYNC_FLUSH
#Broker 对外服务的监听端口
listenPort=11011
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.31.186:9876;192.168.31.231:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=8
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubscriptionGroup=true
#设置BrokerIP
brokerIP1=192.168.31.186
#存储路径
storePathRootDir=/data/rocketmq-all-4.7.0-bin-release/data/store-b
#commitLog 存储路径
storePathCommitLog=/data/rocketmq-all-4.7.0-bin-release/data/store-b/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/data/rocketmq-all-4.7.0-bin-release/data/store-b/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq-all-4.7.0-bin-release/data/store-b/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq-all-4.7.0-bin-release/data/store-b/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq-all-4.7.0-bin-release/data/store-b/abort
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000

在192.168.31.231机器上的Master Broker的配置文件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名字,注意此处不同的配置文件填写的不一样  例如:在a.properties 文件中写 broker-a  在b.properties 文件中写 broker-b
brokerName=broker-b
#0 表示 Master,>0 表示 Slave
brokerId=0
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
#Broker 的角色,ASYNC_MASTER=异步复制Master,SYNC_MASTER=同步双写Master,SLAVE=slave节点
brokerRole=ASYNC_MASTER
#刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘 
flushDiskType=SYNC_FLUSH
#Broker 对外服务的监听端口
listenPort=10911
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.31.186:9876;192.168.31.231:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=8
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubscriptionGroup=true
#设置BrokerIP
brokerIP1=192.168.31.231
#存储路径
storePathRootDir=/data/rocketmq-all-4.7.0-bin-release/data/store-b
#commitLog 存储路径
storePathCommitLog=/data/rocketmq-all-4.7.0-bin-release/data/store-b/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/data/rocketmq-all-4.7.0-bin-release/data/store-b/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq-all-4.7.0-bin-release/data/store-b/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq-all-4.7.0-bin-release/data/store-b/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq-all-4.7.0-bin-release/data/store-b/abort
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000

在192.168.31.231机器上的Slave Broker的配置文件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名字,注意此处不同的配置文件填写的不一样  例如:在a.properties 文件中写 broker-a  在b.properties 文件中写 broker-b
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=1
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
#Broker 的角色,ASYNC_MASTER=异步复制Master,SYNC_MASTER=同步双写Master,SLAVE=slave节点
brokerRole=SLAVE
#刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘 
flushDiskType=SYNC_FLUSH
#Broker 对外服务的监听端口
listenPort=11011
#nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3)
namesrvAddr=192.168.31.186:9876;192.168.31.231:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=8
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubscriptionGroup=true
#设置BrokerIP
brokerIP1=192.168.31.231
#存储路径
storePathRootDir=/data/rocketmq-all-4.7.0-bin-release/data/store-a
#commitLog 存储路径
storePathCommitLog=/data/rocketmq-all-4.7.0-bin-release/data/store-a/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/data/rocketmq-all-4.7.0-bin-release/data/store-a/consumequeue
#消息索引存储路径
storePathIndex=/data/rocketmq-all-4.7.0-bin-release/data/store-a/index
#checkpoint 文件存储路径
storeCheckpoint=/data/rocketmq-all-4.7.0-bin-release/data/store-a/checkpoint
#abort 文件存储路径
abortFile=/data/rocketmq-all-4.7.0-bin-release/data/store-a/abort
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000

5.2 启动broker

启动时,先启动两台机器上的Master节点,再启动两台机器上的Slave节点。

192.168.31.186上启动broker-a

# -c 后面为指定配置文件启动 tail -f ./mqbroker.log 为输出日志
nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties & tail -f ./mqbroker.log

192.168.31.231上启动broker-b

nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties & tail -f ./mqbroker.log

192.168.31.231上启动broker-a-s

nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties  & tail -f ./mqbroker.log

192.168.31.186上启动broker-b-s

nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties & tail -f ./mqbroker.log

6.注意事项

6.1内存不足

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000005c0000000, 8589934592, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map 8589934592 bytes for committing reserved memory.
# An error report file with more information is saved as:

# /data/rocketmq-all-4.7.0-bin-release/hs_err_pid1841.log
  • 原因:broker的java启动内存参数配置的是8g,请根据服务器实际资源做调整。
  • 解决方案:修改内存参数,在bin/runbroker.sh文件中

JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn512m"

6.2开放RocketMQ相关端口

NameServer的9876端口、Broker的10911、11011、10909、11009端口

可以选择关闭服务器防火墙或者开放相应的端口

6.3mqadmin工具

sh bin/mqadmin clusterList -n "192.168.31.186:9876;192.168.31.231:9876"

结果

[root@rocketmq-1 rocketmq-all-4.7.0-bin-release]# sh bin/mqadmin clusterList -n "192.168.31.186:9876;192.168.31.231:9876"
RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent0).
RocketMQLog:WARN Please initialize the logger system properly.
#Cluster Name     #Broker Name            #BID  #Addr                  #Version                #InTPS(LOAD)       #OutTPS(LOAD) #PCWait(ms) #Hour #SPACE
rocketmq-cluster  broker-a                0     192.168.31.186:10911   V4_7_0                   0.00(0,0ms)         0.00(0,0ms)          0 441917.21 -1.0000
rocketmq-cluster  broker-a                1     192.168.31.231:11011   V4_7_0                   0.00(0,0ms)         0.00(0,0ms)          0 441917.21 0.1308
rocketmq-cluster  broker-b                0     192.168.31.231:10911   V4_7_0                   0.00(0,0ms)         0.00(0,0ms)          0 441917.21 -1.0000
rocketmq-cluster  broker-b                1     192.168.31.186:11011   V4_7_0

6.4关闭服务

先关闭Broker、再关闭NameServer,服务启动的时候正好相反。

[root@rocketmq-1 rocketmq-all-4.7.0-bin-release]# sh bin/mqshutdown broker

[root@rocketmq-1 rocketmq-all-4.7.0-bin-release]# sh bin/mqshutdown namesrv