【Docker】RocketMQ5.1.0的配置部署与基于SpringBoot3.0.5的代码基础配置
RocketMQ5.1.0的Docker配置部署与基于SpringBoot3.0.5的代码基础配置
·
一、项目信息
1 版本
- JAVA 17
- SpringBoot 3.0.5
- CentOS 7.5
- Rocket 5.1.0
2 依赖
<!-- SpringBoot 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- RocketMQ 依赖 -->
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.2.3</version>
</dependency>
3 配置
rocketmq.name-server=127.0.0.1:9876
rocketmq.producer.group=BREAD_ORDER_GROUP
4 项目结构
二、RocketMQ的Docker配置部署
1 RocketMQ部署
1.1 查看镜像
docker search apache/rocketmq
1.2 拉取镜像
docker pull apache/rocketmq:5.1.0
1.3 查看已拉取镜像
docker images
1.4 创建挂载文件夹
1.4.1 创建Broker挂载文件夹
mkdir /home/rocketmq/broker/bin -p
mkdir /home/rocketmq/broker/conf -p
mkdir /home/rocketmq/broker/logs -p
mkdir /home/rocketmq/broker/store -p
1.4.2 创建NameServer挂载文件夹
mkdir /home/rocketmq/nameserver/bin -p
mkdir /home/rocketmq/nameserver/conf -p
1.5 启动容器以复制配置文件
1.5.1 启动NamerServer容器
docker run -d \
--privileged=true \
--name testnameserver \
apache/rocketmq:5.1.0 sh mqnamesrv
1.5.2 复制NameServer启动脚本
docker cp testnameserver:/home/rocketmq/rocketmq-5.1.0/bin/runserver.sh /home/rocketmq/nameserver/bin/runserver.sh
1.5.3 修改脚本文件
vim /home/rocketmq/nameserver/bin/runserver.sh
# 注释该行
calculate_heap_sizes
1.5.4 停止并删除NameServer容器
docker stop testnameserver
docker rm testnameserver
1.5.5 启动Broker容器
docker run -d \
--name testbroker \
--privileged=true \
apache/rocketmq:5.1.0 \
sh mqbroker
1.5.6 复制Broker启动脚本
docker cp testbroker:/home/rocketmq/rocketmq-5.1.0/bin/runbroker.sh /home/rocketmq/broker/bin/runbroker.sh
1.5.7 修改脚本文件
vim /home/rocketmq/broker/bin/runbroker.sh
# 注释该行
calculate_heap_sizes
1.5.8 停止并删除Broker容器
docker stop testbroker
docker rm testbroker
1.6 创建broker.conf配置文件
vim /home/rocketmq/broker/conf/broker.conf
# 你的NameServer服务器公网IP地址与端口号
namesrvAddr=127.0.0.1:9876
# 集群名称
brokerClusterName = DefaultCluster
# 节点名称
brokerName = broker-a
# broker id节点ID, 0 表示 master, 其他的正整数表示 slave,不能小于0
brokerId = 0
# Broker服务地址(你的服务器公网IP地址)
brokerIP1 = 127.0.0.1
# Broker角色
brokerRole = ASYNC_MASTER
# 刷盘方式
flushDiskType = ASYNC_FLUSH
# 删除时间(4点)
deleteWhen = 04
# 文件保留时间(72小时)
fileReservedTime = 72
# 是否允许Broker自动创建Topic,建议线下开启,线上关闭
autoCreateTopicEnable = true
# 是否允许Broker自动创建订阅组,建议线下开启,线上关闭
autoCreateSubscriptionGroup = true
# 检测可用的磁盘空间大小,超过后会写入报错(95%)
diskMaxUsedSpaceRatio=95
1.7 文件夹赋权
chmod -R 777 /home/rocketmq/
1.8 启动容器
1.8.1 启动NameServer容器
docker run -d \
--privileged=true \
--restart=always \
--name nameserver \
-p 9876:9876 \
-v /home/rocketmq/nameserver/logs:/home/rocketmq/logs \
-v /home/rocketmq/nameserver/bin/runserver.sh:/home/rocketmq/rocketmq-5.1.0/bin/runserver.sh \
-e "MAX_HEAP_SIZE=256M" \
-e "HEAP_NEWSIZE=128M" \
-e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" \
apache/rocketmq:5.1.0 sh mqnamesrv
1.8.2 启动Broker容器
docker run -d \
--restart=always \
--name broker \
-p 10911:10911 -p 10909:10909 \
--privileged=true \
-v /home/rocketmq/broker/logs:/root/logs \
-v /home/rocketmq/broker/store:/root/store \
-v /home/rocketmq/broker/conf/broker.conf:/home/rocketmq/broker.conf \
-v /home/rocketmq/broker/bin/runbroker.sh:/home/rocketmq/rocketmq-5.1.0/bin/runbroker.sh \
-e "MAX_HEAP_SIZE=512M" \
-e "HEAP_NEWSIZE=256M" \
-e "JAVA_OPT_EXT=-server -Xms128m -Xmx128m -Xmn128m" \
apache/rocketmq:5.1.0 \
sh mqbroker -c /home/rocketmq/broker.conf
1.9 查看当前容器
docker ps
2 部署RocketMQ可视化控制台
2.1 查看镜像
docker search apacherocketmq/rocketmq-dashboard
2.2 拉取镜像(默认最新版本)
docker pull apacherocketmq/rocketmq-dashboard
2.3 启动容器
# 你的NameServer服务器公网IP地址与端口号
docker run -d \
--restart=always \
--name dashboard \
-e "JAVA_OPTS=-Xmx256M -Xms256M -Xmn128M -Drocketmq.namesrv.addr=127.0.0.1:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false" \
-p 8082:8080 \
apacherocketmq/rocketmq-dashboard
2.4 查看当前容器
docker ps
3 开放对应端口
4 访问可视化控制台
# 你的服务器公网IP地址
http://127.0.0.1:8082/
三、代码测试
1 根据项目信息搭建完成模块
2 补充配置信息(由于SpringBoot3.0以上版本所引发)
2.1 在resource下创建META-INF文件夹
2.2 在META-INF文件夹下创建spring文件夹
2.3 创建文件
# 文件名信息
org.springframework.boot.autoconfigure.AutoConfiguration.imports
# 文件内容
org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration
3 创建消息发送测试类
package com.catalpa.study.test;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@Autowired
private RocketMQTemplate rocketMQTemplate;
@RequestMapping("/test")
public String order() {
String test = "123测试";
rocketMQTemplate.convertAndSend("ORDER_TOPIC", test);
return test;
}
}
4 创建消费测试类
package com.catalpa.study.listener;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;
@Service
@RocketMQMessageListener(topic = "ORDER_TOPIC", consumerGroup = "BREAD_ORDER_GROUP", consumeMode = ConsumeMode.ORDERLY)
public class NormalRocketMqListener implements RocketMQListener<String> {
@Override
public void onMessage(String s) {
System.out.println("普通订阅-接收到的信息:{}"+ s);
}
}
5 测试
5.1 启动项目
5.2 访问消息发送测试接口
http://127.0.0.1:8081/test
5.3 测试成功
更多推荐
已为社区贡献1条内容
所有评论(0)