网站设计 公司 长沙八桂职教
文章目录
- 关键配置说明
- Dockerfile
- entrypoint.sh
- Broker 配置文件
- master1的broker.conf文件
- master2的broker.conf文件
- salve1的broker.conf文件
- salve2的broker.conf文件
- docker-compose.yml
- 部署步骤
- 构建镜像:
- 启动集群:
- 查看服务启动情况
- 如果启动失败,查看对应的容器日志
- 关闭容器并删除镜像
本章内容主要介绍部署一个双主双从的rocketmq集群
关键配置说明
NameServer:负责服务发现和路由管理。
Broker:
- Master Broker 负责消息的写入和读取。
- Slave Broker 负责从 Master 同步数据,提供高可用性。
RocketMQ Console:提供 Web 管理界面,方便监控和管理集群。(如果拉取镜像失败,则先注释掉)
数据持久化:通过挂载卷(volumes)将 Broker 的日志和数据存储到宿主机,确保数据持久化。
我们需要按如下目录去创建文件夹和文件
/tmp/rocketmq-cluster/
├── docker-compose.yml
├── Dockerfile
├── entrypoint.sh
├── broker-master1/
│ ├── conf/
│ │ └── broker.conf
│ ├── logs/
│ └── store/
├── broker-slave1/
│ ├── conf/
│ │ └── broker.conf
│ ├── logs/
│ └── store/
├── broker-master2/
│ ├── conf/
│ │ └── broker.conf
│ ├── logs/
│ └── store/
└── broker-slave2/├── conf/│ └── broker.conf├── logs/└── store/
Dockerfile
通用的 RocketMQ Dockerfile,用于构建 NameServer 和 Broker 的镜像
# 使用官方的 OpenJDK 镜像作为基础镜像
FROM openjdk:8-jdk-alpine# 设置 RocketMQ 版本
ENV ROCKETMQ_VERSION=4.9.4# 安装必要的工具
RUN apk add --no-cache bash curl# 下载并解压 RocketMQ
RUN mkdir -p /opt/rocketmq \&& cd /opt/rocketmq \&& curl -O https://archive.apache.org/dist/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip \&& unzip rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip \&& mv rocketmq-all-${ROCKETMQ_VERSION}-bin-release/* . \&& rm -rf rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip rocketmq-all-${ROCKETMQ_VERSION}-bin-release# 设置环境变量
ENV ROCKETMQ_HOME=/opt/rocketmq
ENV PATH=${ROCKETMQ_HOME}/bin:${PATH}# 暴露端口
# NameServer 默认端口:9876
# Broker 默认端口:10911(Broker)、10909(主从同步)、10912(HA)
EXPOSE 9876 10911 10909 10912# 设置工作目录
WORKDIR ${ROCKETMQ_HOME}# 启动脚本
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh# 启动 NameServer 或 Broker
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
这是一个启动脚本,用于根据环境变量启动 NameServer 或 Broker:
#!/bin/bash# 根据环境变量启动 NameServer 或 Broker
if [ "$ROCKETMQ_ROLE" = "namesrv" ]; thenecho "Starting NameServer..."${ROCKETMQ_HOME}/bin/mqnamesrv
elif [ "$ROCKETMQ_ROLE" = "broker" ]; thenecho "Starting Broker..."${ROCKETMQ_HOME}/bin/mqbroker -c ${ROCKETMQ_HOME}/conf/broker.conf
elseecho "Unknown role: $ROCKETMQ_ROLE"exit 1
fi
Broker 配置文件
master1的broker.conf文件
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
namesrvAddr=namesrv1:9876;namesrv2:9876
listenPort=10911
storePathRootDir=/opt/rocketmq/store/broker-master1
master2的broker.conf文件
brokerClusterName=DefaultCluster
brokerName=broker-b
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
namesrvAddr=namesrv1:9876;namesrv2:9876
listenPort=10912
storePathRootDir=/opt/rocketmq/store/broker-master2
salve1的broker.conf文件
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
namesrvAddr=namesrv1:9876;namesrv2:9876
listenPort=10921
storePathRootDir=/opt/rocketmq/store/broker-slave1
salve2的broker.conf文件
brokerClusterName=DefaultCluster
brokerName=broker-b
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
namesrvAddr=namesrv1:9876;namesrv2:9876
listenPort=10922
storePathRootDir=/opt/rocketmq/store/broker-slave2
docker-compose.yml
用于部署双主双从的 RocketMQ 集群
version: '3.8'services:# NameServer 1namesrv1:build: .container_name: rocketmq-namesrv1environment:- ROCKETMQ_ROLE=namesrvports:- "9876:9876"networks:- rocketmq-net# NameServer 2namesrv2:build: .container_name: rocketmq-namesrv2environment:- ROCKETMQ_ROLE=namesrvports:- "9877:9876"networks:- rocketmq-net# Broker Master 1broker-master1:build: .container_name: rocketmq-broker-master1environment:- ROCKETMQ_ROLE=brokervolumes:- /tmp/rocketmq-cluster/broker-master1/conf/broker.conf:/opt/rocketmq/conf/broker.conf- /tmp/rocketmq-cluster/broker-master1/logs:/opt/rocketmq/logs- /tmp/rocketmq-cluster/broker-master1/store:/opt/rocketmq/storeports:- "10911:10911"- "10909:10909"depends_on:- namesrv1- namesrv2networks:- rocketmq-net# Broker Slave 1broker-slave1:build: .container_name: rocketmq-broker-slave1environment:- ROCKETMQ_ROLE=brokervolumes:- /tmp/rocketmq-cluster/broker-slave1/conf/broker.conf:/opt/rocketmq/conf/broker.conf- /tmp/rocketmq-cluster/broker-slave1/logs:/opt/rocketmq/logs- /tmp/rocketmq-cluster/broker-slave1/store:/opt/rocketmq/storeports:- "10921:10911"- "10919:10909"depends_on:- namesrv1- namesrv2networks:- rocketmq-net# Broker Master 2broker-master2:build: .container_name: rocketmq-broker-master2environment:- ROCKETMQ_ROLE=brokervolumes:- /tmp/rocketmq-cluster/broker-master2/conf/broker.conf:/opt/rocketmq/conf/broker.conf- /tmp/rocketmq-cluster/broker-master2/logs:/opt/rocketmq/logs- /tmp/rocketmq-cluster/broker-master2/store:/opt/rocketmq/storeports:- "10912:10911"- "10910:10909"depends_on:- namesrv1- namesrv2networks:- rocketmq-net# Broker Slave 2broker-slave2:build: .container_name: rocketmq-broker-slave2environment:- ROCKETMQ_ROLE=brokervolumes:- /tmp/rocketmq-cluster/broker-slave2/conf/broker.conf:/opt/rocketmq/conf/broker.conf- /tmp/rocketmq-cluster/broker-slave2/logs:/opt/rocketmq/logs- /tmp/rocketmq-cluster/broker-slave2/store:/opt/rocketmq/storeports:- "10922:10911"- "10920:10909"depends_on:- namesrv1- namesrv2networks:- rocketmq-net# RocketMQ Console(可选)console:image: apacherocketmq/rocketmq-console:2.0.0container_name: rocketmq-consoleports:- "8080:8080"environment:- JAVA_OPTS=-Drocketmq.namesrv.addr=namesrv1:9876;namesrv2:9876depends_on:- namesrv1- namesrv2networks:- rocketmq-netnetworks:rocketmq-net:driver: bridge
部署步骤
构建镜像:
运行以下命令构建镜像:
docker-compose build
启动集群:
docker-compose up -d
查看服务启动情况
docker ps
如果启动失败,查看对应的容器日志
rocketmq-broker-master1是对应的容器名
docker logs rocketmq-broker-master1
关闭容器并删除镜像
如果要重新构建镜像,则可以通过下面这行命令
docker-compose down