-
pom.xml导入RocketMQ依赖
<dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> <version>2.2.2</version> </dependency> -
application.yml中添加配置
rocketmq: name-server: 127.0.0.1:9876 producer: group: x access-key: myaccesskey secret-key: mysecretKey send-message-timeout: 10000 tls-enable: true consumer: group: x access-key: myaccesskey secret-key: mysecretKey tls-enable: true -
创建MQ工具类
public class MqUtil { private final RocketMQTemplate rocketMQTemplate; public MqUtil(RocketMQTemplate rocketMQTemplate) { this.rocketMQTemplate = rocketMQTemplate; } /** * 单条通知发送 * * @param topic 主题 * @param message 消息 */ public void convertAndSend(String topic, Object message) { rocketMQTemplate.convertAndSend(topic, message); } /** * 批量通知发送 * * @param topic 主题 * @param messages 消息集合 */ public <T extends Message<?>> SendResult syncSend(String topic, Collection<T> messages) { return rocketMQTemplate.syncSend(topic, messages); } /** * 批量通知发送 * * @param topic 主题 * @param messages 消息集合 * @param sendCallback 回调函数 */ public <T extends Message<?>> void asyncSend(String topic, Collection<T> messages, SendCallback sendCallback) { rocketMQTemplate.asyncSend(topic, messages, sendCallback); } } -
注入工具类Bean
@Bean public MqUtil mqUtil(RocketMQTemplate rocketMQTemplate) { return new MqUtil(rocketMQTemplate); } -
测试发消息
@Resource private MqUtil mqUtil; @Test public void test() { mqUtil.convertAndSend(TopicConstant.TOPIC_B, "123456"); } -
订阅接收消息
@Slf4j @Service @RequiredArgsConstructor(onConstructor = @__({@Autowired})) @RocketMQMessageListener(consumerGroup = GroupConstant.GROUP_A, topic = TopicConstant.TOPIC_B) public class TopicAConsumer implements RocketMQListener<Message> { @Override public void onMessage(Message message) { log.info(JsonUtil.toJsonStr(message)); } }
Spring Boot整合RocketMQ
阿拉狗2023-12-28 8:02
相关推荐
万亿少女的梦16810 小时前
基于SpringBoot的考研学习交流系统设计与开发二宝哥12 小时前
创建Gradle多模块SpringBoot项目杨运交13 小时前
[044][Web模块]基于 Google Authenticator 的 TOTP 双因素认证框架设计与实现二宝哥14 小时前
springboot项目使用Gradle工具实现依赖版本控制卓怡学长15 小时前
w273基于springboot的智能笔记的开发与应用Kyrie_Li1 天前
Spring Boot Kafka 生产级配置全解析:从入门到精通小强库计算机毕业设计1 天前
源码分享Spring Boot + Vue3 的校园社团管理系统格子软件1 天前
2026年分布式GEO代理流量调度:源码级状态机防重挂实战万亿少女的梦1681 天前
基于Spring Boot的天空影院电影网站系统设计与实现