首發於個人博客: www.liutf.com/
首發連接:www.liutf.com/posts/14196…html
因爲公司內部用的RocketMQ消息中間件是個單點實例,隨着業務發展,愈來愈多的應用接入了同一個中間件,耦合性太強。也以爲愈來愈不安心,不穩心。怕哪天宕了後,全部業務也跟着受到牽連影響。爲了組件高可用,決定新搭建一套集羣,業務後續逐步遷移過來。git
生產中目前所用版本爲:v3.2,當時的版本仍是歸屬於阿里。github
新搭建一套用官網最新版 v4.2.0
,現已歸屬於Apache
。express
NameServer簡介apache
NameServer顧名思義,在系統中確定是作命名服務,服務治理方面的工做,功能應該是和zookeeper差很少,聽說RocketMq的早期版本確實是使用的zookeeper,後來改成了本身實現的NameServer。bash
NameServer在RocketMQ中的兩個主要做用:網絡
NameServer維護了一份Broker的地址列表和,broker在啓動的時候會去NameServer進行註冊,會維護Broker的存活狀態。架構
NameServer維護了一份Topic和Topic對應隊列的地址列表,broker每次發送心跳過來的時候都會把Topic信息帶上。app
NameServer的穩定性很是高。緣由有二:負載均衡
- NameServer互相獨立,彼此沒有通訊關係,單臺NameServer掛掉,不影響其餘NameServer,即便所有掛掉,也不影響業務系統使用。無狀態。
- 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,異步刷盤
的方案。
IP | 備註 |
---|---|
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,可自行調整)
軟件:
- 64bit OS, Linux/Unix/Mac is recommended;
- 64bit JDK 1.8+;
- Maven 3.2.x(非必要)
- Git(非必須)
先搭建單機環境,參考單機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
複製代碼
重點配置說明
配置文件配置完成後,咱們開始啓動。
# 進入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" &
複製代碼