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