docker-compose搭建RocketMq集群

集群架构:双主

机器:172.50.2.41 

           172.50.2.42

一、创建目录

mkdir -p /docker/alibaba/rocketmq/logs/nameserver
mkdir -p /docker/alibaba/rocketmq/logs/broker
mkdir -p /docker/alibaba/rocketmq/store/broker
mkdir -p /docker/alibaba/rocketmq/broker/
mkdir -p /docker/alibaba/rocketmq/console-ng/data

设置权限

chmod 777 -R /docker/alibaba/rocketmq/logs/*
chmod 777 -R /docker/alibaba/rocketmq/store/*

注意:这里如果不设置权限,会导致docker写入文件失败,导致rocketmq启动异常。

二、创建broker.conf

vim /docker/alibaba/rocketmq/broker/broker.conf

编辑内容,这里需要根据自己的实际环境做适当修改
注意:brokerIP1,namesrvAddr,这2个参数,一般配置为内网ip,提供内网访问。如果需要公网访问,这里一定要配置公网ip,否则无法访问。

1、第一个节点broker.conf

brokerClusterName = rocketmq-cluster
brokerName = broker-a
brokerId = 0
#这个很有讲究 如果是正式环境 这里一定要填写内网地址(安全)
#如果是用于测试或者本地这里建议要填外网地址,因为你的本地代码是无法连接到阿里云内网,只能连接外网。
brokerIP1 = 172.50.2.41
deleteWhen = 04
fileReservedTime = 48
# 内网的(阿里云有内网IP和外网IP)
namesrvAddr = 172.50.2.41:9876;172.50.2.42:9876
autoCreateTopicEnable=true
#Broker 对外服务的监听端口,
listenPort = 10911
#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole = ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType = ASYNC_FLUSH

2、第二个节点broker.conf

brokerClusterName = rocketmq-cluster
brokerName = broker-b
brokerId = 0
#这个很有讲究 如果是正式环境 这里一定要填写内网地址(安全)
#如果是用于测试或者本地这里建议要填外网地址,因为你的本地代码是无法连接到阿里云内网,只能连接外网。
brokerIP1 = 172.50.2.42
deleteWhen = 04
fileReservedTime = 48
# 内网的(阿里云有内网IP和外网IP)
namesrvAddr = 172.50.2.41:9876;172.50.2.42:9876
autoCreateTopicEnable=true
#Broker 对外服务的监听端口,
listenPort = 10911
#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole = ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType = ASYNC_FLUSH

3、编辑rocketmq的docker-compose.yml文件

version: '3.9'
services:
  rmqnamesrv:
    image: apache/rocketmq:4.9.4
    container_name: rmqnamesrv
    ports:
      - 9876:9876
    network_mode: "host"
    privileged: true
    volumes:
      #- /docker/alibaba/rocketmq/logs/nameserver:/home/rocketmq/logs
      - /docker/alibaba/rocketmq/broker/broker.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    command: sh mqnamesrv

  rmqbroker:
    image: apache/rocketmq:4.9.4
    container_name: rmqbroker
    ports:
      - 10911:10911
    network_mode: "host"
    privileged: true
    volumes:
     # - /docker/alibaba/rocketmq/logs/broker/logs:/home/rocketmq/logs
     # - /docker/alibaba/rocketmq/store/broker/store:/home/rocketmq/store
      - /docker/alibaba/rocketmq/broker/broker.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv:9876"
      JAVA_OPTS: " -Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms256m -Xmx256m -Xmn256m"
    command: sh mqbroker -c /home/rocketmq/rocketmq-4.9.4/conf/broker.conf

3、编辑rocketmq-dashboard的docker-compose.yml文件(新版改了,好像叫rocketmq-console)

version: '3.9'
services:
  rmqconsole:
    image: apacherocketmq/rocketmq-dashboard
    container_name: rmqconsole
    ports:
      - 8087:8087
    network_mode: "host"
    privileged: true
    environment:
      JAVA_OPTS: -Dserver.port=8087 -Drocketmq.namesrv.addr=172.50.2.41:9876;172.50.2.42:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false -Drocketmq.config.accessKey=rocketmq2 -Drocketmq.config.secretKey=12345678
    volumes:
      - /docker/alibaba/rocketmq/console-ng/data:/tmp/rocketmq-console/data
    network_mode: "host"
    privileged: true

4、启动

注意:启动之前一定要开放端口,不然会出现集群不通的情况

#批量添加多个端口
firewall-cmd --permanent --add-port=8087/tcp --add-port=9876/tcp --add-port=10911/tcp
 
# 防火墙重载
firewall-cmd --reload
docker-compose up -d

5、访问控制台

 

docker-compose搭建RocketMq集群_第1张图片

四、数据持久化

上面的 docker-compose.yml,注释掉了一些文件挂载操作,因为新版本的rocketmq无法启动创建文件,直接挂载一个空目录,会启动报错。

复制docker里面的文件

docker cp rmqnamesrv:/home/rocketmq/logs /docker/alibaba/rocketmq/logs/nameserver
docker cp rmqbroker:/home/rocketmq/logs /docker/alibaba/rocketmq/logs/broker
docker cp rmqbroker:/home/rocketmq/store /docker/alibaba/rocketmq/store/broker

再执行一遍设置权限,否则会写入文件失败,造成启动失败。

chmod 777 -R /docker/alibaba/rocketmq/logs/*
chmod 777 -R /docker/alibaba/rocketmq/store/*

关闭相关docker进程

docker stop rmqnamesrv rmqbroker

修改docker-compose.yml,开启持久化配置

version: '3.9'
services:
  rmqnamesrv:
    image: apache/rocketmq:4.9.4
    container_name: rmqnamesrv
    ports:
      - 9876:9876
    network_mode: "host"
    privileged: true
    volumes:
      - /docker/alibaba/rocketmq/logs/nameserver:/home/rocketmq/logs
      - /docker/alibaba/rocketmq/broker/broker.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    command: sh mqnamesrv

  rmqbroker:
    image: apache/rocketmq:4.9.4
    container_name: rmqbroker
    ports:
      - 10911:10911
    network_mode: "host"
    privileged: true
    volumes:
      - /docker/alibaba/rocketmq/logs/broker/logs:/home/rocketmq/logs
      - /docker/alibaba/rocketmq/store/broker/store:/home/rocketmq/store
      - /docker/alibaba/rocketmq/broker/broker.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv:9876"
      JAVA_OPTS: " -Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms256m -Xmx256m -Xmn256m"
    command: sh mqbroker -c /home/rocketmq/rocketmq-4.9.4/conf/broker.conf

五、重新启动

docker-compose up -d

你可能感兴趣的:(docker,rocketmq,容器,rocketmq集群,docker-compose)