部署方式
在 Apache RocketMQ 5.0 版本中,已完成基本的消息收发,包括 NameServer、Broker 和 Proxy 组件。在 5.0 版本中,Proxy 和 Broker 可以根据实际需求分为本地模式和集群模式。通常,如果没有特殊需求或您遵循从早期版本平滑升级的方法,可以使用本地模式。
- 在本地模式下,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,并且存在多个 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,并且存在多个 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,并且存在多个 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 &