RocketMQ集群搭建-4.2.0版本
首發(fā)于我的博客: www.liutf.com/
首發(fā)鏈接:www.liutf.com/posts/14196…
背景
由于公司內(nèi)部用的RocketMQ消息中間件是個(gè)單點(diǎn)實(shí)例,隨著業(yè)務(wù)發(fā)展,越來(lái)越多的應(yīng)用接入了同一個(gè)中間件,耦合性太強(qiáng)。也覺(jué)得越來(lái)越不安心,不穩(wěn)心。怕哪天宕了后,所有業(yè)務(wù)也跟著受到牽連影響。為了組件高可用,決定新搭建一套集群,業(yè)務(wù)后續(xù)逐步遷移過(guò)來(lái)。
生產(chǎn)中目前所用版本為:v3.2,當(dāng)時(shí)的版本還是歸屬于阿里。
新搭建一套用官網(wǎng)最新版 v4.2.0 ,現(xiàn)已歸屬于Apache。
集群網(wǎng)絡(luò)架構(gòu)圖
?
NameServer簡(jiǎn)介
NameServer顧名思義,在系統(tǒng)中肯定是做命名服務(wù),服務(wù)治理方面的工作,功能應(yīng)該是和zookeeper差不多,據(jù)說(shuō)RocketMq的早期版本確實(shí)是使用的zookeeper,后來(lái)改為了自己實(shí)現(xiàn)的NameServer。
NameServer在RocketMQ中的兩個(gè)主要作用:
-
NameServer維護(hù)了一份Broker的地址列表和,broker在啟動(dòng)的時(shí)候會(huì)去NameServer進(jìn)行注冊(cè),會(huì)維護(hù)Broker的存活狀態(tài)。
-
NameServer維護(hù)了一份Topic和Topic對(duì)應(yīng)隊(duì)列的地址列表,broker每次發(fā)送心跳過(guò)來(lái)的時(shí)候都會(huì)把Topic信息帶上。
NameServer的穩(wěn)定性非常高。原因有二:
Broker簡(jiǎn)介
broker是消息接收處理,落地的核心模塊。這個(gè)模塊用于接收producer發(fā)送的消息以及consumer。
與NameServer關(guān)系
-
連接
單個(gè)broker和所有nameServer保持長(zhǎng)連接
-
心跳
- 心跳間隔:每隔30秒(此時(shí)間無(wú)法更改)向所有nameserver發(fā)送心跳,心跳包含了自身的topic配置信息。
- 心跳超時(shí):nameserver每隔10秒鐘(此時(shí)間無(wú)法更改),掃描所有還存活的broker連接,若某個(gè)連接2分鐘內(nèi)(當(dāng)前時(shí)間與最后更新時(shí)間差值超過(guò)2分鐘,此時(shí)間無(wú)法更改)沒(méi)有發(fā)送心跳數(shù)據(jù),則斷開(kāi)連接。
-
斷開(kāi)
-
時(shí)機(jī):broker掛掉;心跳超時(shí)導(dǎo)致nameserver主動(dòng)關(guān)閉連接
-
動(dòng)作:一旦連接斷開(kāi),nameserver會(huì)立即感知,更新topc與隊(duì)列的對(duì)應(yīng)關(guān)系,但不會(huì)通知生產(chǎn)者和消費(fèi)者
?
負(fù)載均衡
- 一個(gè)topic分布在多個(gè)broker上,一個(gè)broker可以配置多個(gè)topic,它們是多對(duì)多的關(guān)系。
- 如果某個(gè)topic消息量很大,應(yīng)該給它多配置幾個(gè)隊(duì)列,并且盡量多分布在不同broker上,減輕某個(gè)broker的壓力。
- topic消息量都比較均勻的情況下,如果某個(gè)broker上的隊(duì)列越多,則該broker壓力越大。
集群介紹
單個(gè)Master
這種方式風(fēng)險(xiǎn)較大,一旦Broker重啟或者宕機(jī)時(shí),會(huì)導(dǎo)致整個(gè)服務(wù)不可用,不建議線上環(huán)境使用。
?
多 Master 模式
一個(gè)集群無(wú) Slave,全是 Master,例如 2 個(gè) Master 或者 3 個(gè) Master。
?
優(yōu)點(diǎn):
配置簡(jiǎn)單,單個(gè)Master 宕機(jī)或重啟維護(hù)對(duì)應(yīng)用無(wú)影響,在磁盤(pán)配置為
RAID10 時(shí),即使機(jī)器宕機(jī)不可恢復(fù)情況下,由與 RAID10
磁盤(pán)非常可靠,消息也不會(huì)丟(異步刷盤(pán)丟失少量消息,同步刷盤(pán)一條不丟)。性能最高。
?
缺點(diǎn):
單臺(tái)機(jī)器宕機(jī)期間,這臺(tái)機(jī)器上未被消費(fèi)的消息在機(jī)器恢復(fù)之前不可訂閱,消息實(shí)時(shí)性會(huì)受到受到影響。
啟動(dòng)步驟:
先啟動(dòng) NameServer
在機(jī)器 A,啟動(dòng)第一個(gè) Master
在機(jī)器 B,啟動(dòng)第二個(gè) Master
?
多 Master 多 Slave 模式,同步刷盤(pán)
每個(gè) Master 配置一個(gè) Slave,有多對(duì)Master-Slave,HA采用同步雙寫(xiě)方式,主備都寫(xiě)成功,向應(yīng)用返回成功。
?
優(yōu)點(diǎn):
數(shù)據(jù)與服務(wù)都無(wú)單點(diǎn),Master宕機(jī)情況下,消息無(wú)延遲,服務(wù)可用性與數(shù)據(jù)可用性都非常高。
?
缺點(diǎn):
性能比異步復(fù)制模式略低,大約低 10%左右,發(fā)送單個(gè)消息的 RT會(huì)略高。目前主宕機(jī)后,備機(jī)不能自動(dòng)切換為主機(jī),后續(xù)會(huì)支持自動(dòng)切換功能。
啟動(dòng)步驟:
先啟動(dòng) NameServer
在機(jī)器 A,啟動(dòng)第一個(gè) Master
在機(jī)器 B,啟動(dòng)第二個(gè) Master
在機(jī)器 C,啟動(dòng)第一個(gè) Slave
在機(jī)器 D,啟動(dòng)第二個(gè) Slave
以上 Broker 與 Slave 配對(duì)是通過(guò)指定相同的brokerName 參數(shù)來(lái)配對(duì),Master的 BrokerId 必須是 0,Slave 的BrokerId 必須是大與 0 的數(shù)。另外一個(gè) Master下面可以?huà)燧d多個(gè) Slave,同一 Master 下的多個(gè) Slave通過(guò)指定不同的 BrokerId來(lái)區(qū)分。
?
多 Master 多 Slave 模式,異步刷盤(pán)
每個(gè) Master 配置一個(gè) Slave,有多對(duì)Master-Slave,HA采用異步復(fù)制方式,主備有短暫消息延遲,毫秒級(jí)。
?
優(yōu)點(diǎn):
即使磁盤(pán)損壞,消息丟失的非常少,且消息實(shí)時(shí)性不會(huì)受影響,因?yàn)?/p>
Master 宕機(jī)后,消費(fèi)者仍然可以從 Slave消費(fèi),此過(guò)程對(duì)應(yīng)用透明。不需要人工干預(yù)。性能同多 Master 模式幾乎一樣。
?
缺點(diǎn):
Master 宕機(jī),磁盤(pán)損壞情況,會(huì)丟失少量消息。
啟動(dòng)步驟:
先啟動(dòng) NameServer
在機(jī)器 A,啟動(dòng)第一個(gè) Master
在機(jī)器 B,啟動(dòng)第二個(gè) Master
在機(jī)器 C,啟動(dòng)第一個(gè) Slave
在機(jī)器 D,啟動(dòng)第二個(gè) Slave
集群搭建
綜上幾種集群方式,我選取的是多Master多Slave,異步刷盤(pán) 的方案。
環(huán)境準(zhǔn)備
- 主機(jī)信息
| 10.10.10.31 | slave-b&namesrv |
| 10.10.10.43 | slave-a&namesrv |
| 10.10.10.44 | master-a&namesrv |
| 10.10.10.45 | master-b&namesrv |
4臺(tái)主機(jī)都啟動(dòng)了namesrv服務(wù),做namesrv集群。
44和45兩臺(tái)機(jī)分別為master-a和master-b。
43和31兩臺(tái)機(jī)分別為slave-a和slave-b。
配置要求
硬件:
12G+內(nèi)存(broker默認(rèn)分配8G,namesrv默認(rèn)分配4G,可自行調(diào)整)
軟件:
正式搭建
先搭建單機(jī)環(huán)境,參考單機(jī)Quick Start官方教程 。讓各單機(jī)能跑起來(lái),以及熟悉基本的指令操作。
?
各單機(jī)搭建成功后, 然后我們開(kāi)始做集群配置。集群的核心也就是在于各配置文件配置了。
?
進(jìn)入配置文件目錄/home/rocket/apache-rocketmq/conf 。這里我以其中一臺(tái)機(jī)器rocket-master-b 示例。
?
目錄介紹
- 2m-noslave: 多Master模式
- 2m-2s-sync: 多Master多Slave模式,同步雙寫(xiě)
- 2m-2s-async:多Master多Slave模式,異步復(fù)制
?
我選擇的是多Master多Slave,異步刷盤(pán) 方式,進(jìn)入2m-2s-async 目錄做配置。
[rocket@rocket-master-b conf]$ cd 2m-2s-async/ [rocket@rocket-master-b 2m-2s-async]$ ls broker-a.properties broker-a-s.properties broker-b.properties broker-b-s.properties 復(fù)制代碼?
這里可以看到默認(rèn)有4份文件,這也就是我們的重點(diǎn)內(nèi)容了。
?
修改各配置文件
-
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.# 配置參考官方鏈接:http://rocketmq.apache.org/docs/rmq-deployment/# 所屬集群名字 brokerClusterName=rocketmq-cluster# broker名字,注意此處不同的配置文件填寫(xiě)的不一樣 brokerName=broker-a# 0 表示 Master,>0 表示 Slave brokerId=0# 刪除文件時(shí)間點(diǎn),默認(rèn)凌晨4點(diǎn)。24小時(shí)制,單位小時(shí) deleteWhen=04# 文件保留時(shí)間,默認(rèn) 72 小時(shí)。根據(jù)業(yè)務(wù)情況調(diào)整 fileReservedTime=168# Broker 對(duì)外服務(wù)的監(jiān)聽(tīng)端口 listenPort=10911# nameServer地址,分號(hào)分割 namesrvAddr=10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876# Details:Should be configured if having multiple addresses; Default value:InetAddress for network interface # 本機(jī)ip地址,默認(rèn)系統(tǒng)自動(dòng)識(shí)別,但是某些多網(wǎng)卡機(jī)器會(huì)存在識(shí)別錯(cuò)誤的情況,這種情況下可以人工配置。 brokerIP1=10.10.10.45# commitLog 存儲(chǔ)路徑 storePathCommitLog=/home/rocket/app/rocketmq/store/commitlog# 消費(fèi)隊(duì)列存儲(chǔ)路徑存儲(chǔ)路徑 storePathConsumerQueue=/home/rocket/app/rocketmq/store/consumequeue# commitLog每個(gè)文件的大小默認(rèn)1G mapedFileSizeCommitLog=1073741824# Broker 的角色 # - ASYNC_MASTER 異步復(fù)制Master # - SYNC_MASTER 同步雙寫(xiě)Master # - SLAVE brokerRole=ASYNC_MASTER# 刷盤(pán)方式 # - ASYNC_FLUSH 異步刷盤(pán) # - SYNC_FLUSH 同步刷盤(pán) flushDiskType=ASYNC_FLUSH復(fù)制代碼?
-
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.# 配置參考官方鏈接:http://rocketmq.apache.org/docs/rmq-deployment/# 所屬集群名字 brokerClusterName=rocketmq-cluster# broker名字,注意此處不同的配置文件填寫(xiě)的不一樣 brokerName=broker-a# 0 表示 Master,>0 表示 Slave brokerId=1# 刪除文件時(shí)間點(diǎn),默認(rèn)凌晨4點(diǎn)。24小時(shí)制,單位小時(shí) deleteWhen=04# 文件保留時(shí)間,默認(rèn) 72 小時(shí)。根據(jù)業(yè)務(wù)情況調(diào)整 fileReservedTime=168# Broker 對(duì)外服務(wù)的監(jiān)聽(tīng)端口 listenPort=10911# nameServer地址,分號(hào)分割 namesrvAddr=10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876# Details:Should be configured if having multiple addresses; Default value:InetAddress for network interface # 本機(jī)ip地址,默認(rèn)系統(tǒng)自動(dòng)識(shí)別,但是某些多網(wǎng)卡機(jī)器會(huì)存在識(shí)別錯(cuò)誤的情況,這種情況下可以人工配置。 brokerIP1=10.10.10.44# commitLog 存儲(chǔ)路徑 storePathCommitLog=/home/rocket/app/rocketmq-slave/store/commitlog# 消費(fèi)隊(duì)列存儲(chǔ)路徑存儲(chǔ)路徑 storePathConsumerQueue=/home/rocket/app/rocketmq-slave/store/consumequeue# commitLog每個(gè)文件的大小默認(rèn)1G mapedFileSizeCommitLog=1073741824# Broker 的角色 # - ASYNC_MASTER 異步復(fù)制Master # - SYNC_MASTER 同步雙寫(xiě)Master # - SLAVE brokerRole=SLAVE# 刷盤(pán)方式 # - ASYNC_FLUSH 異步刷盤(pán) # - SYNC_FLUSH 同步刷盤(pán) flushDiskType=ASYNC_FLUSH復(fù)制代碼?
-
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.# 配置參考官方鏈接:http://rocketmq.apache.org/docs/rmq-deployment/# 所屬集群名字 brokerClusterName=rocketmq-cluster# broker名字,注意此處不同的配置文件填寫(xiě)的不一樣 brokerName=broker-b# 0 表示 Master,>0 表示 Slave brokerId=0# 刪除文件時(shí)間點(diǎn),默認(rèn)凌晨4點(diǎn)。24小時(shí)制,單位小時(shí) deleteWhen=04# 文件保留時(shí)間,默認(rèn) 72 小時(shí)。根據(jù)業(yè)務(wù)情況調(diào)整 fileReservedTime=168# Broker 對(duì)外服務(wù)的監(jiān)聽(tīng)端口 listenPort=10921# nameServer地址,分號(hào)分割 namesrvAddr=10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876# Details:Should be configured if having multiple addresses; Default value:InetAddress for network interface # 本機(jī)ip地址,默認(rèn)系統(tǒng)自動(dòng)識(shí)別,但是某些多網(wǎng)卡機(jī)器會(huì)存在識(shí)別錯(cuò)誤的情況,這種情況下可以人工配置。 brokerIP1=10.10.10.46# commitLog 存儲(chǔ)路徑 storePathCommitLog=/home/rocket/app/rocketmq/store/commitlog# 消費(fèi)隊(duì)列存儲(chǔ)路徑存儲(chǔ)路徑 storePathConsumerQueue=/home/rocket/app/rocketmq/store/consumequeue# commitLog每個(gè)文件的大小默認(rèn)1G mapedFileSizeCommitLog=1073741824# Broker 的角色 # - ASYNC_MASTER 異步復(fù)制Master # - SYNC_MASTER 同步雙寫(xiě)Master # - SLAVE brokerRole=ASYNC_MASTER# 刷盤(pán)方式 # - ASYNC_FLUSH 異步刷盤(pán) # - SYNC_FLUSH 同步刷盤(pán) flushDiskType=ASYNC_FLUSH復(fù)制代碼?
-
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.# 配置參考官方鏈接:http://rocketmq.apache.org/docs/rmq-deployment/# 所屬集群名字 brokerClusterName=rocketmq-cluster# broker名字,注意此處不同的配置文件填寫(xiě)的不一樣 brokerName=broker-b# 0 表示 Master,>0 表示 Slave brokerId=1# 刪除文件時(shí)間點(diǎn),默認(rèn)凌晨4點(diǎn)。24小時(shí)制,單位小時(shí) deleteWhen=04# 文件保留時(shí)間,默認(rèn) 72 小時(shí)。根據(jù)業(yè)務(wù)情況調(diào)整 fileReservedTime=168# Broker 對(duì)外服務(wù)的監(jiān)聽(tīng)端口 listenPort=10921# nameServer地址,分號(hào)分割 namesrvAddr=10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876# Details:Should be configured if having multiple addresses; Default value:InetAddress for network interface # 本機(jī)ip地址,默認(rèn)系統(tǒng)自動(dòng)識(shí)別,但是某些多網(wǎng)卡機(jī)器會(huì)存在識(shí)別錯(cuò)誤的情況,這種情況下可以人工配置。 brokerIP1=10.10.10.31# commitLog 存儲(chǔ)路徑 storePathCommitLog=/home/persiancat/rocketmq-slave-p/app/rocketmq-slave/store/commitlog# 消費(fèi)隊(duì)列存儲(chǔ)路徑存儲(chǔ)路徑 storePathConsumerQueue=/home/persiancat/rocketmq-slave-p/app/rocketmq-slave/store/consumequeue# commitLog每個(gè)文件的大小默認(rèn)1G mapedFileSizeCommitLog=1073741824# Broker 的角色 # - ASYNC_MASTER 異步復(fù)制Master # - SYNC_MASTER 同步雙寫(xiě)Master # - SLAVE brokerRole=SLAVE# 刷盤(pán)方式 # - ASYNC_FLUSH 異步刷盤(pán) # - SYNC_FLUSH 同步刷盤(pán) flushDiskType=ASYNC_FLUSH復(fù)制代碼
?
重點(diǎn)配置說(shuō)明
- brokerClusterName:同一個(gè)集群中,brokerClusterName需一致
- brokerId:0 表示 Master,>0 表示 Slave
- namesrvAddr:配置多個(gè)用分號(hào)分隔
- brokerIP1:默認(rèn)系統(tǒng)自動(dòng)識(shí)別,但是某些多網(wǎng)卡機(jī)器會(huì)存在識(shí)別錯(cuò)誤的情況,建議都手工指定
- brokerRole:選擇Broker的角色
- flushDiskType:選擇刷盤(pán)方式
啟動(dòng)
配置文件配置完成后,我們開(kāi)始啟動(dòng)。
# 進(jìn)入rocketmq根目錄 cd /home/rocket/apache-rocketmq# 后臺(tái)執(zhí)行bin目錄文件夾下mqnamesrv服務(wù) nohup sh bin/mqnamesrv &# broker-a機(jī)器下執(zhí)行broker-a.properties文件啟動(dòng) nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a.properties -n "10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876" &# broker-b機(jī)器下執(zhí)行broker-b.properties文件啟動(dòng) nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b.properties -n "10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876" &# broker-a-s機(jī)器下執(zhí)行broker-a-s.properties文件啟動(dòng) nohup sh bin/mqbroker -c conf/2m-2s-async/broker-a-s.properties -n "10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876" &# broker-b-s機(jī)器下執(zhí)行broker-b-s.properties文件啟動(dòng) nohup sh bin/mqbroker -c conf/2m-2s-async/broker-b-s.properties -n "10.10.10.44:9876;10.10.10.45:9876;10.10.10.46:9876;10.10.10.31:9876" & 復(fù)制代碼注意事項(xiàng)
- 注意防火墻,剛開(kāi)始搭建可以先關(guān)閉掉防火墻。搞定后再開(kāi)防火墻,配置對(duì)應(yīng)端口。
RocketMQ控制臺(tái)
- 官方下載:https://github.com/apache/rocketmq-externals/tree/master/rocketmq-console
轉(zhuǎn)載于:https://juejin.im/post/5a911ea16fb9a0633f0e36a1
總結(jié)
以上是生活随笔為你收集整理的RocketMQ集群搭建-4.2.0版本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java并发编程实战系列15之原子遍历与
- 下一篇: 事务、视图、索引、备份、还原