前言
本节内容延续前期博客内容,使用docker-compose一键式搭建docker环境下的RocketMQ集群及其管理工具,实现rocketmq在docker环境容器外部访问。特别说明,内网版的RocketMQ集群只能实现docker环境内部访问,并不适用于测试使用,本节内容通过搭建外网版的RocketMQ集群,实现docker容器中和宿主机都可以使用的消息中间键集群。
Docker环境下使用docker-compose一键式搭建RocketMQ(4.5.0版本)集群及其管理工具(内网版)_北溟溟的博客-CSDN博客_docker-compose rocketmq
正文
-
创建docker-compose的rocketmq.yml启动脚本
version: "3.3"
services:
rocketmq-namesrv-1:
image: rocketmqinc/rocketmq-namesrv:4.5.0-alpine-uid
restart: always
container_name: rocketmq-namesrv-1
ports:
- "9876:9876"
volumes:
- /usr/local/bin/atp/rocketmq/logs/nameserver1:/home/rocketmq/logs/rocketmqlogs
command: sh mqnamesrv
networks:
- rocketmq
rocketmq-namesrv-2:
image: rocketmqinc/rocketmq-namesrv:4.5.0-alpine-uid
restart: always
container_name: rocketmq-namesrv-2
ports:
- "9877:9876"
volumes:
- /usr/local/bin/atp/rocketmq/logs/nameserver2:/home/rocketmq/logs/rocketmqlogs
command: sh mqnamesrv
networks:
- rocketmq
rocketmq-broker-1:
image: rocketmqinc/rocketmq-broker:4.5.0-alpine-uid
restart: always
container_name: rocketmq-broker-1
ports:
- "10911:10911"
volumes:
- /usr/local/bin/atp/rocketmq/logs/broker1:/home/rocketmq/logs/rocketmqlogs
- /usr/local/bin/atp/rocketmq/conf/broker1/broker.conf:/home/rocketmq/rocketmq-4.5.0/conf/broker.conf
environment:
TZ: Asia/Shanghai
NAMESRV_ADDR: "rocketmq-namesrv-1:9876;rocketmq-namesrv-2:9876"
JAVA_OPTS: "-Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
command: sh mqbroker -c /home/rocketmq/rocketmq-4.5.0/conf/broker.conf autoCreateTopicEnable=true &
depends_on:
- rocketmq-namesrv-1
- rocketmq-namesrv-2
networks:
- rocketmq
rocketmq-broker-2:
image: rocketmqinc/rocketmq-broker:4.5.0-alpine-uid
restart: always
container_name: rocketmq-broker-2
ports:
- "20911:20911"
volumes:
- /usr/local/bin/atp/rocketmq/logs/broker2:/home/rocketmq/logs/rocketmqlogs
- /usr/local/bin/atp/rocketmq/conf/broker2/broker.conf:/home/rocketmq/rocketmq-4.5.0/conf/broker.conf
environment:
TZ: Asia/Shanghai
NAMESRV_ADDR: "rocketmq-namesrv-1:9876;rocketmq-namesrv-2:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
command: sh mqbroker -c /home/rocketmq/rocketmq-4.5.0/conf/broker.conf autoCreateTopicEnable=true &
depends_on:
- rocketmq-namesrv-1
- rocketmq-namesrv-2
networks:
- rocketmq
rocketmq-broker-3:
image: rocketmqinc/rocketmq-broker:4.5.0-alpine-uid
restart: always
container_name: rocketmq-broker-3
ports:
- "30911:30911"
volumes:
- /usr/local/bin/atp/rocketmq/logs/broker3:/home/rocketmq/logs/rocketmqlogs
- /usr/local/bin/atp/rocketmq/conf/broker3/broker.conf:/home/rocketmq/rocketmq-4.5.0/conf/broker.conf
environment:
TZ: Asia/Shanghai
NAMESRV_ADDR: "rocketmq-namesrv-1:9876;rocketmq-namesrv-2:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m"
command: sh mqbroker -c /home/rocketmq/rocketmq-4.5.0/conf/broker.conf autoCreateTopicEnable=true &
depends_on:
- rocketmq-namesrv-1
- rocketmq-namesrv-2
networks:
- rocketmq
rocketmq-console:
image: styletang/rocketmq-console-ng:1.0.0
restart: always
container_name: rocketmq-console
ports:
- "9090:8080"
environment:
JAVA_OPTS: -Drocketmq.namesrv.addr=rocketmq-namesrv-1:9876;rocketmq-namesrv-2:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false
networks:
- rocketmq
depends_on:
- rocketmq-broker-1
- rocketmq-broker-2
- rocketmq-broker-3
networks:
rocketmq:
driver: bridge
PS: 特别说明,这里区别于内网版,为了避免broker之间外网访问端口冲突,内网的端口broker2使用20911,broker3使用30911
-
创建broker服务器配置文件broker.conf
①创建映射目录
mkdir -p /usr/local/bin/atp/rocketmq/conf/broker1
mkdir -p /usr/local/bin/atp/rocketmq/conf/broker2
mkdir -p /usr/local/bin/atp/rocketmq/conf/broker3
②使用vi编辑器创建broker配置文件broker.conf,完成配置文件的映射
broker1服务器配置
#broker1服务器配置 brokerClusterName = DefaultCluster #broker名称,master和slave使用相同的名称,表明他们的主从关系 brokerName = rocketmq-broker-1 #0表示master,大于0表示不同的slave brokerId = 0 #表示几点做消息删除动作,默认是凌晨4点 deleteWhen = 04 #在磁盘上保留消息的时长,单位是小时 fileReservedTime = 48 #sync_master,async_master,slave;同步和异步表示master和slave之间同步数据的机制 brokerRole = ASYNC_MASTER #刷盘策略,取值为:async_flush,sync_flush表示同步刷盘和异步刷盘;sync_flush消息写入磁盘后才返回成功状态,async_flush不需要; flushDiskType = ASYNC_FLUSH #broker外网访问的ip地址 brokerIP1 = 192.168.56.10 #broker服务端口 listenPort = 10911
broker2服务器配置
#broker2服务器配置 brokerClusterName = DefaultCluster #broker名称,master和slave使用相同的名称,表明他们的主从关系 brokerName = rocketmq-broker-2 #0表示master,大于0表示不同的slave brokerId = 0 #表示几点做消息删除动作,默认是凌晨4点 deleteWhen = 04 #在磁盘上保留消息的时长,单位是小时 fileReservedTime = 48 #sync_master,async_master,slave;同步和异步表示master和slave之间同步数据的机制 brokerRole = ASYNC_MASTER #刷盘策略,取值为:async_flush,sync_flush表示同步刷盘和异步刷盘;sync_flush消息写入磁盘后才返回成功状态,async_flush不需要; flushDiskType = ASYNC_FLUSH #broker外网访问的ip地址 brokerIP1 = 192.168.56.10 #broker服务端口 listenPort = 20911
broker3服务器配置
#broker2服务器配置 brokerClusterName = DefaultCluster #broker名称,master和slave使用相同的名称,表明他们的主从关系 brokerName = rocketmq-broker-3 #0表示master,大于0表示不同的slave brokerId = 0 #表示几点做消息删除动作,默认是凌晨4点 deleteWhen = 04 #在磁盘上保留消息的时长,单位是小时 fileReservedTime = 48 #sync_master,async_master,slave;同步和异步表示master和slave之间同步数据的机制 brokerRole = ASYNC_MASTER #刷盘策略,取值为:async_flush,sync_flush表示同步刷盘和异步刷盘;sync_flush消息写入磁盘后才返回成功状态,async_flush不需要; flushDiskType = ASYNC_FLUSH #broker外网访问的ip地址 brokerIP1 = 192.168.56.10 #broker服务端口 listenPort = 30911
PS: 其它配置同上,注意修改broker的名称和端口号,brokerIP1为宿主机的IP
-
使用docker-compose启动RocketMQ集群
命令:
docker-compose -f rocketmq.yml -p rocketmq up -d
-
访问RocketMQ控制台,验证集群安装状态
结语
至此,关于Docker环境下使用docker-compose一键式搭建RocketMQ(4.5.0版本)集群及其管理工具(外网版)到这里就结束了,我们下期见。。。