集群模式
在RocketMQ中,集群的部署模式是比较多的,有以下几种:
-
单个Master
- 这种方式风险较大,一旦Broker重启或者宕机时,会导致整个服务不可用,不建议线上环境使用。
-
多Master模式
- 一个集群无Slave,全是Master,例如2个Master或者3个Master
- 单台机器宕机期间,这台机器上未被消费的消息在机器恢复之前不可订阅,消息实时性会受到影响。
-
多Master多Slave模式,异步复制
- 每个Master配置一个Slave,有多对Master-Slave,HA采用异步复制方式,主备有短暂消息延迟,毫秒级。
- 优点:即使磁盘损坏,消息丢失的非常少,且消息实时性不会受影响,因为Master宕机后,消费者仍然 可以从Slave消费,此过程对应用透明,不需要人工干预。性能同多Master模式几乎一样。
- 缺点:Master宕机,磁盘损坏情况,会丢失少量消息。
-
多Master多Slave模式,同步双写
- 每个Master配置一个Slave,有多对Master-Slave,HA采用同步双写方式,主备都写成功,向应用返回 成功。
- 优点:数据与服务都无单点,Master宕机情况下,消息无延迟,服务可用性与数据可用性都非常高。
- 缺点:性能比异步复制模式略低,大约低10%左右。
搭建2m2s集群
下面通过docker搭建2master+2slave的集群。
创建数据挂载目录
mkdir -p /usr/dockerrocketmq/rmqserver01/logs
mkdir -p /usr/dockerrocketmq/rmqserver01/store
mkdir -p /usr/dockerrocketmq/rmqserver02/logs
mkdir -p /usr/dockerrocketmq/rmqserver02/store
mkdir -p /usr/dockerrocketmq/rmqbroker01/logs
mkdir -p /usr/dockerrocketmq/rmqbroker01/store
mkdir -p /usr/dockerrocketmq/rmqbroker01/conf
mkdir -p /usr/dockerrocketmq/rmqbroker02/logs
mkdir -p /usr/dockerrocketmq/rmqbroker02/store
mkdir -p /usr/dockerrocketmq/rmqbroker02/conf
mkdir -p /usr/dockerrocketmq/rmqbroker03/logs
mkdir -p /usr/dockerrocketmq/rmqbroker03/store
mkdir -p /usr/dockerrocketmq/rmqbroker03/conf
mkdir -p /usr/dockerrocketmq/rmqbroker04/logs
mkdir -p /usr/dockerrocketmq/rmqbroker04/store
mkdir -p /usr/dockerrocketmq/rmqbroker04/conf
编辑broker01(主)配置文件
vi /usr/dockerrocketmq/rmqbroker01/conf/broker.conf
broker.conf
关于brokerIP1和brokerIP2
brokerIP1 当前broker监听的IP
brokerIP2 存在broker主从时,在broker主节点上配置了brokerIP2的话,broker从节点会连接主节点配置的brokerIP2来同步。
# nameServer地址,如果nameserver是多台集群的话,就用分号分割
namesrvAddr=192.168.130.128:9876;192.168.130.128:9877
# 所属集群名字(同一主从下:Master和slave名称要一致)
brokerClusterName = DefaultCluster
# broker名字,主从名字应相同
brokerName = broker01
# 0 表示 Master,>0 表示 Slave
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
# Broker 的角色: ASYNC_MASTER 异步复制Master ; SYNC_MASTER 同步双写Master; SLAVE
brokerRole = ASYNC_MASTER
# 刷盘方式 ASYNC_FLUSH 异步刷盘; SYNC_FLUSH 同步刷盘
flushDiskType = ASYNC_FLUSH
# 主brokerIP
brokerIP1 = 192.168.130.128
# 主brokerIP
brokerIP2 = 192.168.130.128
# 对外提供服务端口
listenPort=10911
编辑broker02(主)配置文件
vi /usr/dockerrocketmq/rmqbroker02/conf/broker.conf
broker.conf
namesrvAddr=192.168.130.128:9876;192.168.130.128:9877
brokerClusterName = DefaultCluster
brokerName = broker02
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
brokerIP1 = 192.168.130.128
brokerIP2 = 192.168.130.128
listenPort=10811
编辑broker01(从)配置文件
vi /usr/dockerrocketmq/rmqbroker03/conf/broker.conf
broker.conf
namesrvAddr=192.168.130.128:9876;192.168.130.128:9877
brokerClusterName = DefaultCluster
brokerName = broker01
brokerId = 1
deleteWhen = 04
fileReservedTime = 48
brokerRole = SLAVE
flushDiskType = ASYNC_FLUSH
brokerIP1 = 192.168.130.128
listenPort=10711
编辑broker02(从)配置文件
vi /usr/dockerrocketmq/rmqbroker04/conf/broker.conf
broker.conf
namesrvAddr=192.168.130.128:9876;192.168.130.128:9877
brokerClusterName = DefaultCluster
brokerName = broker02
brokerId = 1
deleteWhen = 04
fileReservedTime = 48
brokerRole = SLAVE
flushDiskType = ASYNC_FLUSH
brokerIP1 = 192.168.130.128
listenPort=10611
创建nameserver容器 ,启动
#创建nameserver容器
docker create -p 9876:9876 --name rmqserver01 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /usr/dockerrocketmq/rmqserver01/logs:/opt/logs -v /usr/dockerrocketmq/rmqserver01/store:/opt/store foxiswho/rocketmq:server-4.3.2
docker create -p 9877:9876 --name rmqserver02 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /usr/dockerrocketmq/rmqserver02/logs:/opt/logs -v /usr/dockerrocketmq/rmqserver02/store:/opt/store foxiswho/rocketmq:server-4.3.2
# 启动
docker start rmqserver01 rmqserver02
创建并启动broker
docker run -it -d --net host --name rmqbroker01 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /usr/dockerrocketmq/rmqbroker01/conf/broker.conf:/etc/rocketmq/broker.conf -v /usr/dockerrocketmq/rmqbroker01/logs:/opt/logs -v /usr/dockerrocketmq/rmqbroker01/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
docker run -it -d --net host --name rmqbroker02 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /usr/dockerrocketmq/rmqbroker02/conf/broker.conf:/etc/rocketmq/broker.conf -v /usr/dockerrocketmq/rmqbroker02/logs:/opt/logs -v /usr/dockerrocketmq/rmqbroker02/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
docker run -it -d --net host --name rmqbroker03 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /usr/dockerrocketmq/rmqbroker03/conf/broker.conf:/etc/rocketmq/broker.conf -v /usr/dockerrocketmq/rmqbroker03/logs:/opt/logs -v /usr/dockerrocketmq/rmqbroker03/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
docker run -it -d --net host --name rmqbroker04 -e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" -e "JAVA_OPTS=-Duser.home=/opt" -v /usr/dockerrocketmq/rmqbroker04/conf/broker.conf:/etc/rocketmq/broker.conf -v /usr/dockerrocketmq/rmqbroker04/logs:/opt/logs -v /usr/dockerrocketmq/rmqbroker04/store:/opt/store --privileged=true foxiswho/rocketmq:broker-4.3.2
查看是否启动成功
docker ps
查看监控页面