Docker 入门到实战 之 安装RocketMQ
消息队列RocketMQ版(原名开放消息服务,简称ONS)是阿里云基于Apache RocketMQ构建的低延迟、高并发、高可用、高可靠的分布式消息中间件。
--- Don`t be the same, be better!---
RocketMQ 是一个队列模型的消息中间件,具有高性能、高可靠、高实时、分布式特点。
具体的描述可参考官网文档:https://rocketmq.apache.org/docs/motivation/
RocketMQ 署
RocketMQ 镜像
docker search rocketmq
一、部署namesrv
namesrv
就类似于消息队列的注册中心,因为rocketmq
原生支持集群,所以namesrv
就显得很重要。
拉取镜像
首先是拉取镜像,这里拉取的是rocketMQ
的核心镜像。这里选取4.4.0版本进行操作。
docker pull rocketmqinc/rocketmq:4.4.0
如果不加版本号,默认拉取的是最新版本:latest。想确认拉取到的镜像的版本的话,可以利用 docker inspect 命令查看。
docker inspect 镜像名/镜像ID
[root@localhost roker]# docker inspect 09bbc30a03b6
[
{
"Id": "sha256:09bbc30a03b682786ad205beb772a382d4cecc8a521206d7b3c50e95fb725530",
"RepoTags": [
"rocketmqinc/rocketmq:latest"
],
"RepoDigests": [
"rocketmqinc/rocketmq@sha256:b08556227ad8d169b7927f4c48335877cb10060e6781374ce5a644ea29f6fbe1"
],
"Parent": "",
"Comment": "",
"Created": "2019-01-24T09:27:57.997168997Z",
"Container": "8d5461f9b1304311d307c113cb91261c591b54a820db2333dc24a53f901d3374",
"ContainerConfig": {
"Hostname": "a8b39eae818b",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"10909/tcp": {},
"10911/tcp": {},
"9876/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"ROCKETMQ_VERSION=4.4.0",
"ROCKETMQ_HOME=/opt/rocketmq-4.4.0"
],
"Cmd": [
"/bin/sh",
"-c",
"#(nop) WORKDIR /opt/rocketmq-4.4.0/bin"
],
"ArgsEscaped": true,
"Image": "sha256:cf18f677a66a1ecfa2016959abdd94951ad7a35c89b280f31f39f1a5ab61beb1",
"Volumes": null,
"WorkingDir": "/opt/rocketmq-4.4.0/bin",
"Entrypoint": null,
"OnBuild": [],
"Labels": {
"org.label-schema.build-date": "20181205",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"DockerVersion": "1.13.1",
"Author": "",
"Config": {
"Hostname": "a8b39eae818b",
"Domainname": "",
"User": "",
"AttachStdin": false,
"AttachStdout": false,
"AttachStderr": false,
"ExposedPorts": {
"10909/tcp": {},
"10911/tcp": {},
"9876/tcp": {}
},
"Tty": false,
"OpenStdin": false,
"StdinOnce": false,
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"ROCKETMQ_VERSION=4.4.0",
"ROCKETMQ_HOME=/opt/rocketmq-4.4.0"
],
"Cmd": [
"/bin/bash"
],
"ArgsEscaped": true,
"Image": "sha256:cf18f677a66a1ecfa2016959abdd94951ad7a35c89b280f31f39f1a5ab61beb1",
"Volumes": null,
"WorkingDir": "/opt/rocketmq-4.4.0/bin",
"Entrypoint": null,
"OnBuild": [],
"Labels": {
"org.label-schema.build-date": "20181205",
"org.label-schema.license": "GPLv2",
"org.label-schema.name": "CentOS Base Image",
"org.label-schema.schema-version": "1.0",
"org.label-schema.vendor": "CentOS"
}
},
"Architecture": "amd64",
"Os": "linux",
"Size": 380393601,
"VirtualSize": 380393601,
"GraphDriver": {
"Data": {
"LowerDir": "/var/lib/docker/overlay2/be88140e2ac3afee73aea9c896483957b551f7ce26a59654a132c9369a71ef42/diff:/var/lib/docker/overlay2/705f8e8991114ea481b858c81fbb509926bdc0c39a51c2740046deceee406683/diff:/var/lib/docker/overlay2/dc8719daa823b125ef5710e4ecc3b009b06a8075376a1f0782ca8efbb23bf09f/diff:/var/lib/docker/overlay2/103f52afcf6b6c1c8edf0a98bd3b2e0c0242ed9990be9ed7c81113abf6812366/diff:/var/lib/docker/overlay2/e2e0be6dd3bbec94da46873708347d706dc82bb448fac0a1b98ce8279df397db/diff:/var/lib/docker/overlay2/5eb13e209a54015521756f4c8568100a01be13f0b73410f99abdbb7b6afcc4f3/diff:/var/lib/docker/overlay2/e90ea65557175e5555810565c14e4938772870a4300b27e1aeb79f4f31868bed/diff",
"MergedDir": "/var/lib/docker/overlay2/de0928e884167c9933c1561dc721a805c3cddb1425c021256807ab88a4da572f/merged",
"UpperDir": "/var/lib/docker/overlay2/de0928e884167c9933c1561dc721a805c3cddb1425c021256807ab88a4da572f/diff",
"WorkDir": "/var/lib/docker/overlay2/de0928e884167c9933c1561dc721a805c3cddb1425c021256807ab88a4da572f/work"
},
"Name": "overlay2"
},
"RootFS": {
"Type": "layers",
"Layers": [
"sha256:071d8bd765171080d01682844524be57ac9883e53079b6ac66707e192ea25956",
"sha256:f3e23290ed2c15b97180d39a7fbf55236e5e750aae44e08c054b90ffd371ddba",
"sha256:edd13d457cc130b61e1d96448502d0c90baf846abd04f1b76a0c33e4d90cd907",
"sha256:bd01a461b66b9da34ff5efb91cb15f3b5fe7ecc7e0e069786517b12240ccc37e",
"sha256:a42c26b79bf8dcf87557460a20448a7fd9a0725bcb76d3ba0dc0c2a955cccaf5",
"sha256:3dd012a176dfd1a397f84eae13d07505bccaffd74db3b1a5d4698fecbfede8a5",
"sha256:5ab4fc027c7ede59acb94f10e9292f88ee5096c112c9375f69aca6b152aada6b",
"sha256:1e990feb9b9d79d57f3b48ed803a56cfeced51347f72d450c303e5efa86f7003"
]
},
"Metadata": {
"LastTagTime": "0001-01-01T00:00:00Z"
}
}
]
其中 ROCKETMQ_VERSION=4.4.0 说明当前最新版本为4.4.0。
启动namesrv
启动过程也很简单,主要是docker
的一些参数和配置指定。启动命令如下:
docker run -d -p 9876:9876 \
--name rmqnamesrv \
--restart=always \
-v /data/docker/rocketmq/data/namesrv/logs:/root/logs \
-v /data/docker/rocketmq/namesrv/store:/root/store \
-e "MAX_POSSIBLE_HEAP=100000000" \
rocketmqinc/rocketmq:latest \
sh mqnamesrv
命令详解:
# 后台运行,并映射端口
docker run -d -p 9876:9876 \
# 设置容器名称
--name rmqnamesrv \
# 设置重启策略
--restart=always \
# 映射日志文件夹(前面的为本地路径)
-v /data/docker/rocketmq/data/namesrv/logs:/root/logs \
# 映射书籍存在文件夹(同上)
-v /data/docker/rocketmq/namesrv/store:/root/store \
# 指定环境变量
-e "MAX_POSSIBLE_HEAP=100000000" \
# 指定镜像版本,这里建议指定具体版本,否则在和java交互的时候会报错,目前最新版本是4.4.0,所以maven的依赖也必须对应,否则会报错
rocketmqinc/rocketmq:latest \
# 启动命令
sh mqnamesrv
查看容器:
二、部署broker
由于broker
和namesrv
是同一个镜像,所以这里不需要再拉取其他镜像。
创建broker配置
除了最后两行配置,其他的配置都是系统默认的,具体说明可以参考官方文档。
mkdir
-p
/data/docker/rocketmq/conf
touch
broker.conf
vi
broker.conf
# broker集群名称
brokerClusterNam = DefaultCluster
# broker节点名称
brokerName = broker-a
# broker节点id
brokerId = 0
# 删除条件?这个还不清楚用途,后续研究下
deleteWhen = 04
# 文件保留时间(单位小时),默认为3天
fileReservedTime = 48
# broker角色
brokerRole = ASYNC_MASTER
# 磁盘同步方式:同步,异步
flushDiskType = ASYNC_FLUSH
# 类似注册中心
namesrvAddr=192.168.10.128:9876
# 当前broker监听的IP(主)
brokerIP1 = 192.168.10.128
注:若忽略此步,可能在下步启动broker时出现 如下问题时
docker: Error response from daemon: failed to create shim task:
OCI runtime create failed: runc create failed: unable to start container process: error during container init:
error mounting "/data/docker/rocketmq/conf/broker.conf" to rootfs at "/opt/rocketmq-4.4.0/conf/broker.conf": mount /data/docker/rocketmq/conf/broker.conf:/opt/rocketmq-4.4.0/conf/broker.conf (via /proc/self/fd/6),
flags: 0x5000: not a directory: unknown: Are you trying to mount a directory onto a file (or vice-versa)?
Check if the specified host path exists and is the expected type.
启动broker
这里的启动命令最核心的其实就是最后sh
操作,这里制定了配置文件的路径。
docker run -d \
-p 10911:10911 \
-p 10909:10909 \
--name rmqbroker \
--restart=always \
-v /data/docker/rocketmq/data/broker/logs:/root/logs \
-v /data/docker/rocketmq/data/broker/store:/root/store \
-v /data/docker/rocketmq/conf/broker.conf:/opt/rocketmq-4.4.0/conf/broker.conf \
--link rmqnamesrv:namesrv \
-e "NAMESRV_ADDR=namesrv:9876" \
-e "MAX_POSSIBLE_HEAP=200000000" rocketmqinc/rocketmq:latest \
sh mqbroker -c /opt/rocketmq-4.4.0/conf/broker.conf
命令详解:
# 后台运行
docker run -d \
# 映射端口
-p 10911:10911 \
# 映射端口
-p 10909:10909 \
# 容器名称
--name rmqbroker \
# 重启策略
--restart=always \
# 映射日志路径
-v /data/docker/rocketmq/data/broker/logs:/root/logs \
# 映射文件存储路径
-v /data/docker/rocketmq/data/broker/store:/root/store \
# 映射配置文件
-v /data/docker/rocketmq/conf/broker.conf:/opt/rocketmq-4.4.0/conf/broker.conf \
# 链接namesrv
--link rmqnamesrv:namesrv \
# 指定环境设置
-e "NAMESRV_ADDR=namesrv:9876" \
# 指定环境设置,指定镜像
-e "MAX_POSSIBLE_HEAP=200000000" rocketmqinc/rocketmq:latest \
# 启动命令(指定配置启动)
sh mqbroker -c /opt/rocketmq-4.4.0/conf/broker.conf
查看容器:
由于这里我们把rocketmq
的日志映射到了本机目录,所以我们可以直接通过tail
查看broker
的启动日志,比如broker.log
:
tail -f /data/docker/rocketmq/data/broker/logs/rocketmqlogs/broker.log
当然,你也可以通过docker exec
命令的方式进入容器内查看日志,当然就是没有上面这种方式方便。
三、安装console
console
是rocketmq
的扩展组件,console
组件提供了图形化的界面,便于我们管理和监控rocketmq
,界面截图如下:
官方文档:
拉取镜像
默认拉取最新版本镜像:
docker pull styletang/rocketmq-console-ng
启动console
启动命令也很简单,只是指定rocketmq
的namesrv
的地址,除了端口有修改,其他直接复制官方文档
docker run -e "JAVA_OPTS=-Drocketmq.namesrv.addr=192.168.10.128:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false" -p 8000:8080 -t styletang/rocketmq-console-ng
启动成功后直接访问我们配置的端口,示例地址:http://192.168.10.128:8000 :
- END -
至此,我们可以在实际项目中引入RocketMQ进行实战了。
更多推荐
所有评论(0)