编译安装 Rocket MQ
需要以下环境:
# 下载安装包;
wget https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.7.1/rocketmq-all-4.7.1-source-release.zip
# 解压;
unzip rocketmq-all-4.7.1-source-release.zip
cd rocketmq-all-4.7.1-source-release/
# 编译;
mvn -Prelease-all -DskipTests clean install -U
# 进入安装目录;
cd distribution/target/rocketmq-4.7.1/rocketmq-4.7.1/
# 启动 Name Server
nohup sh bin/mqnamesrv &
tail -f ~/logs/rocketmqlogs/namesrv.log
nohup sh bin/mqbroker -n localhost:9876 &
tail -f ~/logs/rocketmqlogs/broker.log
注意:
若启动 Name Server 时提示 Cannot allocate memory
,修改 bin/runserver.sh
:
# 根据自己的机器内存大小适当分配内存
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
若启动 Broker 时提示 Cannot allocate memory
,修改 bin/runbroker.sh
:
# 根据自己的机器内存大小适当分配内存
JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
在发送/接收消息之前,我们需要先告诉客户端名称服务器(Name Server)的位置,RocketMQ 提供了多种方式设置 Name Server 位置,此处为了简单起见,我们使用环境变量 NAMESRV_ADDR
来设置;
# 指定名称服务器(Name Server)地址;
export NAMESRV_ADDR=localhost:9876
# 发送消息;
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
消息发送成功能看到如下内容:
SendResult [sendStatus=SEND_OK, msgId=C0A8017808D62B193F2D094EC5220000, offsetMsgId=C0A8017800002A9F0000000000000000, messageQueue=MessageQueue [topic=TopicTest, brokerName=shaw-pc, queueId=0], queueOffset=0]
...
若没设置环境变量 NAMESRV_ADDR
,发送消息时会出现如下错误信息:
RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent0).
RocketMQLog:WARN Please initialize the logger system properly.
java.lang.IllegalStateException:
...
# 消费消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
消息接收成功能看到如下内容:
Consumer Started.
ConsumeMessageThread_1 Receive New Messages: [MessageExt [brokerName=shaw-pc, queueId=0, storeSize=201, queueOffset=282, sysFlag=0, bornTimestamp=1593693473750, bornHost=/192.168.1.120:44386, storeTimestamp=1593693473752, storeHost=/192.168.1.120:10911, msgId=C0A8017800002A9F0000000000037E63, commitLogOffset=228963, bodyCRC=1250039395, reconsumeTimes=0, preparedTransactionOffset=0, toString()=Message{topic='TopicTest', flag=0, properties={MIN_OFFSET=0, MAX_OFFSET=296, CONSUME_START_TIME=1593693478959, UNIQ_KEY=C0A801780A3D2B193F2D0993AFD60002, CLUSTER=DefaultCluster, WAIT=true, TAGS=TagA}, body=[72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 50], transactionId='null'}]]
...
# 关闭 broker 服务;
sh bin/mqshutdown broker
# 关闭 name server;
sh bin/mqshutdown namesrv