一、集群搭建可选方式
Producer和Consumer对应的是我们的应用程序,多个NameServer实例组成集群,但相互独立,没有信息交换,所以对于NameServer来说部署两个或两个以上即可保证高可用,对于Broker来说,我们可以选择以下几种集群部署方式:
这种方式风险较大,一旦Broker重启或者宕机时,会导致整个服务不可用。不建议线上环境使用,可以用于本地测试。
一个集群无Slave,全是Master,例如2个Master或者3个Master,这种模式的优缺点如下:
优点:配置简单,单个Master宕机或重启维护对应用无影响,在磁盘配置为RAID10时,即使机器宕机不可恢复情况下,由于RAID10磁盘非常可靠,消息也不会丢(异步刷盘丢失少量消息,同步刷盘一条不丢),性能最高;
缺点:单台机器宕机期间,这台机器上未被消费的消息在机器恢复之前不可订阅,消息实时性会受到影响。
每个Master配置一个Slave,有多对Master-Slave,HA采用异步复制方式,主备有短暂消息延迟(毫秒级),这种模式的优缺点如下:
优点:即使磁盘损坏,消息丢失的非常少,且消息实时性不会受影响,同时Master宕机后,消费者仍然可以从Slave消费,而且此过程对应用透明,不需要人工干预,性能同多Master模式几乎一样;
缺点:Master宕机,磁盘损坏情况下会丢失少量消息。
每个Master配置一个Slave,有多对Master-Slave,HA采用同步双写方式,即只有主备都写成功,才向应用返回成功,这种模式的优缺点如下:
优点:数据与服务都无单点故障,Master宕机情况下,消息无延迟,服务可用性与数据可用性都非常高;
缺点:性能比异步复制模式略低(大约低10%左右),发送单个消息的RT会略高,且目前版本在主节点宕机后,备机不能自动切换为主机。
本篇文章介绍如何用两台服务器搭建双Nameserver、双主Broker、双从Broker、无单点故障的高可用RocketMQ集群,两台服务器IP分别为:192.168.31.186和192.168.31.231。
二、安装环境
三、安装步骤
cd /usr/local/
mkdir java
tar zxvf jdk-8u161-linux-x64.tar.gz
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
java -version
mkdir /data
[root@rocketmq-2 data]# wget https://mirror.bit.edu.cn/apache/rocketmq/4.7.0/rocketmq-all-4.7.0-bin-release.zip
[root@rocketmq-2 data]# unzip rocketmq-all-4.7.0-bin-release.zip
[root@rocketmq-2 data]# nohup sh /data/rocketmq-all-4.7.0-bin-release/bin/mqnamesrv &
[root@rocketmq-2 data]# jps
1842 NamesrvStartup
[root@rocketmq-2 rocketmq-all-4.7.0-bin-release]# sh /data/rocketmq-all-4.7.0-bin-release/bin/mqshutdown namesrv
# 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
# 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
# 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
# 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
[root@rocketmq-1 rocketmq-all-4.7.0-bin-release]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties &
[root@rocketmq-2 rocketmq-all-4.7.0-bin-release]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties &
[root@rocketmq-2 rocketmq-all-4.7.0-bin-release]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties &
[root@rocketmq-1 rocketmq-all-4.7.0-bin-release]# nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties &
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
JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn512m"
[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
[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
[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
[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
[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'
四、推荐阅读:
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
END
官方站点:www.linuxprobe.com
Linux命令大全:www.linuxcool.com
刘遄老师QQ:5604922
Linux技术交流群:193666693
(新群,火热加群中……)
想要学习Linux系统的读者可以点击"阅读原文"按钮来了解书籍《Linux就该这么学》,同时也非常适合专业的运维人员阅读,成为辅助您工作的高价值工具书!