部署方式
在 Apache RocketMQ 5.0 版本中,完成了基本訊息傳送和接收,包括 NameServer、Broker 和 Proxy 元件。在 5.0 版本中,Proxy 和 Broker 可以根據實際需求分為 Local 模式和叢集模式。一般而言,如果沒有特殊需求或遵循從較早版本順利升級的方法,可以使用 Local 模式。
- 在 Local 模式中,Broker 和 Proxy 部署在同一個程序中,您只需在原有的 Broker 設定上新增 Proxy 設定即可執行。
- 在叢集模式中,Broker 和 Proxy 分開部署,也就是說,除了現有的叢集外,您還可以另外部署 Proxy。
在 Local 模式中部署
由於 Proxy 和 Broker 在 Local 模式中部署在同一個程序中,因此 Proxy 是無狀態的,所以主要的叢集設定仍可以以 Broker 為基礎。
啟動 NameServer
### Start Name Server first
$ nohup sh mqnamesrv &
### Verify that the Name Server has started successfully.
$ tail -f ~/logs/rocketmqlogs/namesrv.log
The Name Server boot success...
啟動 Broker+Proxy
單節點單複本模式
此方法風險很高,因為 Broker 只有單一節點,如果 Broker 重新啟動或關閉,整個服務將無法使用。不建議在線上環境中使用,但可用於本地測試。
$ nohup sh bin/mqbroker -n localhost:9876 --enable-proxy &
### Verify that the Broker has started successfully, for example, the broker IP is 192.168.1.2, and the name is broker-A
$ tail -f ~/logs/rocketmqlogs/broker_default.log
The broker[xxx, 192.169.1.2:10911] boot success...
多節點(叢集)單複本模式
叢集中的所有節點都部署為 Master 角色,沒有部署 Slave 複本,例如 2 個 Master 或 3 個 Master。此模式的優缺點如下
- 優點:配置簡單,單個 Master 宕機或重啟對應用無影響,且當磁盤配置為 RAID10 時,即使機器不可恢復地宕機,消息也不會因 RAID10 磁盤的可靠性而丟失(異步磁盤刷盤會丟失少量消息,同步磁盤刷盤不會丟失一條消息),且性能最高;
- 缺點:單機宕機期間,該機器上未被消費的消息在機器恢復前無法被訂閱,消息時效性會受到影響。
啟動 Broker+Proxy 集群
### On machine A, start the first Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-noslave/broker-a.properties --enable-proxy &
### On machine A, start the second Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-noslave/broker-b.properties --enable-proxy &
...
以上啟動命令是單個 NameServer 的情況下使用的,對於有多個 NameServer 的集群,Broker 啟動命令中 -n
後的地址列表使用分號分隔,比如 192.168.1.1:9876;192.161.2:9876
。
多節點(集群)多副本模式 - 異步複製
每個 Master 都配置一個 Slave,有多個 Master-Slave 對。HA 使用異步複製,主從之間存在短暫的消息延時(毫秒級)。此模式的優缺點如下
- 優點:即使磁盤損壞,丟失的消息極少,且消息時效性不受影響。同時,Master 宕機後,消費者仍然可以從 Slave 消費,且此過程對應用透明,無需人工介入,且性能與多 Master 模式幾乎相同;
- 缺點:Master 宕機或磁盤損壞時會丟失少量消息。
啟動 Broker+Proxy 集群
### On machine A, start the first Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-a.properties --enable-proxy &
### On machine B, start the second Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-b.properties --enable-proxy &
### On machine C, start the first slave, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-a-s.properties --enable-proxy &
### On machine D, start the second slave, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-b-s.properties --enable-proxy &
多節點(集群)多副本模式 - 同步雙寫
每個主節點配置一個從節點,並且有多個主從節點對。HA 使用同步雙寫,這表示只有主節點和從節點寫入都成功時,才會回傳成功給應用程式。此模式的優缺點如下
- 優點:資料和服務都沒有單點故障,主節點中斷時訊息不會延遲,服務可用性和資料可用性都很高;
- 缺點:效能比非同步複製模式稍低(約低 10%),發送單一訊息的 RT 稍高,且在當前版本中,主節點掛掉後,備用節點無法自動切換為主節點。
啟動 Broker+Proxy 集群
### On machine A, start the first Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-sync/broker-a.properties --enable-proxy &
### On machine B, start the second Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-sync/broker-b.properties --enable-proxy &
### On machine C, start the first slave, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-sync/broker-a-s.properties --enable-proxy &
### On machine D, start the second slave, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-sync/broker-b-s.properties --enable-proxy &
上面 Broker 和 Slave 的配對是透過指定相同的 BrokerName 參數來完成的。主節點的 BrokerId 必須為 0,從節點的 BrokerId 必須為大於 0 的數字。此外,多個從節點可以掛載在另一個主節點上,並且同一個主節點下的多個從節點透過指定不同的 BrokerId 來區分。$ROCKETMQ_HOME 指的是 RocketMQ 安裝目錄,這個環境變數需要使用者自行設定。
5.0 HA
RocketMQ 5.0 提供更靈活的 HA 機制,讓使用者可以更好地平衡成本、服務可用性和資料可靠性,同時支援訊息傳遞和串流儲存場景。 查看詳細資訊
在集群模式中部署
在集群模式下,Broker 和 Proxy 分別部署,可以在 NameServer 和 Broker 啟動後再部署 Proxy。
在集群模式下,一個 Proxy 集群和一個 Broker 集群是一一對應的,可以在 Proxy 的配置文件 rmq-proxy.json
中配置 rocketMQClusterName
。
啟動 NameServer
### Start NameServer first
$ nohup sh mqnamesrv &
### Verify tha Name Server has started successfully
$ tail -f ~/logs/rocketmqlogs/namesrv.log
The Name Server boot success...
啟動 Broker
單節點單副本模式
這種方式風險較高,因為 Broker 只有單個節點,如果 Broker 重啟或宕機,則整個服務不可用。不建議在生產環境中使用,可以作為本地測試使用。
### On machine A, start the first Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 &
多節點(集群)單副本模式
這種模式下,一個集群中的所有節點都部署為 Master 角色,不部署任何 Slave 副本,比如 2 個 Master 或者 3 個 Master。這種模式的優缺點如下
- 優點:配置簡單,單個 Master 宕機或重啟對應用無影響,並且當磁盤配置為 RAID10 時,即使機器宕機無法恢復,由於 RAID10 磁盤的可靠性,消息也不會丟失(異步刷盤會丟失少量消息,同步刷盤不會丟失任何消息),並且性能最高;
- 缺點:單個機器宕機期間,該機器上未被消費的消息在機器恢復之前無法被訂閱,會影響消息的實時性。
### On machine A, start the first Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-noslave/broker-a.properties &
### On machine B, start the first Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-noslave/broker-b.properties &
...
以上啟動命令是在單個 NameServer 的情況下使用的,對於多個 NameServer 的集群,Broker 啟動命令中 -n
後面的地址列表可以使用分號分隔,比如 192.168.1.1:9876;192.161.2:9876
。
多節點(集群)多副本模式 - 異步複製
每個 Master 配置一個 Slave,有多個 Master-Slave 對。HA 使用異步複製,主備之間存在短暫的延時(毫秒級)。這種模式的優缺點如下
- 優點:即使磁盤損壞,消息丟失也很少,並且消息的時效性不受影響。另外,Master 宕機後,消費者仍然可以從 Slave 消費,這個過程對應用透明,不需要人工干預。性能與多 Master 模式幾乎相同。
- 缺點:Master 發生故障或磁碟損壞時,會遺失少數訊息。
### On machine A, start the first Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-a.properties &
### On machine B, start the second Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-b.properties &
### On machine C, start the first Slave, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-a-s.properties &
### On machine B, start the second Slave, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-async/broker-b-s.properties &
多節點(叢集)多副本模式 - 同步雙寫
每個 Master 都配置一個 Slave,有多個 Master-Slave 組合。HA 使用同步雙寫,只有主備都寫入成功才會回傳成功給應用程式。此模式的優缺點如下
- 優點:資料與服務都無單點故障。Master 發生故障時,訊息不會延遲,服務可用性與資料可用性都非常高。
- 缺點:效能比非同步複製模式略低(約低 10%),單訊息發送 RT 稍高,且目前版本中,主節點掛掉後,備用節點無法自動切換為主節點。
### On machine A, start the first Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-sync/broker-a.properties &
### On machine B, start the second Master, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-sync/broker-b.properties &
### On machine C, start the first Slave, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-sync/broker-a-s.properties &
### On machine B, start the second Slave, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqbroker -n 192.168.1.1:9876 -c $ROCKETMQ_HOME/conf/2m-2s-sync/broker-b-s.properties &
Broker 與 Slave 的配對,是透過指定相同的 BrokerName 參數。Master 的 BrokerId 必須為 0,Slave 的 BrokerId 必須為大於 0 的數字。此外,一個 Master 下可以掛載多個 Slave,同一個 Master 下的多個 Slave,透過指定不同的 BrokerId 來區分。$ROCKETMQ_HOME 指的是 RocketMQ 安裝目錄,需要使用者自行設定為環境變數。
5.0 HA
RocketMQ 5.0 提供更靈活的 HA 機制,讓使用者可以更好地平衡成本、服務可用性和資料可靠性,同時支援訊息傳遞和串流儲存場景。 查看詳細資訊
啟動 Proxy
可以在多台機器上啟動多個 Proxy。
### On machine A start the first Proxy, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqproxy -n 192.168.1.1:9876 &
### On machine B start the second Proxy, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqproxy -n 192.168.1.1:9876 &
### On machine C start the third Proxy, for example, the IP of the NameServer is: 192.168.1.1
$ nohup sh bin/mqproxy -n 192.168.1.1:9876 &
如果需要指定設定檔,可以使用 -pc
或 --proxyConfigPath
來指定。
### custom config file
$ nohup sh bin/mqproxy -n 192.168.1.1:9876 -pc /path/to/proxyConfig.json &