1. 程式人生 > >RocketMQ學習筆記(一)eclipse版的quickstart

RocketMQ學習筆記(一)eclipse版的quickstart

學而時習之,不亦說乎!

自己搭建個學習用的RocketMQ總是很麻煩,需要虛擬機器環境,網路,需要安裝rocketmq,啟動。時間久了再去看,又不知道這個虛擬機器是幹嘛的了。

直接在eclipse中啟動,簡單省事,用的時候啟動,用完關掉就行了。

先看下圖,給還沒開始的你一點信心。

 

RocketMQ

  • 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:

  1. 64bit OS, Linux/Unix/Mac is recommended;

  2. 64bit JDK 1.8+;

  3. Maven 3.2.x

  4. 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

  1. 安裝STS(Spring Tool Suite)或者Eclipse

  2. 下載rocketmqrocketmq-externals原始碼

  3. 新建rocketmq和rocketmq-externals各自的Java Working Set

  4. 分別匯入原始碼到各自的Java Working Set

  5. 解決報錯,ignore或者update

  6. 修改下設定,比如字型什麼的

接下來幹什麼?

要在STS中啟動RocketMQ,那麼首先分析下官方提供的指令碼是怎麼啟動的。對照官網quick start開始下面的流程。

分析mqnamesrv

  • 位置:rocketmq\distribution\bin\

#!/bin/sh
#[ -z STRING ]  “STRING” 的長度為零則為真
# [ -h FILE ]  如果 FILE 存在且是一個符號連線則為真。
#$0 Shell本身的檔名
#[email protected] 是傳給指令碼的所有引數列表
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 [email protected]
#這個shell的目的是在當前目錄執行runserver.sh org.apache.rocketmq.namesrv.NamesrvStartup [email protected]

 



分析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} [email protected]

 

那麼我們要幹什麼呢?

  • 從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

啟動後重新整理當前專案,可以看到日誌目錄和資料目錄

啟動控制檯

  1. 我們已經匯入了rocketmq-externals,找到rocketmq-console-ng專案

  2. 修改aplication.properties:rocketmq.config.namesrvAddr=localhost:9876

  3. 執行App類的main方法:Run As ->Java Application

  4. 訪問localhost:8080

  5. 至此,我們一共啟動了三個專案,一個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");

執行