服务器环境(centos7.6)
- 192.168.191.111 rocketmq-nameserver1 rocketmq-master1
- 192.168.192.112 rocketmq-nameserver2 rocketmq-master2
- vim /etc/hosts追加如下内容:
192.168.191.111 rocketmq-nameserver1
192.168.191.111 rocketmq-master1
192.168.191.112 rocketmq-nameserver2
192.168.191.112 rocketmq-master2
关闭防火墙
- 输入 systemctl status firewalld.service ,若,防火墙处于running状态,则执行一下操作
- 输入 systemctl stop firewalld.service ,关闭防火墙
- 输入 systemctl disable firewalld.service ,禁用防火墙
下载rocketmq源码(现在官方不提供编译好得文件下载)
- 下载地址:https://github.com/apache/rocketmq/archive/rocketmq-all-4.4.0.zip
- 下载maven:http://mirrors.shu.edu.cn/apache/maven/maven-3/3.6.0/binaries/apache-maven-3.6.0-bin.tar.gz
- 下载jdk1.8:http://download.oracle.com/otn/java/jdk/8u192-b12/750e1c8617c5452694857ad95c3ee230/jdk-8u192-linux-x64.tar.gz
jdk安装及环境变量配置
- cd /usr/local
- mkdir soft
- cd soft
- 把下载好的jdk通过xftp放入到/usr/local/soft下,通过解压"tar zxvf jdk-8u192-linux-x64.tar.gz"
- vim /etc/profile文件,追加如下内容
JAVA_HOME=/usr/local/soft/jdk1.8.0_192
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
- 执行source /etc/profile,使环境变量生效,运行java命令,看jdk安装是否成功
maven安装及环境变量配置
- cd /usr/local/soft
- 把下载好的maven通过xftp放入到/usr/local/soft下,通过解压"tar zxvf apache-maven-3.6.0-bin.tar.gz"
- vim /etc/profile文件,追加如下内容
#maven环境变量配置
MAVEN_HOME=/usr/local/soft/apache-maven-3.6.0
PATH=$MAVEN_HOME/bin:$PATH
export MAVEN_HOME
export PATH
- 执行source /etc/profile,使环境变量生效,执行"mvn -v",看maven安装是否成功
编译rocketmq源码
- cd /usr/local/soft
- mkdir rocketmq
- 把下载好的源码包通过xftp放入到/usr/local/soft下,通过解压"unzip rocketmq-all-4.4.0.zip -d rocketmq"
- cd rocketmq/rocketmq-rocketmq-all-4.4.0
- mvn -Prelease-all -DskipTests clean install -U,编译成功会有“building success”
- 编译好的文件在“/usr/local/soft/rocketmq/rocketmq-rocketmq-all-4.4.0/distribution/target”目录下,有apache-rocketmq,有tar.gz,zip的编译好文件
2m-noslave模式配置
- mkdir /usr/local/rocketmq/store
- mkdir /usr/local/rocketmq/store/commitlog
- mkdir /usr/local/rocketmq/store/consumequeue
- mkdir /usr/local/rocketmq/store/index
- cd /usr/local/rocketmq/conf/2m-noslave/
- vim /usr/local/rocketmq/conf/2m-noslave/broker-a.properties
- cd /usr/local/rocketmq/conf/2m-noslave
- vim broker-a.properties
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样
#0 表示 Master,>0 表示 Slave
brokerName=broker-a
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=ASYNC_FLUSH
#checkTransactionMessageEnable=false
- vim broker-b.properties
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker名字,注意此处不同的配置文件填写的不一样
#0 表示 Master,>0 表示 Slave
brokerName=broker-b
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=ASYNC_FLUSH
#checkTransactionMessageEnable=false
-
vim /usr/local/rocketmq/bin/runserver.sh
-
vim /usr/local/rocketmq/bin/runbr
oker.sh
启动(两台机器执行)
- cd /usr/local/rocketmq/bin
- nohup sh mqnamesrv &
- jps查看nameserver是否启动成功
- 也可以查看日志 tail -f -n 500 /usr/local/rocketmq/logs/rocketmqlogs/namesrv.log
- 在第一台服务器上执行:nohup sh mqbroker -c /usr/local/rocketmq/conf/2m-noslave/broker-a.properties >/dev/null 2>&1 &
- 在第二台服务器上执行:nohup sh mqbroker -c /usr/local/rocketmq/conf/2m-noslave/broker-b.properties >/dev/null 2>&1 &
- 在两台机器上执行:tail -f -n 500 /usr/local/rocketmq/logs/rocketmqlogs/broker.log
rocketmq-console安装
-
下载地址:https://github.com/apache/rocketmq-externals
,
-
解压以后,
-
修改项目下的pom.xml文件,以及src/java/resources下的application.properites下的nameserver配置
运行mvn clean package -Dmaven.test.skip=true,如果编译错误,请修改rocketmq-console项目下的pom.xml文件,一般需要修改rocket-tool的版本。
java -jar target/rocketmq-console-ng-1.0.0.jar
成功运行rockemq-console
启动脚本(一键启动)
大家是否觉得启动比较复杂,需要启动nameserver,broker,console,大家可以写启动脚本一键启动,
- 免密码登陆
192.168.191.111执行:ssh-keygen -t rsa,一路回车执行
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.191.111
ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.191.112
ssh 192.168.191.112验证免密码登陆是否成功,同理,192.168.191.112同样操作。 - 在两台服务器上分别,vim ~/.bashrc追加
JAVA_HOME=/usr/local/soft/jdk1.8.0_192
PATH=$JAVA_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export JAVA_HOME
export PATH
export CLASSPATH
解决环境变量引发的问题
- 创建启动脚本
cd /usr/local/rocketmq/script
vim rocketmq-manager.sh,内容如下
#!/bin/bash
if [[ $1 = 'start' ]];
then
#rocketmq的启动
#启动namesrv服务:
ssh 192.168.191.111 "nohup sh /usr/local/rocketmq/bin/mqnamesrv >/dev/null 2>&1 &"
ssh 192.168.191.112 "nohup sh /usr/local/rocketmq/bin/mqnamesrv >/dev/null 2>&1 &"
echo "============================启动nameserver===================================="
sleep 3s
#启动broker服务
ssh 192.168.191.111 "nohup sh /usr/local/rocketmq/bin/mqbroker -c /usr/local/rocketmq/conf/2m-noslave/broker-a.properties >/dev/null 2>&1 &"
ssh 192.168.191.112 "nohup sh /usr/local/rocketmq/bin/mqbroker -c /usr/local/rocketmq/conf/2m-noslave/broker-b.properties >/dev/null 2>&1 &"
echo "============================启动broker========================================"
sleep 3s
#启动rocketmq-console
ssh 192.168.191.111 "nohup java -jar /usr/local/soft/rocketmq-console-ng-1.0.0.jar >/dev/null 2>&1 &"
echo "========================启动rocketmq-console=================================="
elif [[ $1 = 'stop' ]];
then
#关闭rocketmq-console
ssh 192.168.191.111 "sudo kill -9 $(ps -ef|grep rocketmq-console-ng-1.0.0.jar | grep java|awk '{print $2}' )"
echo "=======================关闭rocketmq-console==================================="
sleep 3s
#关闭broker服务
ssh 192.168.191.111 "nohup sh /usr/local/rocketmq/bin/mqshutdown broker"
ssh 192.168.191.112 "nohup sh /usr/local/rocketmq/bin/mqshutdown broker"
echo "==========================关闭broker=========================================="
sleep 3s
#关闭namesrv服务:
ssh root@192.168.191.111 "nohup sh /usr/local/rocketmq/bin/mqshutdown namesrv"
ssh root@192.168.191.112 "nohup sh /usr/local/rocketmq/bin/mqshutdown namesrv"
echo "=========================关闭nameserver========================================"
else
echo "参数只能输入start|stop"
fi
- chmod +x rocketmq-manager.sh
启动脚本命令就可以/usr/local/rocketmq/script/rocketmq-manager.sh start
关闭脚本就可以/usr/local/rocketmq/script/rocketmq-manager.sh stop
总结
终于达到我想要的效果,查看了官方文档,官方对于环境搭建的文档少之又少,对于搭建还是花了不少的时间,后续会出很多关于rocketmq的文章,请各位大佬多多关照。