目录
本文导读
- 对于 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.5.0 版本的 “快速入门” 文档:https://rocketmq.apache.org/docs/quick-start/
环境说明
- 对于 RocketMQ 4.5.0版本,官方要求环境如下,其中 Git 用于从 GitHub 获取源码,没有安装也没关系,可以直接下载
- 官方推荐的流程是:Linux 系统上安装 Git 工具 、Maven、Java JDK
- Git 工具用于直接从 GitHub 获取 RocketMQ 项目源码下载到 Linux 系统上
- 然后 Maven 将 RocketMQ 源码进行编译成二进制文件
- 安装了 Java JDK 就可以运行 RocketMQ 了
The following softwares are assumed installed:
- 64bit OS, Linux/Unix/Mac is recommended;
- 64bit JDK 1.8+;
- Maven 3.2.x
- Git (可不使用)
- 实际操作的 Linux 系统的是 CentOS 7.2.1511 版本,没有实际Linux系统的可以参考《VMware 安装 CentOS 7.* 》
-
[root@localhost ~]#
lsb_release
-a
-
LSB
Version:
:core-4.1-amd64
:core-4.1-noarch
-
Distributor
ID:
CentOS
-
Description:
CentOS
Linux
release 7
.2
.1511 (
Core)
-
Release: 7
.2
.1511
-
Codename:
Core
-
[root@localhost ~]#
- 实际使用的 Java JDK 是 1.8.0_172 版本,没有安装的可以参考《Linux 安装Java JDK》
-
[root@localhost ~]#
java
-version
-
java
version "1
.8
.0_172"
-
Java(
TM)
SE
Runtime
Environment (
build 1
.8
.0_172-b11)
-
Java
HotSpot(
TM) 64
-Bit
Server
VM (
build 25
.172-b11,
mixed
mode)
-
[root@localhost ~]#
- 实际操作安装的是 Maven 3.5.4,没有安装的可以参考《CentOS 7.2 安装 Maven》
-
[root@localhost conf]
# mvn -v
-
Apache Maven
3.5.
4 (
1edded0938998edf8bf061f1ceb3cfdeccf443fe;
2018-
06-
18T02:
33:
14+08:
00)
-
Maven home:
/usr/local
/apache-maven-3.5.4
-
Java version: 1.8.0_172, vendor: Oracle Corporation, runtime: /usr
/local/jdk1.
8.0_172/jre
-
Default locale: zh_CN, platform encoding: UTF-
8
-
OS name:
"linux", version:
"3.10.0-327.el7.x86_64", arch:
"amd64", family:
"unix"
-
[root@localhost conf]
#
RocketMQ 下载
下面提供两种下载方式,即可以从 Apache RocketMQ 官网下载,也可以从 gitHub 上进行下载。
从 Apache 下载
- Apache 官方下载网址:https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.5.0/rocketmq-all-4.5.0-bin-release.zip
- 这里选择直接下载整个 RocketMQ 项目的源码(带source的,而不是二进制文件[带Binary的])
从 GitHub 下载
- GitHub 官方下载网址:https://github.com/apache/rocketmq
- 因为项目是开源的,所以GitHub也有托管整个项目的源码,无论哪种方式下载下来都是一样的。
- 上面 “环境说明” 中 Git 工具功能之一就是下载 GitHub 上的项目源码。但是如图所示,也可以直接下载即可。
RocketMQ 安装
- 上面下载的是整个 RocketMQ 项目的源码,可以自己学习或者定制开发,但是要想使用显然这样是不行的
文件上传
- 因为没有从 Linux 上直接使用 Git 将 RocketMQ 项目源码下载到 Linux 系统上,而是下载到了 Windons 系统上,所以第一步是将项目先上传到 Linux 系统上
项目解压
- 官方推荐解压命令: > unzip rocketmq-all-4.3.0-source-release.zip
- 当然应该也是可以使用其它解压命令的,如 tar 等,这里还是和官方保持一致。当没有安装 unzip 命令包时是使用不了,此时应该先安装 unzip 命令包
- 使用如下的 “ yum list unzip” 可以查看 unzip 包的安装情况
-
[root@localhost local]#
yum
list
unzip
-
已加载插件:
fastestmirror
-
Repodata
is
over 2
weeks
old.
Install
yum-cron?
Or
run:
yum
makecache
fast
-
Loading
mirror
speeds
from
cached
hostfile
-
*
base:
mirrors
.aliyun
.com
-
*
extras:
mirrors
.aliyun
.com
-
*
updates:
centos
.ustc
.edu
.cn
-
可安装的软件包
-
unzip
.x86_64 6
.0-19
.el7
base
-
[root@localhost local]#
- 没有安装时,使用 yum install xx 进行安装
-
[root@localhost
local]
# yum install unzip.x86_64
-
已加载插件:fastestmirror
-
Repodata is over
2 weeks old. Install yum-cron? Or run: yum makecache fast
-
base |
3.6 kB
00:
00:
00
-
extras |
3.4 kB
00:
00:
00
-
mysql-connectors-community |
2.5 kB
00:
00:
00
-
mysql-tools-community |
2.5 kB
00:
00:
00
-
mysql8
0-community |
2.5 kB
00:
00:
00
-
updates |
3.4 kB
00:
00:
00
-
(
1/
5): extras/
7/x86_64/primary_db |
173 kB
00:
00:
00
-
(
2/
5): mysql-tools-community/x86_64/primary_db |
45 kB
00:
00:
01
-
(
3/
5): mysql8
0-community/x86_64/primary_db |
26 kB
00:
00:
01
-
(
4/
5): mysql-connectors-community/x86_64/primary_db
-
.....
-
Running transaction
-
正在安装 : unzip-
6.0-
19.el7.x86_64
1/
1
-
验证中 : unzip-
6.0-
19.el7.x86_64
1/
1
-
-
已安装:
-
unzip.x86_64
0:
6.0-
19.el7
-
-
完毕!
-
[root@localhost
local]
#
- RocketMQ 源码压缩包解压,大量的 inflating 输出:
-
[root@localhost
local]
# unzip rocketmq-all-4.3.0-source-release.zip
-
.......
-
inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/
command/message/QueryMsgByKeySubCommand.java
-
inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/
command/namesrv/UpdateKvConfigCommand.java
-
inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/
command/topic/DeleteTopicSubCommand.java
-
inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/
command/topic/TopicListSubCommand.java
-
inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/
command/topic/UpdateOrderConfCommand.java
-
inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/monitor/DefaultMonitorListener.java
-
inflating: rocketmq-all-4.3.0/tools/src/main/java/org/apache/rocketmq/tools/monitor/FailedMsgs.java
-
inflating: rocketmq-all-4.3.0/tools/src/
test/java/org/apache/rocketmq/tools/
command/message/ConsumeMessageCommandTest.java
-
inflating: rocketmq-all-4.3.0/tools/s
-
-
[root@localhost
local]
#
- 解压完成之后,同目录下会多一个目录,如下所示的 “ rocketmq-all-4.3.-0”,才6.8M。
-
[root@localhost local]#
ls
-l
-
总用量 1588
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
bin
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
etc
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
games
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
include
-
drwxr-xr-x. 8 10 143 4096 3月 29 12
:55
jdk1
.8
.0_172
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
lib
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
lib64
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
libexec
-
drwxr-xr-x. 11
root
root 4096 7月 6 00
:33
nginx
-
drwxr-xr-x. 9 1001 1001 4096 7月 6 03
:34
nginx-1
.15
.0
-
drwxr-xr-x. 20
root
root 4096 7月 26 12
:13
rocketmq-all-4
.3
.0
-
-rw-r--r--. 1
root
root 1605734 8月 9 23
:19
rocketmq-all-4
.3
.0-source-release
.zip
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
sbin
-
drwxr-xr-x. 5
root
root 46 7月 3 20
:18
share
-
drwxr-xr-x. 2
root
root 6 8月 12 2015
src
-
drwxr-xr-x. 4
root
root 54 7月 6 02
:12
tomcat_1
.8
-
[root@localhost local]#
du
-sh
rocketmq-all-4
.3
.0
-
6
.8M
rocketmq-all-4
.3
.0
-
[root@localhost local]#
du
-sh
rocketmq-all-4
.3
.0-source-release
.zip
-
1
.6M
rocketmq-all-4
.3
.0-source-release
.zip
-
[root@localhost local]#
- rocketmq-all-4.3.-0 目录中的内容默认如下:
-
[root@localhost rocketmq-all-4.3.0]#
ls
-l
-
总用量 60
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
broker
-
-rw-r--r--. 1
root
root 997 7月 26 12
:13
BUILDING
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
client
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
common
-
-rw-r--r--. 1
root
root 1997 7月 26 12
:13
CONTRIBUTING
.md
-
-rw-r--r--. 1
root
root 271 7月 26 12
:13
DEPENDENCIES
-
drwxr-xr-x. 2
root
root 33 7月 26 12
:13
dev
-
drwxr-xr-x. 5
root
root 4096 7月 26 12
:13
distribution
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
example
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
filter
-
-rw-r--r--. 1
root
root 11365 7月 26 12
:13
LICENSE
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
logappender
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
logging
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
namesrv
-
-rw-r--r--. 1
root
root 168 7月 26 12
:13
NOTICE
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
openmessaging
-
-rw-r--r--. 1
root
root 23729 7月 26 12
:13
pom
.xml
-
-rw-r--r--. 1
root
root 2426 7月 26 12
:13
README
.md
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
remoting
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
srvutil
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
store
-
drwxr-xr-x. 3
root
root 71 7月 26 12
:13
style
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
test
-
drwxr-xr-x. 3
root
root 30 7月 26 12
:13
tools
-
[root@localhost rocketmq-all-4.3.0]#
编译部署
- 使用 Maven 来编译整个项目,官方推荐命令
-
> 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-rocketm
q (进入指定目录)
- 进入到上面解压后的目录 “rocketmq-all-4.3.-0” 下,指令命令 “mvn -Prelease-all -DskipTests clean install -U”
- 接着就会从 Maven 中央仓库下载项目依赖的 jar 包编译部署,这可能需要几分钟的时间,如下所示从 阿里云 中央仓库进行下载
- 如下所示 5 分钟之后项目构建成功
-
[INFO]
Reactor
Summary:
-
[INFO]
-
[INFO]
Apache
RocketMQ 4
.3
.0 4
.3
.0 ........................
SUCCESS
[02:48 min]
-
[INFO]
rocketmq-logging 4
.3
.0 .............................
SUCCESS
[ 17.417 s]
-
[INFO]
rocketmq-remoting 4
.3
.0 ............................
SUCCESS
[ 11.507 s]
-
[INFO]
rocketmq-common 4
.3
.0 ..............................
SUCCESS
[ 8.931 s]
-
[INFO]
rocketmq-client 4
.3
.0 ..............................
SUCCESS
[ 14.016 s]
-
[INFO]
rocketmq-store 4
.3
.0 ...............................
SUCCESS
[ 8.584 s]
-
[INFO]
rocketmq-srvutil 4
.3
.0 .............................
SUCCESS
[ 2.288 s]
-
[INFO]
rocketmq-filter 4
.3
.0 ..............................
SUCCESS
[ 2.438 s]
-
[INFO]
rocketmq-broker 4
.3
.0 ..............................
SUCCESS
[ 7.253 s]
-
[INFO]
rocketmq-tools 4
.3
.0 ...............................
SUCCESS
[ 3.480 s]
-
[INFO]
rocketmq-namesrv 4
.3
.0 .............................
SUCCESS
[ 1.642 s]
-
[INFO]
rocketmq-logappender 4
.3
.0 .........................
SUCCESS
[ 2.533 s]
-
[INFO]
rocketmq-openmessaging 4
.3
.0 .......................
SUCCESS
[ 3.220 s]
-
[INFO]
rocketmq-example 4
.3
.0 .............................
SUCCESS
[ 1.757 s]
-
[INFO]
rocketmq-test 4
.3
.0 ................................
SUCCESS
[ 5.503 s]
-
[INFO]
rocketmq-distribution 4
.3
.0 4
.3
.0 ..................
SUCCESS
[ 51.228 s]
-
[INFO]
------------------------------------------------------------------------
-
[INFO]
BUILD
SUCCESS
-
[INFO]
------------------------------------------------------------------------
-
[INFO]
Total
time: 05
:13
min
-
[INFO]
Finished
at: 2018
-08-10T01
:14
:58+08
:00
-
[INFO]
------------------------------------------------------------------------
-
[root@localhost rocketmq-all-4.3.0]#
- 进入到 distribution/target/apache-rocketmq 目录 ,其内容默认如下:
-
[root@localhost apache-rocketmq]
# pwd
-
/usr/
local/rocketmq-all-
4.3.
0/distribution/target/apache-rocketm
q
-
[root@localhost apache-rocketmq]
# ls -l
-
总用量
40
-
drwxr-xr-x.
2 root root
79
8月
10
01:
14 benchmark
-
drwxr-xr-x.
2 root root
4096
8月
10
01:
14 bin
-
drwxr-xr-x.
5 root root
4096
7月
26
12:
13 conf
-
drwxr-xr-x.
2 root root
4096
8月
10
01:
14 lib
-
-rw-r--r--.
1 root root
17336
7月
26
12:
13 LICENSE
-
-rw-r--r--.
1 root root
1337
7月
26
12:
13 NOTICE
-
-rw-r--r--.
1 root root
2426
7月
26
12:
13 README.md
-
[root@localhost apache-rocketmq]
#
RocketMQ 操作
启动 NameServer
- 先进入 RocketMQ 安装目录下的 distribution/target/apache-rocketmq 目录
- 启动的时候先启动 namesrv,然后启动 broker
- 官方指导命令:
-
# 启动命令,并且常驻内存
-
$ nohup sh bin/mqnamesrv &
-
# 查看启动日志能看到:The Name Server boot success字样则成功
-
$ tail -f ~
/logs/rocketmqlogs
/namesrv.log
nohup sh mqnamesrv & :属于后台启动
sh mqnamesrv :属于终端启动,直接输出日志信息,按 ctrl+c 可直接关闭退出
- 如下所示,看到 “ The Name Server boot success. serializeType=JSON ” 表示启动成功
-
[
root@localhost apache-rocketmq]
# pwd
-
/usr/local/rocketmq-all
-4.3
.0/distribution/target/apache-rocketmq
-
[
root@localhost apache-rocketmq]
# nohup sh bin/mqnamesrv &
-
[
1]
5865
-
[
root@localhost apache-rocketmq]
# nohup: 忽略输入并把输出追加到"nohup.out"
-
tail -f ~/logs/rocketmqlogs/namesrv.log
-
2018
-08
-10
01:
29:
12 INFO main - tls.client.keyPath =
null
-
2018
-08
-10
01:
29:
12 INFO main - tls.client.keyPassword =
null
-
2018
-08
-10
01:
29:
12 INFO main - tls.client.certPath =
null
-
2018
-08
-10
01:
29:
12 INFO main - tls.client.authServer =
false
-
2018
-08
-10
01:
29:
12 INFO main - tls.client.trustCertPath =
null
-
2018
-08
-10
01:
29:
13 INFO main - Using OpenSSL provider
-
2018
-08
-10
01:
29:
14 INFO main - SSLContext created
for server
-
2018
-08
-10
01:
29:
14 INFO NettyEventExecutor - NettyEventExecutor service started
-
2018
-08
-10
01:
29:
14 INFO main - The Name Server boot success. serializeType=JSON
-
2018
-08
-10
01:
29:
14 INFO FileWatchService - FileWatchService service started
- 在当前目录下会有一个 nohup.out 的日志文件,可以打开查看 namesrv 的启动情况
启动 Broker
- 同样进入 RocketMQ 安装目录下的 distribution/target/apache-rocketmq 目录
- 官方指导命令:
-
# 启动命令,并且常驻内存:注意ip地址要配置成为服务的ip地址,保证地址以及端口能够访问
-
> nohup sh bin/mqbroker -n
localhost:
9876 &
-
-
# 查看启动日志
-
> 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..." ,则表示启动成功
-
[
root@localhost apache-rocketmq]
# nohup sh bin/mqbroker -n localhost:9876 &
-
[
1]
7202
-
[
root@localhost apache-rocketmq]
# nohup: 忽略输入并把输出追加到"nohup.out"
- 如果 tail -f ~/logs/rocketmqlogs/broker.log 提示找不到文件,则打开 当前目录(apache-rocketmq)下的 nohup.out 日志文件查看,发现启动 Broker 失败:无法分配内存
-
Java HotSpot(TM)
64-Bit Server VM warning: INFO: os::commit_memory(
0x00000005c0000000,
8589934592,
0) failed; error=
'Cannot allocate memory' (errno=
12)
-
#
-
# There
is insufficient memory
for the Java Runtime Environment
to
continue.
-
# Native memory allocation (mmap) failed
to map
8589934592 bytes
for committing reserved memory.
-
# An error report
file
with more information
is saved
as:
-
# /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 文件
-
[root@localhost bin]# pwd
-
/usr/local/rocketmq-all-4.3.0/distribution/target/apache-rocketmq/bin
-
[root@localhost bin]# ls -l
-
总用量 136
-
-rwxr-xr-x. 1 root root 1654 7月 26 12:13 cachedog.sh
-
-rwxr-xr-x. 1 root root 845 7月 26 12:13 cleancache.sh
-
-rwxr-xr-x. 1 root root 1116 7月 26 12:13 cleancache.v1.sh
-
-rwxr-xr-x. 1 root root 1398 7月 26 12:13 mqadmin
-
-rwxr-xr-x. 1 root root 1029 7月 26 12:13 mqadmin.cmd
-
-rwxr-xr-x. 1 root root 1422 7月 26 12:13 mqadmin.xml
-
-rwxr-xr-x. 1 root root 1394 7月 26 12:13 mqbroker
-
-rwxr-xr-x. 1 root root 1084 7月 26 12:13 mqbroker.cmd
-
-rwxr-xr-x. 1 root root 1373 7月 26 12:13 mqbroker.numanode0
-
-rwxr-xr-x. 1 root root 1373 7月 26 12:13 mqbroker.numanode1
-
-rwxr-xr-x. 1 root root 1373 7月 26 12:13 mqbroker.numanode2
-
-rwxr-xr-x. 1 root root 1373 7月 26 12:13 mqbroker.numanode3
-
-rwxr-xr-x. 1 root root 1388 7月 26 12:13 mqbroker.xml
-
-rwxr-xr-x. 1 root root 1400 7月 26 12:13 mqfiltersrv
-
-rwxr-xr-x. 1 root root 1094 7月 26 12:13 mqfiltersrv.cmd
-
-rwxr-xr-x. 1 root root 1394 7月 26 12:13 mqfiltersrv.xml
-
-rwxr-xr-x. 1 root root 1396 7月 26 12:13 mqnamesrv
-
-rwxr-xr-x. 1 root root 1088 7月 26 12:13 mqnamesrv.cmd
-
-rwxr-xr-x. 1 root root 1390 7月 26 12:13 mqnamesrv.xml
-
-rwxr-xr-x. 1 root root 1571 7月 26 12:13 mqshutdown
-
-rwxr-xr-x. 1 root root 1398 7月 26 12:13 mqshutdown.cmd
-
-rwxr-xr-x. 1 root root 2222 7月 26 12:13 os.sh
-
-rwxr-xr-x. 1 root root 1148 7月 26 12:13 play.cmd
-
-rwxr-xr-x. 1 root root 1008 7月 26 12:13 play.sh
-
-rwxr-xr-x. 1 root root 772 7月 26 12:13 README.md
-
-rwxr-xr-x. 1 root root 819 7月 26 12:13 runbroker
-
-rwxr-xr-x. 1 root root 2206 7月 26 12:13 runbroker.cmd
-
-rwxr-xr-x. 1 root root 2928 8月 10 02:37 runbroker.sh
-
-rwxr-xr-x. 1 root root 1816 7月 26 12:13 runserver.cmd
-
-rwxr-xr-x. 1 root root 2551 8月 10 02:35 runserver.sh
-
-rwxr-xr-x. 1 root root 1156 7月 26 12:13 setcache.sh
-
-rwxr-xr-x. 1 root root 1408 7月 26 12:13 startfsrv.sh
-
-rwxr-xr-x. 1 root root 1601 7月 26 12:13 tools.cmd
-
-rwxr-xr-x. 1 root root 1870 7月 26 12:13 tools.sh
-
[root@localhost bin]#
- runserver.sh 文件中 修改 JVM 配置下的第一行,将原来 4g 调小一点
-
#===========================================================================================
-
# JVM Configuration
-
#===========================================================================================
-
#JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
-
JAVA_OPT=
"${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn125m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
-
JAVA_OPT=
"${JAVA_OPT} -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:CMSInitiatingOccupancyFraction=70 -XX:+CMSParallelRemarkEnabled -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+CMSClassUnloadingEnabled -XX:SurvivorRatio=8 -XX:-UseParNewGC"
-
JAVA_OPT=
"${JAVA_OPT} -verbose:gc -Xloggc:/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"
-
JAVA_OPT=
"${JAVA_OPT} ${JAVA_OPT_EXT}"
-
JAVA_OPT=
"${JAVA_OPT} -cp ${CLASSPATH}"
-
-
$JAVA
${JAVA_OPT}
$@
- runbroker.sh 文件中 修改 JVM 配置下的第一行,将原来 8g 调小一点
-
#===========================================================================================
-
# JVM Configuration
-
#===========================================================================================
-
#JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
-
JAVA_OPT=
"${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn125m"
-
JAVA_OPT=
"${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0 -XX:SurvivorRatio=8"
-
JAVA_OPT=
"${JAVA_OPT} -verbose:gc -Xloggc:/dev/shm/mq_gc_%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCApplicationStoppedTime -XX:+PrintAdaptiveSizePolicy"
-
JAVA_OPT=
"${JAVA_OPT} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=30m"
-
JAVA_OPT=
"${JAVA_OPT} -XX:-OmitStackTraceInFastThrow"
-
JAVA_OPT=
"${JAVA_OPT} -XX:+AlwaysPreTouch"
-
JAVA_OPT=
"${JAVA_OPT} -XX:MaxDirectMemorySize=15g"
-
JAVA_OPT=
"${JAVA_OPT} -XX:-UseLargePages -XX:-UseBiasedLocking"
-
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"
-
JAVA_OPT=
"${JAVA_OPT} ${JAVA_OPT_EXT}"
-
JAVA_OPT=
"${JAVA_OPT} -cp ${CLASSPATH}"
- 然后再次启动 broker 时就成功了,broker 注册到了 nameserver 上了(localhost:9876
-
[root@localhost apache-rocketmq]# nohup sh bin/mqbroker -n localhost:
9876 &
-
[
2]
7512
-
[
1] 退出
1 nohup sh bin/mqbroker -n localhost:
9876
-
[root@localhost apache-rocketmq]# nohup: 忽略输入并把输出追加到
"nohup.out"
-
[root@localhost apache-rocketmq]# tail -f ~/logs/rocketmqlogs/broker.
log
-
2018
-08
-10
02:
39:
20 WARN main - Load
default discard message hook service: DefaultTransactionalMessageCheckListener
-
2018
-08
-10
02:
39:
21 INFO FileWatchService - FileWatchService service started
-
2018
-08
-10
02:
39:
21 INFO PullRequestHoldService - PullRequestHoldService service started
-
2018
-08
-10
02:
39:
23 INFO brokerOutApi_thread_1 - register broker
to name
server localhost:
9876 OK
-
2018
-08
-10
02:
39:
23 INFO main - Start transaction service!
-
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
-
2018
-08
-10
02:
39:
30 INFO BrokerControllerScheduledThread1 - dispatch behind commit
log
0 bytes
-
2018
-08
-10
02:
39:
30 INFO BrokerControllerScheduledThread1 - Slave fall behind master:
0 bytes
-
2018
-08
-10
02:
39:
33 INFO brokerOutApi_thread_2 - register broker
to name
server localhost:
9876 OK
-
2018
-08
-10
02:
40:
03 INFO brokerOutApi_thread_3 - register broker
to name
server localhost:
9876 OK
发送消息
- 发送/接收消息之前,需要告诉客户端(Producer、Consumer)名称服务器的位置,RocketMQ 提供了多种方法来实现这一点:
- 编程方式,如:
producer.setNamesrvAddr("ip:port")
- Java 选项,如
:rocketmq.namesrv.addr
- 环境变量,如:
NAMESRV_ADDR
- HTTP 端点
- 官方指导命令:
-
>
export NAMESRV_ADDR=localhost:
9876
-
> sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
-
SendResult [sendStatus=SEND_OK, msgId= ...
- 如下所示官方提供这个例子属于生产者,用于发送消息,运行之后会发送大量的消息,之后就会退出
-
[
root@localhost apache-rocketmq]
# export NAMESRV_ADDR=localhost:9876
-
[
root@localhost apache-rocketmq]
# sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
-
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=
128m; support was removed
in
8.0
-
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=
128m; support was removed
in
8.0
-
03:
05:
13.152 [main] DEBUG i.n.u.i.l.InternalLoggerFactory - Using SLF4J
as the
default logging framework
-
SendResult [sendStatus=SEND_OK, msgId=C0A83A8120207D4991AD2F02D2280000, offsetMsgId=C0A83A8100002A9F00000000000AFAC8, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=
0], queueOffset=
1000]
-
SendResult [sendStatus=SEND_OK, msgId=C0A83A8120207D4991AD2F02D2740001, offsetMsgId=C0A83A8100002A9F00000000000AFB7A, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=
1], queueOffset=
1000]
-
SendResult [sendStatus=SEND_OK, msgId=C0A83A8120207D4991AD2F02D27A0002, offsetMsgId=C0A83A8100002A9F00000000000AFC2C, messageQueue=MessageQueue [topic=TopicTest, brokerName=localhost.localdomain, queueId=
2], queueOffset=
1000]
-
.........................
接收消息
- 官方指导命令:
-
> sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
-
ConsumeMessageThread_%d Receive
New Messages: [MessageExt...
- 上面的消息发送完毕之后就会退出,在同一窗口中可以使用消费者类来进行接收消息,如下所示,显然是多线程的
-
[root@localhost apache-rocketmq]
# sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
-
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'}]]
-
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'}]]
-
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'}]]
-
...............
关闭服务器
- 同样都是在 RocketMQ 安装目录下的 distribution/target/apache-rocketmq 目录下执行命令
- 与启动顺序相反进行关闭,先关闭 broker、在关闭 nameserv
-
> 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
常用命令
除了上面几个命令之外,还有如下一些较常用的命令,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
**转自 https://blog.csdn.net/wangmx1993328/article/details/81536168**