Rocket描述:
RocketMQ: producer:消息的发送者,写信人; consumer:消息的接收者;收信人; broker:存储和传递消息,邮局 nameserv:管理broker,相当于邮局的管理机构一样!(数据无状态,) topic:区分消息的种类,一个producer可以给一个或多个topic发送短信,一个consumer可以接收一个或多个topic的消息; Message queue:相当于消息的分区,用于并行发送和接收消息;
Rocket集群描述
集群描述: nameserv/producer/consumer:启动多个nameserv就是集群 1.nameservver是一个几乎无状态节点,可集群部署,节点直接无任何数据同步; 2.broker: 2.1:分为master和slave节点;一个master可以有多个salver,但是一个slave只能有一个master; 2.2:他们通过brokerName进行分组,brokerId=0代表master,brokerId=1代表salver; 2.3:每个broker和nameserver建立长连接,定时注册topic信息到所有的nameserver; 3.producer:producer和nameserver集群中的一个节点建立长连接(随机),定期从nameserver中获取topic路由信息,并向提供topic服务的master建立长连接,并定时向master节点发送心跳;producer完全无状态可集群部署; 4.consumer:consumer和namesever集群中的任一节点建立长连接,定期从namserver中获取topic路由信息,并向提过topic服务的maseter节点和slaver节点建立长连接,然后定时向master和slaver发送心跳,consumer可以master和slaver节点中都订阅消息,订阅规则由broker配置的规则决定;
三.集群搭建模式;
1.单master模式;不推荐使用,宕机玩完了!
2.多master模式:
3.多master多slaver(异步):单台master宕机后数据会有少量的丢失;
4.多master多slaver(同步):
四:集群的工作流程:
1.启动namaserver:nameserver启动后,监听broker,producer和consumer是否上线,相当于一个路由中心; 2.启动broker:broker和所有的nameserver建立长连接,定时发送心跳包,心跳包包含当前broker(ip+端口)以及topic储藏信息;当注册成功后,namserver就有了所有的broker和topic的映射关系; 3.创建topic:发送消息前需要先创建topic,需要指明当前topic要储藏在那个broker上 去; 4.producer生产消息:先和nameserver建立长连接,看当前的topic是在那些broker上面去,轮询从队列列表中选择一个队列,然后与所在队列的broker建立长连接从而向其发送信息; 5.consumer消费消息:跟其中的一台namsever建立长连接,获取当前的topic在那些Broker上,然后和broker建立连接,消费信息!
五:Rocket集群搭建:
服务器环境:
序号 | IP | 角色 | 架构模式 |
1 | 192.168.237.14 | nameserver/broker | Master1/slave2 |
2 | 192.168.237.9 | nameserver/broker | Master2/slave1 |
1.host添加信息
vim /etc/hosts #nameserver 192.168.237.14 rocketmq-nameserver1 192.168.237.9 rocketmq-nameserver2 #broker 192.168.237.14 rocketmq-master1 192.168.237.14 rocketmq-slave2 192.168.237.9 rocketmq-master2 192.168.237.9 rocketmq-slave1 配置完场重启网卡: systemctl restart network
2.防火墙关闭;
#查看防火墙状态; filewall-cmd --state #禁止firewalld开机启动; systemctl disable filewalld.server
在实际的生产环境可以指定开放端口: #开放nameserver端口: filewalld-cmd --remove-port=9876/tcp--permanent #开放master端口: filewalld-cmd --remove-port=10911/tcp--permanent #开放slaved端口(集群模式下不可开启) filewalld-cmd --remove-port=11011/tcp--permanent 重启防火墙: filewalld-cmd --reload
3.环境变量配置:
[root@docker ~]# vim /etc/profile
#set rocketmq
ROCKETMQ_HOME=/usr/local/rocketmq/rocketmq-all-4.2.0-bin-release
PATH=$ROCKETMQ_HOME/bin:$PATH
export ROCKETMQ_HOME PATH
[root@docker ~]# source /etc/profile
4.创建消息存储路径:
在配置双主双从集群时,配置文件中相关的存储路径需要区分,主节点、从节点的数据不能保存在同一个目录下,不然则会出现:启动NamesrvStartup成功,启动第一个broker master成功,启动第二个broker slave则一直失败。
master文件目录:
mkdir /usr/local/rocketmq/store
mkdir /usr/local/rocketmq/store/commitlog
mkdir /usr/local/rocketmq/store/consumequeue
mkdir /usr/local/rocketmq/store/index
slave文件目录:
mkdir /usr/local/rocketmq/store_slave mkdir /usr/local/rocketmq/store_slave/commitlog mkdir /usr/local/rocketmq/store_slave/consumequeue mkdir /usr/local/rocketmq/store_slave/index
5.broker配置文件:
采用的多master多slave,同步刷新策略(ASYNC)
1、在192.238.237.14主机上对broker-a.properties文件进行Master1配置
[root@docker ~]# cd /usr/local/rocketmq/conf/2m-2s-async/ [root@docker 2m-2s-async]# ll total 16 -rw-r--r--. 1 root root 929 Sep 19 2017 broker-a.properties -rw-r--r--. 1 root root 922 Sep 19 2017 broker-a-s.properties -rw-r--r--. 1 root root 929 Sep 19 2017 broker-b.properties -rw-r--r--. 1 root root 922 Sep 19 2017 broker-b-s.properties [root@docker 2m-2s-async]#vim broker-a.properties
在192.168.237.14机器上的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 #nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3) namesrvAddr=192.168.237.14:9876;192.168.237.9:9876 #每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡 defaultTopicQueueNums=4 #是否允许 Broker 自动创建Topic,生产建议关闭 autoCreateTopicEnable= true #是否允许 Broker 自动创建订阅组,生产建议关闭 autoCreateSubionGroup= true #Broker 对外服务的监听端口 listenPort=10911 #删除文件时间点,默认凌晨 4点 deleteWhen=04 #文件保留时间,默认 48 小时 fileReservedTime=120 #commitLog每个文件的大小默认1G mapedFileSizeCommitLog=1073741824 #ConsumeQueue每个文件默认存30W条,根据业务情况调整 mapedFileSizeConsumeQueue=300000 #检测物理文件磁盘空间 disMaxUsedSpaceRatio=88 #存储路径 storePathRootDir=/usr/local/rocketmq/store #commitLog 存储路径 storePathCommitLog=/usr/local/rocketmq/store/commitlog #消费队列存储路径存储路径 storePathConsumerQueue=/usr/local/rocketmq/store/consumequeue #消息索引存储路径 storePathIndex=/usr/local/rocketmq/store/index #checkpoint 文件存储路径 storeCheckpoint=/usr/local/rocketmq/store/checkpoint #abort 文件存储路径 abortFile=/usr/local/rocketmq/store/abort #限制消息的大小 maxMessageSize=65536 #Broker 的角色,ASYNC_MASTER=异步复制Master,SYNC_MASTER=同步双写Master,SLAVE=slave节点 brokerRole=ASYNC_MASTER #刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘 flushDiskType=SYNC_FLUSH
2、在192.238.237.14主机上对broker-b-s.properties文件进行slvave2配置:
[root@docker 2m-2s-async]#vim broker-b-s.properties
在192.168.237.14机器上的slave2 Broker的配置文件broker-b-s.properties
#所属集群名字
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.237.14:9876;192.168.237.9:9876
#每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,生产建议关闭
autoCreateTopicEnable= true
#是否允许 Broker 自动创建订阅组,生产建议关闭
autoCreateSubionGroup= true
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#检测物理文件磁盘空间
disMaxUsedSpaceRatio=88
#限制消息的大小
maxMessageSize=65536
#存储路径
storePathRootDir=/usr/local/rocketmq/store_slave
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store_slave/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/usr/local/rocketmq/store_slave/consumequeue
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store_slave/index
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store_slave/checkpoint
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store_slave/abort
3.在192.168.237.9主机上对 broker-b.properties进行Master2配置; 、
[root@node1 2m-2s-sync]# vim broker-b.properties
#所属集群名字 brokerClusterName=rocketmq-cluster #broker名字,注意此处不同的配置文件填写的不一样 例如:在a.properties 文件中写 broker-a 在b.properties 文件中写 broker-b brokerName=broker-b #0 表示 Master,>0 表示 Slave brokerId=0 #nameServer地址,这里nameserver是单台,如果nameserver是多台集群的话,就用分号分割(即namesrvAddr=ip1:port1;ip2:port2;ip3:port3) namesrvAddr=192.168.237.14:9876;192.168.237.9:9876 #每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡 defaultTopicQueueNums=4 #是否允许 Broker 自动创建Topic,生产建议关闭 autoCreateTopicEnable= true #是否允许 Broker 自动创建订阅组,生产建议关闭 autoCreateSubionGroup= true #Broker 对外服务的监听端口 listenPort=10911 #删除文件时间点,默认凌晨 4点 deleteWhen=04 #文件保留时间,默认 48 小时 fileReservedTime=120 #commitLog每个文件的大小默认1G mapedFileSizeCommitLog=1073741824 #ConsumeQueue每个文件默认存30W条,根据业务情况调整 mapedFileSizeConsumeQueue=300000 检测物理文件磁盘空间 disMaxUsedSpaceRatio=88 #存储路径 storePathRootDir=/usr/local/rocketmq/store #commitLog 存储路径 storePathCommitLog=/usr/local/rocketmq/store/commitlog #消费队列存储路径存储路径 storePathConsumerQueue=/usr/local/rocketmq/store/consumequeue #消息索引存储路径 storePathIndex=/usr/local/rocketmq/store/index #checkpoint 文件存储路径 storeCheckpoint=/usr/local/rocketmq/store/checkpoint #abort 文件存储路径 abortFile=/usr/local/rocketmq/store/abort #限制消息的大小 maxMessageSize=65536 #Broker 的角色,ASYNC_MASTER=异步复制Master,SYNC_MASTER=同步双写Master,SLAVE=slave节点 brokerRole=ASYNC_MASTER #刷盘方式,ASYNC_FLUSH=异步刷盘,SYNC_FLUSH=同步刷盘 flushDiskType=SYNC_FLUSH
4.在192.168.237.9主机上对 broker-a-s.properties进行slave1配
[root@node1 2m-2s-sync]# vim broker-a-s.properties
在192.168.237.9机器上的Slave Broker的配置文件broker-a-s.properties #所属集群名字 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.237.14:9876;192.168.237.9:9876 #每个topic对应队列的数量,默认为4,实际应参考consumer实例的数量,值过小不利于consumer负载均衡 defaultTopicQueueNums=4 #是否允许 Broker 自动创建Topic,生产建议关闭 autoCreateTopicEnable= true #是否允许 Broker 自动创建订阅组,生产建议关闭 autoCreateSubionGroup= true #commitLog每个文件的大小默认1G mapedFileSizeCommitLog=1073741824 #ConsumeQueue每个文件默认存30W条,根据业务情况调整 mapedFileSizeConsumeQueue=300000 #检测物理文件磁盘空间 disMaxUsedSpaceRatio=88 #限制消息的大小 maxMessageSize=65536
#存储路径
storePathRootDir=/usr/local/rocketmq/store_slave
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store_slave/commitlog
#消费队列存储路径存储路径
storePathConsumerQueue=/usr/local/rocketmq/store_slave/consumequeue
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store_slave/index
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store_slave/checkpoint
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store_slave/abort
6.修改启动脚本文件:
7.服务启动:
1.启动Namaserver集群: 分别在192.168.237.14和192.168.237.9启动nameserv: cd /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/bin nohup sh mqnamesrv & sh shutdown nameserv 2.启动Broker集群 在92.168.237.14主机上启动master1和slave2 master1: cd /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/bin nohup sh mqbroker -c /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/conf/2m-2s-sync/broker-a.properties & slave2: cd /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/bin nohup sh mqbroker -c /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/conf/2m-2s-sync/broker-b-s.properties &
master2: cd /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/bin nohup sh mqbroker -c /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/conf/2m-2s-sync/broker-b.properties & slave2: cd /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/bin nohup sh mqbroker -c /usr/local/rocketmq/rocketmq-all-4.2.0-bin-release/conf/2m-2s-sync/broker-a-s.properties &