Bootstrap

Docker搭建RocketMQ集群

前言

服务器环境:centos7 

安装用户:root,关闭防火墙,安装jdk8环境

使用docker-compose一键式搭建docker环境下的RocketMQ集群及其管理工具,实现rocketmq在docker环境容器外部访问。

一、安装docker

yum -y install docker  //安装docker

systemctl start docker  //启动docker

systemctl status docker  //查看状态

systemctl enable docker.service  //开机自启

二、安装docker Compose

1、卸载旧版本Docker Compose 
sudo rm /usr/local/bin/docker-compose

2、下载Docker Compose最新版
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

3、授权Docker Compose二进制文件执行权限
sudo chmod +x /usr/local/bin/docker-compose

4、检查Docker Compose版本
docker-compose --version

三、安装dokcker管理面板portainer 汉化版本

docker pull portainer/portainer-ce

wget https://imnks.com/usr/uploads/2021/11/portainer-ce-2.9.2-public-cn-1101.zip
unzip portainer-ce-2.9.2-public-cn-1101.zip

docker run -d -p 9000:9000 --name portainer --restart always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data -v /root/public:/public portainer/portainer-ce

浏览器输入IP:9000

如果无法进入管理面板,提示权限不足,进行如下设置

vi /etc/selinux/config 

将SELINUX=enforcing改为SELINUX=disabled

四、安装rocketmq集群和管理工具

在/usr/local/bin/目录(第二步安装的位置)下创建脚本:vi rocketmq.yml

 脚本如下(保持yml文件格式)-----------------------------------

services:
  rocketmq-namesrv-1:
    image: apache/rocketmq:4.9.3
    container_name: rocketmq-namesrv-1
    ports:
      - 9876:9876
    volumes:
      - /usr/local/bin/atp/rocketmq/logs/nameserver1:/home/rocketmq/logs/rocketmqlogs
    command: sh mqnamesrv
    networks:
      - rocketmq
  rocketmq-namesrv-2:
    image: apache/rocketmq:4.9.3
    container_name: rocketmq-namesrv-2
    ports:
      - 9877:9876
    volumes:
      - /usr/local/bin/atp/rocketmq/logs/nameserver2:/home/rocketmq/logs/rocketmqlogs
    command: sh mqnamesrv
    networks:
      - rocketmq

  rocketmq-broker-1:
    image: apache/rocketmq:4.9.3
    container_name: rocketmq-broker-1
    ports:
      - 10911:10911
    volumes:
      - /usr/local/bin/atp/rocketmq/logs/broker1:/home/rocketmq/logs/rocketmqlogs
      #- /usr/local/bin/atp/rocketmq/store/broker1:/home/rocketmq/store
      - /usr/local/bin/atp/rocketmq/conf/broker1/broker.conf:/home/rocketmq/rocketmq-4.5.0/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesrv-1:9876;rocketmq-namesrv-2:9876"
      JAVA_OPTS: "-Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
    command: sh mqbroker -c /home/rocketmq/rocketmq-4.5.0/conf/broker.conf autoCreateTopicEnable=true &
    depends_on:
      - rocketmq-namesrv-1
      - rocketmq-namesrv-2
    networks:
      - rocketmq

  rocketmq-broker-2:
    image: apache/rocketmq:4.9.3
    container_name: rocketmq-broker-2
    ports:
      - 10912:10911
    volumes:
      - /usr/local/bin/atp/rocketmq/logs/broker2:/home/rocketmq/logs/rocketmqlogs
      #- /usr/local/bin/atp/rocketmq/store/broker2:/home/rocketmq/store
      - /usr/local/bin/atp/rocketmq/conf/broker2/broker.conf:/home/rocketmq/rocketmq-4.5.0/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesrv-1:9876;rocketmq-namesrv-2:9876"
      JAVA_OPTS: " -Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
    command: sh mqbroker -c /home/rocketmq/rocketmq-4.5.0/conf/broker.conf autoCreateTopicEnable=true &
    depends_on:
      - rocketmq-namesrv-1
      - rocketmq-namesrv-2
    networks:
      - rocketmq

  rocketmq-broker-3:
    image: apache/rocketmq:4.9.3
    container_name: rocketmq-broker-3
    ports:
      - 10913:10911
    volumes:
      - /usr/local/bin/atp/rocketmq/logs/broker3:/home/rocketmq/logs/rocketmqlogs
      #- /usr/local/bin/atp/rocketmq/store/broker3:/home/rocketmq/store
      - /usr/local/bin/atp/rocketmq/conf/broker3/broker.conf:/home/rocketmq/rocketmq-4.5.0/conf/broker.conf
    environment:
      TZ: Asia/Shanghai
      NAMESRV_ADDR: "rocketmq-namesrv-1:9876;rocketmq-namesrv-2:9876"
      JAVA_OPTS: " -Duser.home=/opt"
      JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
    command: sh mqbroker -c /home/rocketmq/rocketmq-4.5.0/conf/broker.conf autoCreateTopicEnable=true &
    depends_on:
      - rocketmq-namesrv-1
      - rocketmq-namesrv-2
    networks:
      - rocketmq

  rocketmq-console:
    image: styletang/rocketmq-console-ng
    container_name: rocketmq-console
    ports:
      - 9090:8080
    environment:
      JAVA_OPTS: -Drocketmq.namesrv.addr=rocketmq-namesrv-1:9876;rocketmq-namesrv-2:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false
    networks:
      - rocketmq
    depends_on:
      - rocketmq-broker-1
      - rocketmq-broker-2
      - rocketmq-broker-3

networks:
  rocketmq:
    driver: bridge

创建文件夹

mkdir -p /usr/local/bin/atp/rocketmq/conf/broker1

mkdir -p /usr/local/bin/atp/rocketmq/conf/broker2

mkdir -p /usr/local/bin/atp/rocketmq/conf/broker3

分别在三个目录下创建对应的broker.conf

vi  /usr/local/bin/atp/rocketmq/conf/broker1/broker.conf

brokerClusterName = DefaultCluster
#broker名称
brokerName = rocketmq-broker-1
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH

vi  /usr/local/bin/atp/rocketmq/conf/broker2/broker.conf

brokerClusterName = DefaultCluster
#broker名称
brokerName = rocketmq-broker-2
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH

vi  /usr/local/bin/atp/rocketmq/conf/broker3/broker.conf

brokerClusterName = DefaultCluster
#broker名称
brokerName = rocketmq-broker-3
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH

启动集群:

cd /usr/local/bin/

docker-compose -f rocketmq.yml -p rocketmq up -d

启动报错:validating /usr/local/bin/rocketmq.yml: (root) Additional property ervices is not allowed

解决:rocketmq.yml检查配置文件是否格式错误。
        

访问控制台(ip为宿主机ip):http//ip:9090    

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;