一、rocketmq集群方法
1、单节点 :
优点:本地开发测试,配置简单,同步刷盘消息⼀条都不会丢
缺点:不可靠,如果宕机,会导致服务不可⽤
2、主从(异步、同步双写) :
优点:同步双写消息不丢失, 异步复制存在少量丢失 ,主节点宕机,从节点可以对外提供消息的消费,但是不⽀持 写⼊
缺点:主备有短暂消息延迟,毫秒级,⽬前不⽀持⾃动切换,需要脚本或者其他程序进⾏检测然后进⾏停⽌broker, 重启让从节点成为主节点
3、双主:
优点:配置简单, 可以靠配置RAID磁盘阵列保证消息可靠,异步刷盘丢失少量消息
缺点: master机器宕机期间,未被消费的消息在机器恢复之前不可消费,实时性会受到影响
4、双主双从,多主多从模式(异步复制)
优点:磁盘损坏,消息丢失的⾮常少,消息实时性不会受影响,Master 宕机后,消费者仍然可以从Slave消费
缺点:主备有短暂消息延迟,毫秒级,如果Master宕机,磁盘损坏情况,会丢失少量消息
5、双主双从,多主多从模式(同步双写)
优点:同步双写⽅式,主备都写成功,向应⽤才返回成功,服务可⽤性与数据可⽤性都⾮常⾼
缺点:性能⽐异步复制模式略低,主宕机后,备机不能⾃动切换为主机
【注】:推荐⽅案2、4、5
下⾯介绍如何⽤两台服务器搭建双Nameserver、双主Broker、双从Broker、⽆单点故障的RocketMQ集群,两台服务 器IP分别为:192.168.0.9和192.168.0.10。
二、安装环境
Linux版本:CenterOS 7.6
RocketMQ版本:4.9.2
Java版本:jdk1.8
三、安装步骤
安装java环境
1.1.下载1.8.0版本
yum install java
1.2.验证安装是否成功
java -version
安装NameServer
在两台机器上分别安装启动NameServer,以其中一台为例。
2.1.创建data目录并进入到data目录
mkdir /data
2.2.下载RocketMQ安装包
wget https://mirror.tuna.tsinghua.edu.cn/apache/rocketmq/4.9.2/rocketmq-all-4.9.2-binrelease.zip --no-check-certificate
2.3.解压RocketMQ安装包
[root@ecs-ac0e data]# unzip rocketmq-all-4.9.2-bin-release.zip
2.4.启动NameServer
[root@ecs-ac0e data]# nohup sh /data/rocketmq-4.9.2/bin/mqnamesrv 2>&1 &
2.5.停止NameServer
[root@ecs-ac0e data]# sh /data/rocketmq-4.9.2/bin/mqshutdown namesrv
安装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.0.9机器上的Master Broker的配置⽂件broker-a.properties
[root@ecs-ac0e 2m-2s-async]$ vi /data/rocketmq-4.9.2/conf/2m-2s-async/brokera.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.0.9:9876;192.168.0.10:9876 #每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过⼩不利于consumer负载均衡 defaultTopicQueueNums=8 #是否允许 Broker ⾃动创建Topic,⽣产建议关闭 autoCreateTopicEnable=true #是否允许 Broker ⾃动创建订阅组,⽣产建议关闭 autoCreateSubscriptionGroup=true #设置BrokerIP brokerIP1=192.168.0.9 #存储路径 storePathRootDir=/data/rocketmq-4.9.2/data/store-a #commitLog 存储路径 storePathCommitLog=/data/rocketmq-4.9.2/data/store-a/commitlog #消费队列存储路径存储路径 storePathConsumerQueue=/data/rocketmq-4.9.2/data/store-a/consumequeue #消息索引存储路径 storePathIndex=/data/rocketmq-4.9.2/data/store-a/index #checkpoint ⽂件存储路径 storeCheckpoint=/data/rocketmq-4.9.2/data/store-a/checkpoint #abort ⽂件存储路径 abortFile=/data/rocketmq-4.9.2/data/store-a/abort #commitLog每个⽂件的⼤⼩默认1G mapedFileSizeCommitLog=1073741824 #ConsumeQueue每个⽂件默认存30W条,根据业务情况调整 mapedFileSizeConsumeQueue=300000
在192.168.0.9机器上的Slave Broker的配置⽂件broker-b-s.properties
[root@ecs-ac0e 2m-2s-async]# vi /data/rocketmq-4.9.2/conf/2m-2s-async/broker-bs.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 表示 SlavebrokerId=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.0.9:9876;192.168.0.10:9876 #每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过⼩不利于consumer负载均衡 defaultTopicQueueNums=8 #是否允许 Broker ⾃动创建Topic,⽣产建议关闭 autoCreateTopicEnable=true #是否允许 Broker ⾃动创建订阅组,⽣产建议关闭 autoCreateSubscriptionGroup=true #设置 BrokerIPbrokerIP1=192.168.0.9 #存储路径 storePathRootDir=/data/rocketmq-4.9.2/data/store-b#commitLog 存储路径 storePathCommitLog=/data/rocketmq-4.9.2/data/store-b/commitlog #消费队列存储路径存储路径 storePathConsumerQueue=/data/rocketmq-4.9.2/data/store-b/consumequeue #消息索引存储路径 storePathIndex=/data/rocketmq-4.9.2/data/store-b/index #checkpoint ⽂件存储路径 storeCheckpoint=/data/rocketmq-4.9.2/data/store-b/checkpoint #abort ⽂件存储路径 abortFile=/data/rocketmq-4.9.2/data/store-b/abort#commitLog 每个⽂件的⼤⼩默认1G mapedFileSizeCommitLog=1073741824 #ConsumeQueue每个⽂件默认存30W条,根据业务情况调整 mapedFileSizeConsumeQueue=300000
在192.168.0.10 机器上的Master Broker的配置⽂件broker-b.properties
[root@ecs-eafa data]$ vi /data/rocketmq-4.9.2/conf/2m-2s-async/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.0.9:9876;192.168.0.10:9876 #每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过⼩不利于consumer负载均衡 defaultTopicQueueNums=8 #是否允许 Broker ⾃动创建Topic,⽣产建议关闭 autoCreateTopicEnable=true #是否允许 Broker ⾃动创建订阅组,⽣产建议关闭 autoCreateSubscriptionGroup=true #设置BrokerIP brokerIP1=192.168.0.10 #存储路径 storePathRootDir=/data/rocketmq-4.9.2/data/store-b #commitLog 存储路径 storePathCommitLog=/data/rocketmq-4.9.2/data/store-b/commitlog #消费队列存储路径存储路径 storePathConsumerQueue=/data/rocketmq-4.9.2/data/store-b/consumequeue #消息索引存储路径 storePathIndex=/data/rocketmq-4.9.2/data/store-b/index #checkpoint ⽂件存储路径 storeCheckpoint=/data/rocketmq-4.9.2/data/store-b/checkpoint #abort ⽂件存储路径 abortFile=/data/rocketmq-4.9.2/data/store-b/abort #commitLog每个⽂件的⼤⼩默认1G mapedFileSizeCommitLog=1073741824 #ConsumeQueue每个⽂件默认存30W条,根据业务情况调整 mapedFileSizeConsumeQueue=300000
在192.168.0.10 机器上的Slave Broker的配置⽂件broker-a-s.properties
[root@ecs-eafa data]$ vi /data/rocketmq-4.9.2/conf/2m-2s-async/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.0.9:9876;192.168.0.10:9876 #每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过⼩不利于consumer负载均衡 defaultTopicQueueNums=8 #是否允许 Broker ⾃动创建Topic,⽣产建议关闭 autoCreateTopicEnable=true #是否允许 Broker ⾃动创建订阅组,⽣产建议关闭 autoCreateSubscriptionGroup=true #设置BrokerIP brokerIP1=192.168.0.10 #存储路径 storePathRootDir=/data/rocketmq-4.9.2/data/store-a #commitLog #存储路径 storePathCommitLog=/data/rocketmq-4.9.2/data/store-a/commitlog #消费队列存储路径存储路径 storePathConsumerQueue=/data/rocketmq-4.9.2/data/store-a/consumequeue #消息索引存储路径 storePathIndex=/data/rocketmq-4.9.2/data/store-a/index #checkpoint ⽂件存储路径 storeCheckpoint=/data/rocketmq-4.9.2/data/store-a/checkpoint #abort ⽂件存储路径 abortFile=/data/rocketmq-4.9.2/data/store-a/abort #commitLog每个⽂件的⼤⼩默认1G mapedFileSizeCommitLog=1073741824 #ConsumeQueue每个⽂件默认存30W条,根据业务情况调整 mapedFileSizeConsumeQueue=300000
3.2.启动broker
启动时,先启动两台机器上的Master节点,再启动两台机器上的Slave节点。
192.168.0.9上启动broker-a
cd /data/rocketmq-4.9.2 nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties 2>&1 &
报错如下是系统内存不⾜
原因:broker的java启动内存参数配置的是8g,请根据服务器实际资源做调整。
解决⽅案:修改内存参数,在bin/runbroker.sh⽂件中
[root@ecs-ac0e rocketmq-4.9.2]# vi bin/runbroker.sh JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn512m"
192.168.0.10上启动broker-b
cd /data/rocketmq-4.9.2 nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties 2>&1 &
192.168.0.10上启动broker-a-s
cd /data/rocketmq-4.9.2 nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties 2>&1 &
192.168.0.9上启动broker-b-s
cd /data/rocketmq-4.9.2 nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties 2>&1 &
开放RocketMQ相关端口
NameServer的9876端⼝、Broker的10911、11011、10909、11009端⼝
可以选择关闭服务器防⽕墙或者开放相应的端⼝
查看集群情况
cd /data/rocketmq-4.9.2 sh bin/mqadmin clusterList -n "192.168.0.9:9876;192.168.0.10:9876"
如果出现下⾯这个报错,原因是缺少java⽂件,需要进⾏导⼊
解决方案如下:
[root@ecs-ac0e rocketmq-4.9.2]# cd /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07- 1.el7_9.x86_64/jre/lib/ext [root@ecs-ac0e rocketmq-4.9.2]# cp sunjce_provider.jar /data/rocketmq-4.9.2/lib
集群显示的状态如下,即集群成功