Rocketmq专题
注
:
-
本教程由羞涩梦整理同步发布,本人技术分享站点:blog.hukanfa.com
-
转发本文请备注原文链接,本文内容整理日期:2024-01-28
-
csdn 博客名称:五维空间-影子,欢迎关注
-
说明
- 地址相关:
# 官网地址 https://rocketmq.apache.org/ # 下载地址 https://rocketmq.apache.org/zh/download
- 环境相关
# 要求 64位操作系统,推荐 Linux/Unix/macOS 64位 JDK 1.8+ # jdk请加入全局变量中 $ vim /etc/profile export JAVA_HOME=/usr/local/jdk1.8.0_77 export PATH=$JAVA_HOME/bin:$PATH $ source /etc/profile
1 下载
-
操作步骤
- 截至目前,5.0及以上的版本如下。建议用5.0.0稳定版本
- 本次下载5.0.0版本
# 地址 , 选择 Binary 下载 https://dist.apache.org/repos/dist/release/rocketmq/5.0.0/rocketmq-all-5.0.0-bin-release.zip # 百度网盘 https://pan.baidu.com/s/1kyybjTSeJzZ2bf8js_QZFg?pwd=uxns
- 将下载好的压缩包上传到服务器并解压
# 解压 unzip rocketmq-all-5.0.0-bin-release.zip # 按照需要,放到规划好的目录 mv rocketmq-all-5.0.0-bin-release /usr/local/rocketmq-5.0.0
- 配置环境变量
$ vim /etc/profile export ROCKETMQ_HOME=/usr/local/rocketmq-5.0.0 export PATH=$ROCKETMQ_HOME/bin:$PATH $ source /etc/profile
2 调整配置
-
操作步骤
- 调整
namesrv
的内存资源
# 操作路径 /usr/local/rocketmq-5.0.0/bin $ vim runserver.sh # 输入Xms搜索 # 71行,按需修改 JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256m # 76行,按需修改 JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m
- 调整
brocker
内存资源
# 操作路径 /usr/local/rocketmq-5.0.0/bin $ vim runbroker.sh # 输入Xms搜索 # 85行,按需修改 JAVA_OPT="${JAVA_OPT} -server -Xms1g-Xmx2g"
- 修改
broker.conf
配置文件,示例路径:/home/hukanfa/rocketmq/conf
brokerClusterName = DefaultCluster brokerName = broker-a brokerIP1=192.168.26.21 brokerIP2=192.168.26.21 namesrvAddr=localhost:9876 brokerId = 0 deleteWhen = 04 fileReservedTime = 48 brokerRole = ASYNC_MASTER flushDiskType = ASYNC_FLUSH # 持久化消息存储根路径 storePathRootDir=/home/hukanfa/rocketmq/store
- 添加
namesrv.properties
# 路径:conf 目录下 listenPort=9876
- 修改运行日志文件路径
# 路径:conf 目录下,默认存在当前用户logs目录下,如果这个logs目录不存在则直接放在当前用户家目录下 sed -i 's#${user.home}#/home/hukanfa/rocketmq#g' logback_broker.xml sed -i 's#${user.home}#/home/hukanfa/rocketmq#g' logback_controller.xml sed -i 's#${user.home}#/home/hukanfa/rocketmq#g' logback_namesrv.xml sed -i 's#${user.home}#/home/hukanfa/rocketmq#g' logback_proxy.xml sed -i 's#${user.home}#/home/hukanfa/rocketmq#g' logback_tools.xml
- 调整
3 启动
-
方式一:操作如下
- 启动脚本
manager.sh
,这个脚本适合集群方式,单机方式请按需调整下
#/bin/bash # 参数个数 if [ $# -eq 1 ];theninStr="default"action=$1 elif [ $# -eq 2 ];theninStr=$1action=$2 elseecho "提示01:传入1个参数,只能是 ps ,查看节点管理服务"echo "提示02:传入2个参数,分别为: namesrv/broker-(a/b/c)|-s start/stop/status"echo "start 启动 | stop 停止 | status 查看服务运行状态 | ps 查看本节点管理的服务 --> sh manager.sh ps" exit 0 fi # rocketmq存放路径 mqPath=/data_mq/rocketmq-5.0.0 # 存放服务数组 brokerArry=(default namesrv broker-a broker-a-s broker-b broker-b-s broker-c broker-c-s broker) # 存放动作数组 actionArry=(start stop status ps) # 存放本节点管理的服务 localSrvArry=(namesrv broker-a broker-b-s) ### 前置判断 # 函数,判断变量是否为指定值 function checkVars(){srcInStr=$1srcAcStr=$2brokerArryLength=${#brokerArry[@]}# 默认为0,匹配到值为1tagOne=0tagTwo=0# 校验服务名称for i in ${brokerArry[@]};doif [ ! "$srcInStr" == "$i" ];thencontinueelsetagOne=1fidone# 校验动作名称for j in ${actionArry[@]};doif [ ! "$srcAcStr" == "$j" ];thencontinueelsetagTwo=1fidone# 判断,如果action为ps则手动赋值为1,即不检查以下项if [ "$action" == "ps" ];thentagOne=1tagTwo=1fi# 统一判断传参匹配if [ $tagOne -eq 0 -a $tagTwo -eq 1 ];thenecho "警告:服务名称非法,请按照下面提示输入"echo "提示:namesrv broker-a broker-a-s broker-b broker-b-s broker-c broker-c-s broker"exit 1elif [ $tagOne -eq 1 -a $tagTwo -eq 0 ];thenecho "警告:动作名称非法,请按照下面提示输入"echo "提示:status stop start"echo "提示:如只有1个参数,请输入 ps"exit 1elif [ $tagOne -eq 0 -a $tagTwo -eq 0 ];thenecho "警告:服务及动作名称非法,请按照下面提示输入"echo "提示:namesrv broker-a broker-a-s broker-b broker-b-s broker-c broker-c-s broker"echo "提示:status stop start"echo "提示:如只有1个参数,请输入 ps"exit 1fi } # 调用函数,验证传参 checkVars $inStr $action # 函数,判断命令是否执行成功 function checkCmdStatus(){srvStr=$1cmdStr=$2if [ $? -ne 0 ];thenecho "${srvStr}操作${cmdStr}失败"exit 1fi } # 函数,按操作执行指令 function runAction(){srvStr=$1# 区别出brokerbrokerNum=`echo $inStr | grep broker | wc -l`case $action in"start")if [ "$srvStr" == "namesrv" -a $brokerNum -eq 0 ];thennohup ${mqPath}/bin/mqnamesrv -c ${mqPath}/conf/namesrv.properties > ${mqPath}/logs/${srvStr}.log 2>&1 &checkCmdStatus $srvStr $actionelif [ $brokerNum -gt 0 ];then# 指定配置文件启动方式nohup ${mqPath}/bin/mqbroker -c ${mqPath}/conf/3m-3s-async/${srvStr}.properties > ${mqPath}/logs/${srvStr}.log 2>&1 &checkCmdStatus $srvStr $actionelseecho "$srvStr 启动异常,请核查"exit 1fiecho "start success";;"stop")pidNum=`ps -ef | grep $srvStr | grep -v grep | grep java | awk '{print $2}'`kill -9 $pidNumcheckCmdStatus $srvStr $actionecho "stop success";;"status")pidNum=`ps -ef | grep $srvStr | grep -v grep | grep java | awk '{print $2}'`if [ -z $pidNum ];thenecho "${srvStr} 未运行"elseecho "${srvStr} 运行中 $pidNum"fi;;"ps")for s in ${localSrvArry[@]};doecho "sh manager.sh $s start|stop|status"done;; *)echo "指令异常"exit 1;;esac }### 主逻辑 # 执行启停操作 runAction $inStr
- 启停操作
# 启动sh manager namesrv startsh manager broker start# 停止sh manager broker stopsh manager namesrv stop# 请按需配置开机自启动
- 启动脚本
4 测试
- 操作如下
# 新增topic --> topicWarning
mqadmin updateTopic -n localhost:9876 -c DefaultCluster -t topicWarning
# 查看某个topic状态
mqadmin topicStatus -n localhost:9876 -t topicWarning
# 查看所有消费组
mqadmin consumerProgress -n localhost:9876
# 查看所有topic
mqadmin topicList -n localhost:9876
# 删除topic
mqadmin deleteTopic -n localhost:9876 -c DefaultCluster -t topicWarning