一、集群搭建可选方式
RocketMQ的物理部署结构图如下:
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。
二、安装环境
- Linux版本:CenterOS 7
- RocketMQ版本:4.7.0
- Java版本:jdk1.8
三、安装步骤
1.安装java环境
在两台机器上分别安装Java环境,以其中一台为例。
1.1.下载jdk安装包
可以上Oracle官网下载Linux版的jdk安装包,jdk-8u161-linux-x64.tar.gz
1.2.上传jdk包到服务器
在/usr/local目录下创建一个java目录,并将jdk包上传至java目录下
cd /usr/local/
mkdir java
解压jdk安装包
tar zxvf jdk-8u161-linux-x64.tar.gz
1.3.配置环境变量
vi /etc/profile
在文件的最下方添加
JAVA_HOME=/usr/local/java/jdk1.8.0_161
CLASSPATH=.:$JAVA_HOME/lib/
PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH
使环境变量修改生效
source /etc/profile
1.4.验证安装是否成功
java -version
2.安装NameServer
在两台机器上分别安装启动NameServer,以其中一台为例。
2.1.创建data目录并进入到data目录
mkdir /data
2.2.下载RocketMQ安装包
[root@rocketmq-2 data]# wget https://mirror.bit.edu.cn/apache/rocketmq/4.7.0/rocketmq-all-4.7.0-bin-release.zip
2.3.解压RocketMQ安装包
[root@rocketmq-2 data]# unzip rocketmq-all-4.7.0-bin-release.zip
2.4.启动NameServer
[root@rocketmq-2 data]# nohup sh /data/rocketmq-all-4.7.0-bin-release/bin/mqnamesrv &
2.5.验证NameServer是否启动成功
[root@rocketmq-2 data]# jps
1842 NamesrvStartup
通过jps命令查看有NameServer进程表示启动成功,NameServer的日志文件在/root/logs/rocketmqlogs目录中,通过/data/rocketmq-all-4.7.0-bin-release/conf/logback_namesrv.xml可以配置日志文件目录。
2.6 停止NameServer
[root@rocketmq-2 rocketmq-all-4.7.0-bin-release]# sh /data/rocketmq-all-4.7.0-bin-release/bin/mqshutdown namesrv
3.安装Broker
每台机器上都要启动一个Master角色和Slave角色的Broker,并互为主备,即在A机器上启动broker-a的master节点、broker-b-s的slave节点;在B机器上启动broker-b的master节点、broker-a-s的slave节点。
在conf目录下提供了几种集群方式配置文件的示例,2m-noslave=双master模式;2m-2s-sync=双master双slave同步双写模式;2m-2s-async=双master双slave异步复制模式。
本次安装采用2m-2s-async模式
3.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
3.2 启动broker
启动时,先启动两台机器上的Master节点,再启动两台机器上的Slave节点。
192.168.31.186上启动broker-a
[root@rocketmq-1 rocketmq-all-4.7.0-bin-release]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties &
192.168.31.231上启动broker-b
[root@rocketmq-2 rocketmq-all-4.7.0-bin-release]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties &
192.168.31.231上启动broker-a-s
[root@rocketmq-2 rocketmq-all-4.7.0-bin-release]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties &
192.168.31.186上启动broker-b-s
[root@rocketmq-1 rocketmq-all-4.7.0-bin-release]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties &
3.3 注意事项
3.3.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"
3.3.2 开放RocketMQ相关端口
NameServer的9876端口、Broker的10911、11011、10909、11009端口
可以选择关闭服务器防火墙或者开放相应的端口
3.4 验证
查看broker.log日志文件输出正常,日志文件的目录在/data/rocketmq-all-4.7.0-bin-release/conf/logback_broker.xml可以查看/修改
3.5 mqadmin工具
查看集群情况
[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 0.00(0,0ms) 0.00(0,0ms) 0 441917.21 -1.0000
mqadmin还有其他的用法,具体的可以参考文档。
3.6 关闭服务
先关闭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
4.安装rocketmq-console(可选)
rocketmq-console是RocketMQ项目的扩展插件,是一个图形化管理控制台,提供Broker集群状态查看,Topic管理,Producer、Consumer状态展示,消息查询等常用功能,这个功能在安装好RocketMQ后需要额外单独安装、运行。
在本地有git+maven的开发环境可以自行在https://github.com/apache/rocketmq-externals克隆下载项目后,再通过maven打包rocketmq-console,或者可以在服务器上参考如下步骤进行。
4.1 安装maven
下载安装包
[root@rocketmq-1 data]# wget http://mirrors.cnnic.cn/apache/maven/maven-3/3.5.4/binaries/apache-maven-3.5.4-bin.tar.gz
解压
[root@rocketmq-1 data]# tar zxvf apache-maven-3.5.4-bin.tar.gz
配置环境变量
[root@rocketmq-1 data]# vi /etc/profile
在最后输入:
export MAVEN_HOME=/data/apache-maven-3.5.4
export PATH=$MAVEN_HOME/bin:$PATH
保存后退出使之生效
[root@rocketmq-1 data]# source /etc/profile
验证
[root@rocketmq-1 data]# mvn -v
4.2 下载打包rocketmq-console
在https://github.com/apache/rocketmq-externals页面下载zip包上传至任意一个服务器上。
解压
[root@rocketmq-1 data]# unzip rocketmq-externals-master.zip
进入rocketmq-console目录
[root@rocketmq-1 data]# cd rocketmq-externals-master/rocketmq-console/
打包
[root@rocketmq-1 rocketmq-console]# mvn clean package -Dmaven.test.skip=true
4.3 启动rocketmq-console
[root@rocketmq-1 rocketmq-console]# java -jar target/rocketmq-console-ng-1.0.1.jar --rocketmq.config.namesrvAddr='192.168.31.186:9876;192.168.31.231:9876'
4.4 验证
启动无异常后,访问http://192.168.31.186:8080即可看到如下界面:
四、推荐阅读:
http://rocketmq.apache.org/docs/quick-start/
https://github.com/apache/rocketmq/tree/master/docs/cn
https://github.com/apache/rocketmq-externals/tree/master/rocketmq-console