开发者学堂课程【全面讲解 Spring Cloud Alibaba 技术栈(知识精讲+项目实战)第四阶段:RocketMQ 环境搭建】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/686/detail/11903
RocketMQ 环境搭建
内容介绍:
一、RocketMQ 入门
二、RocketMQ 环境搭建
一、RocketMQ 入门
RocketMQ 是阿里巴巴开源的分布式消息中间件,现在是 Apache 的一个顶级项目。在阿里内部使用非常广泛,已经经过了"双11"这种万亿级的消息流转。
二、RocketMQ环境搭建
接下来我们先在 linux 平台下安装一个 RocketMQ 的服务(先准备好一台linux服务器)
1、环境准备
(1)下载 RocketMQ
http://rocketmg.apache.org/releasenotes/reease-notes-4.4.0/
访问官方地址,里面有最新的发行版4.6.0点进去,下面有下载地址源码包、二进制,可以根据自己的兴趣选择,如果想学习可以选源码包,为了安装服务用的是二进制包,这是最新的4.6.0的,课堂上使用的4.4.0。
切一下版本
(2)环境要求
①Linux64位操作系统
②64bitJDK1.8+
2、安装 RocketMQ
(1)上传文件到 Linux 系统
[root@heimarocketmq]#1s/usr/1ocal/src/
rocketmq-a11-4.1.0-bin-release.zip
(2)解压到安装目录
[root@heimasrc]#unziprocketmq-a11-4.4.0-bin-release.zip
[root@heimasrc]#mvrocketmg-a11-4.4.0-bin-release../rock
etmg
找到 usr-local-src 目录:
[root@heima~]#cd/usr/
bin/games/lib/libexec/sbin/src/
etc/include/lib64/local/share/tmp/
[root@heima~]#cd/usr/local/src/
[root@heimasrc]#clear
//包已经上传,解压缩
[root@heimasrc]#ls
rocketmq-all-4.4.0-bin-release.zip
[root@heimasrc]#unziprocketmq-all-4.4.0-bin-release.zip
creating:rocketmg-all-4.4.0-bin-release/cont/2m-2s-sync
inflating:rocketmq-all-4.4.0-bin-release/conf/2m-2s-sync/br
oker-b.properties
inflating:rocketmq-all-4.4.0-bin-release/conf/2m-2s-sync/br
oker-b-s.properties
inflating:rocketmq-all-4.4.0-bin-release/conf/2m-2s-sync/br
oker-a.properties
inflating:rocketmg-all-4.4.0-bin-release/conf/2m-2s-sync/br
oker-a-s.properties
creating:rocketmg-all-4.4.0-bin-release/conf/2m-2s-async/
inflating:rocketmg-all-4.4.0-bin-release/conf/2m-2s-async/b
roker-b.properties
inflating:rocketmg-all-4.4.0-bin-release/conf/2m-2s-asvnc/b
roker-b-s.properties
inflating:rocketmg-all-4.4.0-bin-release/conf/2m-2s-async/b
roker-a.properties
inflating:rocketmg-all-4.4.0-bin-release/conf/2m-2s-async/b
roker-a-s.properties
inflating:rocketmg-all-4.4.0-bin-release/conf/logback
broker.xml
inflating:rocketmg-all-4.4.0-bin-release/conf/logbacktools.
xml
inflating:rocketmg-all-4.4.0-bin-release/conf/broker.conf
inflating:rocketmg-all-4.4.0-bin-release/conf/tools.yml
creating:rocketmg-all-4.4.0-bin-release/conf/2m-noslave/
inflating:rocketmg-all-4.4.0-bin-release/conf/2m-noslave/br
oker-b.properties
inflating:rocketmg-all-4.4.0-bin-release/conf/2m-noslave/br
oker-trace.properties
inflating:rocketmg-all-4.4.0-bin-release/conf/2m-noslave/br
oker-a.properties
inflating:rocketmg-all-4.4.0-bin-release/conf/plainacl.yml
inflating:rocketmq-all-4.4.0-bin-release/conf/logbacknames
rv.xml
[root@heimasrc]#
[root@heimasrc]#
[root@heimasrc]#ls
rocketmq-all-4.4.0-bin-releaserocketma-all-4.4.0-bin-releas
e.zip
[root@heimasrc]#mvrocketmq-all-4.4.0-bin-release/usr/loc
al/rocketmq
[root@heimasrc]#
[root@heimasrc]#clear
//切一下:
[root@heimasrc]#cd../
[rootdheimalocal]#pwd
/usr/local
[root@heimalocal]#ls
binetcgamesincludeliblib64libexecrocketmgsbinsharesrc
[root@heimalocal]#cdrocketmq/
[root@heimarocketmg]#ls
benchmarkbiniconflibLICENSENOTICEREADME.md[rootaheima
rocketmg]#cdin
-bash:cd:in:没有那个文件或目录
[rootheimarocketmg]#cdbin
[rootaheimabin]#ll
//有很多启动命令以及配置文件
[rootaheimarocketmg]#cdbin
[rootaheimabin]#ll
总用量120
//用启动命令把里面的服务启动起来
[root@heimabin]#clear
3、启动 RocketMQ
(1)切换到安装目录
[root@heimarocketmg]#1s
benchmarkbinconflibLICENSENOTICEREADME.md
(2)启动 NameServer
[root@heimarocketma]#nohup./bin/mgnamesrv&[1]1467
#只要进程不报错,就应该是启动成功了,可以查看一下日志
[root@heimarocketmg]#tail-f/root/logs/rocketmglogs/nam
esrv.1og
启动 RocketMQ 需要跑两个组件,第一个为 NameServer 第二个为 Broker,Rocket 运行必须的两个组件。
①启动 NameServer
nohup 的命令,server 让后台启动,如果没有报错基本上问题就不大,可以通过查看日志来看是否启动成功,日志在目录 log 下面的 rocking clocks 里面有 name server.log 就是目录,有 the name server boot success 说明启动成功。
[root@heimabin]#nohup./mqnamesrv&
[1]15861
[rootaheimabin]#nohup:忽略输入并把输出追加到"nohup.out
//根据端口netstat-an|grep9876也能证明启动成功
[root@heimabin]#netstat-angrep9876
tcp600:::9876:::*LISTEN
[root@heimabin]#
②启动 Broker
启动之前需要编辑配置文件,修改两个配置
第一个组件有 runserver.sh:
[root@heimabin]#netstat-angrep9876
tcp600:::9876:::*LISTEN
[root@heimabin]#vimrunserver.sh
里面有内存的限制,一开始需要内存比较大,服务器内存没有那么大,改成256兆,后面也改成256兆,再后面改成128兆,第一个文件修改完。
exportJAVAHOME
exportJAVA="$JAVAHOME/bin/java'
exportBASEDIR=$(dirname$0)/..
exportCLASSPATH=.:${BASEDIR}/conf:${CLASSPATH}
#======================================================
#JVMConfiguration
#======================================================
JAVAOPT="${JAVAOPT!-server-Xms156m-Xmx256m-Xmn128m
-XX:MetaspaceSize=128m-xx:MaxMetaspaceSize=320m"
JAVAOPT="${JAVAOPT}-XX:+UseConcMarkSweepGC-XX:+Use
CMSCompactAtFullCollection-XX:CMSInitiatina0ccupancyFrac
tion=70-XX:+CMSParallelRemarkEnabled-XX:SoftRefLRUPoli
cyMSPerMB=0XX:+CMSClassUnloadingEnabled-xX:Survivor
Ratio=8-XX:-UseParNewGC"
JAVAOPT="${JAVAOPT}-verbose:gc-Xloggc:/dev/shm/rmq
srvgc.log-XX:+PrintGCDetails"
JAVAOPT="${JAVAOPT}-XX:-0mitStackTraceInFastThrow’
JAVAOPT="${JAVAOPT!-XX:-UseLargePages"
JAVA-OPT="${JAVA-OPT}-Diava.ext.dirs=${
JAVAHOME}/ire/lib/ext:${BASEDIR}/lib"
#JAVAOPT="${JAVAOPT}-Xdebug-Xrunidwp:transport=dt
socket,address=9555,server=y,suspend=n"
JAVAOPT="${JAVAOPT}${JAVAOPTEXT}"
JAVAOPT="${JAVAOPT}-cp${CLASSPATH}”
//修改第二个为runbroker.sh
[root@heimabin]#netstat-angrep9876
tcp600:::9876:::*LISTEN
[root@heimabin]#vimrunserver.sh
[root@heimabin]#vimrunbroker.sh
同样需要修改配置,调整一下改成256兆、256兆、128兆保存:
[-e"$JAVAHOME/bin/java"]&&JAVAHOME=$HOME/jdk/java!
[-e"$JAVAHOME/bin/java"]&&JAVAHOME=/usr/java
[!-e"$JAVA_HOME/bin/java"]&&errorexit"Pleasesetthe
JAVA_HOMEvariableinyourenvironment,Weneedjava(x64)!"
exportJAVA_HOME
exportJAVA="$JAVAHOME/bin/java'
exportBASEDIR=$(dirname$0)/..
exportCLASSPATH=.:${BASEDIR}/conf:${CLASSPATH}
#======================================================
#JVMConfiguration
#======================================================
JAVAOPT="${JAVAOPT!-server-Xms256m-Xmx256m-Xmn128m"
JAVAOPT="${JAVAOPT}-XX:+UseG1GC-XX:G1HeapReaionSize=16m
-XX:G1ReservePercent=25-XX:InitiatingHeap0ccupan
cyPercent=30-XX:SoftRefLRUPolicyMSPerMB=0"
JAVAOPT="${JAVAOPT}-verbose:gc-Xloggc:/dev/shm/magc
&p.log-xx:+PrintGCDetails-Xx:+PrintGCDateStamps--XX:+
PrintGCApplicationStoppedTime-XX:+PrintAdaptiveSizePolicy"
JAVAOPT="${JAVAOPT}-XX:+UseGCLogFileRotation-XX:
NumberOfGCLogFiles=5-XX:GCLogFileSize=30m"
启动 broker,nphup.MQ 不 rock./mabroker,需要指定 name server 的地址localhost:9876,然后后台运行,同样可以查看日志里面的输出,速度目录下面的 logs/rocketmqlogs/broker.log
[root@heimabin]#netstat-angrep9876
tcp600:::9876:::*LISTEN
[root@heimabin]#vimrunserver.sh
[rootaheimabin]#vimrunbroker.sh
[root@heimabin]#nohup./mgbroker-nlocalhost:9876&
[2]16486
[root@heimabin]#nohup:忽略输入并把输出追加到"nohup.out'
[rootaheimabin]#
[rootaheimabin]#
[rootaheimabin]#tail-f~/logs/rocketmglogs/broker.log
只要能看里面有 starch,服务启动成功的标识 goodsuccess 启动成功就可以。
[rootaheimabin]#nohup./mgbroker-nlocalhost:9876&
[2]16486
[root@heimabin]#nohup:忽略输入并把输出追加到"nohup.out'
[root@heimabin]#
[rootaheimabin]#
[root@heimabin]#tail-f~/logs/rocketmglogs/broker.log
2020-01-0712:01:14WARNmain-Loaddefaultdiscardmessaae
hookservice:DefaultTransactionalMessageCheckListener
2020-01-0712:01:14INFOmain-Thebrokerdosenotenableacl
2020-01-0712:01:14INFOFileWatchService-FileWatchService
servicestarted
2020-01-0712:01:14INFOPullRequestHoldService-Pull
RequestHoldServiceservicestarted
2020-01-0712:01:15INFObrokerOutApithread1-register
brokertonameserverlocalhosts98760K
2020-01-0712:01:15INFOmain-Starttransactionservice!
2020-01-0712:01:15INFOmain-Thebroker[heima
192.168.109.131:10911]bootsuccess.serializeType=JSONand
nameserverislocalhost:9876
2020-01-0712:01:24INFOBrokerControllerScheduledThreadl-
dispatchbehindcommitlog0bytes
2020-01-0712:01:24INFOBrokerControllerScheduledThreadl-
Slavefallbehindmaster:0bytes
2020-01-0712:01:25INFObrokerOutApithread_2-register
brokertonameserverlocalhost;
98760K
^C
[root@heimabin]#
[root@heimabin]#
[root@heimabin]#clear
整个组建起来以后整个RocketMQ已经可以使用。
4、测试 RocketMQ
官方给了两个测试文件,只需要通过这两个脚本从一端发送另一端接收就可以,可以测一下MQ现在跑起来,只要通过一个脚本从左边向 MQ 发东西,右边通过一个脚本从 MQ 里面接东西,只要能发成功,并且能接到就证明 MQ 能够正常工作。
(1)测试消息发送
[rootGheimarocketmg]#exportNAMESRV_ADDR=1oca1host:9876
[root@heimarocketmq]#bin/tools.shorg.apache.rocketmq.
example.quickstart.Producer
(2)测试消息接收
[root@heimarocketmq]#exportNAMESRV_ADDR=1oca1host:9876
[root@heimarocketmg]#bin/toels.shorg.apache.rocketm
g.example.quickstart.Consumer
测试再开一个客户端:
Lastlogin:TueJan711:53:542020from192.168.1
[root@heima~]#clear
切到/usr/local/rocketmq 安装目录 bin 下面:
[root@heima~]#cd/usr/local/rocketmq/bbenchmark/bin/
[root@heima~]#cd/usr/local/rocketmq/bin/
[root@heima~]#clear
两边都到 bin 目录下,任务为从左边利用脚本发,从右边利用脚本收。
①发先声明一个 NameServer 地址,接下来通过脚本发送。
[root@heimabin]#exportNAMESRVADDR=localhost:9876
[rootaheimabin]#bin/tools.shora.apache.rocketma.example.
quickstart.Producer-bash:bin/tools.sh:没有那个文件或目录
[root@heimabin]#./tools.shorg.apache.rocketmq.example.
quickstart.Producer
OpenJDK64-BitServerVMwarning:ignoringoption
PermSize=128m;supportwasremovedin8.0
OpenJDK64-BitServerVMwarning:ignoringoption
MaxPermSize=128m;supportwasremovedin8.0
12:03:32.934[main]DEBUGi.n.u.i.l.InternalLoggerFactory
-UsingSLF4]asthedefaultloggingframework
//已经开始发送
SendResult[sendStatus=SENDOK,msgId=COA86D8342841D4
4BCFA217CA9BC03E6,offsetMsgId=COA86D8300002A9FO0000
0000002BD4A,messageQueue=MessageQueue[topic=TopicTest,
brokerName=heima,queueId=2],queueOffset=249]
SendResult[sendStatus=SENDOK.msaId=C0A86D8342841D44
BCFA217CA9BE03E7,offsetMsaId=C0A86D8300002A9F0000000
00002BDFE,messageQueue=MessageQueue[topic=TopicTest,
brokerName=heima,queueId=3],queueOffset=249]
12:03:38.138[NettyClientSelector1]INFORocketmqRemoting
-closeChannel:closetheconnectiontoremote
address[127.0.0.1:9876]result:true
12:03:38.139[NettyClientSelector1]INFORocketmaRemotina
-closeChannel:closetheconnectiontoremote
address[192.168.109.131:10909]result:true
12:03:38.155[NettyClientSelector1]INFORocketmgRemoting-
closeChannel:closetheconnectionre
moteaddress[192.168.109.131:10911]result:true
[root@heimabin]#
[root@heimabin]#
[root@heimabin]#
[root@heimabin]#
[rootaheimabin]#
[root@heimabin]#
[root@heimabin]#
//发送完成
②启动接收脚本,接收后要去声明 NameServer 的地址。
[root@heimabin]#exportNAMESRVADDR=localhost:9876
[root@heimabin]#./tools.shorg.apache.rocketmq.example.
quickstart.Consumer
OpenJDK64-BitServerVMwarning:ignoringoption
PermSize=128m;supportwasremovedin8.0
OpenJDK64-BitServerVMwarning:ignoringoption
MaxPermSize=128m;supportwasremovedin8.0
12:04:09.231[main]DEBUGi.n.u.i.l.InternalLoggerFactory-
UsingSLF4]asthedefaultloggingframewdrk
ConsumerStarted.
//已经可以接到消息,接处于监听状态
再来发一次,已经接收到。
证明 RocketMQ 是正常可运行的。
5、关闭 RocketMQ
[root@heimarocketmg]#bin/mgshutdownbroker
[root@heimarocketmq]#bin/mqshutdownnamesrv
两个命令停止 block、namesrv 的。