RocketMQ整体认知
RocketMQ是一款分布式、队列模型的消息中间件:
- 天然支持集群模型、负载均衡、水平扩展能力
- 拥有亿级别的消息堆积能力
- 采用零拷贝的原理、顺序写盘、随机读
- 提供了丰富的API,是MQ圈子里功能最为强大的
- 代码优秀,底层通信框架采用Netty NIO框架
- NameServer代替Zookeper,定制化更轻量
- 强调集群无单点,可扩展,任意一点高可用,水平可扩展
- 消息失败重试机制、消息可查询
- 开源社区活跃、成熟度(经过双十一考验)
RocketMQ概念模型
**Producer:**消息生产者,负责产生消息,一般由业务系统负责产生消息 **Consumer:**消息消费者,负责消费消息,一般是后台系统负责异步消费 **Push Consumer:**Consumer的一种,需要向Consumer对象注册监听 **Pull Consumer:**Consumer的一 种,需要主动请求Broker拉取消息 **Producer Group:**生产者集合,一般用于发送一类消息 **Consumer Group:**消费者集合,一般用于接受一类消息进行消费 **Broker:**MQ消息服务(中转角色,用于消息存储与生产消费转发)
RocketMQ源码包编译
首先,准备好Java和Maven环境:
[root@rocketmq01 ~]# java --version
java version "1.8.0_261"
Java(TM) SE Runtime Environment (build 1.8.0_261-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.261-b12, mixed mode)
[root@rocketmq01 ~]# mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/maven
Java version: 1.8.0_261, vendor: Oracle Corporation, runtime: /usr/local/jdk/1.8/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.el7.x86_64", arch: "amd64", family: "unix"
[root@rocketmq01 ~]#
- **Tips:**最好是使用JDK1.8,因为目前版本的RocketMQ的启动脚本都是基于1.8的,使用高版本的JDK需要自己去修改启动脚本比较麻烦
根据官方文档的描述下载最新版本的源码包:
- http://rocketmq.apache.org/docs/quick-start/
然后上传到服务器:
[root@rocketmq01 /usr/local/src]# ls
rocketmq-all-4.7.1-source-release.zip
[root@rocketmq01 /usr/local/src]#
解压源码包:
[root@rocketmq01 /usr/local/src]# unzip rocketmq-all-4.7.1-source-release.zip
[root@rocketmq01 /usr/local/src]# cd rocketmq-all-4.7.1-source-release
[root@rocketmq01 /usr/local/src/rocketmq-all-4.7.1-source-release]# ls
acl BUILDING common dev docs filter logappender namesrv openmessaging README.md srvutil style tools
broker client CONTRIBUTING.md distribution example LICENSE logging NOTICE pom.xml remoting store test
[root@rocketmq01 /usr/local/src/rocketmq-all-4.7.1-source-release]#
RocketMQ源码包结构说明:
- broker:主要的业务逻辑,消息收发,主从同步,pagecache
- client:客户端接口,比如生产者和消费者
- example:示例,比如生产者和消费者
- common:公用数据结构等等
- distribution:编译模块,编译输出等
- fliter:进行Broker过滤的不感兴趣的消息传输,减小带宽压力
- logappender、logging:日志相关
- namesrv:Namesrver服务,用于服务协调
- openmessaging:对外提供服务
- remoting:远程调用接口,封装Netty底层通信
- srvutil:提供-些公用的工具方法,比如解析命令行参数
- store:消息存储
- tools:管理工具,比如有名的mqadmin工具
然后使用如下命令对源码进行编译:
[root@rocketmq01 /usr/local/src/rocketmq-all-4.7.1-source-release]# mvn -Prelease-all -DskipTests clean install -U
编译成功最后会输出如下内容,所有模块的编译结果都是SUCCESS状态:
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Apache RocketMQ 4.7.1 4.7.1:
[INFO]
[INFO] Apache RocketMQ 4.7.1 .............................. SUCCESS [04:15 min]
[INFO] rocketmq-logging 4.7.1 ............................. SUCCESS [ 24.843 s]
[INFO] rocketmq-remoting 4.7.1 ............................ SUCCESS [ 13.108 s]
[INFO] rocketmq-common 4.7.1 .............................. SUCCESS [ 26.304 s]
[INFO] rocketmq-client 4.7.1 .............................. SUCCESS [ 11.852 s]
[INFO] rocketmq-store 4.7.1 ............................... SUCCESS [ 8.760 s]
[INFO] rocketmq-srvutil 4.7.1 ............................. SUCCESS [ 0.855 s]
[INFO] rocketmq-filter 4.7.1 .............................. SUCCESS [ 6.202 s]
[INFO] rocketmq-acl 4.7.1 ................................. SUCCESS [ 7.349 s]
[INFO] rocketmq-broker 4.7.1 .............................. SUCCESS [ 2.162 s]
[INFO] rocketmq-tools 4.7.1 ............................... SUCCESS [ 1.289 s]
[INFO] rocketmq-namesrv 4.7.1 ............................. SUCCESS [ 0.628 s]
[INFO] rocketmq-logappender 4.7.1 ......................... SUCCESS [ 3.754 s]
[INFO] rocketmq-openmessaging 4.7.1 ....................... SUCCESS [ 26.613 s]
[INFO] rocketmq-example 4.7.1 ............................. SUCCESS [ 0.729 s]
[INFO] rocketmq-test 4.7.1 ................................ SUCCESS [ 14.090 s]
[INFO] rocketmq-distribution 4.7.1 ........................ SUCCESS [01:20 min]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 08:08 min
[INFO] Finished at: 2020-11-30T10:40:29+08:00
[INFO] ------------------------------------------------------------------------
然后在distribution/target/
目录下,可以看到编译打包好的可分发包:
[root@rocketmq01 /usr/local/src/rocketmq-all-4.7.1-source-release]# ls distribution/target/
archive-tmp checkstyle-cachefile checkstyle-checker.xml checkstyle-result.xml maven-shared-archive-resources rocketmq-4.7.1 rocketmq-4.7.1.tar.gz rocketmq-4.7.1.zip
[root@rocketmq01 /usr/local/src/rocketmq-all-4.7.1-source-release]#
RocketMQ环境搭建
配置hosts
:
[root@rocketmq01 ~]# vim /etc/hosts
192.168.243.169 rocketmq-nameserver1 rocketmq-master1
[root@rocketmq01 ~]#
将编译打包好的可分发包解压到合适的目录下,并进入解压后的目录:
[root@rocketmq01 /usr/local/src/rocketmq-all-4.7.1-source-release]# tar -zxvf distribution/target/rocketmq-4.7.1.tar.gz -C /usr/local
[root@rocketmq01 /usr/local/src/rocketmq-all-4.7.1-source-release]# cd /usr/local/rocketmq-4.7.1/
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# ls
benchmark bin conf lib LICENSE NOTICE README.md
[root@rocketmq01 /usr/local/rocketmq-4.7.1]#
创建数据存储目录:
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# mkdir -p store/commitlog store/consumequeue store/index
- commitlog:生产者投递到rocketmq的数据所存储的目录
- consumequeue:存储与数据库索引文件类似的数据
- index:随机读时用到的索引文件
修改配置文件:
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# echo "" > conf/2m-2s-async/broker-a.properties
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# vim conf/2m-2s-async/broker-a.properties
#所属集群名字
brokerClusterName=rocketmq-cluster
#broker 名字,注意此处不同的配置文件填写的不一样
brokerName=broker-a
#0 表示 Master,>0 表示 Slave
brokerId=0
#nameServer 地址,分号分割
namesrvAddr=rocketmq-nameserver1:9876
#在发送消息时,自动创建服务器不存在的 topic,默认创建的队列数
defaultTopicQueueNums=4
#是否允许 Broker 自动创建 Topic,建议线下开启,线上关闭
autoCreateTopicEnable=true
#是否允许 Broker 自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup=true
#Broker 对外服务的监听端口
listenPort=10911
#删除文件时间点,默认凌晨 4 点
deleteWhen=04
#文件保留时间,默认 48 小时
fileReservedTime=120
#commitLog 每个文件的大小默认 1G
mapedFileSizeCommitLog=1073741824
#ConsumeQueue 每个文件默认存 30W 条,根据业务情况调整
mapedFileSizeConsumeQueue=300000
#destroyMapedFileIntervalForcibly=120000
#redeleteHangedFileInterval=120000
#检测物理文件磁盘空间
diskMaxUsedSpaceRatio=88
#存储路径
storePathRootDir=/usr/local/rocketmq-4.7.1/store
#commitLog 存储路径
storePathCommitLog=/usr/local/rocketmq-4.7.1/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/usr/local/rocketmq-4.7.1/store/consumequeue
#消息索引存储路径
storePathIndex=/usr/local/rocketmq-4.7.1/store/index
#checkpoint 文件存储路径
storeCheckpoint=/usr/local/rocketmq-4.7.1/store/checkpoint
#abort 文件存储路径
abortFile=/usr/local/rocketmq-4.7.1/store/abort
#限制的消息大小
maxMessageSize=65536
#flushCommitLogLeastPages=4
#flushConsumeQueueLeastPages=2
#flushCommitLogThoroughInterval=10000
#flushConsumeQueueThoroughInterval=60000
#Broker 的角色
#- ASYNC_MASTER 异步复制 Master
#- SYNC_MASTER 同步双写 Master
#- SLAVE
brokerRole=ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
#checkTransactionMessageEnable=false
#发消息线程池数量
#sendMessageThreadPoolNums=128
#拉消息线程池数量
#pullMessageThreadPoolNums=128
修改日志配置文件:
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# mkdir logs
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# sed -i 's#${user.home}#/usr/local/rocketmq-4.7.1#g' conf/*.xml
如果机器内存比较小(小于8G),就需要根据实际情况修改下启动脚本的JVM参数,但不能低于1G:
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# vim bin/runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn2g"
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# vim bin/runserver.sh
JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
配置环境变量:
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# vim /etc/profile
export ROCKETMQ_HOME=/usr/local/rocketmq-4.7.1
export PATH=$PATH:$ROCKETMQ_HOME/bin
[root@rocketmq01 /usr/local/rocketmq-4.7.1]# source /etc/profile
完成配置后,就可以启动RocketMQ了:
[root@rocketmq01 ~]# nohup sh mqnamesrv &
[root@rocketmq01 ~]# nohup sh mqbroker -c /usr/local/rocketmq-4.7.1/conf/2m-2s-async/broker-a.properties >/dev/null 2>&1 &
检查服务的进程和端口是否正常:
[root@rocketmq01 ~]# jps
3602 BrokerStartup
3502 NamesrvStartup
3662 Jps
[root@rocketmq01 ~]# netstat -lntp |grep java
tcp6 0 0 :::10909 :::* LISTEN 3602/java
tcp6 0 0 :::10911 :::* LISTEN 3602/java
tcp6 0 0 :::10912 :::* LISTEN 3602/java
tcp6 0 0 :::9876 :::* LISTEN 3502/java
[root@rocketmq01 ~]#
若想停止Name Server和Broker,则依次执行以下两条命令即可:
[root@rocketmq01 ~]# mqshutdown broker
[root@rocketmq01 ~]# mqshutdown namesrv
RocketMQ控制台使用介绍
RocketMQ官方提供了一个基于Spring Boot开发的可视化控制台,可以方便我们查看RocketMQ的运行情况以及提升运维效率。RocketMQ在如下仓库提供了一些扩展组件,我们要使用到的控制台就包含在内:
- https://github.com/apache/rocketmq-externals/tree/master/
RocketMQ控制台是使用Spring Boot编写的,我们需要将源码克隆下载并修改相关配置即可使用:
[root@rocketmq01 /usr/local/src]# git clone https://github.com/apache/rocketmq-externals.git
修改rocketmq-console
项目中的application.properties
配置文件,我这里主要是修改了监听端口和Name Server的连接地址,至于其他配置项有需要的话可按照说明自行修改:
[root@rocketmq01 /usr/local/src]# cd rocketmq-externals/rocketmq-console/
[root@rocketmq01 /usr/local/src/rocketmq-externals/rocketmq-console]# vim src/main/resources/application.properties
# console的监听端口,默认是8080
server.port=8999
# Name Server的连接地址;非必须,可以在启动了console后,在控制台导航栏 - 运维 - NameSvrAddrList一栏设置
rocketmq.config.namesrvAddr=192.168.243.169:9876
然后执行如下命令进行编译打包:
[root@rocketmq01 /usr/local/src/rocketmq-externals/rocketmq-console]# mvn clean package -DskipTests
...
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 04:52 min
[INFO] Finished at: 2020-11-30T14:26:20+08:00
[INFO] ------------------------------------------------------------------------
打包完成后就可以启动RocketMQ控制台了:
[root@rocketmq01 /usr/local/src/rocketmq-externals/rocketmq-console]# java -jar target/rocketmq-console-ng-2.0.0.jar
RocketMQ控制台在运行的过程中,可能会输出如下错误:
ERROR Exception caught: mqAdminExt get broker stats data TOPIC_PUT_NUMS failed
但是这个错误并不影响正常运行,具体原因可以参考如下文章的说明:
- https://juejin.cn/post/6870762347166728200
使用浏览器访问控制台,正常的情况下能看到如下界面: