1.概述
2.安装部署
2.1 centos7上安装RocketMQ单机版
#在 /opt/software 目录下, 执行下载命令
wget https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/4.7.1/rocketmq-all-4.7.1-bin-release.zip
#解压缩
unzip rocketmq-all-4.7.1-bin-release.zip
#启动nameserver (默认端口9876)
nohup sh bin/mqnamesrv &
#查看nameserver启动情况
lsof -i:9876
#查看nameserver启动日志 (有 The Name Server boot success. serializeType=JSON 表示成功)
tail -f ~/logs/rocketmqlogs/namesrv.log
#启动broker前, 先修改下 bin/runbroker.sh 和 bin/runserver.sh启动参数
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
#启动broker前, 先修改下 conf/broker.conf配置文件, 启动时指定broker注册ip地址
namesrvAddr=192.168.0.156:9876
brokerIP1=192.168.0.156
#启动broker, 指定 broker.conf
nohup sh bin/mqbroker -n 192.168.0.156:9876 -c conf/broker.conf &
#查看broker日志
tail -f ~/logs/rocketmqlogs/broker.log
#查看进程
[root@bogon rocketmqlogs]# jps
4067 Kafka
9862 Jps
9705 BrokerStartup
8495 NamesrvStartup
#消息生产者
#xshell中打开一个窗口, 设置临时环境变量
export NAMESRV_ADDR=localhost:9876
#使用安装包中的sh脚本发送消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Producer
#消息消费者
>> xshell中新打开一个窗口, 设置临时环境变量
export NAMESRV_ADDR=localhost:9876
>> 使用安装包中的sh脚本接收消息
sh bin/tools.sh org.apache.rocketmq.example.quickstart.Consumer
#关闭namesrv
sh bin/mqshutdown namesrv
#关闭broker
sh bin/mqshutdown broker
2.2 centos7 上借助 docker-compose安装集群版(2主)
2.2.1 在centos7上创建文件夹
mkdir -p /opt/rocketmq/logs/nameserver-a
mkdir -p /opt/rocketmq/logs/nameserver-b
mkdir -p /opt/rocketmq/store/nameserver-a
mkdir -p /opt/rocketmq/store/nameserver-b
mkdir -p /opt/rocketmq/logs/broker-a
mkdir -p /opt/rocketmq/logs/broker-b
mkdir -p /opt/rocketmq/store/broker-a
mkdir -p /opt/rocketmq/store/broker-b
mkdir -p /home/rocketmq/broker-a/
mkdir -p /home/rocketmq/broker-b/
2.2.2 创建文件
/home/rocketmq/broker-a/ 文件夹下创建文件: broker-a.conf
brokerClusterName=rocketmq-cluster
brokerName=broker-a
brokerId=0
#这个很有讲究 如果是正式环境 这里一定要填写内网地址(安全)
#如果是用于测试或者本地这里建议要填外网地址,因为你的本地代码是无法连接到阿里云内网,只能连接外网。
brokerIP1=192.168.0.156
deleteWhen=04
fileReservedTime=48
brokerRole=ASYNC_MASTER
flushDiskType=ASYNC_FLUSH
# 内网的(阿里云有内网IP和外网IP)
namesrvAddr=192.168.0.156:9876;192.168.0.156:9877
autoCreateTopicEnable=true
#Broker 对外服务的监听端口,
listenPort=10911
#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
/home/rocketmq/broker-b/ 文件夹下创建文件: broker-b.conf
brokerClusterName = rocketmq-cluster
brokerName = broker-b
brokerId = 0
brokerIP1 = 192.168.0.156
deleteWhen = 04
fileReservedTime = 48
brokerRole = ASYNC_MASTER
flushDiskType = ASYNC_FLUSH
# 内网的(阿里云有内网IP和外网IP)
namesrvAddr=192.168.0.156:9876;192.168.0.156:9877
autoCreateTopicEnable=true
#Broker 对外服务的监听端口,
listenPort = 10909
#Broker角色
#- ASYNC_MASTER 异步复制Master
#- SYNC_MASTER 同步双写Master
#- SLAVE
brokerRole=ASYNC_MASTER
#刷盘方式
#- ASYNC_FLUSH 异步刷盘
#- SYNC_FLUSH 同步刷盘
flushDiskType=ASYNC_FLUSH
/opt/rocketmq/ 文件夹下创建文件: docker-compose.yml
version: '3.5'
services:
rmqnamesrv-a:
image: rocketmqinc/rocketmq:4.4.0
container_name: rmqnamesrv-a
ports:
- 9876:9876
volumes:
- /opt/rocketmq/logs/nameserver-a:/opt/logs
- /opt/rocketmq/store/nameserver-a:/opt/store
command: sh mqnamesrv
networks:
rmq:
aliases:
- rmqnamesrv-a
rmqnamesrv-b:
image: rocketmqinc/rocketmq:4.4.0
container_name: rmqnamesrv-b
ports:
- 9877:9876
volumes:
- /opt/rocketmq/logs/nameserver-b:/opt/logs
- /opt/rocketmq/store/nameserver-b:/opt/store
command: sh mqnamesrv
networks:
rmq:
aliases:
- rmqnamesrv-b
rmqbroker-a:
image: rocketmqinc/rocketmq:4.4.0
container_name: rmqbroker-a
ports:
- 10911:10911
#解决 docker-compose 的 volume 映射发生 Permission denied 问题
privileged: true
volumes:
- /opt/rocketmq/logs/broker-a:/opt/logs
- /opt/rocketmq/store/broker-a:/opt/store
- /home/rocketmq/broker-a/broker-a.conf:/opt/rocketmq-4.4.0/conf/broker.conf
environment:
TZ: Asia/Shanghai
NAMESRV_ADDR: "rmqnamesrv-a:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms256m -Xmx256m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
command: sh mqbroker -n 192.168.0.156:9876;192.168.0.156:9877 -c /opt/rocketmq-4.4.0/conf/broker.conf autoCreateTopicEnable=true &
depends_on:
- rmqnamesrv-a
- rmqnamesrv-b
links:
- rmqnamesrv-a:rmqnamesrv-a
- rmqnamesrv-b:rmqnamesrv-b
networks:
rmq:
aliases:
- rmqbroker-a
rmqbroker-b:
image: rocketmqinc/rocketmq:4.4.0
container_name: rmqbroker-b
ports:
- 10909:10909
#解决 docker-compose 的 volume 映射发生 Permission denied 问题
privileged: true
volumes:
- /opt/rocketmq/logs/broker-b:/opt/logs
- /opt/rocketmq/store/broker-b:/opt/store
- /home/rocketmq/broker-b/broker-b.conf:/opt/rocketmq-4.4.0/conf/broker.conf
environment:
TZ: Asia/Shanghai
NAMESRV_ADDR: "rmqnamesrv-b:9876"
JAVA_OPTS: " -Duser.home=/opt"
JAVA_OPT_EXT: "-server -Xms256m -Xmx256m -Xmn256m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
command: sh mqbroker -n 192.168.0.156:9876;192.168.0.156:9877 -c /opt/rocketmq-4.4.0/conf/broker.conf autoCreateTopicEnable=true &
depends_on:
- rmqnamesrv-a
- rmqnamesrv-b
links:
- rmqnamesrv-a:rmqnamesrv-a
- rmqnamesrv-b:rmqnamesrv-b
networks:
rmq:
aliases:
- rmqbroker-b
rmqconsole:
image: styletang/rocketmq-console-ng
container_name: rmqconsole
# 注意, 这里端口映射时, 要是8080.
ports:
- 9001:8080
environment:
JAVA_OPTS: "-Drocketmq.namesrv.addr=rmqnamesrv-a:9876;rmqnamesrv-b:9877 -Dcom.rocketmq.sendMessageWithVIPChannel=false"
depends_on:
- rmqnamesrv-a
- rmqnamesrv-b
networks:
rmq:
aliases:
- rmqconsole
networks:
rmq:
name: rmq
driver: bridge
2.2.3 启动集群
#执行前, 先确保
#开启docker挂载权限: chmod a+rw /var/run/docker.sock
#开启了可执行权限: chmod +x /usr/local/bin/docker-compose
docker-compose -f docker-compose.yml up -d
2.2.4 启动反馈
启动成功后, 自行用java api 连接进行测试即可.
6.常见问题
6.1 org.apache.rocketmq.client.exception.MQClientException: No route info of this topic
sh mqbroker -n 192.168.0.156:9876 -c /opt/rocketmq-4.4.0/conf/broker.conf autoCreateTopicEnable=true
升级至4.4.0 以上版本, 基本可解决
参考资料
https://www.cnblogs.com/ifme/p/13079735.html (常规虚拟机搭建集群)
https://www.cnblogs.com/qdhxhz/p/11096682.html (docker搭建2m集群)
https://blog.csdn.net/wangxiaoyan2/article/details/106237900 (docker搭建2m2s集群)