环境搭建
环境准备
下载地址: https://downloads.apache.org/rocketmq/4.9.5/
安装
上传至服务器
mkdir /usr/soft
#上传至此目录/usr/soft
mkdir /usr/soft
解压
cd /usr/soft
unzip rocketmq-all-4.9.5-bin-release.zip
移动
mkdir /usr/local/rocketmq
cd /usr/soft
mv rocketmq-all-4.9.5-bin-release /usr/local/rocketmq
配置环境变量
vim /etc/profile
末尾加上
#set rocketmq
ROCKETMQ_HOME=/usr/local/rocketmq/
PATH=$PATH:$ROCKETMQ_HOME/bin
export ROCKETMQ_HOME PATH
输入:wq! 保存并退出, 并使得配置立刻生效:
source /etc/profile
配置内存大小
cd /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/bin
vim runbroker.sh
vim runserver.sh
配置如下:
runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m"
runserver.sh
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
启动
启动NameServer#
1.启动NameServer
nohup mqnamesrv &
# 2.查询启动日志
tail -f ~/logs/rocketmqlogs/namesrv.log
启动Broker
# 1.启动Broker
nohup mqbroker -n localhost:9876 &
# 2.查看启动日志
tail -f ~/logs/rocketmqlogs/broker.log
测试
jps #查看java当前运行的程序
发送消息
JAVA_OPT=“${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m”
# 1.设置环境变量
export NAMESRV_ADDR=localhost:9876
# 2.使用安装包的Demo发送消息
sh /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
接收消息
# 1.设置环境变量
export NAMESRV_ADDR=localhost:9876
# 2.接收消息
sh /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
关闭
# 1.关闭NameServer
mqshutdown namesrv
# 2.关闭Broker
mqshutdown broker
高可用集群搭建
双主双从集群搭建
添加Host
vim /etc/hosts
# nameserver
192.168.56.101 rocketmq-nameserver1
192.168.56.102 rocketmq-nameserver2
# broker
192.168.56.101 rocketmq-master1
192.168.56.101 rocketmq-slave2
192.168.56.102 rocketmq-master2
192.168.56.102 rocketmq-slave1
配置完成后, 重启网卡
systemctl restart network
配置防火墙
关闭防火墙(或者开放端口)
# 关闭防火墙
systemctl stop firewalld.service
# 查看防火墙的状态
firewall-cmd --state
# 禁止firewall开机启动
systemctl disable firewalld.service
或者开放端口
nameserver
默认使用 9876 端口master
默认使用 10911 端口slave
默认使用11011 端口
# 关闭name server默认端口
#firewall-cmd --remove-port=9876/tcp --permanent
# 关闭master默认端口
#firewall-cmd --remove-port=10911/tcp --permanent
# 关闭slave默认端口 (当前集群模式可不开启)
#firewall-cmd --remove-port=11011/tcp --permanent
# 重启防火墙
#firewall-cmd --reload
# 开放name server默认端口
firewall-cmd --permanent --zone=public --add-port=9876/tcp
# 开放master默认端口
firewall-cmd --permanent --zone=public --add-port=10911/tcp
# 开放slave默认端口
firewall-cmd --permanent --zone=public --add-port=11011/tcp
# 重启防火墙
firewall-cmd --reload
firewall-cmd --permanent --zone=public --add-port=9881/tcp
# 查看开放的端口
firewall-cmd --permanent --zone=public --list-ports
创建消息存储路径
mkdir /usr/local/rocketmq/store
mkdir /usr/local/rocketmq/store/commitlog
mkdir /usr/local/rocketmq/store/consumequeue
mkdir /usr/local/rocketmq/store/index
mkdir /usr/local/rocketmq/store2
mkdir /usr/local/rocketmq/store2/commitlog
mkdir /usr/local/rocketmq/store2/consumequeue
mkdir /usr/local/rocketmq/store2/index
broker配置文件
主1(server1)
vim /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-a.properties
#所属集群名字
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=ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=SYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
从2(server1)
vim /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-b-s.properties
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-b
#0 表示 Master,>0 表示 Slave
brokerId=1
#nameServer地址,分号分割
namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=11011
#删除文件时间点,默认凌晨 4点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
#commitLog每个文件的大小默认1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue每个文件默认存30W条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/usr/local/rocketmq/store2
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store2/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq/store2/consumequeue
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store2/index
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store2/checkpoint
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store2/abort
#限制的消息大小
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
主2(server2)
vim /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-b.properties
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-b
#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/store2
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq/store2/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq/store2/consumequeue
#消息索引存储路径
storePathIndex=/usr/local/rocketmq/store2/index
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq/store2/checkpoint
#abort 文件存储路径
abortFile=/usr/local/rocketmq/store2/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=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
从1(server2)
vim /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-a-s.properties
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=1
#nameServer地址,分号分割
namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876
#在发送消息时,自动创建服务器不存在的topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=11011
#删除文件时间点,默认凌晨 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=SLAVE
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
配置内存大小
cd /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/bin
vim runbroker.sh
vim runserver.sh
配置如下:
runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m"
runserver.sh
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
启动
启动NameServer
nohup mqnamesrv &
启动主1(server1)
nohup mqbroker -c /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-a.properties &
启动从2(server1)
nohup mqbroker -c /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-b-s.properties &
启动主2(server2)
nohup mqbroker -c /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-b.properties &
启动从1(server2)
nohup mqbroker -c /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-a-s.properties &
Server1
nohup mqnamesrv &
nohup mqbroker -c /usr/local/rocketmq/rocketmq-all-4.9.5bin-release/conf/2m-2s-sync/broker-a.properties &
nohup mqbroker -c /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-b-s.properties &
nohup java -jar /usr/soft/rocketmq-console-ng-2.0.0.jar &
Server2
nohup mqnamesrv &
nohup mqbroker -c /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-b.properties &
nohup mqbroker -c /usr/local/rocketmq/rocketmq-all-4.9.5-bin-release/conf/2m-2s-sync/broker-a-s.properties &
http://192.168.56.101:8080
http://192.168.32.61:9881
http://183.238.254.254:9881
集群监控平台
下载监控平台
https://github.com/apache/rocketmq-externals/tree/master/rocketmq-console
git clone https://github.com/apache/rocketmq-externals
master分支看不到rocketmq-console时,可以切换develop分支
cd rocketmq-console
# 打包
mvn clean package -Dmaven.test.skip=true
如果编译出现 Failed to execute goal on project rocketmq-console-ng: Could not resolve dependencies for project org.apache:rocketmq-console-ng:jar:1.0.0: The following artifacts could not be resolved: org.apache.rocketmq:rocketmq-tools:jar:4.4.0-SNAPSHOT, org.apache.rocketmq:rocketmq-namesrv:jar:4.4.0-SNA
PSHOT, org.apache.rocketmq:rocketmq-broker:jar:4.4.0-SNAPSHOT: Failure to find org.apache.rocketmq:rocketmq-tools:jar:4.4.0-SNAPSHOT in http://maven.aliyun.com/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of alimaven ha
s elapsed or updates are forced -> [Help 1] 错误
解决方法:<rocketmq.version>4.4.0-SNAPSHOT</rocketmq.version> 改为: <rocketmq.version>4.4.0</rocketmq.version>
# 打包完的jar包会放在rocketmq-externals/rocketmq-console/target里 rocketmq-console-ng-1.0.0.jar
# 启动
java -jar rocketmq-console-ng-1.0.0.jar
# 后台运行
nohup java -jar rocketmq-console-ng-1.0.0.jar &
注意:打包前在rocketmq-console
中配置namesrv
集群地址:
#修改配置文件
cd /usr/local/rocketmq-console/src/main/resources
vim application.properties
rocketmq.config.namesrvAddr=192.168.56.101:9876;192.168.56.102:9876
启动rocketmq-console:
.java -jar /usr/soft/rocketmq-console-ng-2.0.0.jar
# 后台运行
nohup java -jar /usr/soft/rocketmq-console-ng-2.0.0.jar &
# 或者是指定namesrv集群地址启动
nohup java -jar /usr/soft/rocketmq-console-ng-2.0.0.jar --rocketmq.config.namesrvAddr=192.168.56.101:9876;192.168.56.102:9876 &
启动成功后,我们就可以通过浏览器访问http://localhost:8080
进入控制台界面了,如下图:
[rocketmq-console-ng-2.0.0.jar]
链接:https://pan.baidu.com/s/15jo61OaWhvhy80oOPMCUnA 提取码:lr78
[start-rocketmq-console.sh]
链接:https://pan.baidu.com/s/1yLEBzOvlmR5S3OhicyMcTQ 提取码:snay