RocketMQ快速入门
- 准备工作
- 下载RocketMQ
- 环境要求
- JDK下载安装
- JDK下载
- JDK安装
- 安装RocketMQ
- 安装步骤
- 目录介绍
- 启动RocketMQ
- 测试RocketMQ
- 发送消息
- 接收消息
- 关闭RocketMQ
RocketMQ是阿里巴巴2016年开源的MQ中间件,使用Java语言开发,在阿里内部,RocketMQ承接了例如"双11"等高并发场景的消息流转,能够处理万亿级别的消息。
准备工作
下载RocketMQ
RocketMQ最新版本:5.2.0
下载地址
环境要求
- Linux64位系统
- JDK1.8(64位)
- 源码安装需要安装Maven3.2.x
JDK下载安装
JDK下载
JDK官网下载地址:https://www.oracle.com/java/technologies/downloads/
注意:根据项目需要选择对应的JDK版本号,本教程以jdk-8u371-linux-x64.tar.gz进行安装说明。
JDK安装
1.解压JDK安装包
[root@master ~]# cd /home/data/mq/
[root@master mq]# tar -xvf jdk-8u371-linux-x64.tar.gz
2.配置环境变量
[root@master mq]# vi /etc/profile.d/my_env.sh
添加内容如下:
#java environment
export JAVA_HOME=/home/data/mq/jdk1.8.0_371
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
3.刷新境变量
[root@master mq]# source /etc/profile
4.检查是否安装成功
[root@master mq]# java -version
安装RocketMQ
安装步骤
本教程以二进制包方式安装
1.解压安装包
[root@master ~]# cd /home/data/mq/
[root@master mq]# unzip rocketmq-all-5.2.0-bin-release.zip
2.进入安装目录
[root@master mq]# cd rocketmq-all-5.2.0-bin-release/
目录介绍
- bin:启动脚本,包括shell脚本和CMD脚本
- conf:实例配置文件,包括broker配置文件、logback配置文件等
- lib:依赖jar包,包括Netty、commons-lang、FastJSON等
启动RocketMQ
1.启动NameServer
# 1.启动NameServer
[root@master rocketmq-all-5.2.0-bin-release]# nohup sh bin/mqnamesrv &
[1] 51976
# 2.查看启动日志
[root@master rocketmq-all-5.2.0-bin-release]# tail -f ~/logs/rocketmqlogs/namesrv.log
NameServer服务启动成功日志输出如下:
2024-03-11 08:47:26 INFO main - rocketmqHome=/home/data/mq/rocketmq-all-5.2.0-bin-release
2024-03-11 08:47:26 INFO main - kvConfigPath=/root/namesrv/kvConfig.json
2024-03-11 08:47:26 INFO main - configStorePath=/root/namesrv/namesrv.properties
2024-03-11 08:47:26 INFO main - productEnvName=center
2024-03-11 08:47:26 INFO main - clusterTest=false
2024-03-11 08:47:26 INFO main - orderMessageEnable=false
2024-03-11 08:47:26 INFO main - returnOrderTopicConfigToBroker=true
2024-03-11 08:47:26 INFO main - clientRequestThreadPoolNums=8
2024-03-11 08:47:26 INFO main - defaultThreadPoolNums=16
2024-03-11 08:47:26 INFO main - clientRequestThreadPoolQueueCapacity=50000
2024-03-11 08:47:26 INFO main - defaultThreadPoolQueueCapacity=10000
2024-03-11 08:47:26 INFO main - scanNotActiveBrokerInterval=5000
2024-03-11 08:47:26 INFO main - unRegisterBrokerQueueCapacity=3000
2024-03-11 08:47:26 INFO main - supportActingMaster=false
2024-03-11 08:47:26 INFO main - enableAllTopicList=true
2024-03-11 08:47:26 INFO main - enableTopicList=true
2024-03-11 08:47:26 INFO main - notifyMinBrokerIdChanged=false
2024-03-11 08:47:26 INFO main - enableControllerInNamesrv=false
2024-03-11 08:47:26 INFO main - needWaitForService=false
2024-03-11 08:47:26 INFO main - waitSecondsForService=45
2024-03-11 08:47:26 INFO main - deleteTopicWithBrokerRegistration=false
2024-03-11 08:47:26 INFO main - configBlackList=configBlackList;configStorePath;kvConfigPath
2024-03-11 08:47:26 INFO main - bindAddress=0.0.0.0
2024-03-11 08:47:26 INFO main - listenPort=9876
2024-03-11 08:47:26 INFO main - serverWorkerThreads=8
2024-03-11 08:47:26 INFO main - serverCallbackExecutorThreads=0
2024-03-11 08:47:26 INFO main - serverSelectorThreads=3
2024-03-11 08:47:26 INFO main - serverOnewaySemaphoreValue=256
2024-03-11 08:47:26 INFO main - serverAsyncSemaphoreValue=64
2024-03-11 08:47:26 INFO main - serverChannelMaxIdleTimeSeconds=120
2024-03-11 08:47:26 INFO main - serverSocketSndBufSize=0
2024-03-11 08:47:26 INFO main - serverSocketRcvBufSize=0
2024-03-11 08:47:26 INFO main - writeBufferHighWaterMark=0
2024-03-11 08:47:26 INFO main - writeBufferLowWaterMark=0
2024-03-11 08:47:26 INFO main - serverSocketBacklog=1024
2024-03-11 08:47:26 INFO main - serverPooledByteBufAllocatorEnable=true
2024-03-11 08:47:26 INFO main - enableShutdownGracefully=false
2024-03-11 08:47:26 INFO main - shutdownWaitTimeSeconds=30
2024-03-11 08:47:26 INFO main - useEpollNativeSelector=false
2024-03-11 08:47:26 INFO main - Server is running in TLS permissive mode
2024-03-11 08:47:26 INFO main - Tls config file doesn't exist, skip it
2024-03-11 08:47:26 INFO main - Log the final used tls related configuration
2024-03-11 08:47:26 INFO main - tls.test.mode.enable = true
2024-03-11 08:47:26 INFO main - tls.server.need.client.auth = none
2024-03-11 08:47:26 INFO main - tls.server.keyPath = null
2024-03-11 08:47:26 INFO main - tls.server.keyPassword = null
2024-03-11 08:47:26 INFO main - tls.server.certPath = null
2024-03-11 08:47:26 INFO main - tls.server.authClient = false
2024-03-11 08:47:26 INFO main - tls.server.trustCertPath = null
2024-03-11 08:47:26 INFO main - tls.client.keyPath = null
2024-03-11 08:47:26 INFO main - tls.client.keyPassword = null
2024-03-11 08:47:26 INFO main - tls.client.certPath = null
2024-03-11 08:47:26 INFO main - tls.client.authServer = false
2024-03-11 08:47:26 INFO main - tls.client.trustCertPath = null
2024-03-11 08:47:27 INFO main - Using OpenSSL provider
2024-03-11 08:47:28 INFO main - SSLContext created for server
2024-03-11 08:47:28 INFO NSScanScheduledThread - start scanNotActiveBroker
2024-03-11 08:47:28 INFO main - RemotingServer started, listening 0.0.0.0:9876
2024-03-11 08:47:28 INFO main - Try to start service thread:NettyEventExecutor started:false lastThread:null
2024-03-11 08:47:28 INFO main - Start service thread:NettyEventExecutor started:true lastThread:Thread[NettyEventExecutor,5,main]
2024-03-11 08:47:28 INFO NettyEventExecutor - NettyEventExecutor service started
2024-03-11 08:47:28 INFO main - name server address updated. NEW : [10.244.0.0:9876] , OLD: null
2024-03-11 08:47:28 INFO main - Try to start service thread:NettyEventExecutor started:false lastThread:null
2024-03-11 08:47:28 INFO main - Start service thread:NettyEventExecutor started:true lastThread:Thread[NettyEventExecutor,5,main]
2024-03-11 08:47:28 INFO NettyEventExecutor - NettyEventExecutor service started
2024-03-11 08:47:28 INFO main - Try to start service thread:FileWatchService started:false lastThread:null
2024-03-11 08:47:28 INFO main - Start service thread:FileWatchService started:true lastThread:Thread[FileWatchService,5,main]
2024-03-11 08:47:28 INFO main - Try to start service thread:org.apache.rocketmq.namesrv.routeinfo.BatchUnregistrationService started:false lastThread:null
2024-03-11 08:47:28 INFO FileWatchService - FileWatchService service started
2024-03-11 08:47:28 INFO main - Start service thread:org.apache.rocketmq.namesrv.routeinfo.BatchUnregistrationService started:true lastThread:Thread[org.apache.rocketmq.namesrv.routeinfo.BatchUnregistrationService,5,main]
2024-03-11 08:47:28 INFO main - The Name Server boot success. serializeType=JSON, address 0.0.0.0:9876
2024-03-11 08:47:28 INFO NettyClientScan_thread_1 - createChannel: begin to connect remote host[10.244.0.0:9876] asynchronously
2024-03-11 08:47:28 INFO NettyClientWorkerThread_1 - NETTY CLIENT PIPELINE: CONNECT UNKNOWN => 10.244.0.0:9876
2024-03-11 08:47:28 INFO NettyClientScan_thread_1 - createChannel: connect remote host[10.244.0.0:9876] success, AbstractBootstrap$PendingRegistrationPromise@21da008d(success)
2.启动Broker
# 1.启动Broker
[root@master rocketmq-all-5.2.0-bin-release]# nohup sh bin/mqbroker -n localhost:9876 &
# 2.查看启动日志
tail -f ~/logs/rocketmqlogs/broker.log
- 问题描述
RocketMQ默认的虚拟机内存较大,启动Broker如果因为内存不足失败,需要编辑如下两个配置文件,修改JVM内存大小
# 编辑runbroker.sh 和 runserver.sh 修改默认JVM大小
[root@master ~]# cd /home/data/mq/rocketmq-all-5.2.0-bin-release/bin/
[root@master bin]# vi runbroker.sh
[root@master bin]# vi runserver.sh
-参数设置:
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
3.查看RocketMQ的进程是否启动
[root@master rocketmq-all-5.2.0-bin-release]# jps
52864 NamesrvStartup
56411 BrokerStartup
57739 Jps
测试RocketMQ
发送消息
# 1.设置环境变量
export NAMESRV_ADDR=localhost:9876
# 2.使用安装包的Demo发送消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
接收消息
# 1.设置环境变量
export NAMESRV_ADDR=localhost:9876
# 2.接收消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
关闭RocketMQ
[root@master ~]# cd /home/data/mq/rocketmq-all-5.2.0-bin-release/
# 1.关闭NameServer
[root@master rocketmq-all-5.2.0-bin-release]# sh bin/mqshutdown namesrv
The mqnamesrv(52864) is running...
Send shutdown request to mqnamesrv(52864) OK
# 2.关闭Broker
[root@master rocketmq-all-5.2.0-bin-release]# sh bin/mqshutdown broker
The mqbroker(56411) is running...
Send shutdown request to mqbroker(56411) OK