mkdir -p /usr/local/chen/temp/rocketmq/demo
,接下来
RocketMQ
相关文件都放在该目录下,下载
rocketmq-all-4.7.0-bin-release.zip
文件。在工作目录下载文件。运行
wget https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.7.0/rocketmq-all-4.7.0-bin-release.zip
。
unzip rocketmq-all-4.7.0-bin-release.zip
。
runserver.sh
文件。运行
vim /usr/local/chen/temp/rocketmq/demo/rocketmq-all-4.7.0-bin-release/bin/runserver.sh
。修改默认的配置
JAVA_OPT="${JAVA_OPT} -server -Xms4g -Xmx4g -Xmn2g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
为
JAVA_OPT="${JAVA_OPT} -server -Xms1g -Xmx1g -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m"
。修改后如下图所示。
runbroker.sh
文件。运行
vim /usr/local/chen/temp/rocketmq/demo/rocketmq-all-4.7.0-bin-release/bin/runbroker.sh
。修改默认配置
JAVA_OPT="${JAVA_OPT} -server -Xms8g -Xmx8g -Xmn4g"
JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=25 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=0"
为
JAVA_OPT="${JAVA_OPT} -server -Xms512m -Xmx512m -Xmn256m"
JAVA_OPT="${JAVA_OPT} -XX:+UseG1GC -XX:G1HeapRegionSize=16m -XX:G1ReservePercent=10 -XX:InitiatingHeapOccupancyPercent=30 -XX:SoftRefLRUPolicyMSPerMB=1000"
修改后如下图所示:
然后建立集群需要的配置文件。在根目录建立broker-conf
文件夹。运行mkdir -p /usr/local/chen/temp/rocketmq/demo/broker-conf
。然后建立两个broker
集群需要的配置文件。目录结构如下:
每个文件对应的内容为:
broker0-n0.conf
brokerClusterName = DefaultCluster
brokerName = broker0
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
# dleger
enableDLegerCommitLog = true
dLegerGroup = broker0
dLegerPeers = n0-broker0n0:40911;n1-broker0n1:40911;n2-broker0n2:40911
dLegerSelfId = n0
sendMessageThreadPoolNums = 4
# namesrv的地址和端口,这里设置为虚拟机的IP,以便于让测试机访问
namesrvAddr=192.168.114.135:9876
# 该broker的IP地址,由于测试需要让其他机器访问,因此设置为虚拟机的IP
brokerIP1 = 192.168.114.135
listenPort = 10911
#存储路径
storePathRootDir=/app/data/store
#commitLog存储路径
storePathCommitLog=/app/data/store/commitlog
#消费队列存储路径
storePathConsumeQueue=/app/data/store/consumequeue
#索引存储路径
storePathIndex=/app/data/store/index
#checkpoint文件存储路径
storeCheckpoint=/app/data/store/checkpoint
#abort文件存储路径
abortFile=/app/data/store/abort
其中brokerIP1 = 192.168.114.135
配置的是虚拟机的IP,读者需要根据自己的IP进行修改。sendMessageThreadPoolNums = 4
因为本机是4核,所以配置为4,读者可以根据自己机器配置进行修改。以下内容大同小异,直接贴源码。
broker0-n1.conf
brokerClusterName = DefaultCluster
brokerName = broker0
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
# dleger
enableDLegerCommitLog = true
dLegerGroup = broker0
dLegerPeers = n0-broker0n0:40911;n1-broker0n1:40911;n2-broker0n2:40911
dLegerSelfId = n1
sendMessageThreadPoolNums = 4
# namesrv的地址和端口,这里设置为虚拟机的IP,以便于让测试机访问
namesrvAddr=192.168.114.135:9876
# 该broker的IP地址,由于测试需要让其他机器访问,因此设置为虚拟机的IP
brokerIP1 = 192.168.114.135
listenPort = 11911
#存储路径
storePathRootDir=/app/data/store
#commitLog存储路径
storePathCommitLog=/app/data/store/commitlog
#消费队列存储路径
storePathConsumeQueue=/app/data/store/consumequeue
#索引存储路径
storePathIndex=/app/data/store/index
#checkpoint文件存储路径
storeCheckpoint=/app/data/store/checkpoint
#abort文件存储路径
abortFile=/app/data/store/abort
broker0-n2.conf
brokerClusterName = DefaultCluster
brokerName = broker0
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
# dleger
enableDLegerCommitLog = true
dLegerGroup = broker0
dLegerPeers = n0-broker0n0:40911;n1-broker0n1:40911;n2-broker0n2:40911
dLegerSelfId = n2
sendMessageThreadPoolNums = 4
# namesrv的地址和端口,这里设置为虚拟机的IP,以便于让测试机访问
namesrvAddr=192.168.114.135:9876
# 该broker的IP地址,由于测试需要让其他机器访问,因此设置为虚拟机的IP
brokerIP1 = 192.168.114.135
listenPort = 12911
#存储路径
storePathRootDir=/app/data/store
#commitLog存储路径
storePathCommitLog=/app/data/store/commitlog
#消费队列存储路径
storePathConsumeQueue=/app/data/store/consumequeue
#索引存储路径
storePathIndex=/app/data/store/index
#checkpoint文件存储路径
storeCheckpoint=/app/data/store/checkpoint
#abort文件存储路径
abortFile=/app/data/store/abort
broker1-n0.conf
brokerClusterName = DefaultCluster
brokerName = broker1
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
# dleger
enableDLegerCommitLog = true
dLegerGroup = broker1
dLegerPeers = n0-broker1n0:40911;n1-broker1n1:40911;n2-broker1n2:40911
dLegerSelfId = n0
sendMessageThreadPoolNums = 4
# namesrv的地址和端口,这里设置为虚拟机的IP,以便于让测试机访问
namesrvAddr=192.168.114.135:9876
# 该broker的IP地址,由于测试需要让其他机器访问,因此设置为虚拟机的IP
brokerIP1 = 192.168.114.135
listenPort = 20911
#存储路径
storePathRootDir=/app/data/store
#commitLog存储路径
storePathCommitLog=/app/data/store/commitlog
#消费队列存储路径
storePathConsumeQueue=/app/data/store/consumequeue
#索引存储路径
storePathIndex=/app/data/store/index
#checkpoint文件存储路径
storeCheckpoint=/app/data/store/checkpoint
#abort文件存储路径
abortFile=/app/data/store/abort
broker1-n1.conf
brokerClusterName = DefaultCluster
brokerName = broker1
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
# dleger
enableDLegerCommitLog = true
dLegerGroup = broker1
dLegerPeers = n0-broker1n0:40911;n1-broker1n1:40911;n2-broker1n2:40911
dLegerSelfId = n1
sendMessageThreadPoolNums = 4
# namesrv的地址和端口,这里设置为虚拟机的IP,以便于让测试机访问
namesrvAddr=192.168.114.135:9876
# 该broker的IP地址,由于测试需要让其他机器访问,因此设置为虚拟机的IP
brokerIP1 = 192.168.114.135
listenPort = 21911
#存储路径
storePathRootDir=/app/data/store
#commitLog存储路径
storePathCommitLog=/app/data/store/commitlog
#消费队列存储路径
storePathConsumeQueue=/app/data/store/consumequeue
#索引存储路径
storePathIndex=/app/data/store/index
#checkpoint文件存储路径
storeCheckpoint=/app/data/store/checkpoint
#abort文件存储路径
abortFile=/app/data/store/abort
broker1-n2.conf
brokerClusterName = DefaultCluster
brokerName = broker1
brokerId = 0
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
# dleger
enableDLegerCommitLog = true
dLegerGroup = broker1
dLegerPeers = n0-broker1n0:40911;n1-broker1n1:40911;n2-broker1n2:40911
dLegerSelfId = n2
sendMessageThreadPoolNums = 4
# namesrv的地址和端口,这里设置为虚拟机的IP,以便于让测试机访问
namesrvAddr=192.168.114.135:9876
# 该broker的IP地址,由于测试需要让其他机器访问,因此设置为虚拟机的IP
brokerIP1 = 192.168.114.135
listenPort = 22911
#存储路径
storePathRootDir=/app/data/store
#commitLog存储路径
storePathCommitLog=/app/data/store/commitlog
#消费队列存储路径
storePathConsumeQueue=/app/data/store/consumequeue
#索引存储路径
storePathIndex=/app/data/store/index
#checkpoint文件存储路径
storeCheckpoint=/app/data/store/checkpoint
#abort文件存储路径
abortFile=/app/data/store/abort
然后在根目录下新建文件rocketmq-namesrv.dockerfile
以构建namrsrv
镜像的Dockerfile
。运行vim rocketmq-namesrv.dockerfile
。源码如下:
rocketmq-namesrv.dockerfile
FROM openjdk:8u212-jre-alpine3.9
LABEL MAINTAINER='cn.edu.njust.chen'
ADD rocketmq-all-4.7.0-bin-release /app/rocketmq
ENTRYPOINT exec sh /app/rocketmq/bin/mqnamesrv -n 127.0.0.1:9876
RUN echo "Asia/Shanghai" > /etc/timezone
EXPOSE 9876
接着构建broker
镜像的Dockerfile
,根目录下新建文件rocketmq-broker.dockerfile
, 运行vim rocketmq-broker.dockerfile
,内容如下:
FROM openjdk:8u212-jre-alpine3.9
LABEL MAINTAINER='cn.edu.njust.chen'
ADD rocketmq-all-4.7.0-bin-release /app/rocketmq
RUN echo "Asia/Shanghai" > /etc/timezone
ENTRYPOINT exec sh /app/rocketmq/bin/mqbroker -c /app/data/conf/broker.conf
VOLUME /app/data
最后创建docker-compose
文件,在根目录下新建文件docker-compose.yml
, 运行vim docker-compose.yml
。内容如下:
version: "3.5"
services:
# 运行一个name server
namesrv1:
build:
context: .
dockerfile: rocketmq-namesrv.dockerfile
image: rocketmq-namesrv/4.7.0
container_name: namesrv1
restart: always
networks:
rocketmq-dledger:
ports:
- 9876:9876
# 运行一个rocketmq控制台服务
console:
image: styletang/rocketmq-console-ng
container_name: console
depends_on:
- namesrv1
environment:
- JAVA_OPTS= -Dlogging.level.root=info -Drocketmq.namesrv.addr=namesrv1:9876
- Dcom.rocketmq.sendMessageWithVIPChannel=false
networks:
rocketmq-dledger:
ports:
- 8087:8080
# broker0
broker0-n0:
build:
context: .
dockerfile: rocketmq-broker.dockerfile
image: rocketmq-broker/4.7.0
depends_on:
- namesrv1
container_name: broker0n0
restart: always
networks:
rocketmq-dledger:
volumes:
- ./broker-conf/broker0-n0.conf:/app/data/conf/broker.conf
- ./store/broker0n0:/app/data/store
ports:
- 10909:10909
- 10911:10911
- 10912:10912
broker0-n1:
build:
context: .
dockerfile: rocketmq-broker.dockerfile
image: rocketmq-broker/4.7.0
depends_on:
- namesrv1
container_name: broker0n1
restart: always
networks:
rocketmq-dledger:
volumes:
- ./broker-conf/broker0-n1.conf:/app/data/conf/broker.conf
- ./store/broker0n1:/app/data/store
ports:
- 11909:11909
- 11911:11911
- 11912:11912
broker0-n2:
build:
context: .
dockerfile: rocketmq-broker.dockerfile
image: rocketmq-broker/4.7.0
depends_on:
- namesrv1
container_name: broker0n2
restart: always
networks:
rocketmq-dledger:
volumes:
- ./broker-conf/broker0-n2.conf:/app/data/conf/broker.conf
- ./store/broker0n2:/app/data/store
ports:
- 12909:12909
- 12911:12911
- 12912:12912
# broker1
broker1-n0:
build:
context: .
dockerfile: rocketmq-broker.dockerfile
image: rocketmq-broker/4.7.0
depends_on:
- namesrv1
container_name: broker1n0
restart: always
networks:
rocketmq-dledger:
volumes:
- ./broker-conf/broker1-n0.conf:/app/data/conf/broker.conf
- ./store/broker1n0:/app/data/store
ports:
- 20909:20909
- 20911:20911
- 20912:20912
broker1-n1:
build:
context: .
dockerfile: rocketmq-broker.dockerfile
image: rocketmq-broker/4.7.0
depends_on:
- namesrv1
container_name: broker1n1
restart: always
networks:
rocketmq-dledger:
volumes:
- ./broker-conf/broker1-n1.conf:/app/data/conf/broker.conf
- ./store/broker1n1:/app/data/store
ports:
- 21909:21909
- 21911:21911
- 21912:21912
broker1-n2:
build:
context: .
dockerfile: rocketmq-broker.dockerfile
image: rocketmq-broker/4.7.0
depends_on:
- namesrv1
container_name: broker1n2
restart: always
networks:
rocketmq-dledger:
volumes:
- ./broker-conf/broker1-n2.conf:/app/data/conf/broker.conf
- ./store/broker1n2:/app/data/store
ports:
- 22909:22909
- 22911:22911
- 22912:22912
networks:
rocketmq-dledger:
至此系统需要文件全部创建完毕。目录结构如下:
然后运行docker-compose up
启动服务。
服务成功启动。运行docker info
查看服务状态。
服务已经全部启动。然后在浏览器输入http://192.168.114.135:8087/#/
访问系统即可。
书上的代码直接运行绝大部分是对的,但是总有一些软件的更新使得作者无能为力。之前的API是对的,但是之后就废弃了或修改了是常有的事。所以我们需要跟踪源代码。这只是一个小小的问题,如果没有前辈的无私奉献,很难想象我们自己一天能学到多少内容。感谢各位前辈的辛勤付出,让我们少走了很多的弯路!
点个赞再走呗!欢迎留言哦!