环境准备

docker安装与配置阿里镜像加速器
docker-compose安装—pip3

rocketMQ集群组成(双主双从异步刷盘)

容器名称IP与宿主机的端口映射
rmqnamesrv-armqnamesrv-a9876:9876
rmqnamesrv-brmqnamesrv-b9876:9877
rmqbroker-armqbroker-a10911:10911
rmqbroker-a-srmqbroker-a-s10911:10912
rmqbroker-brmqbroker-b10911:10913
rmqbroker-b-srmqbroker-b-s10911:10914
rmqconsolermqconsole8080:8080

容器内部通信使用自定义网络,IP别名和容器名一样

传送门:docker网络探究

准备docker-compose文件

docker-compose.yml (注意映射目录中含有rocketMQ的版本号,按需修改)

version: '3.5'
services:
  rmqnamesrv-a:
    image: rocketmqinc/rocketmq:latest
    container_name: rmqnamesrv-a
    ports:
      - 9876:9876
    volumes:
      - ./rocketmq/logs/nameserver-a:/root/logs
      - ./rocketmq/store/nameserver-a:/root/store
    command: sh mqnamesrv
    networks:
      rmq:
        aliases:
          - rmqnamesrv-a

  rmqnamesrv-b:
    image: rocketmqinc/rocketmq:latest
    container_name: rmqnamesrv-b
    ports:
      - 9877:9876
    volumes:
      - ./rocketmq/store/nameserver-b:/root/store
      - ./rocketmq/logs/nameserver-b:/root/logs
    command: sh mqnamesrv
    networks:
      rmq:
        aliases:
          - rmqnamesrv-b

  rmqbroker-a:
    image: rocketmqinc/rocketmq:latest
    container_name: rmqbroker-a
    ports:
      - 10911:10911
    volumes:
      - ./rocketmq/logs/broker-a:/root/logs
      - ./rocketmq/store/broker-a:/root/store
      - ./rocketmq/broker-a.conf:/opt/rocketmq-4.4.0/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv-a:9876"
      JAVA_OPTS: " -Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
    command: sh mqbroker -c /opt/rocketmq-4.4.0/conf/broker.conf autoCreateTopicEnable=true &
    links:
      - rmqnamesrv-a:rmqnamesrv-a
      - rmqnamesrv-b:rmqnamesrv-b
    networks:
      rmq:
        aliases:
          - rmqbroker-a

  rmqbroker-a-s:
    image: rocketmqinc/rocketmq:latest
    container_name: rmqbroker-a-s
    ports:
      - 10912:10911
    volumes:
      - ./rocketmq/logs/broker-a-s:/root/logs
      - ./rocketmq/store/broker-a-s:/root/store
      - ./rocketmq/broker-a-s.conf:/opt/rocketmq-4.4.0/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv-a:9876"
      JAVA_OPTS: " -Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
    command: sh mqbroker -c /opt/rocketmq-4.4.0/conf/broker.conf autoCreateTopicEnable=true &
    links:
      - rmqnamesrv-a:rmqnamesrv-a
      - rmqnamesrv-b:rmqnamesrv-b
    networks:
      rmq:
        aliases:
          - rmqbroker-a-s

  rmqbroker-b:
    image: rocketmqinc/rocketmq:latest
    container_name: rmqbroker-b
    ports:
      - 10913:10911
    volumes:
      - ./rocketmq/logs/broker-b:/root/logs
      - ./rocketmq/store/broker-b:/root/store
      - ./rocketmq/broker-b.conf:/opt/rocketmq-4.4.0/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv-a:9876"
      JAVA_OPTS: " -Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
    command: sh mqbroker -c /opt/rocketmq-4.4.0/conf/broker.conf autoCreateTopicEnable=true &
    links:
      - rmqnamesrv-a:rmqnamesrv-a
      - rmqnamesrv-b:rmqnamesrv-b
    networks:
      rmq:
        aliases:
          - rmqbroker-b

  rmqbroker-b-s:
    image: rocketmqinc/rocketmq:latest
    container_name: rmqbroker-b-s
    ports:
      - 10914:10911
    volumes:
      - ./rocketmq/logs/broker-b-s:/root/logs
      - ./rocketmq/store/broker-b-s:/root/store
      - ./rocketmq/broker-b-s.conf:/opt/rocketmq-4.4.0/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv-a:9876"
      JAVA_OPTS: " -Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
    command: sh mqbroker -c /opt/rocketmq-4.4.0/conf/broker.conf autoCreateTopicEnable=true &
    links:
      - rmqnamesrv-a:rmqnamesrv-a
      - rmqnamesrv-b:rmqnamesrv-b
    networks:
      rmq:
        aliases:
          - rmqbroker-b-s

  rmqconsole:
    image: styletang/rocketmq-console-ng:latest
    container_name: rmqconsole
    ports:
      - 8080:8080
    environment:
      JAVA_OPTS: -Drocketmq.namesrv.addr=rmqnamesrv-a:9876;rmqnamesrv-b:9877 -Dcom.rocketmq.sendMessageWithVIPChannel=false
    networks:
      rmq:
        aliases:
          - rmqconsole

# 自定义网络
networks:
  rmq:
    name: rmq
    driver: bridge

准备配置文件

broker-a.conf

brokerClusterName = rocketmq-cluster
brokerName = broker-a

# 0表示broker master  非0表示 broker slaver
brokerId = 0

# brokerIP1 当前broker监听的IP
brokerIP1 = rmqbroker-a

# brokerIP2 存在broker主从时,在broker主节点上配置了brokerIP2的话,broker从节点会连接主节点配置的brokerIP2来同步
# brokerIP2 = 192.168.111.129

deleteWhen = 04
fileReservedTime = 48


namesrvAddr = rmqnamesrv-a:9876;rmqnamesrv-b:9876

# 是否能够自动创建topic
autoCreateTopicEnable = true

#Broker 对外服务的监听端口, 默认10911
# listenPort = 10911

#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole = ASYNC_MASTER

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

broker-a-s.conf

brokerClusterName = rocketmq-cluster
brokerName = broker-a

# 0表示broker master  非0表示 broker slaver
brokerId = 1

brokerIP1 = rmqbroker-a-s

deleteWhen = 04
fileReservedTime = 48


namesrvAddr = rmqnamesrv-a:9876;rmqnamesrv-b:9876

# autoCreateTopicEnable=true

#Broker 对外服务的监听端口,
#listenPort = 10912

#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole = SLAVE

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

broker-b.conf

brokerClusterName = rocketmq-cluster
brokerName = broker-b

# 0表示broker master  非0表示 broker slaver
brokerId = 0

# brokerIP1 当前broker监听的IP
brokerIP1 = rmqbroker-b

# brokerIP2 存在broker主从时,在broker主节点上配置了brokerIP2的话,broker从节点会连接主节点配置的brokerIP2来同步
# brokerIP2 = 192.168.111.129

deleteWhen = 04
fileReservedTime = 48


namesrvAddr = rmqnamesrv-a:9876;rmqnamesrv-b:9876

# 是否能够自动创建topic
autoCreateTopicEnable = true

#Broker 对外服务的监听端口, 默认10911
# listenPort = 10913

#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole = ASYNC_MASTER

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

broker-b-s.conf

brokerClusterName = rocketmq-cluster
brokerName = broker-b

# 0表示broker master  非0表示 broker slaver
brokerId = 1

brokerIP1 = rmqbroker-b-s

deleteWhen = 04
fileReservedTime = 48


namesrvAddr = rmqnamesrv-a:9876;rmqnamesrv-b:9876

# 是否能够自动创建topic
autoCreateTopicEnable = true

#Broker 对外服务的监听端口,
# listenPort = 10914

#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole = SLAVE

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

一键启动集群

将docker-compose.yml文件和配置文件上传到服务器,注意对应目录
执行命令并查看宿主机映射过来的启动日志

docker-compose up -d

访问控制台:http://宿主机IP:8080/
在这里插入图片描述

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐