Docker部署RocketMQ集群

一、概述

Apache RocketMQ是阿里开源的一款高性能、高吞吐量、队列模型的消息中间件的分布式消息中间件。

关于RocketMQ集群架构的详细介绍,请参考链接:https://blog.csdn.net/Weixiaohuai/article/details/123607472

 

二、下载镜像

docker pull apache/rocketmq:4.9.4
docker pull apacherocketmq/rocketmq-dashboard

主要用到了2个镜像,第1个用来部署NameServer和Broker,第2个用来管理rocketmq

 

三、部署

本文主要采用一台服务器来部署RocketMQ集群,集群部署模式采用多master模式,也就是2个NameServer和2个Broker

刷盘机制采用异步复制,异步刷盘

 

服务器配置:

操作系统:centos 7.6

硬件配置:2核4g

ip地址:192.168.137.138

 

创建目录

mkdir -p /opt/rocketmq/logs/nameserver-a
mkdir -p /opt/rocketmq/logs/nameserver-b
mkdir -p /opt/rocketmq/logs/broker-a
mkdir -p /opt/rocketmq/logs/broker-b
mkdir -p /opt/rocketmq/store/broker-a
mkdir -p /opt/rocketmq/store/broker-b
mkdir -p /opt/rocketmq/broker-a/
mkdir -p /opt/rocketmq/broker-b/

设置权限

chmod 777 -R /opt/rocketmq/logs/*
chmod 777 -R /opt/rocketmq/store/*

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

 

创建broker.conf

修改第一个节点配置

vim /opt/rocketmq/broker-a/broker-a.conf

内容如下:

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

注意:brokerIP1,namesrvAddr,这2个参数,一般配置为内网ip,提供内网访问。如果需要公网访问,这里一定要配置公网ip,否则无法访问。

 

修改第二个节点配置

vim /opt/rocketmq/broker-b/broker-b.conf

内容如下:

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

注意:brokerName,listenPort这2个参数和第一个节点不一样,其他内容和第一个节点配置一样

 

编写 docker-compose.yml

vim /opt/rocketmq/docker-compose.yml

内容如下:

version: '3.5'
services:
  rmqnamesrv-a:
    image: apache/rocketmq:4.9.4
    container_name: rmqnamesrv-a
    ports:
      - 9876:9876
    volumes:
      #- /opt/rocketmq/logs/nameserver-a:/home/rocketmq/logs
      - /opt/rocketmq/broker-b/broker-b.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    command: sh mqnamesrv
    networks:
      rmq:
        aliases:
          - rmqnamesrv-a

  rmqnamesrv-b:
    image: apache/rocketmq:4.9.4
    container_name: rmqnamesrv-b
    ports:
      - 9877:9876
    volumes:
      #- /opt/rocketmq/logs/nameserver-b:/home/rocketmq/logs
      - /opt/rocketmq/broker-b/broker-b.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    command: sh mqnamesrv
    networks:
      rmq:
        aliases:
          - rmqnamesrv-b

  rmqbroker-a:
    image: apache/rocketmq:4.9.4
    container_name: rmqbroker-a
    ports:
      - 10911:10911
    volumes:
      #- /opt/rocketmq/logs/broker-a/logs:/home/rocketmq/logs
      #- /opt/rocketmq/store/broker-a/store:/home/rocketmq/store
      - /opt/rocketmq/broker-a/broker-a.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv-a: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
    links:
      - rmqnamesrv-a:rmqnamesrv-a
      - rmqnamesrv-b:rmqnamesrv-b
    networks:
      rmq:
        aliases:
          - rmqbroker-a

  rmqbroker-b:
    image: apache/rocketmq:4.9.4
    container_name: rmqbroker-b
    ports:
      - 10912:10912
    volumes:
      #- /opt/rocketmq/logs/broker-b/logs:/home/rocketmq/logs
      #- /opt/rocketmq/store/broker-b/store:/home/rocketmq/store
      - /opt/rocketmq/broker-b/broker-b.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv-b:9877"
      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
    links:
      - rmqnamesrv-a:rmqnamesrv-a
      - rmqnamesrv-b:rmqnamesrv-b
    networks:
      rmq:
        aliases:
          - rmqbroker-b
  rmqconsole:
    image: apacherocketmq/rocketmq-dashboard
    container_name: rmqconsole
    ports:
      - 8087:8080
    environment:
      JAVA_OPTS: -Drocketmq.namesrv.addr=rmqnamesrv-a:9876;rmqnamesrv-b:9877 -Dcom.rocketmq.sendMessageWithVIPChannel=false -Drocketmq.config.accessKey=rocketmq2 -Drocketmq.config.secretKey=12345678
    volumes:
      - /opt/rocketmq/console-ng/data:/tmp/rocketmq-console/data
    networks:
      rmq:
        aliases:
          - rmqconsole
networks:
  rmq:
    name: rmq
    driver: bridge
View Code

 

最后一步,执行docker-compose

cd /opt/rocketmq
docker-compose up -d

 

查看进程,使用命令docker-compose ps

# docker-compose ps
    Name                  Command               State                Ports              
----------------------------------------------------------------------------------------
rmqbroker-a    sh mqbroker -c /home/rocke ...   Up      10909/tcp,                      
                                                        0.0.0.0:10911->10911/tcp,       
                                                        10912/tcp, 9876/tcp             
rmqbroker-b    sh mqbroker -c /home/rocke ...   Up      10909/tcp, 10911/tcp,           
                                                        0.0.0.0:10912->10912/tcp,       
                                                        9876/tcp                        
rmqconsole     sh -c java $JAVA_OPTS -jar ...   Up      0.0.0.0:8087->8080/tcp          
rmqnamesrv-a   sh mqnamesrv                     Up      10909/tcp, 10911/tcp, 10912/tcp,
                                                        0.0.0.0:9876->9876/tcp          
rmqnamesrv-b   sh mqnamesrv                     Up      10909/tcp, 10911/tcp, 10912/tcp,
                                                        0.0.0.0:9877->9876/tcp 

确保State状态为up

 

访问管理平台

http://192.168.137.138:8087

点击右上角切换语言为简体中文,点击集群,效果如下:

 

看到这个图,说明集群启动正常。

 

设置持久化配置

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

 

复制docker里面的文件

docker cp rmqnamesrv-a:/home/rocketmq/logs /opt/rocketmq/logs/nameserver-a
docker cp rmqnamesrv-b:/home/rocketmq/logs /opt/rocketmq/logs/nameserver-b
docker cp rmqbroker-a:/home/rocketmq/logs /opt/rocketmq/logs/broker-a
docker cp rmqbroker-a:/home/rocketmq/store /opt/rocketmq/store/broker-a
docker cp rmqbroker-b:/home/rocketmq/logs /opt/rocketmq/logs/broker-b
docker cp rmqbroker-b:/home/rocketmq/store /opt/rocketmq/store/broker-b

设置权限,否则会写入文件失败,造成启动失败。

chmod 777 -R /opt/rocketmq/logs/*
chmod 777 -R /opt/rocketmq/store/*

 

关闭相关docker进程

docker-compose down

 

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

version: '3.5'
services:
  rmqnamesrv-a:
    image: apache/rocketmq:4.9.4
    container_name: rmqnamesrv-a
    ports:
      - 9876:9876
    volumes:
      - /opt/rocketmq/logs/nameserver-a:/home/rocketmq/logs
      - /opt/rocketmq/broker-b/broker-b.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    command: sh mqnamesrv
    networks:
      rmq:
        aliases:
          - rmqnamesrv-a

  rmqnamesrv-b:
    image: apache/rocketmq:4.9.4
    container_name: rmqnamesrv-b
    ports:
      - 9877:9876
    volumes:
      - /opt/rocketmq/logs/nameserver-b:/home/rocketmq/logs
      - /opt/rocketmq/broker-b/broker-b.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    command: sh mqnamesrv
    networks:
      rmq:
        aliases:
          - rmqnamesrv-b

  rmqbroker-a:
    image: apache/rocketmq:4.9.4
    container_name: rmqbroker-a
    ports:
      - 10911:10911
    volumes:
      - /opt/rocketmq/logs/broker-a/logs:/home/rocketmq/logs
      - /opt/rocketmq/store/broker-a/store:/home/rocketmq/store
      - /opt/rocketmq/broker-a/broker-a.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv-a: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
    links:
      - rmqnamesrv-a:rmqnamesrv-a
      - rmqnamesrv-b:rmqnamesrv-b
    networks:
      rmq:
        aliases:
          - rmqbroker-a

  rmqbroker-b:
    image: apache/rocketmq:4.9.4
    container_name: rmqbroker-b
    ports:
      - 10912:10912
    volumes:
      - /opt/rocketmq/logs/broker-b/logs:/home/rocketmq/logs
      - /opt/rocketmq/store/broker-b/store:/home/rocketmq/store
      - /opt/rocketmq/broker-b/broker-b.conf:/home/rocketmq/rocketmq-4.9.4/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rmqnamesrv-b:9877"
      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
    links:
      - rmqnamesrv-a:rmqnamesrv-a
      - rmqnamesrv-b:rmqnamesrv-b
    networks:
      rmq:
        aliases:
          - rmqbroker-b
  rmqconsole:
    image: apacherocketmq/rocketmq-dashboard
    container_name: rmqconsole
    ports:
      - 8087:8080
    environment:
      JAVA_OPTS: -Drocketmq.namesrv.addr=rmqnamesrv-a:9876;rmqnamesrv-b:9877 -Dcom.rocketmq.sendMessageWithVIPChannel=false -Drocketmq.config.accessKey=rocketmq2 -Drocketmq.config.secretKey=12345678
    volumes:
      - /opt/rocketmq/console-ng/data:/tmp/rocketmq-console/data
    networks:
      rmq:
        aliases:
          - rmqconsole
networks:
  rmq:
    name: rmq
    driver: bridge
View Code

 

启动相关docker进程

docker-compose up -d

 

等待30秒,再次访问管理后台,如果数据能显示,表示正常。

 

测试

点击主题,默认是空的

点击新增

 

 选择集群,BROKER_NAME,输入主题名test

 

点击提交后,会出现一条数据,点击发送消息

 

 点击提交

 

 提示成功

 

 点击消息,选择主题test,点击搜索,就可以看到发送记录了。

 

 

 

本文参考链接:

https://www.136.la/jingpin/show-176132.html

 

posted @ 2023-02-23 19:35  肖祥  阅读(613)  评论(0编辑  收藏  举报