安装前提环境条件:
1, JDK1.8 及以上
2,Maven 3.2x ;
3,Git ;
4,4G+ 空间为 Broker 服务器使用
1,下载binary 压缩包
源码下载地址:https://dlcdn.apache.org/rocketmq/4.9.3/rocketmq-all-4.9.3-source-release.zip
2,解压
unzip rocketmq-all-4.9.3-bin-release.zip cd rocketmq-4.9.3/
3,运行 name-server
> nohup sh bin/mqnamesrv & > tail -f ~/logs/rocketmqlogs/namesrv.log 2022-04-19 15:34:48 INFO main - tls.client.keyPassword = null 2022-04-19 15:34:48 INFO main - tls.client.certPath = null 2022-04-19 15:34:48 INFO main - tls.client.authServer = false 2022-04-19 15:34:48 INFO main - tls.client.trustCertPath = null 2022-04-19 15:34:48 INFO main - Using JDK SSL provider 2022-04-19 15:34:49 INFO main - SSLContext created for server 2022-04-19 15:34:49 INFO main - Try to start service thread:FileWatchService started:false lastThread:null 2022-04-19 15:34:49 INFO NettyEventExecutor - NettyEventExecutor service started 2022-04-19 15:34:49 INFO FileWatchService - FileWatchService service started 2022-04-19 15:34:49 INFO main - The Name Server boot success. serializeType=JSON
4,启动 Broker
nohup sh bin/mqbroker -n localhost:9876 & tail -f ~/logs/rocketmqlogs/broker.log 2022-04-19 15:57:09 INFO main - The broker[iZbp16xc9vwddb2wodsge4Z, 172.26.136.245:10911] boot success. serializeType=JSON and name server is localhost:9876 2022-04-19 15:57:19 INFO BrokerControllerScheduledThread1 - dispatch behind commit log 0 bytes 2022-04-19 15:57:19 INFO BrokerControllerScheduledThread1 - Slave fall behind master: 0 bytes 2022-04-19 15:57:19 INFO brokerOutApi_thread_2 - register broker[0]to name server localhost:9876 OK
5,Producer 发送消息
5.1,设置 NAMESRV_ADDR 环境变量
export NAMESRV_ADDR=localhost:9876
5.2,发送消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
终端出现类似以下打印输出,表示发送成功
SendResult [sendStatus=SEND_OK, msgId=7F0000015C2508EFB84660239ED503E5, offsetMsgId=AC1A88F500002A9F000000000002EB52, messageQueue=MessageQueue [topic=TopicTest, brokerName=iZbp16xc9vwddb2wodsge4Z, queueId=3], queueOffset=249] SendResult [sendStatus=SEND_OK, msgId=7F0000015C2508EFB84660239ED503E6, offsetMsgId=AC1A88F500002A9F000000000002EC12, messageQueue=MessageQueue [topic=TopicTest, brokerName=iZbp16xc9vwddb2wodsge4Z, queueId=0], queueOffset=249] SendResult [sendStatus=SEND_OK, msgId=7F0000015C2508EFB84660239ED603E7, offsetMsgId=AC1A88F500002A9F000000000002ECD2, messageQueue=MessageQueue [topic=TopicTest, brokerName=iZbp16xc9vwddb2wodsge4Z, queueId=1], queueOffset=249]
5.3,Consumer 消费消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
终端出现类似以下打印输出,表示消息消费成功
ConsumeMessageThread_please_rename_unique_group_name_4_13 Receive New Messages: [MessageExt [brokerName=iZbp16xc9vwddb2wodsge4Z, queueId=0, storeSize=192, queueOffset=195, sysFlag=0, bornTimestamp=1650355346976, bornHost=/172.26.136.245:36668, storeTimestamp=1650355346977, storeHost=/172.26.136.245:10911, msgId=AC1A88F500002A9F0000000000024A12, commitLogOffset=150034, bodyCRC=576617176, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=250, CONSUME_START_TIME=1650355501879, UNIQ_KEY=7F0000015C2508EFB84660239E20030E, CLUSTER=DefaultCluster, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 55, 56, 50], transactionId='null'}]] ConsumeMessageThread_please_rename_unique_group_name_4_11 Receive New Messages: [MessageExt [brokerName=iZbp16xc9vwddb2wodsge4Z, queueId=0, storeSize=192, queueOffset=191, sysFlag=0, bornTimestamp=1650355346937, bornHost=/172.26.136.245:36668, storeTimestamp=1650355346940, storeHost=/172.26.136.245:10911, msgId=AC1A88F500002A9F0000000000023E12, commitLogOffset=146962, bodyCRC=1001427791, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=250, CONSUME_START_TIME=1650355500938, UNIQ_KEY=7F0000015C2508EFB84660239DF902FE, CLUSTER=DefaultCluster, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 55, 54, 54], transactionId='null'}]]
6,关闭 boker 与 name-server
6.1 关闭 boker
sh bin/mqshutdown broker The mqbroker(36695) is running... Send shutdown request to mqbroker(36695) OK
6.2 关闭 name server
sh bin/mqshutdown namesrv The mqnamesrv(36664) is running... Send shutdown request to mqnamesrv(36664) OK
安装时可能遇到的错误,以及解决方案:
1,启动 bocker 时,启动不起来,出现类似于以下内存不足报错信息
Java HotSpot(TM) 64-Bit Server VM warning: error='Cannot allocate memory' (errno=12)
解决方案:Linux下 找 runserver.sh,runboker.sh ;Windwos 下找 runserver.cmd,runboker.cmd,打开后,定位到以下位置
替换为以下相关设置,减小JVM最大内存申请量
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn125m -XX:MetaspaceSize=128m -XX:MaxMetasp