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

Rabbitmq扇形队列取消绑定交换机之后任然接收消息问题

Rabbitmq扇形队列取消绑定交换机之后任然接收消息问题

伪代码

  1. 申明队列及绑定关系

    package com.cdn.mqprovider;import org.springframework.amqp.core.Binding;
    import org.springframework.amqp.core.BindingBuilder;
    import org.springframework.amqp.core.FanoutExchange;
    import org.springframework.amqp.core.Queue;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;@Configuration
    public class FanoutRabbitProviderConfig {/*** 创建三个队列 :fanout.A   fanout.B  fanout.C* 将三个队列都绑定在交换机 fanoutExchange 上* 因为是扇型交换机, 路由键无需配置,配置也不起作用*/@Beanpublic Queue queueA() {return new Queue("fanout.A1");}@Beanpublic Queue queueB() {return new Queue("fanout.B1");}@Beanpublic Queue queueC() {return new Queue("fanout.C1");}@BeanFanoutExchange fanoutExchange() {return new FanoutExchange("fanoutExchange1");}@BeanBinding bindingExchangeA() {return BindingBuilder.bind(queueA()).to(fanoutExchange());}@BeanBinding bindingExchangeB() {return BindingBuilder.bind(queueB()).to(fanoutExchange());}@BeanBinding bindingExchangeC() {return BindingBuilder.bind(queueC()).to(fanoutExchange());}}
  2. 监听

package com.cdn.mqprovider.api;import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** 蔡定努* 2025/10/29 17:56*/
@RestController
public class A {@AutowiredRabbitTemplate rabbitTemplate;/*** @author 蔡定努*/@GetMapping("send")public void send() {rabbitTemplate.convertAndSend("fanoutExchange1", "", "消息1");}// -------------------消费----------------------@RabbitListener(queues = "fanout.A1",ackMode = "AUTO")public void processm(String testMessage) {System.out.println("fanout.A1消费者收到消息  : " + testMessage);}@RabbitListener(queues = "fanout.B1",ackMode = "AUTO")public void processmb(String testMessage) {System.out.println("fanout.B1消费者收到消息  : " + testMessage);}@RabbitListener(queues = "fanout.C1",ackMode = "AUTO")public void processmc(String testMessage) {System.out.println("fanout.C1消费者收到消息  : " + testMessage);}}

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

测试

调用send接口

fanout.C1消费者收到消息  : 消息1
fanout.A1消费者收到消息  : 消息1
fanout.B1消费者收到消息  : 消息1

调整

此时我有一个需求,fanout.A1队列我业务中用不到了,那就把写的地方拿掉就好了,于是有了以下代码

  1. 申明队列及绑定关系

package com.cdn.mqprovider;import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class FanoutRabbitProviderConfig {/*** 创建三个队列 :fanout.A   fanout.B  fanout.C* 将三个队列都绑定在交换机 fanoutExchange 上* 因为是扇型交换机, 路由键无需配置,配置也不起作用*/// @Bean// public Queue queueA() {//     return new Queue("fanout.A1");// }@Beanpublic Queue queueB() {return new Queue("fanout.B1");}@Beanpublic Queue queueC() {return new Queue("fanout.C1");}@BeanFanoutExchange fanoutExchange() {return new FanoutExchange("fanoutExchange1");}// @Bean// Binding bindingExchangeA() {//     return BindingBuilder.bind(queueA()).to(fanoutExchange());// }@BeanBinding bindingExchangeB() {return BindingBuilder.bind(queueB()).to(fanoutExchange());}@BeanBinding bindingExchangeC() {return BindingBuilder.bind(queueC()).to(fanoutExchange());}}
  1. 监听消费

    package com.cdn.mqprovider.api;import org.springframework.amqp.rabbit.annotation.Exchange;
    import org.springframework.amqp.rabbit.annotation.Queue;
    import org.springframework.amqp.rabbit.annotation.QueueBinding;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;/*** 蔡定努* 2025/10/29 17:56*/
    @RestController
    public class A {@AutowiredRabbitTemplate rabbitTemplate;/*** @author 蔡定努*/@GetMapping("send")public void send() {rabbitTemplate.convertAndSend("fanoutExchange1", "", "消息1");}// -------------------消费----------------------@RabbitListener(queues = "fanout.A1",ackMode = "AUTO")public void processm(String testMessage) {System.out.println("fanout.A1消费者收到消息  : " + testMessage);}@RabbitListener(queues = "fanout.B1",ackMode = "AUTO")public void processmb(String testMessage) {System.out.println("fanout.B1消费者收到消息  : " + testMessage);}@RabbitListener(queues = "fanout.C1",ackMode = "AUTO")public void processmc(String testMessage) {System.out.println("fanout.C1消费者收到消息  : " + testMessage);}}

    测试

    调用send接口

    fanout.C1消费者收到消息  : 消息1
    fanout.A1消费者收到消息  : 消息1
    fanout.B1消费者收到消息  : 消息1
    

    为什么我取消绑定了,队列创建也注释了,还能消费呢?

    于是我把消费也注释了

    package com.cdn.mqprovider.api;import org.springframework.amqp.rabbit.annotation.Exchange;
    import org.springframework.amqp.rabbit.annotation.Queue;
    import org.springframework.amqp.rabbit.annotation.QueueBinding;
    import org.springframework.amqp.rabbit.annotation.RabbitListener;
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;/*** 蔡定努* 2025/10/29 17:56*/
    @RestController
    public class A {@AutowiredRabbitTemplate rabbitTemplate;/*** @author 蔡定努*/@GetMapping("send")public void send() {rabbitTemplate.convertAndSend("fanoutExchange1", "", "消息1");}// -------------------消费----------------------// @RabbitListener(queues = "fanout.A1",ackMode = "AUTO")// public void processm(String testMessage) {//     System.out.println("fanout.A1消费者收到消息  : " + testMessage);// }@RabbitListener(queues = "fanout.B1",ackMode = "AUTO")public void processmb(String testMessage) {System.out.println("fanout.B1消费者收到消息  : " + testMessage);}@RabbitListener(queues = "fanout.C1",ackMode = "AUTO")public void processmc(String testMessage) {System.out.println("fanout.C1消费者收到消息  : " + testMessage);}}
    

    测试

    调用send接口

    fanout.C1消费者收到消息  : 消息1
    fanout.B1消费者收到消息  : 消息1
    

看似没有问题了,但是,问题就来了,随着业务的进行,mq报警了,消息堆积

image-20251029下午61909794

原因

因为fanout.A1在之前就创建了,并且绑定关系也建立了,就算你代码中注释掉了队列申明和绑定关系,mq不会主动去删除对应的队列和绑定关系,但是消费者被注释了,所以就出现消息只进不出的情况,导致对接

解决办法

既然a队列都不要了,直接删除即可

http://www.dtcms.com/a/544840.html

相关文章:

  • 网站正在建设中代码哪个软件发视频可以赚钱
  • web网页开发,天气可视化开发,基于Python,FlaskWeb,无数据,数据写死
  • S11e Protocol:重塑品牌资产的 Web3 RWA 基础设施革命
  • 34_AI智能体工具插件之动态注册钉钉待办工具构建企业级任务管理AI助手
  • Java 大视界 -- Java 大数据在智慧文旅旅游线路规划与游客流量均衡调控中的应用实践
  • 33_AI智能体工具插件之钉钉API交互工具封装构建企业级AI应用
  • 数字孪生热力图可视化为何被广泛应用?
  • 国内免费可商用图片素材网站孝感做招聘信息的网站
  • Django在服务端的部署(无废话)
  • 当 AI Agent 遇上 MCP:微软 Agent Framework 的“瑞士军刀“式扩展之道
  • 个人网站建设培训药品网络营销公司
  • 20MW-10MWh储能项目施工方案
  • RHCA - DO374 | Day05:管理主机清单
  • 【完整源码+数据集+部署教程】【天线&水】无人机视角水面漂浮物体检测系统源码&数据集全套:改进yolo11-REPVGGOREPA
  • 思源实时同步设置步骤和节点部署
  • 电脑间如何快速传输大文件?4种高效数据迁移方法任选
  • 云栖实录 | AI原生搜索引擎:Elasticsearch 换“芯”——AI原生搜索内核增强技术
  • 手机可以做网站学校网站建设与管理办法
  • 网站建设流程的过程建设行政主管部门政务网站
  • 计算机网络-物理层
  • css面试题1
  • Git的原理与使用 -- 分支管理
  • 7.1.3 大数据方法论与实践指南-查询平台
  • 什么是所有权
  • 江苏建设网站公司合肥建设工程质量监督局网站
  • js基础:07、作用域(全局作用域、函数作用域)、声明提前(变量的声明提前、函数的声明提前)、执行上下文(this)、新创建对象方法、构造函数
  • 七牛云到阿里云对象存储回源配置
  • Ant Design Landing模版使用教程-react-npm
  • ChatGPT-4o在自然科学中的应用:统计建模、机器学习与时空数据分析实战
  • 仓颉语言包与模块系统深度解析