RocketMQ 安装详细说明

目录

本文导读

环境说明

RocketMQ 下载

从 Apache 下载

从 GitHub 下载

RocketMQ 安装

文件上传

项目解压

编译部署

RocketMQ 操作

启动 NameServer

启动 Broker

内存分配失败解决办法

发送消息

接收消息

关闭服务器

常用命令


本文导读

  • 对于 RockatMQ 的理论知识,可以参考《RocketMQ 分布式消息中间件 理论介绍 》、《RocketMQ 集群部署模式 理论介绍 》
  • 工欲善其事,必先利其器。既然 RocketMQ 这么深受喜爱,那么本文就讲解如何安装使用 RocketMQ
  • Apache 上开源官方地址:https://rocketmq.apache.org/
  • GitHub 托管地址:https://github.com/apache/rocketmq
  • 阿里官方的介绍文档:http://jm.taobao.org/2017/01/12/rocketmq-quick-start-in-10-minutes/
  • Apache 官方提供的 4.3.0 版本的 “快速入门” 文档:https://rocketmq.apache.org/docs/quick-start/

环境说明

  • 对于 RocketMQ 4.3.0版本,官方要求环境如下,其中 Git 用于从 GitHub 获取源码,没有安装也没关系,可以直接下载
  • 官方推荐的流程是:Linux 系统上安装 Git 工具 、Maven、Java JDK
  1. Git 工具用于直接从 GitHub 获取 RocketMQ 项目源码下载到 Linux 系统上
  2. 然后 Maven 将 RocketMQ 源码进行编译成二进制文件
  3. 安装了 Java JDK 就可以运行 RocketMQ 了

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 (可不使用)
  • 实际操作的 Linux 系统的是 CentOS 7.2.1511 版本,没有实际Linux系统的可以参考《VMware 安装 CentOS 7.* 》

     
     
     
     
  1. [root@localhost ~]# lsb_release -a
  2. LSB Version: :core-4.1-amd64 :core-4.1-noarch
  3. Distributor ID: CentOS
  4. Description: CentOS Linux release 7 .2 .1511 ( Core)
  5. Release: 7 .2 .1511
  6. Codename: Core
  7. [root@localhost ~]#
  • 实际使用的 Java JDK 是 1.8.0_172 版本,没有安装的可以参考《Linux 安装Java JDK》

     
     
     
     
  1. [root@localhost ~]# java -version
  2. java version "1 .8 .0_172"
  3. Java( TM) SE Runtime Environment ( build 1 .8 .0_172-b11)
  4. Java HotSpot( TM) 64 -Bit Server VM ( build 25 .172-b11, mixed mode)
  5. [root@localhost ~]#
  • 实际操作安装的是 Maven 3.5.4,没有安装的可以参考《CentOS 7.2 安装 Maven》

     
     
     
     
  1. [root@localhost conf] # mvn -v
  2. Apache Maven 3.5. 4 ( 1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018- 06- 18T02: 33: 14+08: 00)
  3. Maven home: /usr/local /apache-maven-3.5.4
  4. Java version: 1.8.0_172, vendor: Oracle Corporation, runtime: /usr /local/jdk1. 8.0_172/jre
  5. Default locale: zh_CN, platform encoding: UTF- 8
  6. OS name: "linux", version: "3.10.0-327.el7.x86_64", arch: "amd64", family: "unix"
  7. [root@localhost conf] #

RocketMQ 下载

下面提供两种下载方式,即可以从 Apache 官网下载,也可以从 gitHub 上进行下载。

从 Apache 下载

  • Apache 官方下载网址:https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.3.0/rocketmq-all-4.3.0-source-release.zip
  • 这里选择直接下载整个 RocketMQ 项目的源码(带source的,而不是二进制文件[带Binary的])

RocketMQ 安装详细说明_第1张图片

从 GitHub 下载

  • GitHub 官方下载网址:https://github.com/apache/rocketmq
  • 因为项目是开源的,所以GitHub也有托管整个项目的源码,无论哪种方式下载下来都是一样的。
  • 上面 “环境说明” 中 Git 工具功能之一就是下载 GitHub 上的项目源码。但是如图所示,也可以直接下载即可。

RocketMQ 安装详细说明_第2张图片

RocketMQ 安装详细说明_第3张图片

RocketMQ 安装

  • 上面下载的是整个 RocketMQ 项目的源码,可以自己学习或者定制开发,但是要想使用显然这样是不行的

文件上传

  • 因为没有从 Linux 上直接使用 Git 将 RocketMQ 项目源码下载到 Linux 系统上,而是下载到了 Windons 系统上,所以第一步是将项目先上传到 Linux 系统上

RocketMQ 安装详细说明_第4张图片

项目解压

  • 官方推荐解压命令:  > unzip rocketmq-all-4.3.0-source-release.zip
  • 当然应该也是可以使用其它解压命令的,如 tar 等,这里还是和官方保持一致。当没有安装 unzip 命令包时是使用不了,此时应该先安装 unzip 命令包
  • 使用如下的 “ yum list unzip”  可以查看 unzip 包的安装情况

     
     
     
     
  1. [root@localhost local]# yum list unzip
  2. 已加载插件: fastestmirror
  3. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
  4. Loading mirror speeds from cached hostfile
  5. * base: mirrors .aliyun .com
  6. * extras: mirrors .aliyun .com
  7. * updates: centos .ustc .edu .cn
  8. 可安装的软件包
  9. unzip .x86_64 6 .0-19 .el7 base
  10. [root@localhost local]#
  • 没有安装时,使用 yum install xx 进行安装

   
   
   
   
  1. [root@localhost local] # yum install unzip.x86_64
  2. 已加载插件:fastestmirror
  3. Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
  4. base | 3.6 kB 00: 00: 00
  5. extras | 3.4 kB 00: 00: 00
  6. mysql-connectors-community | 2.5 kB 00: 00: 00
  7. mysql-tools-community | 2.5 kB 00: 00: 00
  8. mysql8 0-community | 2.5 kB 00: 00: 00
  9. updates | 3.4 kB 00: 00: 00
  10. ( 1/ 5): extras/ 7/x86_64/primary_db | 173 kB 00: 00: 00
  11. ( 2/ 5): mysql-tools-community/x86_64/primary_db | 45 kB 00: 00: 01
  12. ( 3/ 5): mysql8 0-community/x86_64/primary_db | 26 kB 00: 00: 01
  13. ( 4/ 5): mysql-connectors-community/x86_64/primary_db
  14. .....
  15. Running transaction
  16. 正在安装 : unzip- 6.0- 19.el7.x86_64 1/ 1
  17. 验证中 : unzip- 6.0- 19.el7.x86_64 1/ 1
  18. 已安装:
  19. unzip.x86_64 0: 6.0- 19.el7
  20. 完毕!
  21. [root@localhost local] #
  • RocketMQ 源码压缩包解压,大量的 inflating 输出:

   
   
   
   
  1. [root@localhost local] # unzip rocketmq-all-4.3.0-source-release.zip
  2. .......
  3. inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/ command/message/QueryMsgByKeySubCommand.java
  4. inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/ command/namesrv/UpdateKvConfigCommand.java
  5. inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/ command/topic/DeleteTopicSubCommand.java
  6. inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/ command/topic/TopicListSubCommand.java
  7. inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/ command/topic/UpdateOrderConfCommand.java
  8. inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/monitor/DefaultMonitorListener.java
  9. inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/monitor/FailedMsgs.java
  10. inflating: rocketmq-all-4.3.0/tools/src/ test/java/org/apache/rocketmq/tools/ command/message/ConsumeMessageCommandTest.java
  11. inflating: rocketmq-all-4.3.0/tools/s
  12. [root@localhost local] #
  • 解压完成之后,同目录下会多一个目录,如下所示的 “ rocketmq-all-4.3.-0”,才6.8M。

   
   
   
   
  1. [root@localhost local]# ls -l
  2. 总用量 1588
  3. drwxr-xr-x. 2 root root 6 8月 12 2015 bin
  4. drwxr-xr-x. 2 root root 6 8月 12 2015 etc
  5. drwxr-xr-x. 2 root root 6 8月 12 2015 games
  6. drwxr-xr-x. 2 root root 6 8月 12 2015 include
  7. drwxr-xr-x. 8 10 143 4096 3月 29 12 :55 jdk1 .8 .0_172
  8. drwxr-xr-x. 2 root root 6 8月 12 2015 lib
  9. drwxr-xr-x. 2 root root 6 8月 12 2015 lib64
  10. drwxr-xr-x. 2 root root 6 8月 12 2015 libexec
  11. drwxr-xr-x. 11 root root 4096 7月 6 00 :33 nginx
  12. drwxr-xr-x. 9 1001 1001 4096 7月 6 03 :34 nginx-1 .15 .0
  13. drwxr-xr-x. 20 root root 4096 7月 26 12 :13 rocketmq-all-4 .3 .0
  14. -rw-r--r--. 1 root root 1605734 8月 9 23 :19 rocketmq-all-4 .3 .0-source-release .zip
  15. drwxr-xr-x. 2 root root 6 8月 12 2015 sbin
  16. drwxr-xr-x. 5 root root 46 7月 3 20 :18 share
  17. drwxr-xr-x. 2 root root 6 8月 12 2015 src
  18. drwxr-xr-x. 4 root root 54 7月 6 02 :12 tomcat_1 .8
  19. [root@localhost local]# du -sh rocketmq-all-4 .3 .0
  20. 6 .8M rocketmq-all-4 .3 .0
  21. [root@localhost local]# du -sh rocketmq-all-4 .3 .0-source-release .zip
  22. 1 .6M rocketmq-all-4 .3 .0-source-release .zip
  23. [root@localhost local]#
  • rocketmq-all-4.3.-0 目录中的内容默认如下:

   
   
   
   
  1. [root@localhost rocketmq-all-4.3.0]# ls -l
  2. 总用量 60
  3. drwxr-xr-x. 3 root root 30 7月 26 12 :13 broker
  4. -rw-r--r--. 1 root root 997 7月 26 12 :13 BUILDING
  5. drwxr-xr-x. 3 root root 30 7月 26 12 :13 client
  6. drwxr-xr-x. 3 root root 30 7月 26 12 :13 common
  7. -rw-r--r--. 1 root root 1997 7月 26 12 :13 CONTRIBUTING .md
  8. -rw-r--r--. 1 root root 271 7月 26 12 :13 DEPENDENCIES
  9. drwxr-xr-x. 2 root root 33 7月 26 12 :13 dev
  10. drwxr-xr-x. 5 root root 4096 7月 26 12 :13 distribution
  11. drwxr-xr-x. 3 root root 30 7月 26 12 :13 example
  12. drwxr-xr-x. 3 root root 30 7月 26 12 :13 filter
  13. -rw-r--r--. 1 root root 11365 7月 26 12 :13 LICENSE
  14. drwxr-xr-x. 3 root root 30 7月 26 12 :13 logappender
  15. drwxr-xr-x. 3 root root 30 7月 26 12 :13 logging
  16. drwxr-xr-x. 3 root root 30 7月 26 12 :13 namesrv
  17. -rw-r--r--. 1 root root 168 7月 26 12 :13 NOTICE
  18. drwxr-xr-x. 3 root root 30 7月 26 12 :13 openmessaging
  19. -rw-r--r--. 1 root root 23729 7月 26 12 :13 pom .xml
  20. -rw-r--r--. 1 root root 2426 7月 26 12 :13 README .md
  21. drwxr-xr-x. 3 root root 30 7月 26 12 :13 remoting
  22. drwxr-xr-x. 3 root root 30 7月 26 12 :13 srvutil
  23. drwxr-xr-x. 3 root root 30 7月 26 12 :13 store
  24. drwxr-xr-x. 3 root root 71 7月 26 12 :13 style
  25. drwxr-xr-x. 3 root root 30 7月 26 12 :13 test
  26. drwxr-xr-x. 3 root root 30 7月 26 12 :13 tools
  27. [root@localhost rocketmq-all-4.3.0]#

编译部署

  • 使用 Maven 来编译整个项目,官方推荐命令

   
   
   
   
  1. > unzip rocketmq-all- 4.3. 0-source-release.zip (解压)
  2. > cd rocketmq-all- 4.3. 0/ (进入解压目录)
  3. > mvn -Prelease-all -DskipTests clean install -U (编译部署)
  4. > cd distribution/target/apache-rocketm q (进入指定目录)
  • 进入到上面解压后的目录 “rocketmq-all-4.3.-0” 下,指令命令 “mvn -Prelease-all -DskipTests clean install -U”
  • 接着就会从 Maven 中央仓库下载项目依赖的 jar 包编译部署,这可能需要几分钟的时间,如下所示从 阿里云 中央仓库进行下载

RocketMQ 安装详细说明_第5张图片

  • 如下所示 5 分钟之后项目构建成功

   
   
   
   
  1. [INFO] Reactor Summary:
  2. [INFO]
  3. [INFO] Apache RocketMQ 4 .3 .0 4 .3 .0 ........................ SUCCESS [02:48 min]
  4. [INFO] rocketmq-logging 4 .3 .0 ............................. SUCCESS [ 17.417 s]
  5. [INFO] rocketmq-remoting 4 .3 .0 ............................ SUCCESS [ 11.507 s]
  6. [INFO] rocketmq-common 4 .3 .0 .............................. SUCCESS [ 8.931 s]
  7. [INFO] rocketmq-client 4 .3 .0 .............................. SUCCESS [ 14.016 s]
  8. [INFO] rocketmq-store 4 .3 .0 ............................... SUCCESS [ 8.584 s]
  9. [INFO] rocketmq-srvutil 4 .3 .0 ............................. SUCCESS [ 2.288 s]
  10. [INFO] rocketmq-filter 4 .3 .0 .............................. SUCCESS [ 2.438 s]
  11. [INFO] rocketmq-broker 4 .3 .0 .............................. SUCCESS [ 7.253 s]
  12. [INFO] rocketmq-tools 4 .3 .0 ............................... SUCCESS [ 3.480 s]
  13. [INFO] rocketmq-namesrv 4 .3 .0 ............................. SUCCESS [ 1.642 s]
  14. [INFO] rocketmq-logappender 4 .3 .0 ......................... SUCCESS [ 2.533 s]
  15. [INFO] rocketmq-openmessaging 4 .3 .0 ....................... SUCCESS [ 3.220 s]
  16. [INFO] rocketmq-example 4 .3 .0 ............................. SUCCESS [ 1.757 s]
  17. [INFO] rocketmq-test 4 .3 .0 ................................ SUCCESS [ 5.503 s]
  18. [INFO] rocketmq-distribution 4 .3 .0 4 .3 .0 .................. SUCCESS [ 51.228 s]
  19. [INFO] ------------------------------------------------------------------------
  20. [INFO] BUILD SUCCESS
  21. [INFO] ------------------------------------------------------------------------
  22. [INFO] Total time: 05 :13 min
  23. [INFO] Finished at: 2018 -08-10T01 :14 :58+08 :00
  24. [INFO] ------------------------------------------------------------------------
  25. [root@localhost rocketmq-all-4.3.0]#
  • 进入到 distribution/target/apache-rocketmq 目录 ,其内容默认如下:

   
   
   
   
  1. [root@localhost apache-rocketmq] # pwd
  2. /usr/ local/rocketmq-all- 4.3. 0/distribution/target/apache-rocketm q
  3. [root@localhost apache-rocketmq] # ls -l
  4. 总用量 40
  5. drwxr-xr-x. 2 root root 79 810 01: 14 benchmark
  6. drwxr-xr-x. 2 root root 4096 810 01: 14 bin
  7. drwxr-xr-x. 5 root root 4096 726 12: 13 conf
  8. drwxr-xr-x. 2 root root 4096 810 01: 14 lib
  9. -rw-r--r--. 1 root root 17336 726 12: 13 LICENSE
  10. -rw-r--r--. 1 root root 1337 726 12: 13 NOTICE
  11. -rw-r--r--. 1 root root 2426 726 12: 13 README.md
  12. [root@localhost apache-rocketmq] #

RocketMQ 操作

启动 NameServer

  • 先进入 RocketMQ 安装目录下的  distribution/target/apache-rocketmq 目录
  • 启动的时候先启动 namesrv,然后启动 broker
  • 官方指导命令:

   
   
   
   
  1. # 启动命令,并且常驻内存
  2. $ nohup sh bin/mqnamesrv &
  3. # 查看启动日志能看到:The Name Server boot success字样则成功
  4. $ tail -f ~ /logs/rocketmqlogs /namesrv.log

nohup sh mqnamesrv & :属于后台启动

sh mqnamesrv :属于终端启动,直接输出日志信息,按 ctrl+c 可直接关闭退出

  • 如下所示,看到 “ The Name Server boot success. serializeType=JSON  ” 表示启动成功

   
   
   
   
  1. [ root@localhost apache-rocketmq] # pwd
  2. /usr/local/rocketmq-all -4.3 .0/distribution/target/apache-rocketmq
  3. [ root@localhost apache-rocketmq] # nohup sh bin/mqnamesrv &
  4. [ 1] 5865
  5. [ root@localhost apache-rocketmq] # nohup: 忽略输入并把输出追加到"nohup.out"
  6. tail -f ~/logs/rocketmqlogs/namesrv.log
  7. 2018 -08 -10 01: 29: 12 INFO main - tls.client.keyPath = null
  8. 2018 -08 -10 01: 29: 12 INFO main - tls.client.keyPassword = null
  9. 2018 -08 -10 01: 29: 12 INFO main - tls.client.certPath = null
  10. 2018 -08 -10 01: 29: 12 INFO main - tls.client.authServer = false
  11. 2018 -08 -10 01: 29: 12 INFO main - tls.client.trustCertPath = null
  12. 2018 -08 -10 01: 29: 13 INFO main - Using OpenSSL provider
  13. 2018 -08 -10 01: 29: 14 INFO main - SSLContext created for server
  14. 2018 -08 -10 01: 29: 14 INFO NettyEventExecutor - NettyEventExecutor service started
  15. 2018 -08 -10 01: 29: 14 INFO main - The Name Server boot success. serializeType=JSON
  16. 2018 -08 -10 01: 29: 14 INFO FileWatchService - FileWatchService service started
  • 在当前目录下会有一个 nohup.out 的日志文件,可以打开查看 namesrv 的启动情况

启动 Broker

  • 同样进入 RocketMQ 安装目录下的  distribution/target/apache-rocketmq 目录
  • 官方指导命令:

   
   
   
   
  1. # 启动命令,并且常驻内存:注意ip地址要配置成为服务的ip地址,保证地址以及端口能够访问
  2. > nohup sh bin/mqbroker -n localhost: 9876 &
  3. # 查看启动日志
  4. > tail -f ~ /logs/rocketmqlogs /broker.log

nohup sh bin/mqbroker -n localhost:9876 & :属于后台启动

sh bin/mqbroker -n localhost:9876 :属于终端启动,直接输出日志信息,按 ctrl+c 可直接关闭退出

  • 如下图所示,启动之后同样提示将日志信息追加到了当前目录下的 nohup.out 文件中
  • 输入 tail -f ~/logs/rocketmqlogs/broker.log 查看日志,如果看到 "The broker[%s, 172.30.30.233:10911] boot success..." ,则表示启动成功

   
   
   
   
  1. [ root@localhost apache-rocketmq] # nohup sh bin/mqbroker -n localhost:9876 &
  2. [ 1] 7202
  3. [ root@localhost apache-rocketmq] # nohup: 忽略输入并把输出追加到"nohup.out"
  • 如果 tail -f ~/logs/rocketmqlogs/broker.log 提示找不到文件,则打开 当前目录(apache-rocketmq)下的 nohup.out 日志文件查看,发现启动 Broker 失败:无法分配内存

   
   
   
   
  1. Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory( 0x00000005c0000000, 8589934592, 0) failed; error= 'Cannot allocate memory' (errno= 12)
  2. #
  3. # There is insufficient memory for the Java Runtime Environment to continue.
  4. # Native memory allocation (mmap) failed to map 8589934592 bytes for committing reserved memory.
  5. # An error report file with more information is saved as:
  6. # /usr/ local/rocketmq-all- 4.3. 0/distribution/target/apache-rocketmq/hs_err_pid7209.log

内存分配失败解决办法

  • 这是因为 apache-rocketmq/bin 目录下启动 nameserv 与 broker 的 runbroker.sh 和 runserver.sh 文件中默认分配的内存太大,而系统实际内存却太小导致启动失败
  • 通常像虚拟机上安装的 CentOS 服务器内存可能是没有高的,只能调小。实际中应该根据服务器内存情况,配置一个合适的值
  • 找到下面的 runbroker.sh 和 runserver.sh 文件

   
   
   
   
  1. [root@localhost bin]# pwd
  2. /usr/local/rocketmq-all-4.3.0/distribution/target/apache-rocketmq/bin
  3. [root@localhost bin]# ls -l
  4. 总用量 136
  5. -rwxr-xr-x. 1 root root 1654 7月 26 12:13 cachedog.sh
  6. -rwxr-xr-x. 1 root root 845 7月 26 12:13 cleancache.sh
  7. -rwxr-xr-x. 1 root root 1116 7月 26 12:13 cleancache.v1.sh
  8. -rwxr-xr-x. 1 root root 1398 7月 26 12:13 mqadmin
  9. -rwxr-xr-x. 1 root root 1029 7月 26 12:13 mqadmin.cmd
  10. -rwxr-xr-x. 1 root root 1422 7月 26 12:13 mqadmin.xml
  11. -rwxr-xr-x. 1 root root 1394 7月 26 12:13 mqbroker
  12. -rwxr-xr-x. 1 root root 1084 7月 26 12:13 mqbroker.cmd
  13. -rwxr-xr-x. 1 root root 1373 7月 26 12:13 mqbroker.numanode0
  14. -rwxr-xr-x. 1 root root 1373 7月 26 12:13 mqbroker.numanode1
  15. -rwxr-xr-x. 1 root root 1373 7月 26 12:13 mqbroker.numanode2
  16. -rwxr-xr-x. 1 root root 1373 7月 26 12:13 mqbroker.numanode3
  17. -rwxr-xr-x. 1 root root 1388 7月 26 12:13 mqbroker.xml
  18. -rwxr-xr-x. 1 root root 1400 7月 26 12:13 mqfiltersrv
  19. -rwxr-xr-x. 1 root root 1094 7月 26 12:13 mqfiltersrv.cmd
  20. -rwxr-xr-x. 1 root root 1394 7月 26 12:13 mqfiltersrv.xml
  21. -rwxr-xr-x. 1 root root 1396 7月 26 12:13 mqnamesrv
  22. -rwxr-xr-x. 1 root root 1088 7月 26 12:13 mqnamesrv.cmd
  23. -rwxr-xr-x. 1 root root 1390 7月 26 12:13 mqnamesrv.xml
  24. -rwxr-xr-x. 1 root root 1571 7月 26 12:13 mqshutdown
  25. -rwxr-xr-x. 1 root root 1398 7月 26 12:13 mqshutdown.cmd
  26. -rwxr-xr-x. 1 root root 2222 7月 26 12:13 os.sh
  27. -rwxr-xr-x. 1 root root 1148 7月 26 12:13 play.cmd
  28. -rwxr-xr-x. 1 root root 1008 7月 26 12:13 play.sh
  29. -rwxr-xr-x. 1 root root 772 7月 26 12:13 README.md
  30. -rwxr-xr-x. 1 root root 819 7月 26 12:13 runbroker
  31. -rwxr-xr-x. 1 root root 2206 7月 26 12:13 runbroker.cmd
  32. -rwxr-xr-x. 1 root root 2928 8月 10 02:37 runbroker.sh
  33. -rwxr-xr-x. 1 root root 1816 7月 26 12:13 runserver.cmd
  34. -rwxr-xr-x. 1 root root 2551 8月 10 02:35 runserver.sh
  35. -rwxr-xr-x. 1 root root 1156 7月 26 12:13 setcache.sh
  36. -rwxr-xr-x. 1 root root 1408 7月 26 12:13 startfsrv.sh
  37. -rwxr-xr-x. 1 root root 1601 7月 26 12:13 tools.cmd
  38. -rwxr-xr-x. 1 root root 1870 7月 26 12:13 tools.sh
  39. [root@localhost bin]#
  • runserver.sh 文件中 修改 JVM 配置下的第一行,将原来 4g 调小一点

   
   
   
   
  1. #===========================================================================================
  2. # JVM Configuration
  3. #===========================================================================================
  4. #JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
  5. JAVA_OPT= "${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn125m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
  6. JAVA_OPT= "${JAVA_OPT} -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8 -XX:-UseParNewGC"
  7. JAVA_OPT= "${JAVA_OPT} -verbose:gc -Xloggc:/dev/shm/rmq_srv_gc.log -XX:+PrintGCDetails"
  8. JAVA_OPT= "${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
  9. JAVA_OPT= "${JAVA_OPT} -XX:-UseLargePages"
  10. JAVA_OPT= "${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib"
  11. #JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
  12. JAVA_OPT= "${JAVA_OPT} ${JAVA_OPT_EXT}"
  13. JAVA_OPT= "${JAVA_OPT} -cp ${CLASSPATH}"
  14. $JAVA ${JAVA_OPT} $@
  • runbroker.sh 文件中 修改 JVM 配置下的第一行,将原来 8g 调小一点

   
   
   
   
  1. #===========================================================================================
  2. # JVM Configuration
  3. #===========================================================================================
  4. #JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
  5. JAVA_OPT= "${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn125m"
  6. JAVA_OPT= "${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8"
  7. JAVA_OPT= "${JAVA_OPT} -verbose:gc -Xloggc:/dev/shm/mq_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy"
  8. JAVA_OPT= "${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
  9. JAVA_OPT= "${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
  10. JAVA_OPT= "${JAVA_OPT} -XX:+AlwaysPreTouch"
  11. JAVA_OPT= "${JAVA_OPT} -XX:MaxDirectMemorySize=15g"
  12. JAVA_OPT= "${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking"
  13. JAVA_OPT= "${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${BASE_DIR}/lib"
  14. #JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
  15. JAVA_OPT= "${JAVA_OPT} ${JAVA_OPT_EXT}"
  16. JAVA_OPT= "${JAVA_OPT} -cp ${CLASSPATH}"
  • 然后再次启动 broker 时就成功了,broker 注册到了 nameserver 上了(localhost:9876

   
   
   
   
  1. [root@localhost apache-rocketmq]# nohup sh bin/mqbroker -n localhost: 9876 &
  2. [ 2] 7512
  3. [ 1] 退出 1 nohup sh bin/mqbroker -n localhost: 9876
  4. [root@localhost apache-rocketmq]# nohup: 忽略输入并把输出追加到 "nohup.out"
  5. [root@localhost apache-rocketmq]# tail -f ~/logs/rocketmqlogs/broker. log
  6. 2018 -08 -10 02: 39: 20 WARN main - Load default discard message hook service: DefaultTransactionalMessageCheckListener
  7. 2018 -08 -10 02: 39: 21 INFO FileWatchService - FileWatchService service started
  8. 2018 -08 -10 02: 39: 21 INFO PullRequestHoldService - PullRequestHoldService service started
  9. 2018 -08 -10 02: 39: 23 INFO brokerOutApi_thread_1 - register broker to name server localhost: 9876 OK
  10. 2018 -08 -10 02: 39: 23 INFO main - Start transaction service!
  11. 2018 -08 -10 02: 39: 23 INFO main - The broker[localhost.localdomain, 192.168 .58 .129: 10911] boot success. serializeType=JSON and name server is localhost: 9876
  12. 2018 -08 -10 02: 39: 30 INFO BrokerControllerScheduledThread1 - dispatch behind commit log 0 bytes
  13. 2018 -08 -10 02: 39: 30 INFO BrokerControllerScheduledThread1 - Slave fall behind master: 0 bytes
  14. 2018 -08 -10 02: 39: 33 INFO brokerOutApi_thread_2 - register broker to name server localhost: 9876 OK
  15. 2018 -08 -10 02: 40: 03 INFO brokerOutApi_thread_3 - register broker to name server localhost: 9876 OK

发送消息

  • 发送/接收消息之前,需要告诉客户端(Producer、Consumer)名称服务器的位置,RocketMQ 提供了多种方法来实现这一点:
  1. 编程方式,如:producer.setNamesrvAddr("ip:port")
  2. Java 选项,如:rocketmq.namesrv.addr
  3. 环境变量,如:NAMESRV_ADDR
  4. HTTP 端点
  • 官方指导命令:

   
   
   
   
  1. > export NAMESRV_ADDR=localhost: 9876
  2. > sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
  3. SendResult [sendStatus=SEND_OK, msgId= ...
  • 如下所示官方提供这个例子属于生产者,用于发送消息,运行之后会发送大量的消息,之后就会退出

   
   
   
   
  1. [ root@localhost apache-rocketmq] # export NAMESRV_ADDR=localhost:9876
  2. [ root@localhost apache-rocketmq] # sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
  3. Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize= 128m; support was removed in 8.0
  4. Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize= 128m; support was removed in 8.0
  5. 03: 05: 13.152 [main] DEBUG i.n.u.i.l.InternalLoggerFactory - Using SLF4J as the default logging framework
  6. SendResult [sendStatus=SEND_OK, msgId=C0A83A8120207D4991AD2F02D2280000, offsetMsgId=C0A83A8100002A9F00000000000AFAC8, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId= 0], queueOffset= 1000]
  7. SendResult [sendStatus=SEND_OK, msgId=C0A83A8120207D4991AD2F02D2740001, offsetMsgId=C0A83A8100002A9F00000000000AFB7A, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId= 1], queueOffset= 1000]
  8. SendResult [sendStatus=SEND_OK, msgId=C0A83A8120207D4991AD2F02D27A0002, offsetMsgId=C0A83A8100002A9F00000000000AFC2C, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId= 2], queueOffset= 1000]
  9. .........................

接收消息

  • 官方指导命令:

   
   
   
   
  1. > sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
  2. ConsumeMessageThread_%d Receive New Messages: [MessageExt...
  • 上面的消息发送完毕之后就会退出,在同一窗口中可以使用消费者类来进行接收消息,如下所示,显然是多线程的

   
   
   
   
  1. [root@localhost apache-rocketmq] # sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
  2. ConsumeMessageThread_3 Receive New Messages: [MessageExt [queueId= 0, storeSize= 180, queueOffset= 1031, sysFlag= 0, bornTimestamp= 1533841514766, bornHost= /192.168.58.129:46816, storeTimestamp=1533841514767, storeHost=/ 192.168. 58.129 : 10911, msgId=C0A83A8100002A9F00000000000B518A, commitLogOffset= 741770, bodyCRC= 895437781, reconsumeTimes= 0, preparedTransactionOffset= 0, toString()=Message{topic= 'TopicTest', flag= 0, properties={MIN_OFFSET= 0, MAX_OFFSET= 1250, CONSUME_START_TIME= 1533841719674, UNIQ_KEY=C0A83A8120207D4991AD2F02D50E007C, WAIT= true, TAGS=TagA}, body=[ 72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49, 50, 52], transactionId= 'null'}]]
  3. ConsumeMessageThread_2 Receive New Messages: [MessageExt [queueId= 0, storeSize= 180, queueOffset= 1030, sysFlag= 0, bornTimestamp= 1533841514746, bornHost= /192.168.58.129:46816, storeTimestamp=1533841514747, storeHost=/ 192.168. 58.129 : 10911, msgId=C0A83A8100002A9F00000000000B4EBA, commitLogOffset= 741050, bodyCRC= 842174412, reconsumeTimes= 0, preparedTransactionOffset= 0, toString()=Message{topic= 'TopicTest', flag= 0, properties={MIN_OFFSET= 0, MAX_OFFSET= 1250, CONSUME_START_TIME= 1533841719669, UNIQ_KEY=C0A83A8120207D4991AD2F02D4FA0078, WAIT= true, TAGS=TagA}, body=[ 72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 49, 50, 48], transactionId= 'null'}]]
  4. ConsumeMessageThread_2 0 Receive New Messages: [MessageExt [queueId= 0, storeSize= 179, queueOffset= 1019, sysFlag= 0, bornTimestamp= 1533841514511, bornHost= /192.168.58.129:46816, storeTimestamp=1533841514515, storeHost=/ 192.168. 58.129 : 10911, msgId=C0A83A8100002A9F00000000000B2FE2, commitLogOffset= 733154, bodyCRC= 884882597, reconsumeTimes= 0, preparedTransactionOffset= 0, toString()=Message{topic= 'TopicTest', flag= 0, properties={MIN_OFFSET= 0, MAX_OFFSET= 1250, CONSUME_START_TIME= 1533841719680, UNIQ_KEY=C0A83A8120207D4991AD2F02D40F004C, WAIT= true, TAGS=TagA}, body=[ 72, 101, 108, 108, 111, 32, 82, 111, 99, 107, 101, 116, 77, 81, 32, 55, 54], transactionId= 'null'}]]
  5. ...............

关闭服务器

  • 同样都是在 RocketMQ 安装目录下的  distribution/target/apache-rocketmq 目录下执行命令
  • 与启动顺序相反进行关闭,先关闭 broker、在关闭 nameserv

   
   
   
   
  1. > sh bin/mqshutdown broker
  2. The mqbroker( 36695) is running...
  3. Send shutdown request to mqbroker( 36695) OK
  4. > sh bin/mqshutdown namesrv
  5. The mqnamesrv( 36664) is running...
  6. Send shutdown request to mqnamesrv( 36664) OK

常用命令

除了上面几个命令之外,还有如下一些较常用的命令,ip请以实际为准:

  • 查看集群情况: ./mqadmin clusterList -n 127.0.0.1:9876
  • 查看 broker 状态: ./mqadmin brokerStatus -n 127.0.0.1:9876 -b 172.20.1.138:10911
  • 查看 topic 列表: ./mqadmin topicList -n 127.0.0.1:9876
  • 查看 topic 状态: ./mqadmin topicStatus -n 127.0.0.1:9876 -t MyTopic (换成想查询的 topic)
  • 查看 topic 路由: ./mqadmin topicRoute -n 127.0.0.1:9876 -t MyTopic

 

 

 

 

你可能感兴趣的:(spring)