最近在研究RocketMQ,本着追求源码的精神,想在本地eclispe调试mq的源代码,先从mq的github上把代码拉下来。
1:启动mqnamesrv
RocketMQ官网文档Quick Start中建议在linux上启动RocketMQ,先通过 “ nohup sh bin/mqnamesrv &” 命令启动mqnamesrv服务,这就意味着mq启动启动mqnamesrv服务时通过 mqnamesrv 脚本启动的,我们在源码中找到mqnamesrv脚本(在子项目distribution的\bin目录下)看最后一句,mqnamesrv服务又是通过runserver.sh 脚本来启动的,主函数:org.apache.rocketmq.namesrv.NamesrvStartup
找到org.apache.rocketmq.namesrv.NamesrvStartup类我们启动这个类,启动时可能会报错:Please set the ROCKETMQ_HOME variable in your environment to match the location of the RocketMQ installation。
这是因为没有配置ROCKETMQ_HOME,我们通过linux命令启动时就没有配置ROCKETMQ_HOME为啥没报错呢?那是因为mqnamesrv脚本指定了ROCKETMQ_HOME,所以如果我们没有指定ROCKETMQ_HOME通过程序跑就会报错,解决方法有两种,1是添加ROCKETMQ_HOME环境变量,如果不行还可以在启动类中直接指定rocketmq.home.dir,如下图52行
在启动就不会报错了。启动成功后会提示:The Name Server boot success. serializeType=JSON
2:启动mqbroker
RocketMQ官网文档Quick Start中建议在linux上启动mqbroker,通过命令:nohup sh bin/mqbroker -n localhost:9876 &
来启动broker,和启动mqnamesrv一样,先分析mqbroker脚本,然后根据内容找到org.apache.rocketmq.broker.BrokerStartup类
我们启动org.apache.rocketmq.broker.BrokerStartup,会和mqnamesrv一样可能会报错:Please set the ROCKETMQ_HOME variable in your environment to match the location of the RocketMQ installation。解决方法一样。如下
启动后会提示:The broker[DESKTOP-91MD56R, 172.32.14.66:10911] boot success. serializeType=JSON
但是:虽然broker启动成功了,但是我们并没有指定broker的nameserver.所以我们需要在启动org.apache.rocketmq.broker.BrokerStartup类时需要增加program arguments如下:
debugger启动成功提示:The broker[DESKTOP-91MD56R, 172.32.14.66:10911] boot success. serializeType=JSON and name server is localhost:9876 (对比上面没有指定namesrv address 的成功提示语)
namesrv和broker都启动成功后,我们来看看常用的命令工具 mqadmin.
3:使用mqadmin
使用mq时我们经常用mqadmin 这个命令,我们来看看如何调试这个命令。
首先还是找到mqadmin 脚本,找出 启动类org.apache.rocketmq.tools.command.MQAdminStartup
接着我们启动类org.apache.rocketmq.tools.command.MQAdminStartup:
一样地我们需要在此类中加入下图84行的代码再启动
启动成功后会显示出所有m'q'admin 能运行的命令:
The most commonly used mqadmin commands are:
updateTopic Update or create topic
deleteTopic Delete topic from broker and NameServer.
updateSubGroup Update or create subscription group
deleteSubGroup Delete subscription group from broker.
updateBrokerConfig Update broker's config
updateTopicPerm Update topic perm
topicRoute Examine topic route info
topicStatus Examine topic Status info
topicClusterList get cluster info for topic
brokerStatus Fetch broker runtime status data
queryMsgById Query Message by Id
queryMsgByKey Query Message by Key
queryMsgByUniqueKey Query Message by Unique key
queryMsgByOffset Query Message by offset
printMsg Print Message Detail
printMsgByQueue Print Message Detail
sendMsgStatus send msg to broker.
brokerConsumeStats Fetch broker consume stats data
producerConnection Query producer's socket connection and client version
consumerConnection Query consumer's socket connection, client version and subscription
consumerProgress Query consumers's progress, speed
consumerStatus Query consumer's internal data structure
cloneGroupOffset clone offset from other group.
clusterList List all of clusters
topicList Fetch all topic list from name server
updateKvConfig Create or update KV config.
deleteKvConfig Delete KV config.
wipeWritePerm Wipe write perm of broker in all name server
resetOffsetByTime Reset consumer offset by timestamp(without client restart).
updateOrderConf Create or update or delete order conf
cleanExpiredCQ Clean expired ConsumeQueue on broker.
cleanUnusedTopic Clean unused topic on broker.
startMonitoring Start Monitoring
statsAll Topic and Consumer tps stats
allocateMQ Allocate MQ
checkMsgSendRT check message send response time
clusterRT List All clusters Message Send RT
getNamesrvConfig Get configs of name server.
updateNamesrvConfig Update configs of name server.
getBrokerConfig Get broker config by cluster or special broker!
queryCq Query cq command.
sendMessage Send a message
consumeMessage Consume message
See 'mqadmin help ' for more information on a specific command.
那么我们如何通过程序来运行这些命令呢?
比如我想通过updateTopic指令增加一个topic
我们需要为程序指定启动参数如下图:
运行成功后提示:
create topic to 172.32.14.66:10911 success.
TopicConfig [topicName=HellloTopic, readQueueNums=8, writeQueueNums=8, perm=RW-, topicFilterType=SINGLE_TAG, topicSysFlag=0, order=false]