RocketMQ 集群搭建 (基于Docker-Compose的单机集群部署)

阿里开源消息中间件RocketMQ集群搭建,基于docker-compose, RocketMQ版本为4.7.0,采用二主二备集群方式。

新增RocketMQ Broker配置文件

Broker Node1 节点配置

Broker Node1 Master (broker-master.conf)

## 集群名,需要统一
brokerClusterName=cawa_cluster
## 节点名
brokerName=broker-node1
## 0为主节点,>0为从节点
brokerId=0
## name server 地址 (这里使用的docker中hostname,可以改为宿主机真实IP)
namesrvAddr=namesrv-node1:9876;namesrv-node2:9876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
## 删除时间为每天凌晨4点
deleteWhen=04
## 保存时间48小时
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=50000000
destroyMapedFileIntervalForcibly=120000
redeleteHangedFileInterval=120000
diskMaxUsedSpaceRatio=88

#Broker 对外服务的监听端口
listenPort=10901
#haService中使用
haListenPort=10902
#主要用于slave同步master
fastListenPort=10899
# docker容器中IP地址为容器IP,故需要改为宿主机IP
brokerIP1=192.168.2.105

maxMessageSize=65536
flushCommitLogLeastPages=4
flushConsumeQueueLeastPages=2
flushCommitLogThoroughInterval=10000
flushConsumeQueueThoroughInterval=60000

checkTransactionMessageEnable=false
sendMessageThreadPoolNums=128
pullMessageThreadPoolNums=128

#broker关系
#ASYNC_MASTER:异步复制
#SYNC_MASTER:同步双写
brokerRole=SYNC_MASTER

#刷盘方式
#- ASYNC_FLUSH  异步刷盘
#- SYNC_FLUSH  同步刷盘
flushDiskType=ASYNC_FLUSH

Broker Node1 Slave (broker-slave.conf)

brokerClusterName=cawa_cluster
brokerName=broker-node1
brokerId=1
namesrvAddr=namesrv-node1:9876;namesrv-node2:9876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
deleteWhen=04
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=50000000
destroyMapedFileIntervalForcibly=120000
redeleteHangedFileInterval=120000
diskMaxUsedSpaceRatio=88

#Broker 对外服务的监听端口
listenPort=10911
#haService中使用
haListenPort=10912
#主要用于slave同步master
fastListenPort=10909
# docker容器中IP地址为容器IP,故需要改为宿主机IP
brokerIP1=192.168.2.105

maxMessageSize=65536
flushCommitLogLeastPages=4
flushConsumeQueueLeastPages=2
flushCommitLogThoroughInterval=10000
flushConsumeQueueThoroughInterval=60000

checkTransactionMessageEnable=false
sendMessageThreadPoolNums=128
pullMessageThreadPoolNums=128

brokerRole=SLAVE
#刷盘方式
#- ASYNC_FLUSH  异步刷盘
#- SYNC_FLUSH  同步刷盘
flushDiskType=ASYNC_FLUSH

Broker Node2 节点配置

Broker Node2 Master (broker-master.conf)

brokerClusterName=cawa_cluster
brokerName=broker-node2
brokerId=0
namesrvAddr=namesrv-node1:9876;namesrv-node2:9876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
deleteWhen=04
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=50000000
destroyMapedFileIntervalForcibly=120000
redeleteHangedFileInterval=120000
diskMaxUsedSpaceRatio=88

#Broker 对外服务的监听端口
listenPort=11901
#haService中使用
haListenPort=11902
#主要用于slave同步master
fastListenPort=11899
# docker容器中IP地址为容器IP,故需要改为宿主机IP
brokerIP1=192.168.2.105

maxMessageSize=65536
flushCommitLogLeastPages=4
flushConsumeQueueLeastPages=2
flushCommitLogThoroughInterval=10000
flushConsumeQueueThoroughInterval=60000

checkTransactionMessageEnable=false
sendMessageThreadPoolNums=128
pullMessageThreadPoolNums=128

brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH

Broker Node1 Slave (broker-slave.conf)

brokerClusterName=cawa_cluster
brokerName=broker-node2
brokerId=1
namesrvAddr=namesrv-node1:9876;namesrv-node2:9876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
deleteWhen=04
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=50000000
destroyMapedFileIntervalForcibly=120000
redeleteHangedFileInterval=120000
diskMaxUsedSpaceRatio=88

#Broker 对外服务的监听端口
listenPort=11911
#haService中使用
haListenPort=11912
#主要用于slave同步master
fastListenPort=11909
# docker容器中IP地址为容器IP,故需要改为宿主机IP
brokerIP1=192.168.2.105

maxMessageSize=65536
flushCommitLogLeastPages=4
flushConsumeQueueLeastPages=2
flushCommitLogThoroughInterval=10000
flushConsumeQueueThoroughInterval=60000

checkTransactionMessageEnable=false
sendMessageThreadPoolNums=128
pullMessageThreadPoolNums=128

brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH

Docker-Compose 文件配置

version: "2"
services:
  namesrv-node1:
    hostname: namesrv-node1
    container_name: namesrv-node1
    image: foxiswho/rocketmq:4.7.0
    environment:
      TZ: Asia/Shanghai
      JAVA_OPT_EXT: -Xms512M -Xmx512M -Xmn128m
    volumes:
      - ./logs/nameserver/node1:/home/rocketmq/logs
      - /etc/localtime:/etc/localtime:ro
    ports:
      - 9876:9876
    restart: always
    command: sh mqnamesrv
  namesrv-node2:
    hostname: namesrv-node2
    container_name: namesrv-node2
    image: foxiswho/rocketmq:4.7.0
    environment:
      TZ: Asia/Shanghai
      JAVA_OPT_EXT: -Xms512M -Xmx512M -Xmn128m
    volumes:
      - ./logs/nameserver/node1:/home/rocketmq/logs
      - /etc/localtime:/etc/localtime:ro
    ports:
      - 9877:9876
    restart: always
    command: sh mqnamesrv
  rocketmq-broker-node1-master: 
    hostname: brokder-node1-master 
    image: foxiswho/rocketmq:4.7.0
    volumes:
      - ./logs/broker/node1/master:/home/rocketmq/logs
      - ./store/broker/node1/master:/home/rocketmq/store
      ## 配置文件映射,就是上面写得BrokerNode1 Master配置文件所在路径
      - ./conf/broker-node1/broker-master.conf:/home/rocketmq/conf/broker.conf
      - /etc/localtime:/etc/localtime:ro
    restart: always
    environment:
        TZ: Asia/Shanghai
        JAVA_OPT_EXT: "-server -Xms256m -Xmx1g -Xmn256m -XX:PermSize=128m -XX:MaxPermSize=320m"
        NAMESRV_ADDR: namesrv-node1:9876;namesrv-node2:9876
    command: sh mqbroker -c /home/rocketmq/conf/broker.conf
    ports: 
      ## listenPort
      - 10901:10901
      ## VIP通道默认比listenPort小2
      - 10899:10899
      - 10902:10902
    depends_on: 
      - namesrv-node1
      - namesrv-node2
  rocketmq-broker-node1-slave: 
    hostname: brokder-node1-slave
    image: foxiswho/rocketmq:4.7.0
    restart: always
    volumes:
      - ./logs/broker/node1/slave:/home/rocketmq/logs
      - ./store/broker/node1/slave:/home/rocketmq/store
      - ./conf/broker-node1/broker-slave.conf:/home/rocketmq/conf/broker.conf
      - /etc/localtime:/etc/localtime:ro
    environment:
        TZ: Asia/Shanghai
        JAVA_OPT_EXT: "-server -Xms256m -Xmx1g -Xmn256m -XX:PermSize=128m -XX:MaxPermSize=320m"
    command: sh mqbroker -c /home/rocketmq/conf/broker.conf
    ports: 
      - 10911:10911
      - 10909:10909
      - 10912:10912
    depends_on: 
      - namesrv-node1
      - namesrv-node2
      - rocketmq-broker-node1-master
  rocketmq-broker-node2-master: 
    hostname: brokder-node2-master 
    image: foxiswho/rocketmq:4.7.0
    volumes:
      - ./logs/broker/node2/master:/home/rocketmq/logs
      - ./store/broker/node2/master:/home/rocketmq/store
      ## 配置文件映射,就是上面写得BrokerNode2 Master配置文件所在路径
      - ./conf/broker-node2/broker-master.conf:/home/rocketmq/conf/broker.conf
      - /etc/localtime:/etc/localtime:ro
    restart: always
    environment:
        TZ: Asia/Shanghai
        JAVA_OPT_EXT: "-server -Xms256m -Xmx1g -Xmn256m -XX:PermSize=128m -XX:MaxPermSize=320m"
        NAMESRV_ADDR: namesrv-node1:9876;namesrv-node2:9876
    command: sh mqbroker -c /home/rocketmq/conf/broker.conf
    ports: 
      - 11901:11901
      - 11899:11899
      - 11902:11902
    depends_on: 
      - namesrv-node1
      - namesrv-node2
  rocketmq-broker-node2-slave: 
    hostname: brokder-node1-slave
    image: foxiswho/rocketmq:4.7.0
    restart: always
    volumes:
      - ./logs/broker/node2/slave:/home/rocketmq/logs
      - ./store/broker/node2/slave:/home/rocketmq/store
      - ./conf/broker-node2/broker-slave.conf:/home/rocketmq/conf/broker.conf
      - /etc/localtime:/etc/localtime:ro
    environment:
        TZ: Asia/Shanghai
        JAVA_OPT_EXT: "-server -Xms256m -Xmx1g -Xmn256m -XX:PermSize=128m -XX:MaxPermSize=320m"
    command: sh mqbroker -c /home/rocketmq/conf/broker.conf
    ports: 
      - 11911:11911
      - 11909:11909
      - 11912:11912
    depends_on: 
      - namesrv-node1
      - namesrv-node2
      - rocketmq-broker-node1-master
  rocketmq-console: 
    build: ./console
    container_name: rocketmq-console
    restart: always
    ports: 
      - 9001:9001
    environment:
      TZ: Asia/Shanghai
      ROCKETMQ_CONFIG_NAMESRV_ADDR: namesrv-node1:9876;namesrv-node2:9876
      ## 关闭VIP Channel
      ROCKETMQ_CONFIG_IS_VIP_CHANNEL: "false"
      ## 开启密码验证(用户密码配置文件在./console/users.properties中)可热修改,无需停机
      ROCKETMQ_CONFIG_LOGIN_REQUIRED: "true"
    volumes: 
      - ./console/data:/data/rocketmq-console
      - /etc/localtime:/etc/localtime:ro
    depends_on: 
      - namesrv-node1
      - namesrv-node2
      - rocketmq-broker-node1-master
      - rocketmq-broker-node2-master
      - rocketmq-broker-node1-slave
      - rocketmq-broker-node2-slave

运行

在运行之前,有几个需要注意的地方:
请检查好每个配置文件中的brokerIP1的ip是否为本机IP,我这里在虚拟机中搭建,我的虚拟机的IP地址为192.168.2.105,各位用的时候记得改成RabbitMQ节点所在机器上的IP地址
每个Broker在不同机器上时,nameserAddr中需要将hostname换成name server服务器真实IP

在Docker-Compose文件目录中,执行以下指令即可启动

# docker-compose up

访问http://192.168.2.105:9001即可进入管理页面
image.png
如果需要登录,默认账号密码有admin,admin

查看集群ok即搭建完毕

image.png

GitHub附件

仓库地址

发表评论