本文介绍如何使用docker-compose搭建rocketmq的集群环境
1. 安装docker-compose
1.1 下载docker-compose
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
1.2 增加可执行权限
sudo chmod +x /usr/local/bin/docker-compose
1.3 生成软链接
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
1.4 验证docker-compose是否安装成功
docker-compose version
2. 启动rocketmq
2.1 docker-compose.yml
docker-compose.yml在rocketmq-docker/stages/4.7.1/templates/docker-compose目录下面
version: '2' services: #Service for nameserver namesrv: image: apacherocketmq/rocketmq:4.7.1 container_name: rmqnamesrv ports: - 9876:9876 volumes: - ./data/namesrv/logs:/home/rocketmq/logs command: sh mqnamesrv #Service for broker broker: image: apacherocketmq/rocketmq:4.7.1 container_name: rmqbroker links: - namesrv ports: - 10909:10909 - 10911:10911 - 10912:10912 environment: - NAMESRV_ADDR=namesrv:9876 volumes: - ./data/broker/logs:/home/rocketmq/logs - ./data/broker/store:/home/rocketmq/store - ./data/broker/conf/broker.conf:/opt/rocketmq-4.7.1/conf/broker.conf command: sh mqbroker -c /opt/rocketmq-4.7.1/conf/broker.conf #Service for another broker -- broker1 broker1: image: apacherocketmq/rocketmq:4.7.1 container_name: rmqbroker-b links: - namesrv ports: - 10929:10909 - 10931:10911 - 10932:10912 environment: - NAMESRV_ADDR=namesrv:9876 volumes: - ./data1/broker/logs:/home/rocketmq/logs - ./data1/broker/store:/home/rocketmq/store - ./data1/broker/conf/broker.conf:/opt/rocketmq-4.7.1/conf/broker.conf command: sh mqbroker -c /opt/rocketmq-4.7.1/conf/broker.conf
可以看到docker-compose启动了两个broker分片作为一个broker集群,连接到一个nameserver,我们看下broker-a和broker-b的配置
broker1:/data1/broker/conf/broker.conf
brokerClusterName = DefaultCluster brokerName = broker-b brokerId = 0 deleteWhen = 04 fileReservedTime = 48 brokerRole = ASYNC_MASTER flushDiskType = ASYNC_FLUSH
broker:/data/broker/conf/broker.conf
brokerClusterName = DefaultCluster brokerName = broker-a brokerId = 0 deleteWhen = 04 fileReservedTime = 48 brokerRole = ASYNC_MASTER flushDiskType = ASYNC_FLUSH
2.2 docker-compose启动rocketmq:4.7.1
#进入对应目录 cd rocketmq-docker/stages/4.7.1/template #执行docker-compose脚本(如果build的rocketmq是centos,则直接执行成功) sh play-docker-compose.sh
注意:如果你build的是rocketmq的alpine版本,这里可能会失败。
2.2 解决方法
tag一个新的rocketmq的镜像,执行如下:
docker tag apacherocketmq/rocketmq:4.7.1-alpine apacherocketmq/rocketmq:4.7.1
2.3 重新执行
sh play-docker-compose.sh2.4 docker-compose相关命令
2.4.1 查看docker-compose启动的容器
cd rocketmq-docker/stages/4.7.1/templates/docker-compose docker-compose ps
2.4.2 停止docker-compose启动的容器
cd rocketmq-docker/stages/4.7.1/templates/docker-compose docker-compose stop
2.4.3 启动docker-compose定义的容器
cd rocketmq-docker/stages/4.7.1/templates/docker-compose docker-compose up
2.4.4 后台启动docker-compose定义的容器
cd rocketmq-docker/stages/4.7.1/templates/docker-compose
#-d表示后台启动
docker-compose up -d
2.4.5 强制删除docker-compose启动的容器
cd rocketmq-docker/stages/4.7.1/templates/docker-compose docker-compose rm -fs
3. 启动控制台
启动
docker run --net docker-compose_default -e "NAMESRV_ADDR=namesrv:9876" -e "JAVA_OPTS=-Dcom.rocketmq.sendMessageWithVIPChannel=false" -p 8090:8080 --name rmqconsole -t styletang/rocketmq-console-ng
查看
浏览器打开:http://{你机器的ip}:8090
如图可以看到,docker-compose默认启动了两个broker的集群(master模式)
博主:测试生财(一个不为996而996的测开码农)
座右铭:专注测试开发与自动化运维,努力读书思考写作,为内卷的人生奠定财务自由。
内容范畴:技术提升,职场杂谈,事业发展,阅读写作,投资理财,健康人生。
csdn:https://blog.csdn.net/ccgshigao
博客园:https://www.cnblogs.com/qa-freeroad/
51cto:https://blog.51cto.com/14900374
微信公众号:测试生财(定期分享独家内容和资源)