需要先安装jdk环境,参照linux配置jdk
4.9.3版本:官网地址,也可以通过修改版本号获取自己想要的版本;百度网盘下载,提取码:1km8
上传安装包到服务器,解压缩unzip rocketmq-all-4.9.3-bin-release.zip
,进入解压缩的到的目录,pwd
得到完整路径,编辑环境变量vi /etc/profile
,添加如下内容
#rocketMQ配置
export ROCKETMQ_HOME=/home/ysgs/tools/rocketmq/rocketmq-4.9.3
export PATH=$ROCKETMQ_HOME/bin:$PATH
修改完成后,source /etc/profile
让环境变量生效
RocketMQ需要启动两个服务,nameserver(相当于邮局,默认JVM使用内存4G)和broker(相当于信箱,默认JVM使用内存8G),因此需要先修改内存配置,切换到bin目录下,找到runserver.sh脚本,找到如下内容
修改为下图配置
找到runbroker.sh脚本,修改如下内容:
修改为JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m"
先启动nameserver,进入bin目录,启动nameservernohup sh mqnamesrv &
,查看日志,tail -f nohup.out
发现如下图,启动成功
启动broker,在conf目录下有个broker.conf的配置文件,是broker的一些相关配置,例如开启自动创建topicautoCreateTopicEnable=true
等等,这些在官网可以具体学习。启动broker并链接到nameservernohup sh mqbroker -n localhost:9876 &
,查看日志,启动成功
先关闭broker再关闭nameserver,命令如下,为了尽可能保持集群上namesrv的一致性
# 1.关闭Broker
sh bin/mqshutdown broker
# 2、关闭NameServer
sh bin/mqshutdown namesrv
git地址:https://github.com/apache/rocketmq-dashboard
,将代码拉到本地,mvn clean package -Dmaven.test.skip=true
运行打包得到jar包
百度网盘下载控制台 提取码:gs1d
上传jar包到服务器,nohup java -jar rocketmq-dashboard-1.0.1-SNAPSHOT.jar --server.port=8881 --rocketmq.config.namesrvAddr=localhost:9876 &
运行jar包,访问控制台页面192.168.136.128:8881,如果访问失败,记得关闭虚拟机的防火墙
由于只做了一台虚拟机,因此通过更改端口的方式实现伪集群的部署,实际上真正的集群只是把端口变化体现在主机ip上。
模拟A和B两台主机,在rocket的安装目录conf下,给了三份示例,我们在2m-2s-sync(双主双从同步复制异步刷盘)文件夹下修改,A主机master节点配置如下:
###############################################################################
#伪集群A主机master配置
brokerIP1=192.168.136.128
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
listenPort=10911
storePathRootDir=/home/ysgs/store/a-m
namesrvAddr=192.168.136.128:9876;192.168.136.128:9875
A主机的slave节点配置如下
#伪集群A主机slave配置
brokerIP2=192.168.136.128
brokerClusterName=DefaultCluster
brokerName=broker-a
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=10811
storePathRootDir=/home/ysgs/store/a-s
namesrvAddr=192.168.136.128:9876;192.168.136.128:9875
B主机的master节点配置如下
brokerIP3=192.168.136.128
brokerClusterName=DefaultCluster
brokerName=broker-b
brokerId=0
deleteWhen=04
fileReservedTime=48
brokerRole=SYNC_MASTER
flushDiskType=ASYNC_FLUSH
listenPort=10711
storePathRootDir=/home/ysgs/store/b-m
namesrvAddr=192.168.136.128:9876;192.168.136.128:9875
B主机的slave节点配置如下
brokerIP4=192.168.136.128
brokerClusterName=DefaultCluster
brokerName=broker-b
brokerId=1
deleteWhen=04
fileReservedTime=48
brokerRole=SYNC_SLAVE
flushDiskType=ASYNC_FLUSH
listenPort=10611
storePathRootDir=/home/ysgs/store/b-s
namesrvAddr=192.168.136.128:9876;192.168.136.128:9875
namesrv配置文件,namesrv.properties文件,指定启动端口
listenPort=9876
配置详解
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=0
#nameServer地址,分号分割
namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10911
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/usr/local/rocketmq/store
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq/store/consumequeue
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store/index
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store/checkpoint
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store/abort
#限制的消息大小
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
启动namesrv脚本
nohup sh /home/ysgs/tools/rocketmq/rocketmq-4.9.3/bin/mqnamesrv -c /home/ysgs/tools/rocketmq/rocketmq-4.9.3/conf/namesrv1.properties > /home/ysgs/tools/rocketmq/rocketmq-4.9.3/namesrv1.log 2>&1 &
nohup sh /home/ysgs/tools/rocketmq/rocketmq-4.9.3/bin/mqnamesrv -c /home/ysgs/tools/rocketmq/rocketmq-4.9.3/conf/namesrv2.properties > /home/ysgs/tools/rocketmq/rocketmq-4.9.3/namesrv2.log 2>&1 &
启动broker脚本
nohup sh /home/ysgs/tools/rocketmq/rocketmq-4.9.3/bin/mqbroker -c /home/ysgs/tools/rocketmq/rocketmq-4.9.3/conf/2m-2s-sync/broker-a.properties > /home/ysgs/tools/rocketmq/rocketmq-4.9.3/brokerA-m.log 2>&1 &
nohup sh /home/ysgs/tools/rocketmq/rocketmq-4.9.3/bin/mqbroker -c /home/ysgs/tools/rocketmq/rocketmq-4.9.3/conf/2m-2s-sync/broker-b-s.properties > /home/ysgs/tools/rocketmq/rocketmq-4.9.3/brokerB-s.log 2>&1 &
nohup sh /home/ysgs/tools/rocketmq/rocketmq-4.9.3/bin/mqbroker -c /home/ysgs/tools/rocketmq/rocketmq-4.9.3/conf/2m-2s-sync/broker-b.properties > /home/ysgs/tools/rocketmq/rocketmq-4.9.3/brokerB-m.log 2>&1 &
nohup sh /home/ysgs/tools/rocketmq/rocketmq-4.9.3/bin/mqbroker -c /home/ysgs/tools/rocketmq/rocketmq-4.9.3/conf/2m-2s-sync/broker-a-s.properties > /home/ysgs/tools/rocketmq/rocketmq-4.9.3/brokerA-s.log 2>&1 &
启动mq控制台
nohup java -jar rocketmq-dashboard-1.0.1-SNAPSHOT.jar --server.port=8881 --rocketmq.config.namesrvAddr=localhost:9876,localhost:9875 &
这就是伪集群的搭建方式,真正的集群至少两台主句,每台主机分别启动一个namesrv,主机上AMaster和BSlave为一组broker启动,配置改成对应主机ip即可。
export NAMESRV_ADDR=localhost:9876
#生产者,bin目录下执行
./tools.sh org.apache.rocketmq.example.quickstart.Producer
#消费者
./tools.sh org.apache.rocketmq.example.quickstart.Consumer