本地Eclipse除錯RocketMQ mqnamesrv服務和mqbroker服務以及mqadmin服務
最近在研究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 <command>' 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]