news 2026/8/3 1:34:36

SpringBoot 整合 RabbitMQ 五种消息模型实战

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringBoot 整合 RabbitMQ 五种消息模型实战

RabbitMQ 是消息队列中的主流方案。这篇讲 SpringBoot 整合 RabbitMQ 的五种消息模型。

一、引入依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>

二、配置

spring:rabbitmq:host:localhostport:5672username:guestpassword:guest

三、工作队列模式(Work Queue)

@ConfigurationpublicclassRabbitConfig{publicstaticfinalStringQUEUE="queue.work";@BeanpublicQueueworkQueue(){returnnewQueue(QUEUE);}}
@ServicepublicclassWorkProducer{@AutowiredprivateRabbitTemplaterabbitTemplate;publicvoidsend(Stringmessage){for(inti=0;i<10;i++){rabbitTemplate.convertAndSend(RabbitConfig.QUEUE,message+":"+i);}}}
@ComponentpublicclassWorkConsumer1{@RabbitListener(queues=RabbitConfig.QUEUE)publicvoidreceive(Stringmessage){System.out.println("消费者1收到: "+message);try{Thread.sleep(1000);}catch(Exceptione){}}}

四、发布订阅模式(Publish/Subscribe)

@ConfigurationpublicclassFanoutConfig{@BeanpublicFanoutExchangefanoutExchange(){returnnewFanoutExchange("exchange.fanout");}@BeanpublicQueuequeueA(){returnnewQueue("queue.fanout.a");}@BeanpublicQueuequeueB(){returnnewQueue("queue.fanout.b");}@BeanpublicBindingbindingA(){returnBindingBuilder.bind(queueA()).to(fanoutExchange());}@BeanpublicBindingbindingB(){returnBindingBuilder.bind(queueB()).to(fanoutExchange());}}

五、路由模式(Routing)

@ConfigurationpublicclassDirectConfig{publicstaticfinalStringQUEUE_INFO="queue.direct.info";publicstaticfinalStringQUEUE_ERROR="queue.direct.error";@BeanpublicDirectExchangedirectExchange(){returnnewDirectExchange("exchange.direct");}@BeanpublicQueueinfoQueue(){returnnewQueue(QUEUE_INFO);}@BeanpublicQueueerrorQueue(){returnnewQueue(QUEUE_ERROR);}@BeanpublicBindinginfoBinding(){returnBindingBuilder.bind(infoQueue()).to(directExchange()).with("info");}@BeanpublicBindingerrorBinding(){returnBindingBuilder.bind(errorQueue()).to(directExchange()).with("error");}}
@ServicepublicclassDirectProducer{@AutowiredprivateRabbitTemplaterabbitTemplate;publicvoidsendError(Stringmessage){// 只有 errorQueue 会收到rabbitTemplate.convertAndSend("exchange.direct","error",message);}}

六、主题模式(Topic)

@ConfigurationpublicclassTopicConfig{@BeanpublicTopicExchangetopicExchange(){returnnewTopicExchange("exchange.topic");}@BeanpublicQueueorderQueue(){returnnewQueue("queue.topic.order");}@BeanpublicQueueuserQueue(){returnnewQueue("queue.topic.user");}@BeanpublicBindingorderBinding(){returnBindingBuilder.bind(orderQueue()).to(topicExchange()).with("order.*");}@BeanpublicBindinguserBinding(){returnBindingBuilder.bind(userQueue()).to(topicExchange()).with("user.#");}}

七、五种模型对比

模型Exchange类型路由方式场景
简单/工作无(默认)直接任务分发
发布订阅Fanout广播通知所有消费者
路由Direct精确匹配按级别分发
主题Topic通配符复杂路由
RPC同步请求-响应

💡 觉得有用的话,点赞 + 关注【张老师技术栈】吧!

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/3 1:31:34

B端与C端产品核心差异:从用户角色到技术架构的深度解析

1. 从一次失败的“跨界”尝试说起几年前&#xff0c;我参与过一个至今想起来都让人哭笑不得的项目。当时公司想做一个面向企业客户的内部管理系统&#xff0c;团队里大部分成员都是做惯了面向消费者的App出身&#xff0c;大家摩拳擦掌&#xff0c;信心满满。我们按照做C端产品的…

作者头像 李华
网站建设 2026/8/3 1:31:30

基于USD构建Audio2Face到MetaHuman的高效面部动画工作流

1. 项目概述&#xff1a;从声音到表情的工业化桥梁最近在做一个面部动画项目&#xff0c;客户给了一段音频&#xff0c;要求生成一个高精度、能对口型、有表情变化的数字人面部动画。一开始我尝试了市面上几个独立的工具&#xff0c;比如直接用Audio2Face生成FBX&#xff0c;再…

作者头像 李华
网站建设 2026/8/3 1:21:23

回文侦探:三种境界破解最长回文子串

回文侦探&#xff1a;三种境界破解最长回文子串LeetCode 5. 最长回文子串 —— 从暴力到线性&#xff0c;看懂回文问题的三种解题境界。一、故事开场&#xff1a;你是回文侦探 想象你接到一个任务&#xff1a;在一串字符里&#xff0c;找出最长的"镜像文字"。 比如 &…

作者头像 李华
网站建设 2026/8/3 1:17:02

Claude Cowork重塑AI办公:从Copilot到协同工作的范式转移

1. 从“Copilot”到“Cowork”&#xff1a;AI办公的范式转移最近&#xff0c;如果你关注AI和办公软件&#xff0c;一定被“Claude杀入Office全家桶”这样的标题刷屏了。这背后&#xff0c;是Anthropic公司推出的Claude for Microsoft 365&#xff08;内部代号“Cowork”&#x…

作者头像 李华
网站建设 2026/8/3 1:04:05

QQ音乐解密终极指南:3分钟解锁加密音乐文件的完整教程

QQ音乐解密终极指南&#xff1a;3分钟解锁加密音乐文件的完整教程 【免费下载链接】qmcdump 一个简单的QQ音乐解码&#xff08;qmcflac/qmc0/qmc3 转 flac/mp3&#xff09;&#xff0c;仅为个人学习参考用。 项目地址: https://gitcode.com/gh_mirrors/qm/qmcdump 你是否…

作者头像 李华