部署Apache RocketMQ需要准备NameServer、Broker、Proxy三个组件。它有几种部署模式:
- Local模式:Broker 和 Proxy 是同进程部署
- Cluster模式:Broker 和 Proxy 分别部署
为了部署简便,我们采用local模式。
RocketMQ部署
Dockerfile
FROM ubuntu:22.04
WORKDIR /home/rocketmq-all-in-one/
RUN apt-get update && apt-get upgrade -y
RUN apt-get install default-jdk -y
RUN apt-get install wget unzip -y
RUN wget https://dist.apache.org/repos/dist/release/rocketmq/5.2.0/rocketmq-all-5.2.0-bin-release.zip
RUN unzip rocketmq-all-5.2.0-bin-release.zip
RUN mv rocketmq-all-5.2.0-bin-release rocketmq
ENV NAMESRV_ADDR=localhost:9876
EXPOSE 9876
RUN mkdir logs
RUN echo "nohup sh ./rocketmq/bin/mqnamesrv 1>./logs/ng.log 2>./logs/ng-err.log &" >> ./start.sh
RUN echo "nohup sh ./rocketmq/bin/mqbroker -n localhost:9876 autoCreateTopicEnable=true 1>./logs/mq.log" >> ./start.sh
CMD ["/bin/bash", "-ce", "sh ./start.sh"]
打包运行
docker build --pull --rm -f "dockerfile" -t rocketmq-all-in-one:latest "."
docker container run -d -p 9876:9876 --name rocketmq-all-in-one-container rocketmq-all-in-one:latest
测试
生产者
docker exec -it rocketmq-all-in-one-container /bin/bash /home/rocketmq-all-in-one/rocketmq/bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
消费者
docker exec -it rocketmq-all-in-one-container /bin/bash /home/rocketmq-all-in-one/rocketmq/bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
带Dashboard的RocketMQ
如果我们还想在这个环境中部署图形化管理页面,还可以将rocketmq-dashboard打入镜像。
dockerfile
FROM ubuntu:22.04
WORKDIR /home/rocketmq-all-in-one/
RUN apt-get update && apt-get upgrade -y
RUN apt-get install default-jdk -y
RUN apt-get install wget unzip -y
RUN wget https://dist.apache.org/repos/dist/release/rocketmq/5.2.0/rocketmq-all-5.2.0-bin-release.zip
RUN unzip rocketmq-all-5.2.0-bin-release.zip
RUN rm rocketmq-all-5.2.0-bin-release.zip
RUN mv rocketmq-all-5.2.0-bin-release rocketmq
ENV NAMESRV_ADDR=localhost:9876
EXPOSE 9876
RUN mkdir logs
RUN echo "nohup sh ./rocketmq/bin/mqnamesrv 1>./logs/ng.log 2>./logs/ng-err.log &" >> ./start.sh
RUN echo "nohup sh ./rocketmq/bin/mqbroker -n localhost:9876 autoCreateTopicEnable=true 1>./logs/mq.log &" >> ./start.sh
RUN apt-get install git -y
RUN git clone https://github.com/apache/rocketmq-dashboard.git
RUN apt-get install maven -y
RUN cd rocketmq-dashboard && mvn clean package -Dmaven.test.skip=true
RUN echo "nohup java -jar ./rocketmq-dashboard/target/rocketmq-dashboard-1.0.1-SNAPSHOT.jar 1>./logs/dashboard.log" >> ./start.sh
EXPOSE 8080
CMD ["/bin/bash", "-ce", "sh ./start.sh"]
效果
参考资料
- https://rocketmq.apache.org/docs/quickStart/01quickstart/
- https://rocketmq.apache.org/docs/deploymentOperations/01deploy
- https://www.cnblogs.com/jing99/p/13166602.html