-
官网:http://rocketmq.apache.org/
-
源码:https://github.com/apache/rocketmq
-
github:https://github.com/apache/rocketmq.git
-
扩展源码:https://github.com/apache/rocketmq-externals
-
github:https://github.com/apache/rocketmq-externals.git
-
Apache RocketMQ™ is an open source distributed messaging and streaming data platform.
-
Apache RocketMQ™是一个开源分布式消息和流数据平台
Quick Start(先看看官网的怎么玩的)
This quick start guide is a detailed instruction of setting up RocketMQ messaging system on your local machine to send and receive messages.
这个快速启动指南详细说明了如何在本地机器上设置RocketMQ消息传递系统来发送和接收消息。
Prerequisite
The following softwares are assumed installed:
-
64bit OS, Linux/Unix/Mac is recommended;
-
64bit JDK 1.8+;
-
Maven 3.2.x
-
Git
Download & Build from Release
Click here to download the 4.3.0 source release. Also you could download a binary release from here.
Now execute the following commands to unpack 4.3.0 source release and build the binary artifact.
> unzip rocketmq-all-4.3.0-source-release.zip > cd rocketmq-all-4.3.0/ > mvn -Prelease-all -DskipTests clean install -U > cd distribution/target/apache-rocketmq
Start Name Server
> nohup sh bin/mqnamesrv & > tail -f ~/logs/rocketmqlogs/namesrv.log The Name Server boot success...
Start Broker
> nohup sh bin/mqbroker -n localhost:9876 & > tail -f ~/logs/rocketmqlogs/broker.log The broker[%s, 172.30.30.233:10911] boot success...
Send & Receive Messages
Before sending/receiving messages, we need to tell clients the location of name servers. RocketMQ provides multiple ways to achieve this. For simplicity, we use environment variable NAMESRV_ADDR
> export NAMESRV_ADDR=localhost:9876 > sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer SendResult [sendStatus=SEND_OK, msgId= ... > sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer ConsumeMessageThread_%d Receive New Messages: [MessageExt...
Shutdown Servers
> sh bin/mqshutdown broker The mqbroker(36695) is running... Send shutdown request to mqbroker(36695) OK > sh bin/mqshutdown namesrv The mqnamesrv(36664) is running... Send shutdown request to mqnamesrv(36664) OK
Start In STS
参考官网的quick start,实现STS的quick start。以后再也不用去虚拟机找rocketmq了!
导入源码到STS
-
安装STS(Spring Tool Suite)或者Eclipse
-
下载rocketmq和rocketmq-externals源码
-
新建rocketmq和rocketmq-externals各自的Java Working Set
-
分别导入源码到各自的Java Working Set
-
解决报错,ignore或者update
-
修改下设置,比如字体什么的
接下来干什么?
要在STS中启动RocketMQ,那么首先分析下官方提供的脚本是怎么启动的。对照官网quick start开始下面的流程。
分析mqnamesrv
-
位置:rocketmq\distribution\bin\
#!/bin/sh #[ -z STRING ] “STRING” 的长度为零则为真 # [ -h FILE ] 如果 FILE 存在且是一个符号连接则为真。 #$0 Shell本身的文件名 #$@ 是传给脚本的所有参数列表 if [ -z "$ROCKETMQ_HOME" ] ; then ## resolve links - $0 may be a link to maven's home PRG="$0" # need this for relative symlinks while [ -h "$PRG" ] ; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG="`dirname "$PRG"`/$link" fi done saveddir=`pwd` ROCKETMQ_HOME=`dirname "$PRG"`/.. # make it fully qualified ROCKETMQ_HOME=`cd "$ROCKETMQ_HOME" && pwd` cd "$saveddir" fi #这个if的作用就是没有ROCKETMQ_HOME环境变量的时候,设置一个ROCKETMQ_HOME环境变量。明显我们进来的时候是没有的,通过一系列操作后,ROCKETMQ_HOME就被设置为当前文件所在目录的上层目录,这里就是rocketmq\distribution\bin export ROCKETMQ_HOME sh ${ROCKETMQ_HOME}/bin/runserver.sh org.apache.rocketmq.namesrv.NamesrvStartup $@ #这个shell的目的是在当前目录执行runserver.sh org.apache.rocketmq.namesrv.NamesrvStartup $@
分析NamesrvStartup
#!/bin/sh #[ -e FILE ] 如果 FILE 存在则为真 error_exit () { echo "ERROR: $1 !!" exit 1 } [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/usr/java [ ! -e "$JAVA_HOME/bin/java" ] && error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)!" #这儿是确保找到AVA_HOME,不然就直接报错了 export JAVA_HOME export JAVA="$JAVA_HOME/bin/java" export BASE_DIR=$(dirname $0)/.. #BASE_DIR就是上一级的bin目录,也就是../bin export CLASSPATH=.:${BASE_DIR}/conf:${CLASSPATH} #把上级目录下的conf目录,也就是../conf加到CLASSPATH #=========================================================================================== # JVM Configuration #=========================================================================================== JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m" #运行在server模式;堆内存固定为4g,新生代2g,元空间128m,最大元空间320m JAVA_OPT="${JAVA_OPT} -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8 -XX:-UseParNewGC" #使用CMS垃圾收集器;在FULL GC的时候,对年老代的压缩;使用70%后开始CMS收集;降低标记停顿;每兆堆空闲空间中SoftReference的存活时间=0;新生代比例:1:1:8;新生代使用ParNew JAVA_OPT="${JAVA_OPT} -verbose:gc -Xloggc:/dev/shm/rmq_srv_gc.log -XX:+PrintGCDetails" #输出虚拟机中GC的详细情况;gc日志地址/dev/shm/rmq_srv_gc.log ;-XX:+PrintGCDetails JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow" #禁用吃异常优化 JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages" #启用大内存分页 JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib" #指定扩展类加载器加载的包路径 #JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n" #开启远程debug JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}" JAVA_OPT="${JAVA_OPT} -cp ${CLASSPATH}" $JAVA ${JAVA_OPT} $@
那么我们要干什么呢?
-
从shell看,我们要设置一个ROCKET_HOME,我们的启动类是org.apache.rocketmq.namesrv.NamesrvStartup
-
private String rocketmqHome = System.getProperty(MixAll.ROCKETMQ_HOME_PROPERTY, System.getenv(MixAll.ROCKETMQ_HOME_ENV));
-
ROCKET_HOME主要就是为了找到日志配置文件,那么可以手动指定一下环境
-
查看日志配置文件,使用到了user.home,那么手动写一个
-
虚拟机的启动优化,参数,可以在启动的时候加上,也可以不管
修改rocketmq-namesrv项目下的NamesrvStartup.java
public static void main(String[] args) { System.setProperty("rocketmq.home.dir", "../distribution");//新增 System.setProperty("user.home", ".");//新增 main0(args); }
接下来
Run As ->Java Application
The Name Server boot success. serializeType=JSON
然后用jconsole大致看一下,内存消耗的确不大,但是启动指定几个g是几个意思?
刷新一下,当前目录就可以看到日志了
同样的方法,启动brocker
shell文件大同小异,main方法如下,新增三行代码
rocketmq-brocker项目下的BrokerStartup.java
public static void main(String[] args) { System.setProperty("rocketmq.home.dir", "../distribution");//新增 System.setProperty("user.home", ".");//新增 args=new String[]{"-n","localhost:9876"};//新增 start(createBrokerController(args)); }
Run As ->Java Application
启动后刷新当前项目,可以看到日志目录和数据目录
启动控制台
-
我们已经导入了rocketmq-externals,找到rocketmq-console-ng项目
-
修改aplication.properties:rocketmq.config.namesrvAddr=localhost:9876
-
运行App类的main方法:Run As ->Java Application
-
访问localhost:8080
-
至此,我们一共启动了三个项目,一个nameserver,一个broker,一个console;nameserver类似于zk,broker用于实际消息收发,console用于可视化监控查看。
生产消费消息
找到项目:rocketmq-example
启动生产者
打开org.apache.rocketmq.example.quickstart.Producer类,加上producer.setNamesrvAddr("localhost:9876");
运行
启动消费者
打开org.apache.rocketmq.example.quickstart.Consumer类,加上consumer.setNamesrvAddr("localhost:9876");
运行