RocketMQ:Docker构建部署RocketMQ:4.9.2
1. 构建Namesrv脚本
1.1 创建Dockerfile文件(Java8版本RocketMQ4.8.0先看下方3.3内容)
编辑Dockerfile文件,vim Dockerfile-namesrv
内容:
FROM java:8
ENV ROCKETMQ_VERSION 4.9.2
ENV NAMESRV_HOME="/home/rocketmq/namesrv-${ROCKETMQ_VERSION}" JAVA_OPT="-Duser.home=/home/rocketmq"
WORKDIR ${NAMESRV_HOME}
RUN mkdir -p /home/rocketmq/logs /home/rocketmq/store
RUN curl https://dist.apache.org/repos/dist/release/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip -o rocketmq.zip \
&& unzip rocketmq.zip \
&& rm -f rocketmq.zip \
&& mv rocketmq-${ROCKETMQ_VERSION}/* ./ \
&& rm -rf rocketmq-${ROCKETMQ_VERSION} \
&& cd ${NAMESRV_HOME}/bin \
&& sed -i 's#-Xms[0-9]\+[gm]#-Xms512m#g' runserver.sh \
&& sed -i 's#-Xmx[0-9]\+[gm]#-Xms512m#g' runserver.sh \
&& sed -i 's#-Xmn[0-9]\+[gm]#-Xms128m#g' runserver.sh \
&& sed -i 's#-XX:MetaspaceSize=[0-9]\+[gm]#-XX:MetaspaceSize=128m#' runserver.sh \
&& sed -i 's#-XX:MaxMetaspaceSize=[0-9]\+[gm]#-XX:MaxMetaspaceSize=320m#' runserver.sh \
&& chmod +x ./mqnamesrv
CMD sh ${NAMESRV_HOME}/bin/mqnamesrv
EXPOSE 9876
VOLUME ["/home/rocketmq/logs", "/home/rocketmq/store"]
1.2. 构建namesrv镜像脚本
编辑脚本,vim build-namesrv.sh
内容:
#!/bin/bash
docker build -f Dockerfile-namesrv -t rmq-namesrv:4.9.2 .
1.3. 启动容器脚本
编辑启动脚本,vim start-namesrv.sh
内容:
#!/bin/bash
docker run -d \
--name rmq-namesrv \
--network rmq-bridge \
--network-alias rmq-namesrv \
-e "JAVA_OPT_EXT=-Xms512M -Xmx512M -Xmn128M" \
-p 9876:9876 \
-v /home/data/rocketmq/logs:/home/rocketmq/logs \
-v /home/data/rocketmq/store:/home/rocketmq/store \
rmq-namesrv:4.9.2
2. 构建Broker脚本
2.1. 创建Dockerfile文件
编辑Dockerfile文件,vim Dockerfile-broker
内容:
FROM java:8
ENV ROCKETMQ_VERSION 4.9.2
ENV BROKER_HOME="/home/rocketmq/broker-${ROCKETMQ_VERSION}" JAVA_OPT="-Duser.home=/home/rocketmq"
WORKDIR ${BROKER_HOME}
RUN mkdir -p /home/rocketmq/logs /home/rocketmq/store
RUN curl https://dist.apache.org/repos/dist/release/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip -o rocketmq.zip \
&& unzip rocketmq.zip \
&& rm -f rocketmq.zip \
&& mv rocketmq-${ROCKETMQ_VERSION}/* ./ \
&& rm -rf rocketmq-${ROCKETMQ_VERSION} \
&& cd ${BROKER_HOME}/bin \
&& sed -i 's#-Xms[0-9]\+[gm]#-Xms512m#g' runbroker.sh \
&& sed -i 's#-Xmx[0-9]\+[gm]#-Xms512m#g' runbroker.sh \
&& sed -i 's#-Xmn[0-9]\+[gm]#-Xms128m#g' runbroker.sh \
&& chmod +x ./mqbroker
EXPOSE 10909 10911 10912
VOLUME ["/home/rocketmq/logs", "/home/rocketmq/store"]
CMD sh ${BROKER_HOME}/bin/mqbroker -c /home/rocketmq/conf/broker.conf -n rocketmq-namesrv:9876 autoCreateTopicEnable=true
2.2. 构建broker镜像脚本
编辑脚本,vim build-broker.sh
内容:
#!/bin/bash
docker build -f Dockerfile-broker -t rmq-broker:4.9.2 .
2.3. 启动容器脚本
编辑启动脚本,vim start-broker.sh
内容:
#!/bin/bash
docker run -d \
--name rmq-broker \
--network rmq-bridge \
--network-alias rmq-broker \
-e "JAVA_OPT_EXT=-Xmx512M -Xmx512M -Xmn128M" \
-p 10909:10909 \
-p 10911:10911 \
-p 10912:10912 \
-v /home/data/rocketmq/broker/broker.conf:/home/rocketmq/conf/broker.conf \
-v /home/data/rocketmq/logs:/home/rocketmq/logs \
-v /home/data/rocketmq/store:/home/rocketmq/store \
rmq-broker:4.9.2 \
sh bin/mqbroker -c /home/rocketmq/conf/broker.conf -n rmq-namesrv:9876 autoCreateTopicEnable=true
3. 配置启动RocketMQ
3.1. 创建RocketMQ存储目录和日志目录
mkdir -p /home/data/rocketmq/{logs,store,broker} && touch /home/data/rocketmq/broker/broker.conf
3.2. 创建网桥
docker network create rmq-bridge
3.3. 构建namesrv镜像, 启动namesrv容器
对于脚本增加执行权限, chmod +x *.sh
先构建namesrv镜像,./build-namesrv.sh
; 启动namesrv容器(必须先启动namesrv,然后启动broker), ./start-namesrv.sh
PS: 网上说Java8和RocketMQ4.8.0启动namesrv容器会报错,我用的Java11和RocketMQ4.9.2没有报错。。我用的应该是Java8和RocketMQ4.9.2(因为Dockerfile的第一行From基于Java8):
可以先试试能不能启动,然后再修改
报错信息:
ubuntu@ubuntu:/usr/local/rocketmq$ /usr/local/rocketmq/bin/runserver.sh: 70: [[: not found
Unrecognized option: -Xlog:gc*:file=/dev/shm/rmq_srv_gc_%p_%t.log:time,tags:filecount=5,filesize=30M
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
报错解释:
Java版本使用的是java8,
由于在linux环境(Ubuntu发行版,其它发行版未知)下, 直接启动mqnamesrv会报错,
具体原因是sunserver.sh第69行中判定java版本的脚本有问题, 导致判定java版本为9以上,进而导致程序进入第70行if else结构中的else分支中,java GC参数无法识别而报错.
这里使用sed将第70行,第75-79行注释掉.
解决方案:
在Dockerfile-namesrv文件中增加&& rm -rf rocketmq-${ROCKETMQ_VERSION}
&& cd ${NAMESRV_HOME}/bin
#下边新增两行, 脚本中注释对应代码
&& sed -i '70s/^/#/' runserver.sh
&& sed -i '75,79s/^/#/' runserver.sh
&& sed -i 's#-Xms[0-9]+[gm]#-Xms512m#g' runserver.sh
&& sed -i 's#-Xmx[0-9]+[gm]#-Xms512m#g' runserver.sh \
3.4. 构建broker镜像, 启动broker容器
然后构建broker镜像,./build-broker.sh
; 启动broker容器(必须先启动namesrv,然后启动broker), ./start-broker.sh
3.5. 查看
可以通过docker logs <容器名>
查看容器是否报错。
接下来为了便于操作,可以安装RocketMQ-Console
4. 构建启动rocketmq-console
4.1. 下载编译rocketmq-console
下载地址https://gitee.com/ralph81/rocketmq-console/repository/archive/master.zip
下载文件,修改配置文件(src/main/resources/application.properties)
# 注释这行,不然会报错-端口占用
# server.address=IP
# 修改访问上下文,这个也不需要
# server.contextPath=/rocketmq
# 修改访问端口号,默认8080 这个端口太常见了
server.port=8099
# name server地址
rocketmq.config.namesrvAddr=宿主机IP:9876
使用maven打包,上传编译后的jar包
4.2. 编辑Dockerfile-console文件
FROM java:8
WORKDIR /usr/local/rocketmq-console
VOLUME /tmp
COPY rocketmq-console-ng-2.0.0.jar ./rocketmq-console.jar
ENV JAVA_OPTS ""
ENV PORT ""
ENV NAMESRV_ADDR ""
EXPOSE 8099
ENTRYPOINT ["sh","-c","java $JAVA_OPTS -jar rocketmq-console.jar --server.port=$PORT --rocketmq.config.namesrvAddr=$NAMESRV_ADDR"]
4.3. 构建broker镜像脚本
编辑脚本, vim build-console.sh
内容:
#!/bin/bash
docker build -f Dockerfile-console -t rmq-console:2.0.0 .
4.4. 构建启动容器脚本
编辑脚本,vim start-console.sh
内容:
#!/bin/bash
docker run -d \
-p 8099:8099 \
--name rmq-console \
--network rmq-bridge \
--network-alias rmq-console \
-e "JAVA_OPTS=-Xms512m -Xmx512m -Xmn128m" \
-e "PORT=8099" \
-e "NAMESRV_ADDR=rmq-namesrv:9876" \
rmq-console:2.0.0
5. 浏览器登陆
浏览器登录 http://宿主机IP:8099
PS:账号密码在src/main/resources/users.properties中配置,管理员是admin/admin。