搭建 Apache RocketMQ 單機環境

本文快速入門,RocketMQ消息系統的安裝部署,發送,和接收消息的詳細說明。java

環境須要

64位操做系統,建議使用Linux / Unix /node

  • CentOs7.3
  • 64bit JDK 1.8+
  • Maven 3.2.x

JDK Maven 的安裝自行 Google 或者去我博客 http://www.ymq.io 搜索安裝apache

下載和構建

下載4.2.0源碼版本: https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.2.0/rocketmq-all-4.2.0-source-release.zipapp

下載4.2.0二進制版本: http://rocketmq.apache.org/release_notes/release-notes-4.2.0/tcp

如今執行如下命令來解壓4.2.0源碼版本並構建二進制文件ui

& unzip rocketmq-all-4.2.0-source-release.zip
& cd rocketmq-all-4.2.0/
& mvn -Prelease-all -DskipTests clean install -U
& mv distribution/target/apache-rocketmq /opt/apache-rocketmq

編譯成功的響應spa

[INFO] Reactor Summary:
[INFO] 
[INFO] Apache RocketMQ 4.2.0 .............................. SUCCESS [04:21 min]
[INFO] rocketmq-remoting 4.2.0 ............................ SUCCESS [ 25.561 s]
[INFO] rocketmq-common 4.2.0 .............................. SUCCESS [  4.533 s]
[INFO] rocketmq-client 4.2.0 .............................. SUCCESS [  5.804 s]
[INFO] rocketmq-store 4.2.0 ............................... SUCCESS [  5.239 s]
[INFO] rocketmq-srvutil 4.2.0 ............................. SUCCESS [  2.177 s]
[INFO] rocketmq-filter 4.2.0 .............................. SUCCESS [  1.262 s]
[INFO] rocketmq-broker 4.2.0 .............................. SUCCESS [  3.129 s]
[INFO] rocketmq-tools 4.2.0 ............................... SUCCESS [  1.995 s]
[INFO] rocketmq-namesrv 4.2.0 ............................. SUCCESS [  1.322 s]
[INFO] rocketmq-logappender 4.2.0 ......................... SUCCESS [  1.549 s]
[INFO] rocketmq-openmessaging 4.2.0 ....................... SUCCESS [  1.560 s]
[INFO] rocketmq-example 4.2.0 ............................. SUCCESS [  1.242 s]
[INFO] rocketmq-filtersrv 4.2.0 ........................... SUCCESS [  0.680 s]
[INFO] rocketmq-test 4.2.0 ................................ SUCCESS [  3.047 s]
[INFO] rocketmq-distribution 4.2.0 ........................ SUCCESS [ 27.005 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:09 min
[INFO] Finished at: 2018-02-01T15:51:26+08:00
[INFO] Final Memory: 73M/411M
[INFO] ------------------------------------------------------------------------

Start Name Server

默認 RocketMQ Server 內存須要很大的操作系統

vi /bin/runserver.sh
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"

我修改爲code

JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
& cd /opt/apache-rocketmq
& nohup sh bin/mqnamesrv > /dev/null 2>&1 &
& tail -f ~/logs/rocketmqlogs/namesrv.log

會看到以下響應信息server

2018-02-01 16:15:16 INFO main - The Name Server boot success...

Start Broker

默認 RocketMQ Broker 內存須要很大的

vi /bin/runbroker.sh
JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"

我修改爲

JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m"

啓動Broker

& nohup sh bin/mqbroker -n localhost:9876 > /dev/null 2>&1 &
& tail -f ~/logs/rocketmqlogs/broker.log

會看到以下響應信息

2018-02-01 17:37:48 INFO main - The broker[node1, 192.168.252.121:10911] boot success...

查看進程

[root@node1 apache-rocketmq]# jps
2374 BrokerStartup
2350 NamesrvStartup
[root@node1 apache-rocketmq]# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name          
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1508/master         
tcp6       0      0 :::9876                 :::*                    LISTEN      2350/java                    
tcp6       0      0 ::1:25                  :::*                    LISTEN      1508/master         
tcp6       0      0 :::10909                :::*                    LISTEN      2374/java           
tcp6       0      0 :::10911                :::*                    LISTEN      2374/java           
tcp6       0      0 :::10912                :::*                    LISTEN      2374/java           
[root@node1 apache-rocketmq]#

發送並接受 消息

Send & Receive Messages

發送消息

發送/接收消息以前,咱們須要告訴客戶端 NameServer 地址。RocketMQ 提供了多種方式來實現這一目標。爲簡單起見,咱們使用環境變量 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...

中止服務

中止 broker

> sh bin/mqshutdown broker
The mqbroker(36695) is running...
Send shutdown request to mqbroker(36695) OK

中止 namesrv

> sh bin/mqshutdown namesrv
The mqnamesrv(36664) is running...
Send shutdown request to mqnamesrv(36664) OK

Contact

關注公衆號-搜雲庫

相關文章
相關標籤/搜索