1、主机规划
主机 | 节点 | 地址 |
---|---|---|
主机A | namesrv | 192.168.2.228:9876 |
主机A | broker-a | 192.168.2.228:10911 |
主机A | broker-b | 192.168.2.228:11911 |
主机B | namesrv | 192.168.2.229:9876 |
主机B | broker-c | 192.168.2.229:10911 |
主机B | broker-d | 192.168.2.229:11911 |
2、两台主机都需要执行,创建mq需要的文件
mkdir -p /mydata/rocketmq/data/namesrv/logs
sudo chmod 777 /mydata/rocketmq/data/namesrv/logs
mkdir -p /mydata/rocketmq/data/namesrv/store
chmod 777 /mydata/rocketmq/data/namesrv/store
mkdir -p /mydata/rocketmq/data/broker-a/logs
mkdir -p /mydata/rocketmq/data/broker-a/store
mkdir -p /mydata/rocketmq/etc/broker-a
chmod 777 /mydata/rocketmq/data/broker-a/logs
chmod 777 /mydata/rocketmq/data/broker-a/store
chmod 777 /mydata/rocketmq/etc/broker-a
mkdir -p /mydata/rocketmq/data/broker-b/logs
mkdir -p /mydata/rocketmq/data/broker-b/store
mkdir -p /mydata/rocketmq/etc/broker-b
chmod 777 /mydata/rocketmq/data/broker-b/logs
chmod 777 /mydata/rocketmq/data/broker-b/store
chmod 777 /mydata/rocketmq/etc/broker-b
3、A服务器部署broker-a
cd /mydata/rocketmq/data
vi broker-a.conf
#输入以下内容
brokerClusterName = rocketmq-cluster
brokerName = broker-a
# 0表示broker master 非0表示 broker slaver
brokerId = 0
# brokerIP1 当前broker监听的IP
brokerIP1 = 192.168.2.228
# brokerIP2 存在broker主从时,在broker主节点上配置了brokerIP2的话,broker从节点会连接主节点配置的brokerIP2来同步
brokerIP2 = 192.168.2.228
deleteWhen = 04
fileReservedTime = 48
namesrvAddr = 192.168.2.228:9876;192.168.2.229:9876
# 是否能够自动创建topic
autoCreateTopicEnable = true
#Broker 对外服务的监听端口, 默认10911
# listenPort = 10911
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
4、A服务器部署broker-b
cd /mydata/rocketmq/data
vi broker-b-s.conf
#输入以下内容
brokerClusterName = rocketmq-cluster
brokerName = broker-b
brokerId = 1
brokerIP1 = 192.168.2.228
deleteWhen = 04
fileReservedTime = 48
namesrvAddr = 192.168.2.228:9876;192.168.2.229:9876
listenPort = 11911
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SLAVE
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
5、主机B部署broker-c
cd /mydata/rocketmq/data
vi broker-a.conf
#输入以下内容
brokerClusterName = rocketmq-cluster
brokerName = broker-c
# 0表示broker master 非0表示 broker slaver
brokerId = 0
# brokerIP1 当前broker监听的IP
brokerIP1 = 192.168.2.229
# brokerIP2 存在broker主从时,在broker主节点上配置了brokerIP2的话,broker从节点会连接主节点配置的brokerIP2来同步
brokerIP2 = 192.168.2.229
deleteWhen = 04
fileReservedTime = 48
namesrvAddr = 192.168.2.228:9876;192.168.2.229:9876
# 是否能够自动创建topic
autoCreateTopicEnable = true
#Broker 对外服务的监听端口, 默认10911
# listenPort = 10911
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
6、主机B部署broker-d
cd /mydata/rocketmq/data
vi broker-b-s.conf
#输入以下内容
brokerClusterName = rocketmq-cluster
brokerName = broker-d
brokerId = 1
brokerIP1 = 192.168.2.229
deleteWhen = 04
fileReservedTime = 48
namesrvAddr = 192.168.2.228:9876;192.168.2.229:9876
listenPort = 11911
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=SLAVE
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
7、下载dockercompose文件,如果主机没有网络可以使用有网络的电脑下载上传到/usr/local/bin目录
#下载到/usr/local/bin目录
curl -SL https://github.com/docker/compose/releases/download/v2.7.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
8、两台主机都编辑docker-compose.yml
cd /usr/local/bin
vi docker-compose.yml
#输入以下内容
version: '4.7'
services:
rmqnamesrv-a:
image: apache/rocketmq:4.7.1
restart: always
container_name: rmqnamesrv-a
ports:
- 9876:9876
volumes:
- /mydata/rocketmq/data/namesrv/logs:/opt/logs/rocketmqlogs
- /mydata/rocketmq/data/namesrv/store:/opt/store
environment:
- JAVA_OPT_EXT=-Duser.home=/opt -Xms512m -Xmx512m -Xmn300m
command: sh mqnamesrv
rmqbroker-a:
image: apache/rocketmq:4.7.1
container_name: rmqbroker-a
ports:
- 10911:10911
- 10909:10909
- 10912:10912
volumes:
- /mydata/rocketmq/data/broker-a/logs:/home/rocketmq/logs
- /mydata/rocketmq/data/broker-a/store:/home/rocketmq/store
- /mydata/rocketmq/data/broker-a.conf:/home/rocketmq/rocketmq-4.7.1/conf/broker.conf
environment:
- TZ=Asia/Shanghai
- NAMESRV_ADDR=rmqnamesrv-a:9876
- JAVA_OPTS=-Duser.home=/opt
- JAVA_OPT_EXT=-server -Xms2048m -Xmx4096m -Xmn2048m
command: sh mqbroker -c /home/rocketmq/rocketmq-4.7.1/conf/broker.conf autoCreateTopicEnable=true &
links:
- rmqnamesrv-a:rmqnamesrv-a
rmqbroker-b-s:
image: apache/rocketmq:4.7.1
container_name: rmqbroker-b-s
ports:
- 11911:11911
- 11909:11909
- 11912:11912
volumes:
- /mydata/rocketmq/data/broker-b/logs:/home/rocketmq/logs
- /mydata/rocketmq/data/broker-b/store:/home/rocketmq/store
- /mydata/rocketmq/data/broker-b-s.conf:/home/rocketmq/rocketmq-4.7.1/conf/broker.conf
environment:
- TZ=Asia/Shanghai
- NAMESRV_ADDR=rmqnamesrv-a:9876
- JAVA_OPTS=-Duser.home=/opt
- JAVA_OPT_EXT=-server -Xms2048m -Xmx4096m -Xmn2048m
command: sh mqbroker -c /home/rocketmq/rocketmq-4.7.1/conf/broker.conf autoCreateTopicEnable=true &
links:
- rmqnamesrv-a:rmqnamesrv-a
9、启动部署
#部署启动
docker compose up -d
#卸载命令,不要执行
docker compose down
10、自定义dashbord密码
#部署启动
mkdir -p /apps/rocketmq-dashboard/data
vi users.properties
#输入以下内容
#超级管理员
admin=admin,1
#普通用户
xkc=xkc
11、dashbord启动
docker run -d -it --name dashboard -p 18081:8080 -e "JAVA_OPTS=-Drocketmq.namesrv.addr=192.168.2.228:9876;192.168.2.229:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false -Drocketmq.config.loginRequired=true" -v /apps/rocketmq-dashboard/data:/tmp/rocketmq-console/data apacherocketmq/rocketmq-dashboard:latest /bin/bash
12、自定义的nginx负载均衡配置,需要配置nginx.conf文件,网上有很多坑说配置http模块,实际上配置http模块没法对mq负载均衡,正确需要在stream模块追加以下配置,如下:
stream {
upstream mqserver {
server 192.168.2.227:9876;
server 192.168.2.228:9876;
}
server {
listen 888;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass mqserver;
}
}
13、一键式部署的脚本,我自己写了一个,但暂时不提供,有需要的可以联系博主