RocketMQ安装及使用
本文主要介绍如何在Windows和Linux快速部署一个单节点单副本RocketMQ服务即单点RocketMQ服务,并完成简单的消息收发。
系统要求:
1、64位操作系统,推荐Linux/Unix/MacOS
2、64位JDK1.8+
RocketMQ下载
下载RocketMQ5.0.0版本二进制包:rocketmq-all-5.0.0-bin-release.zip
下载地址:https://rocketmq.apache.org/zh/download/
RocketMQ安装及使用
Windows版本安装
(1)解压缩二进制包,配置ROCKETMQ_HOME环境变量;
变量名:ROCKETMQ_HOME
变量值:D:\tools\rocketmq-all-5.0.0-bin-release
(2)启动NameServer
cd D:\tools\rocketmq-all-5.0.0-bin-release
start bin\mqnamesrv.cmd
启动后显示以下日志,则表示NameServer启动成功;
The Name Server boot success. serializeType=JSON
(3)启动Broker+Proxy
start bin\mqbroker.cmd -n localhost:9876
启动后显示以下日志,则表示broker启动成功;
The broker[LAPTOP-NH51ES2C, 192.168.31.95:10911] boot success. serializeType=JSON and name server is localhost:9876
(4)RocketMQ收发消息测试
rocketMQ创建test主题:
bin\mqadmin.cmd updateTopic -b localhost:10911 -n localhost:9876 -t test
发送消息:
bin\mqadmin.cmd sendMessage -n localhost:9876 -t test -p 100
D:\tools\rocketmq-all-5.0.0-bin-release>bin\mqadmin.cmd sendMessage -n localhost:9876 -t test -p 100
#Broker Name #QID #Send Result #MsgId
LAPTOP-NH51ES2C 0 SEND_OK 7F0000016B905B480CF9703E120C0000
消费消息:
bin\mqadmin.cmd consumeMessage -n localhost:9876 -t test
D:\tools\rocketmq-all-5.0.0-bin-release>bin\mqadmin.cmd consumeMessage -n localhost:9876 -t test
Consume ok
MSGID: 7F0000016B905B480CF9703E120C0000 MessageExt [brokerName=LAPTOP-NH51ES2C, queueId=0, storeSize=211, queueOffset=4, sysFlag=0, bornTimestamp=1669115116044, bornHost=/192.168.31.95:50057, storeTimestamp=1669115116052, storeHost=/192.168.31.95:10911, msgId=C0A81F5F00002A9F0000000000000E6F, commitLogOffset=3695, bodyCRC=595022058, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='test', flag=0, properties={MIN_OFFSET=0, TRACE_ON=true, MAX_OFFSET=5, MSG_REGION=DefaultRegion, UNIQ_KEY=7F0000016B905B480CF9703E120C0000, CLUSTER=DefaultCluster, WAIT=true}, body=[49, 48, 48], transactionId='null'}] BODY: 100
MessageQueue [topic=test, brokerName=LAPTOP-NH51ES2C, queueId=0] print msg finished. status=NO_NEW_MSG, offset=5
D:\tools\rocketmq-all-5.0.0-bin-release>
Linux版本安装
(1)二进制安装包同Windows系统;
(2)启动NameServer
unzip rocketmq-all-5.0.0-bin-release.zip
cd rocketmq-all-5.0.0-bin-release
### 启动namesrv
$ nohup sh bin/mqnamesrv &
### 验证namesrv是否启动成功
$ tail -f ~/logs/rocketmqlogs/namesrv.log
The Name Server boot success...
启动后显示日志:The Name Server boot success,则表示NameServer启动成功;
(3)启动Broker+Proxy
### 先启动broker
$ nohup sh bin/mqbroker -n localhost:9876 --enable-proxy &
### 验证broker是否启动成功, 比如, broker的ip是192.168.1.2 然后名字是broker-a
$ tail -f ~/logs/rocketmqlogs/broker_default.log
The broker[broker-a,192.169.1.2:10911] boot success...
启动后显示以上日志,则表示broker启动成功;
(4)RocketMQ收发消息测试
rocketMQ创建主题:
bin\mqadmin.sh updateTopic -b localhost:10911 -n localhost:9876 -t test
发送消息:
bin\mqadmin.sh sendMessage -n localhost:9876 -t test -p 100
消费消息:
bin\mqadmin.sh consumeMessage -n localhost:9876 -t test
参考文档:
RocketMQ-quickStart
https://rocketmq.apache.org/zh/docs/quickStart/02quickstart
(完)