-
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
相关推荐
六件套是我1 天前
无法访问org.springframeword.beans.factory.annotation.Valueforestsea1 天前
Spring Cloud Alibaba 2025.1.0.0 正式发布:拥抱 Spring Boot 4.0 与 Java 21+ 的新时代IT枫斗者1 天前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!forestsea1 天前
Spring Boot 4.0 + JDK 25 + GraalVM:下一代云原生Java应用架构♡喜欢做梦1 天前
Spring Boot 日志实战:级别、持久化与 SLF4J 配置全指南青衫码上行1 天前
【项目部署】Spring Boot项目部署的四种方式N***p3651 天前
Spring Boot项目接收前端参数的11种方式想不明白的过度思考者1 天前
你真的会打印日志吗?基于 Spring Boot 的全方位日志指南v_cxsj8131 天前
学会写导师都说好的论文——Spring Boot高校实习管理平台18517【部署教程+可完整运行源码+数据库】路中行1 天前
Spring Cloud Gateway 整合 Knife4j 聚合文档:避坑指南与解决方案(Spring Boot 3.X)