以下内容均来自个人笔记并重新梳理,如有错误欢迎指正!
如果对您有帮助,烦请点赞、关注、转发!如果您有其他想要了解的,欢迎私信联系我~
背景介绍
接上篇,笔者完成公司 MySQL 适配 ARM 64 架构工作后,马上又着手推进 RocketMQ 适配 ARM 64 架构,同样由于 Docker Hub 官方未提供 RocketMQ 的 ARM 64 镜像,遂针对 RocketMQ 4.9.4 版本自行构建 ARM 64 镜像,以下为完整过程。
过程回顾
1、获取 Dockerfile
使用 dfimage 工具获取 RocketMQ 4.9.4 官方镜像原始的 Dockerfile。
alias dfimage="docker run -v /var/run/docker.sock:/var/run/docker.sock --rm alpine/dfimage"
dfimage -sV=1.36 apache/rocketmq:4.9.4 > /root/rocketmq_rebuild/Dockerfile
这里需要说明几点:
1、RocketMQ 是使用 Java 语言开发的项目,原 Dockerfile 是直接下载编译好的 jar 包进行镜像构建,但在实测中发现已经编译好的 jar 包在执行 Topic 初始化脚本等操作时,会频繁出现报错:invokeSync call the addr[null] timeout。经排查发现是 RocketMQ 代码中默认超时时间(3000ms 或 5000ms)过短导致的,如部分 Topic 创建时的实际执行时间为 5413ms。
解决方案是:修改源码中 broker、client、openmessaging、remoting、tools 等 distribution 下的相关源码并重新编译。
2、原 Dockerfile 中替换了原有的 runserver-customize.sh、runbroker-customize.sh 文件,可以从 RocketMQ 4.9.4 的 AMD 64 镜像中获取。
附 rocketmq 项目地址:https://github.com/apache/rocketmq
2、编译源码生成 rpm
2.1、在 ARM 环境安装依赖
yum install -y maven-3.0.5-17.el7.noarch
2.2、下载源码
wget https://github.com/apache/rocketmq/archive/refs/tags/rocketmq-all-4.9.4.tar.gz
2.3、编译源码生成 jar 包
tar -xzvf rocketmq-all-4.9.4.tar.gz
cd rocketmq-rocketmq-all-4.9.4
# 修改以下源码文件中的超时时间为 10000ms:
# broker/src/main/java/org/apache/rocketmq/broker/out/BrokerOuterAPI.java
# client/src/main/java/org/apache/rocketmq/client/producer/DefaultMQProducer.java
# client/src/main/java/org/apache/rocketmq/client/impl/consumer/ConsumeMessageConcurrentlyService.java
# client/src/main/java/org/apache/rocketmq/client/trace/AsyncTraceDispatcher.java
# openmessaging/src/main/java/io/openmessaging/rocketmq/config/ClientConfig.java
# openmessaging/src/main/java/io/openmessaging/rocketmq/promise/DefaultPromise.java
# remoting/src/main/java/org/apache/rocketmq/remoting/netty/NettyRemotingClient.java
# tools/src/main/java/org/apache/rocketmq/tools/admin/DefaultMQAdminExt.java
mvn -Prelease-all -DskipTests -Dspotbugs.skip=true clean install -U
提示:执行时间较长,可以在 broker、client、openmessaging、remoting、tools 编译完成后中断即可
生成的 jar 包位于 rocketmq-rocketmq-all-4.9.4/<distribution>/target 目录下,如 rocketmq-rocketmq-all-4.9.4/broker/target
3、构建镜像
cd /root/rocketmq_rebuild && tree
目录结构如下,部分文件的详细内容见附录:
├── Dockerfile
├── jar
│ ├── rocketmq-broker-4.9.4.jar
│ ├── rocketmq-client-4.9.4.jar
│ ├── rocketmq-openmessaging-4.9.4.jar
│ ├── rocketmq-remoting-4.9.4.jar
│ └── rocketmq-tools-4.9.4.jar
├── rocketmq-rocketmq-all-4.9.4.tgz
├── runbroker-customize.sh
└── runserver-customize.sh
docker build --pull --platform=linux/arm64 -t rocketmq:4.9.4-armtest -f Dockerfile . --no-cache
附录
Dockerfile
FROM centos:centos7
LABEL org.label-schema.schema-version=1.0 org.label-schema.name=CentOS_Base_Image org.label-schema.vendor=CentOS org.label-schema.license=GPLv2 org.label-schema.build-date=20231229 org.opencontainers.image.title=CentOS_Base_Image org.opencontainers.image.vendor=CentOS org.opencontainers.image.licenses=GPL-2.0-only org.opencontainers.image.created=2023-12-29-00:00:00+00:00 rebuild_author=Sunzemin
CMD ["/bin/bash"]
RUN yum install -y java-1.8.0-openjdk-devel.aarch64 unzip gettext nmap-ncat which telnet && yum clean all -y
ARG user=rocketmq
ARG group=rocketmq
ARG uid=3000
ARG gid=3000
RUN groupadd -g ${gid} ${group} && useradd -u ${uid} -g ${gid} -m -s /bin/bash ${user}
ARG version
ENV ROCKETMQ_VERSION=4.9.4
ENV ROCKETMQ_HOME=/home/rocketmq/rocketmq-4.9.4
WORKDIR /home/rocketmq/rocketmq-4.9.4
RUN set -eux; curl -L https://archive.apache.org/dist/rocketmq/${ROCKETMQ_VERSION}/rocketmq-all-${ROCKETMQ_VERSION}-bin-release.zip -o rocketmq.zip; unzip rocketmq.zip ; mv rocketmq*/* . ; rmdir rocketmq-* ; rm rocketmq.zip ; rm ${ROCKETMQ_HOME}/lib/rocketmq-remoting-4.9.4.jar
COPY jar/ ${ROCKETMQ_HOME}/lib/
RUN chown -R ${uid}:${gid} ${ROCKETMQ_HOME}
COPY runserver-customize.sh runbroker-customize.sh /home/rocketmq/rocketmq-4.9.4/bin/
EXPOSE 9876
RUN mv ${ROCKETMQ_HOME}/bin/runserver-customize.sh ${ROCKETMQ_HOME}/bin/runserver.sh \
&& chmod a+x ${ROCKETMQ_HOME}/bin/runserver.sh \
&& chmod a+x ${ROCKETMQ_HOME}/bin/mqnamesrv
EXPOSE 10909 10911 10912
RUN mv ${ROCKETMQ_HOME}/bin/runbroker-customize.sh ${ROCKETMQ_HOME}/bin/runbroker.sh \
&& chmod a+x ${ROCKETMQ_HOME}/bin/runbroker.sh \
&& chmod a+x ${ROCKETMQ_HOME}/bin/mqbroker
RUN export JAVA_OPT=" -Duser.home=/opt"
RUN sed -i 's/${JAVA_HOME}\/jre\/lib\/ext/${JAVA_HOME}\/jre\/lib\/ext:${JAVA_HOME}\/lib\/ext/' ${ROCKETMQ_HOME}/bin/tools.sh
USER rocketmq
WORKDIR /home/rocketmq/rocketmq-4.9.4/bin