主从模式好处:
高可用,主节点可以接收消息,从节点不可以接收消息,主节点和从节点都可以读消息。如果主节点挂了,消费端还是可以从从节点消费消息。
搭建环境准本说明:
宿主机系统centos7
docker映射文件:centos7(容器的内置系统),opjdk8
我使用的是一台2G内存的云服务器,然后启动两个docker容器部署RoketMQ主从模式。也可以使用两个虚拟机搭建
master主节点搭建脚本:
#-v /usr/local/software/rocketMQ6/:/usr/local/software/表示将docker容器的/usr/local/software/挂载到宿主机/usr/local/software/rocketMQ6/目录,方便上传压缩包和后续日志查看等
#-p 19876:9876 -p 10911:10911 -p 10909:10909 映射docker容器(宿主机端口:docker容器端口)
1.docker run -itd --name rocketMQ-005 -v /usr/local/software/rocketMQ5/:/usr/local/software/ -p 19876:9876 -p 10911:10911 -p 10909:10909 new-centos:rocketMQ
#进入容器内部
2.docker exec -it rocketMQ-005 bash
3.cd /usr/local/software/
4.mkdir apche-rocketmq
#将安装包上传到宿主机/usr/local/software/rocketMQ5,进入容器内部/usr/local/software/目录发现压缩包已经存在
5.将apache-rocketmq.tar.gz解压tar -zxvf apache-rocketmq.tar.gz -C /usr/local/software/apche-rocketmq
6.建立软连接:ln -s apche-rocketmq rocketmq
6-1.创建所需要的目录(不建立默认会帮创建配置配置的文件夹)
mkdir /usr/local/software/rocketmq/store
mkdir /usr/local/software/rocketmq/store/commitlog
mkdir /usr/local/software/rocketmq/store/consumequeue
mkdir /usr/local/software/rocketmq/store/index
7.vi /usr/local/software/rocketmq/conf/2m-2s-async/broker-a.properties
#8,9,9-1步骤为设置日志保存路径
8.mkdir -p /usr/local/software/rocketmq/logs
9.cd /usr/local/software/rocketmq/conf
9-1.sed -i 's#${user.home}#/usr/local/software/rocketmq#g' *.xml
10.cd /usr/local/software/rocketmq/bin
#11,12步骤修改启动脚本,由于我是在一台内存只有2G的机器上部署主从模式,所以将broker和server的启动内存调整我256m
#修改JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn256m"
11.vi runbroker.sh
#修改JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn256m"
12.vi runserver.sh
13.cd /usr/local/software/rocketmq/bin
#在/usr/local/software/rocketmq/bin目录下启动namesrv和broker,先启动namesrv,再启动broker
14.nohup sh mqnamesrv &
#执行完14步骤后,按回车。然后执行jps 打印出NamesrvStartup说明namesrv 启动成功,可以多刷几次jps,启动需要时间
15.nohup sh mqbroker -c /usr/local/software/rocketmq/conf/2m-2s-async/broker-a.properties >/dev/null 2>&1 &
#执行完15步骤后再执行$jps命令,打印出BrokerStartup说明Broker启动成功
#16,17步骤为在/usr/local/software/rocketmq/bin目录下关闭broker和namesrv,先关闭broker再关闭namesrv
#执行完16步骤后执行jps命令,没有打印出BrokerStartup说明broker关闭成功,关闭需要时间,多刷几次jps确认关闭
16.sh mqshutdown broker
#执行完17步骤后执行jps命令,没有打印出NamesrvStartup说明namesrv关闭成功
17.sh mqshutdown namesrv
18.查看端口占用情况:netstat -tunlp(如果报:netstat: command not found,先使用yum install net-tools安装netstat包)
master主节点/usr/local/software/rocketmq/conf/2m-2s-async/broker-a.properties配置文件内容:
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=0
#RocketMQ后台管理rocketmq-console部署的机器如果跟broker不在同一网段的话就得指定外网IP
#比方说我是用idea启动rocketmq-console,如果我不设置brokerIP1=106.13.88.211就会报10909或10911端口访问不到
#10909没有配置,源代码是根据配置的10911-2得来
brokerIP1=106.13.88.211
#由于主节点broker和主节点namesrv在同一个容器下,所以主节点namesrv可以用容器内部iP:内部端口127.0.0.1:9876
#从节点和主节点不在同一容器,使用不同网段,所以使用宿主机外网IP:宿主机映射端口106.13.88.XXX:29876
namesrvAddr=127.0.0.1:9876;106.13.88.XXX:29876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=10911
deleteWhen=04
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/usr/local/software/rocketmq/store
storePathCommitLog=/usr/local/software/rocketmq/store/commitLog
storePathConsumeQueue=/usr/software/local/rocketmq/store/consumequeue
storePathIndex=/usr/local/software/rocketmq/store/index
storeCheckpoint=/usr/local/software/rocketmq/store/checkpoint
abortFile=/usr/local/software/rocketmq/store/abort
maxMessageSize=65536
brokerRole=ASYNC_MASTER
#同步刷盘还是异步刷盘
flushDiskType=ASYNC_FLUSH
slave从节点搭建脚本:
#-v /usr/local/software/rocketMQ6/:/usr/local/software/表示将docker容器的/usr/local/software/挂载到宿主机/usr/local/software/rocketMQ6/目录,方便上传压缩包和后续日志查看等
#-p 29876:9876 -p 20911:20911 -p 20909:20909 映射docker容器(宿主机端口:docker容器端口)
1.docker run -itd --name rocketMQ-006 -v /usr/local/software/rocketMQ6/:/usr/local/software/ -p 29876:9876 -p 20911:20911 -p 20909:20909 new-centos:rocketMQ
#进入容器内部
2.docker exec -it rocketMQ-006 bash
3.cd /usr/local/software/
4.mkdir apche-rocketmq
5.将apache-rocketmq.tar.gz解压tar -zxvf apache-rocketmq.tar.gz -C /usr/local/software/apche-rocketmq
6.建立软连接:ln -s apche-rocketmq rocketmq
6-1.创建所需要的目录(不建立默认会帮创建配置配置的文件夹)
mkdir /usr/local/software/rocketmq/store
mkdir /usr/local/software/rocketmq/store/commitlog
mkdir /usr/local/software/rocketmq/store/consumequeue
mkdir /usr/local/software/rocketmq/store/index
7.vi /usr/local/software/rocketmq/conf/2m-2s-async/broker-a-s.properties
#8,9,9-1步骤为设置日志保存路径
8.mkdir -p /usr/local/software/rocketmq/logs
9.cd /usr/local/software/rocketmq/conf
9-1.sed -i 's#${user.home}#/usr/local/software/rocketmq#g' *.xml
10.cd /usr/local/software/rocketmq/bin
#11,12步骤修改启动脚本,由于我是在一台内存只有2G的机器上部署主从模式,所以将broker和server的启动内存调整我256m
11.vi runbroker.sh
12.vi runserver.sh
13.cd /usr/local/software/rocketmq/bin
#在/usr/local/software/rocketmq/bin目录下启动namesrv和broker,先启动namesrv,再启动broker
14.nohup sh mqnamesrv &
15.nohup sh mqbroker -c /usr/local/software/rocketmq/conf/2m-2s-async/broker-a-s.properties >/dev/null 2>&1 &
#16,17步骤为在/usr/local/software/rocketmq/bin目录下关闭broker和namesrv,先关闭broker再关闭namesrv
16.sh mqshutdown broker
17.sh mqshutdown namesrv
18.查看端口占用情况:netstat -tunlp(如果报:netstat: command not found,先使用yum install net-tools安装netstat包)
slave从节点配置内容
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=1
brokerIP1=106.13.88.211
namesrvAddr=127.0.0.1:9876;106.13.88.211:19876
defaultTopicQueueNums=4
autoCreateTopicEnable=true
autoCreateSubscriptionGroup=true
listenPort=20911
deleteWhen=04
fileReservedTime=48
mapedFileSizeCommitLog=1073741824
mapedFileSizeConsumeQueue=300000
diskMaxUsedSpaceRatio=88
storePathRootDir=/usr/local/software/rocketmq/store
storePathCommitLog=/usr/local/software/rocketmq/store/commitLog
storePathConsumeQueue=/usr/software/local/rocketmq/store/consumequeue
storePathIndex=/usr/local/software/rocketmq/store/index
storeCheckpoint=/usr/local/software/rocketmq/store/checkpoint
abortFile=/usr/local/software/rocketmq/store/abort
maxMessageSize=65536
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
怎么下载启动rocketmq-console后台管理项目(在我另一篇文章): RockerMQ搭建
启动rocketmq-console后台管理项目:
rocketmq-console文件配置:
server.contextPath=
server.port=8080
#spring.application.index=true
spring.application.name=rocketmq-console
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
logging.config=classpath:logback.xml
#if this value is empty,use env value rocketmq.config.namesrvAddr NAMESRV_ADDR | now, you can set it in ops page.default localhost:9876
rocketmq.config.namesrvAddr=106.13.88.xxx:19876;106.13.88.xxx:29876
#if you use rocketmq version < 3.5.8, rocketmq.config.isVIPChannel should be false.default true
rocketmq.config.isVIPChannel=
#rocketmq-console's data path:dashboard/monitor
rocketmq.config.dataPath=/tmp/rocketmq-console/data
#set it false if you don't want use dashboard.default true
rocketmq.config.enableDashBoardCollect=true
#set the message track trace topic if you don't want use the default one
rocketmq.config.msgTrackTopicName=
浏览器访问rocketmq-console,如我启动端口是8080,就访问127.0.0.1:8080
总结:
这篇文章主要记录一台机器使用docker怎么部署主从模式,配置文件的时候一定要注意,想了解rocketMQ建议主官网,官网往往比博客更将详细