最近有在尝试给项目加入消息中间件服务,首先想到了鼎鼎大名的RocketMQ。RocketMQ是一款高性能的、分布式消息中间件,由阿里开源。它提供了丰富的消息拉取方式,能够处理上亿级的海量数据,甚至在阿里双十上经受了超大的请求峰值,其商业可用性值得依赖和使用。
安装方式比较简单,就是在Rocket官网去下载对应版本的压缩包。有两种选择,一种是binary版本,也就是编译好的bin文件压缩包。还有一种是source版,就是需要被编译的源码包。
我选择使用编译好的bin包,版本为最新的4.8.0。使用如下命令完成下载和解压。顺带提一句本机安装的Java版本是10.0.1,划重点。
cd ~/Downloads
weget https://mirrors.bfsu.edu.cn/apache/rocketmq/4.8.0/rocketmq-all-4.8.0-bin-release.zip
unzip rocketmq-all-4.8.0-bin-release.zip
mv rocketmq-all-4.8.0-bin-release rocketmq
然后需要启动NameServer和Broker Server,首先是NameServer,命令如下所示:
cd ~/Downloads/rocketmq/bin
./mqnamesrv
然后很有可能会遇到如下的报错:
-Djava.ext.dirs=/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/jre/lib/ext:/Users/tony/Downloads/rocketmq-all-4.8.0-source-release/distribution/bin/../lib:/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/lib/ext is not supported. Use -classpath instead.
Error: Could not create the Java Virtual Machine.
我根据这段报错一番艰难地百度ingGoogle之后,发现没有人遇到完全类似的问题,唯一接近的是Could not create the java Virtual Machine的报错,我尝试性进行了如下方法。
首先怀疑是因为我的Java版本太高了,导致有一些runserver.sh里面的参数设置不能兼容,因为namesrv会执行runserver.sh里面的命令。按照腾讯云上的原话是:
由于RocketMQ的启动文件都是按照JDK8配置的,而前面我特意配置的JDK版本是11,有很多命令参数不支持导致的,使用JDK8,正常启动没有问题的。
于是编辑bin/runserver.sh文件,将下面的命令注释掉:
# 注释掉下面这行
#export CLASSPATH=.:${BASE_DIR}/conf:${CLASSPATH}
# 修改成下面这行
export CLASSPATH=.:${BASE_DIR}/lib/*:${BASE_DIR}/conf:${CLASSPATH}
然后还需要将JAVA_OPT的参数注释一部分,它们的位置是在文件内容最后,如下所示:
# 注释下面这两行
#JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib:${JAVA_HOME}/lib/ext"
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
是不是看起来很眼熟?这就是前面我们遇到的报错那段,据说是Java1.9之后使用-Djava.ext.dirs会有问题,我去RocketMQ的github上的issue讨论看了一下,貌似官方团队也没有fix这个问题,2333。
保存好这些修改,然后重新运行mqnamesrv即可,以后台程序方式运行的命令如下:
nohup sh mqnamesrv &
查看运行的日志的命令和其输出如下:
tail -f ~/logs/rocketmqlogs/namesrv.log
2020-12-31 16:15:30 INFO main - tls.client.authServer = false
2020-12-31 16:15:30 INFO main - tls.client.trustCertPath = null
2020-12-31 16:15:30 INFO main - Using OpenSSL provider
2020-12-31 16:15:30 INFO main - SSLContext created for server
2020-12-31 16:15:30 INFO NettyEventExecutor - NettyEventExecutor service started
2020-12-31 16:15:30 INFO main - Try to start service thread:FileWatchService started:false lastThread:null
2020-12-31 16:15:30 INFO FileWatchService - FileWatchService service started
2020-12-31 16:15:30 INFO main - The Name Server boot success. serializeType=JSON
2020-12-31 16:16:30 INFO NSScheduledThread1 - --------------------------------------------------------
2020-12-31 16:16:30 INFO NSScheduledThread1 - configTable SIZE: 0
说明NameServer已经正常启动了。
第二步运行Broker Server,还是在bin目录下执行:
./mqbroker
继续收获报错,错误日志输出如下所示:
[0.002s][warning][gc] -Xloggc is deprecated. Will use -Xlog:gc:/Volumes/RAMDisk/rmq_broker_gc_%p_%t.log instead.
Unrecognized VM option 'PrintGCDateStamps'
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
其本质还是无法创建Java虚拟机,推测还是出在GC的一些参数在Java10.0上无法兼容。查看mqbroker脚本里面的内容,可以看到最后一行执行了如下命令:
sh ${ROCKETMQ_HOME}/bin/runbroker.sh org.apache.rocketmq.broker.BrokerStartup $@
说明执行了runbroker.sh脚本,于是在runbroker.sh文件中找到如下命令:
# 注释掉下面这行
#export CLASSPATH=.:${BASE_DIR}/conf:${CLASSPATH}
# 修改成下面这行
export CLASSPATH=.:${BASE_DIR}/lib/*:${BASE_DIR}/conf:${CLASSPATH}
这段修改和mqnamesrv的类似,还有下面这段也需要调整:
JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${GC_LOG_DIR}/rmq_broker_gc_%p_%t.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy"
JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch"
JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=15g"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking"
JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib:${JAVA_HOME}/lib/ext"
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
注释掉一些无效的命令,改后如下所示:
JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
#JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
#JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:${GC_LOG_DIR}/rmq_broker_gc_%p_%t.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy"
#JAVA_OPT="${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
JAVA_OPT="${JAVA_OPT} -XX:+AlwaysPreTouch"
#JAVA_OPT="${JAVA_OPT} -XX:MaxDirectMemorySize=15g"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking"
JAVA_OPT="${JAVA_OPT} #
-Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib:${JAVA_HOME}/lib/ext"
#JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
保存上述修改,然后就可以正常启动Broker Server了,执行下面命令:
nohup sh bin/mqbroker -n localhost:9876 &
如果想最简单的方式测试一下服务,那么可以使用RocketMQ脚本封装好的工具,命令如下:
export NAMESRV_ADDR=localhost:9876
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
可能遇到的报错如下:
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
-Djava.ext.dirs=bin/../lib:/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/jre/lib/ext:/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/lib/ext is not supported. Use -classpath instead.
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
还是在tools.sh做类似的修改,如下所示:
# 注释下面这行
#export CLASSPATH=.:${BASE_DIR}/conf:${CLASSPATH}
# 新增下面这行
export CLASSPATH=.:${BASE_DIR}/lib/*:${BASE_DIR}/conf:${CLASSPATH}
....
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m"
# 注释掉下面这行,JDK1.9后不再支持该参数
#JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${BASE_DIR}/lib:${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext"
JAVA_OPT="${JAVA_OPT} -cp ${CLASSPATH}"
重新保存后运行,如果看到如下输出则说明成功了:
st, brokerName=Tony-Mac-Pro.local, queueId=3], queueOffset=247]
SendResult [sendStatus=SEND_OK, msgId=7F000001203D1DE0ACA60ED7764803E0, offsetMsgId=AC101A0000002A9F0000000000031232, messageQueue=MessageQueue [topic=TopicTest, brokerName=Tony-Mac-Pro.local, queueId=0], queueOffset=248]
SendResult [sendStatus=SEND_OK, msgId=7F000001203D1DE0ACA60ED7764A03E1, offsetMsgId=AC101A0000002A9F00000000000312FD, messageQueue=MessageQueue [topic=TopicTest, brokerName=Tony-Mac-Pro.local, queueId=1], queueOffset=248]
SendResult [sendStatus=SEND_OK, msgId=7F000001203D1DE0ACA60ED7764C03E2, offsetMsgId=AC101A0000002A9F00000000000313C8, messageQueue=MessageQueue [topic=TopicTest, brokerName=Tony-Mac-Pro.local, queueId=2], queueOffset=248]
SendResult [sendStatus=SEND_OK, msgId=7F000001203D1DE0ACA60ED7764E03E3, offsetMsgId=AC101A0000002A9F0000000000031493, messageQueue=MessageQueue [topic=TopicTest, brokerName=Tony-Mac-Pro.local, queueId=3], queueOffset=248]
SendResult [sendStatus=SEND_OK, msgId=7F000001203D1DE0ACA60ED7764F03E4, offsetMsgId=AC101A0000002A9F000000000003155E, messageQueue=MessageQueue [topic=TopicTest, brokerName=Tony-Mac-Pro.local, queueId=0], queueOffset=249]
SendResult [sendStatus=SEND_OK, msgId=7F000001203D1DE0ACA60ED7764F03E5, offsetMsgId=AC101A0000002A9F0000000000031629, messageQueue=MessageQueue [topic=TopicTest, brokerName=Tony-Mac-Pro.local, queueId=1], queueOffset=249]
SendResult [sendStatus=SEND_OK, msgId=7F000001203D1DE0ACA60ED7765003E6, offsetMsgId=AC101A0000002A9F00000000000316F4, messageQueue=MessageQueue [topic=TopicTest, brokerName=Tony-Mac-Pro.local, queueId=2], queueOffset=249]
SendResult [sendStatus=SEND_OK, msgId=7F000001203D1DE0ACA60ED7765103E7, offsetMsgId=AC101A0000002A9F00000000000317BF, messageQueue=MessageQueue [topic=TopicTest, brokerName=Tony-Mac-Pro.local, queueId=3], queueOffset=249]
21:10:01.562 [NettyClientSelector_1] INFO RocketmqRemoting - closeChannel: close the connection to remote address[172.16.26.0:10911] result: true
21:10:01.564 [NettyClientSelector_1] INFO RocketmqRemoting - closeChannel: close the connection to remote address[127.0.0.1:9876] result: true
最终,我们和RocketMQ和解,完成了这次测试!
鸣谢下面的帮助,可以参考的资料如:
腾讯云上的教程
官方quick start