共计 3646 个字符,预计需要花费 10 分钟才能阅读完成。
rocketmq 是一个开源的消息中间件,以其高性能、高可靠性、高可扩展性和良好的容错性而闻名。它支持多种消息类型,包括但不限于队列模型和发布/订阅模型,能够满足不同场景下的消息传递需求。搭建方式如下:
1. 命令行搭建
首先需要创建一个网络:
docker network create rocketmq
创建 name server
日志和存储目录:
mkdir -p /home/docker/rocketmq/namesrv/logs && \
chown -R 3000:3000 /home/docker/rocketmq/
再运行 name server
容器:
docker run -d --name rmqnamesrv \
-p 9876:9876 \
--network rocketmq \
-v /home/docker/rocketmq/namesrv/logs:/home/rocketmq/logs \
apache/rocketmq:5.1.4 sh mqnamesrv autoCreateTopicEnable=true
创建 broker
配置文件,注意这里 brokerIP1 的地址需要修改为内网 ip 地址:
mkdir -p /home/docker/rocketmq/broker/conf && \
mkdir -p /home/docker/rocketmq/broker/logs && \
mkdir -p /home/docker/rocketmq/broker/store && \
chown -R 3000:3000 /home/docker/rocketmq/ && \
cat > /home/docker/rocketmq/broker/conf/broker.conf<<EOF
# Broker配置文件
# NameServer地址,多个地址使用分号隔开
#namesrvAddr=rmqnamesrv:9876
# 集群名称
brokerClusterName=DefaultCluster
# Broker名称,集群部署时需保证唯一性
brokerName=broker-a
# Broker ID,0表示master,正整数表示slave
brokerId=0
# Broker对外服务的监听端口
#listenPort=10911
# Broker服务地址,如果是内网部署,填写内网IP
brokerIP1=127.0.0.1
# Broker HA 地址,供slave同步消息的地址
#brokerIP2=127.0.0.1
# 存储路径配置,根据实际情况调整
#storePathRootDir=/home/rocketmq/store
#storePathCommitLog=/home/rocketmq/store/commitlog
#storePathConsumerQueue=/home/rocketmq/store/consumerqueue
#storePathIndex=/home/rocketmq/store/index
EOF
这里执行 chown -R 3000:3000 /home/docker/rocketmq/
的目的是,让容器有权限操作这几个目录,因为容器内默认使用 rocketmq
用户,并且 id 为 3000。
当启动容器时指定为 root 用户启动时,就算不修改权限也不会启动报错。但是,
logs
文件夹却变成了/root/logs
,所以猜测这个文件夹的位置和运行的用户有关,为了不让目录太混乱,还是选择用默认的 rocketmq 用户启动。
创建 broker
容器:
docker run -d \
--name rmqbroker \
--network rocketmq \
-p 10911:10911 \
-p 10909:10909 \
-e "NAMESRV_ADDR=rmqnamesrv:9876" \
-v /home/docker/rocketmq/broker/conf/broker.conf:/home/rocketmq/conf/broker.conf \
-v /home/docker/rocketmq/broker/logs:/home/rocketmq/logs \
-v /home/docker/rocketmq/broker/store:/home/rocketmq/store \
apache/rocketmq:5.1.4 sh mqbroker autoCreateTopicEnable=true -c /home/rocketmq/conf/broker.conf
如果有如下报错,那么可能是权限问题,把目录映射可以删了试试。
java.lang.NullPointerException
at org.apache.rocketmq.broker.schedule.ScheduleMessageService.configFilePath(ScheduleMessageService.java:271)
at org.apache.rocketmq.common.ConfigManager.persist(ConfigManager.java:82)
at org.apache.rocketmq.broker.BrokerController.shutdownBasicService(BrokerController.java:1409)
at org.apache.rocketmq.broker.BrokerController.shutdown(BrokerController.java:1484)
at org.apache.rocketmq.broker.BrokerStartup.createBrokerController(BrokerStartup.java:242)
at org.apache.rocketmq.broker.BrokerStartup.main(BrokerStartup.java:50)
创建可视化面板容器:
docker run -d \
--name rmqdashboard \
--network rocketmq \
-p 8080:8080 \
-e "JAVA_OPTS=-Xmx256M -Xms256M -Xmn128M -Drocketmq.namesrv.addr=rmqnamesrv:9876" \
apacherocketmq/rocketmq-dashboard:latest
这里创建一个 topic,然后发送一条消息,发现能查询出来两条一样的消息。
但是通过 key 查询却发现只有一条,暂时不清楚是什么原因,可能是面板的 bug。
2. compose搭建
由于容器有三个,所以使用 compose 管理会更方便。当然了,即使使用 compose 运行,依然需要创建上面的配置文件,并配置好权限。
version: '3'
services:
rmqnamesrv:
image: apache/rocketmq:5.2.0
container_name: rmqnamesrv
command: sh mqnamesrv autoCreateTopicEnable=true
ports:
- "9876:9876"
volumes:
- /home/docker/rocketmq/namesrv/logs:/home/rocketmq/logs
networks:
- rocketmq
rmqbroker:
image: apache/rocketmq:5.2.0
container_name: rmqbroker
command: sh mqbroker autoCreateTopicEnable=true -c /home/rocketmq/conf/broker.conf
ports:
- "10911:10911"
- "10909:10909"
volumes:
- /home/docker/rocketmq/broker/conf/broker.conf:/home/rocketmq/conf/broker.conf
- /home/docker/rocketmq/broker/logs:/home/rocketmq/logs
- /home/docker/rocketmq/broker/store:/home/rocketmq/store
environment:
- NAMESRV_ADDR=rmqnamesrv:9876
networks:
- rocketmq
depends_on:
- rmqnamesrv
rmqdashboard:
image: apacherocketmq/rocketmq-dashboard:latest
container_name: rmqdashboard
environment:
- JAVA_OPTS=-Xmx256M -Xms256M -Xmn128M -Drocketmq.namesrv.addr=rmqnamesrv:9876
ports:
- "8080:8080"
networks:
- rocketmq
networks:
rocketmq:
driver: bridge