最近学习使用 rocketmq,须要搭建 rocketmq 服务端,本文主要记录 rocketmq 搭建过程以及这个过程踩到的一些坑。至于有多简单呢,在本机已有Docker环境的状况下只须要三步便可。git
- 从github上面拉取项目
- 修改
broker.conf
中的brokerIP1
参数,修改成本机IP - 进入
docker-compose.yml
文件所在路径,执行docker-compose up
命令便可
前言
首先咱们是使用Docker进行搭建环境的,因此咱们先要在本身机器上的安装Docker,具体的安装过程以及对于Docker的介绍官方文档里面说的很清楚了https://docs.docker.com/get-started/。github
咱们要搭建RocketMQ服务器,那么咱们就要知道大概搭建RocketMQ服务器须要部署哪些东西。对于RocketMQ有一个架构图,以下所示。而图中所示的Producer(生产者)和Consumer(消费者)无需咱们搭建,由于那是做为一个服务器进行启动的。nameserver就是一个注册中心同样组件,咱们能够将其简单理解成springcloud中的Eureka,那么nameserver是须要咱们搭建的。broker就是真正处理消息的地方,也是须要咱们搭建的。spring
正常状况咱们搭建上面所提到的两个组件其实就能已经可以知足咱们的发送接收消息的需求了。可是一般状况下咱们还须要搭建一个Web可视化的平台用来查看MQ的服务状态、消息的消费状况、主题的队列配置等等。这里使用rocketmq-console
。一样也是经过Docker来进行安装。docker
部署
上面咱们提到了须要安装三个组件,那么这三个组件又是须要可以互相通讯链接的,考虑到分开部署进行配置链接信息比较麻烦,因而这里咱们采用docker-compose
进行配置部署。express
首先咱们须要建立docker-compose.yml
配置文件。文件内容以下apache
version: '3.5' services: rmqnamesrv: image: foxiswho/rocketmq:server container_name: rmqnamesrv ports: - 9876:9876 volumes: - ./logs:/opt/logs - ./store:/opt/store networks: rmq: aliases: - rmqnamesrv rmqbroker: image: foxiswho/rocketmq:broker container_name: rmqbroker ports: - 10909:10909 - 10911:10911 volumes: - ./logs:/opt/logs - ./store:/opt/store - ./conf/broker.conf:/etc/rocketmq/broker.conf environment: NAMESRV_ADDR: "rmqnamesrv:9876" JAVA_OPTS: " -Duser.home=/opt" JAVA_OPT_EXT: "-server -Xms128m -Xmx128m -Xmn128m" command: mqbroker -c /etc/rocketmq/broker.conf depends_on: - rmqnamesrv networks: rmq: aliases: - rmqbroker rmqconsole: image: styletang/rocketmq-console-ng container_name: rmqconsole ports: - 8080:8080 environment: JAVA_OPTS: "-Drocketmq.namesrv.addr=rmqnamesrv:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false" depends_on: - rmqnamesrv networks: rmq: aliases: - rmqconsole networks: rmq: name: rmq driver: bridge
而后在与docker-compose.yml
同级下面相应的创建三个文件夹conf
、logs
、store
。而后在conf
文件夹下面创建broker.conf
配置文件,全部文件的目录位置以下所示。浏览器
docker-compose.yml conf - broker.conf logs store
而后在编写broker.conf
配置文件里面的内容springboot
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You under the Apache License, Version 2.0 # (the "License"); you may not use this file except in compliance with # the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # 所属集群名字 brokerClusterName=DefaultCluster # broker 名字,注意此处不一样的配置文件填写的不同,若是在 broker-a.properties 使用: broker-a, # 在 broker-b.properties 使用: broker-b brokerName=broker-a # 0 表示 Master,> 0 表示 Slave brokerId=0 # nameServer地址,分号分割 # namesrvAddr=rocketmq-nameserver1:9876;rocketmq-nameserver2:9876 # 启动IP,若是 docker 报 com.alibaba.rocketmq.remoting.exception.RemotingConnectException: connect to <192.168.0.120:10909> failed # 解决方式1 加上一句 producer.setVipChannelEnabled(false);,解决方式2 brokerIP1 设置宿主机IP,不要使用docker 内部IP brokerIP1=192.168.1.16 # 在发送消息时,自动建立服务器不存在的topic,默认建立的队列数 defaultTopicQueueNums=4 # 是否容许 Broker 自动建立 Topic,建议线下开启,线上关闭 !!!这里仔细看是 false,false,false autoCreateTopicEnable=true # 是否容许 Broker 自动建立订阅组,建议线下开启,线上关闭 autoCreateSubscriptionGroup=true # Broker 对外服务的监听端口 listenPort=10911 # 删除文件时间点,默认凌晨4点 deleteWhen=04 # 文件保留时间,默认48小时 fileReservedTime=120 # commitLog 每一个文件的大小默认1G mapedFileSizeCommitLog=1073741824 # ConsumeQueue 每一个文件默认存 30W 条,根据业务状况调整 mapedFileSizeConsumeQueue=300000 # destroyMapedFileIntervalForcibly=120000 # redeleteHangedFileInterval=120000 # 检测物理文件磁盘空间 diskMaxUsedSpaceRatio=88 # 存储路径 # storePathRootDir=/home/ztztdata/rocketmq-all-4.1.0-incubating/store # commitLog 存储路径 # storePathCommitLog=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/commitlog # 消费队列存储 # storePathConsumeQueue=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/consumequeue # 消息索引存储路径 # storePathIndex=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/index # checkpoint 文件存储路径 # storeCheckpoint=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/checkpoint # abort 文件存储路径 # abortFile=/home/ztztdata/rocketmq-all-4.1.0-incubating/store/abort # 限制的消息大小 maxMessageSize=65536 # flushCommitLogLeastPages=4 # flushConsumeQueueLeastPages=2 # flushCommitLogThoroughInterval=10000 # flushConsumeQueueThoroughInterval=60000 # Broker 的角色 # - ASYNC_MASTER 异步复制Master # - SYNC_MASTER 同步双写Master # - SLAVE brokerRole=ASYNC_MASTER # 刷盘方式 # - ASYNC_FLUSH 异步刷盘 # - SYNC_FLUSH 同步刷盘 flushDiskType=ASYNC_FLUSH # 发消息线程池数量 # sendMessageThreadPoolNums=128 # 拉消息线程池数量 # pullMessageThreadPoolNums=128
配置文件中的内容咱们只须要改动一点便可,即brokerIP1
这个属性,咱们将其更改成咱们本机的ip,能够利用ipconfig
进行查看。服务器
修改完之后咱们直接在docker-compose.yml
文件所在的位置输入命令docker-compose up
便可启动。启动成功之后在浏览器中输入http://localhost:8080/
便可看到管理页面,就表示咱们搭建成功了。架构
使用Springboot快速上手
这里将会使用 springboot 快速上手使用 mq,将会使用rocketmq-spring-boot-starter
模块。
pom 配置以下
<!--在pom.xml中添加依赖--> <dependency> <groupid>org.apache.rocketmq</groupid> <artifactid>rocketmq-spring-boot-starter</artifactid> <version>2.0.3</version> </dependency>
gradle配置以下
implementation 'org.apache.rocketmq:rocketmq-spring-boot-starter:2.0.3'
消费服务发送方配置以下:
## application.properties rocketmq.name-server=ip:9876 rocketmq.producer.group=my-group
消费服务发送方程序以下:
@SpringBootApplication public class ProducerApplication implements CommandLineRunner { @Resource private RocketMQTemplate rocketMQTemplate; public static void main(String[] args){ SpringApplication.run(ProducerApplication.class, args); } public void run(String... args) throws Exception { rocketMQTemplate.convertAndSend("test-topic-1", "Hello, World!"); rocketMQTemplate.send("test-topic-1", MessageBuilder.withPayload("Hello, World! I'm from spring message").build()); } }
这里图省事的话能够将消费者和生产者写道同一个项目中。
消息消费方配置以下:
## application.properties rocketmq.name-server=ip:9876
消息消费方运行程序以下:
@SpringBootApplication public class ConsumerApplication{ public static void main(String[] args){ SpringApplication.run(ConsumerApplication.class, args); } @Slf4j @Service @RocketMQMessageListener(topic = "test-topic-1", consumerGroup = "my-consumer_test-topic-1") public static class MyConsumer1 implements RocketMQListener<string> { public void onMessage(String message) { log.info("received message: {}", message); } } }
到如今为止咱们就能够在本机上快乐的试验各类关于RocketMQ的相关东西了。
你们能够直接从上面拉取项目,启动RocketMQ只须要两步。
- 修改
broker.conf
中的brokerIP1
参数,修改成本机IP - 进入
docker-compose.yml
文件所在路径,执行docker-compose up
命令便可
若是你们不想本身搭建Springboot项目的话,能够从https://github.com/modouxiansheng/Doraemon上面直接拉取下来就好了。</string>