RocketMQ叢集搭建-4.2.0版本

liutf發表於2018-02-24

首發於我的部落格: www.liutf.com/
首發連結:www.liutf.com/posts/14196…

背景

由於公司內部用的RocketMQ訊息中介軟體是個單點例項,隨著業務發展,越來越多的應用接入了同一個中介軟體,耦合性太強。也覺得越來越不安心,不穩心。怕哪天宕了後,所有業務也跟著受到牽連影響。為了元件高可用,決定新搭建一套叢集,業務後續逐步遷移過來。

生產中目前所用版本為:v3.2,當時的版本還是歸屬於阿里。

新搭建一套用官網最新版 v4.2.0 ,現已歸屬於Apache

叢集網路架構圖

RocketMQ叢集搭建-4.2.0版本

NameServer簡介

NameServer顧名思義,在系統中肯定是做命名服務,服務治理方面的工作,功能應該是和zookeeper差不多,據說RocketMq的早期版本確實是使用的zookeeper,後來改為了自己實現的NameServer。

NameServer在RocketMQ中的兩個主要作用:

  • NameServer維護了一份Broker的地址列表和,broker在啟動的時候會去NameServer進行註冊,會維護Broker的存活狀態。

  • NameServer維護了一份Topic和Topic對應佇列的地址列表,broker每次傳送心跳過來的時候都會把Topic資訊帶上。

NameServer的穩定性非常高。原因有二:

  1. NameServer互相獨立,彼此沒有通訊關係,單臺NameServer掛掉,不影響其他NameServer,即使全部掛掉,也不影響業務系統使用。無狀態。
  2. 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壓力越大。

叢集介紹

  1. 單個Master

    這種方式風險較大,一旦Broker重啟或者當機時,會導致整個服務不可用,不建議線上環境使用。

  2. 多 Master 模式

    一個叢集無 Slave,全是 Master,例如 2 個 Master 或者 3 個 Master。

    優點:

    配置簡單,單個Master 當機或重啟維護對應用無影響,在磁碟配置為

    RAID10 時,即使機器當機不可恢復情況下,由與 RAID10

    磁碟非常可靠,訊息也不會丟(非同步刷盤丟失少量訊息,同步刷盤一條不丟)。效能最高。

    缺點:

    單臺機器當機期間,這臺機器上未被消費的訊息在機器恢復之前不可訂閱,訊息實時性會受到受到影響。

    啟動步驟:

    先啟動 NameServer

    在機器 A,啟動第一個 Master

    在機器 B,啟動第二個 Master

  3. 多 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來區分。

  4. 多 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,可自行調整)

軟體:

  1. 64bit OS, Linux/Unix/Mac is recommended;
  2. 64bit JDK 1.8+;
  3. Maven 3.2.x(非必要)
  4. Git(非必須)

RocketMQ叢集搭建-4.2.0版本

正式搭建

  1. 先搭建單機環境,參考單機Quick Start官方教程 。讓各單機能跑起來,以及熟悉基本的指令操作。

  2. 各單機搭建成功後, 然後我們開始做叢集配置。叢集的核心也就是在於各配置檔案配置了。

  3. 進入配置檔案目錄/home/rocket/apache-rocketmq/conf 。這裡我以其中一臺機器rocket-master-b 示例。

    RocketMQ叢集搭建-4.2.0版本

    目錄介紹

    • 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份檔案,這也就是我們的重點內容了。

  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控制檯

RocketMQ叢集搭建-4.2.0版本

  • 官方下載:https://github.com/apache/rocketmq-externals/tree/master/rocketmq-console

相關文章