当前位置: 首页 > news >正文

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同步请求-响应

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

http://www.cnnetsun.cn/news/3806127.html

相关文章:

  • B端与C端产品核心差异:从用户角色到技术架构的深度解析
  • 基于USD构建Audio2Face到MetaHuman的高效面部动画工作流
  • AI能看懂《蒙娜丽莎》的微笑吗?:3大神经美学指标+7类生成式缺陷识别法,实测准确率92.6%
  • 回文侦探:三种境界破解最长回文子串
  • Claude Cowork重塑AI办公:从Copilot到协同工作的范式转移
  • QQ音乐解密终极指南:3分钟解锁加密音乐文件的完整教程
  • StarRailAssistant:崩坏星穹铁道自动化助手的完整使用指南
  • 终极Windows热键冲突检测指南:如何快速定位并解决快捷键占用问题
  • OpCore-Simplify:如何用智能工具在30分钟内完成黑苹果配置?
  • 【Bug已解决】FSDP2 fails due to KeyError: ‘lm_head.weight‘ 解决方案
  • 【Bug已解决】Degraded performance when resuming from checkpoint 解决方案
  • 【限时解密】头部券商内部使用的AI流失预警模型架构图首次公开:含3层动态阈值引擎与HR协同干预SOP
  • PyTorch入门指南:从环境搭建到自动求导的NLP学习实战
  • 我的智能Agent上线崩了,才明白权限日志比调API更重要
  • 理工科论文去 AI 味会把公式术语改乱吗?亲测一次降到 9% 术语没动
  • 鸣潮自动化解决方案深度解析:基于图像识别的智能游戏辅助架构剖析
  • 周末搓火锅找靠谱店,亲测4家新鲜现切的火锅店
  • OBS Studio色彩校正技术深度解析:从3D LUT到专业级色彩分级
  • Cyclone常见问题解答:新手开发者必知的15个要点
  • 如何永久保存微信聊天记录:3步实现数据自主掌控的终极方案
  • 如何快速下载国家中小学智慧教育平台电子课本PDF文件:完整指南
  • 终极指南:OpenCore Legacy Patcher完整教程,让老款Mac焕发新生
  • Python PDF处理终极指南:pypdf库从入门到精通
  • 三步解锁Windows预览HEIC照片的完整方案
  • 3D外壳设计全流程:从概念到量产,从CAD建模到3D打印实战
  • 从Karpathy内部Claude.md看AI交互工程化:构建可版本控制的提示词系统
  • 洛雪音乐音源终极指南:5分钟免费搭建高品质音乐库 [特殊字符]
  • HDMI外接显示器颜色发灰、过饱和?从显卡设置到硬件校色的完整解决方案
  • Rapid YAML vs 其他YAML库:为什么选择这个高性能解析器?
  • editable-table vs 其他表格插件:为什么选择这个仅120行代码的解决方案