跳转至主要内容
版本: 5.0

部署方式

在 Apache RocketMQ 5.0 版本中,基础的消息收发功能已完备,包括 NameServer、Broker 和 Proxy 组件。在 5.0 版本中,Proxy 和 Broker 可以根据实际需求分为本地模式(Local mode)和集群模式(Cluster mode)。通常情况下,如果没有特殊需求,或者采用从早期版本平滑升级的方式,可以使用本地模式。

  • 在本地模式中,Broker 和 Proxy 部署在同一个进程中,只需在原有的 Broker 配置基础上增加一个 Proxy 配置即可运行。
  • 在集群模式中,Broker 和 Proxy 分开部署,即在现有集群的基础上,可以单独部署 Proxy。

本地模式部署

由于本地模式下 Proxy 和 Broker 部署在同一个进程中,且 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,存在多个主从对。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 &

多节点(集群)多副本模式 - 同步双写

每个 Master 配置一个 Slave,存在多个主从对。HA 使用同步双写,即只有主从均写入成功,才向应用返回成功。该模式的优缺点如下:

  • 优点:数据和服务均无单点故障,在 Master 宕机时消息无延迟,服务可用性和数据可用性都非常高;
  • 缺点:性能比异步复制模式略低(约低 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 参数实现。Master 的 BrokerId 必须为 0,Slave 的 BrokerId 必须大于 0。此外,可以在一个 Master 下挂载多个 Slave,同一 Master 下的多个 Slave 通过指定不同的 BrokerId 来区分。$ROCKETMQ_HOME 指的是 RocketMQ 安装目录,该环境变量需要由用户设置。

5.0 HA

RocketMQ 5.0 提供了更灵活的 HA 机制,允许用户更好地平衡成本、服务可用性和数据可靠性,同时支持消息和流存储场景。查看详情

集群模式部署

在集群模式中,Broker 和 Proxy 分开部署,我可以在 NameServer 和 Broker 启动后部署 Proxy。

在集群模式中,Proxy 集群和 Broker 集群一一对应,rocketMQClusterName 可在 Proxy 的配置文件 rmq-proxy.json 中进行配置。

启动 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,存在多个主从对。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,存在多个主从对。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 &