rabbitmq在微服务中配置监听开关

  1. 监听文件类
java 复制代码
import cn.hutool.json.JSONObject;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import jnpf.util.JsonUtil;
import jzy.model.mom.EventMessageMom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

@Component
@ConditionalOnProperty(name = "spring.rabbitmq.enable", havingValue = "true", matchIfMissing = true)
public class MultiExchangeMessageListener{
    private static final Logger logger = LoggerFactory.getLogger(MultiExchangeMessageListener.class);

    @Autowired
    private IdempotentMessageProcessor idempotentProcessor;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 监听来自多个交换机的消息
     */

    // 声明 Direct 交换机、队列,并绑定路由键
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "order.queue", durable = "true"),    // 队列
            exchange = @Exchange(name = "20250917test1", type = ExchangeTypes.FANOUT) // 交换机
    ))
    public void handleMessage(Message message) throws IOException {
        String messageId = message.getMessageProperties().getMessageId();
        String exchange = message.getMessageProperties().getReceivedExchange();
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        }
  1. 配置文件:在yml里配置具体开关的属性,配置rabbitMq启用开关,false为关闭,true为启用.
yaml 复制代码
  spring:
	  rabbitmq:
	    addresses: 127.0.0.1:5672
	    username: guest
	    password: guest
	    virtual-host: /
	    connection-timeout: 15000
	    #配置rabbitMq启用开关
	    enable: true
	    listener:
	      simple:
	        acknowledge-mode: manual
	        concurrency: 15
	        max-concurrency: 20
相关推荐
城管不管20 小时前
重生——第九次面试2026.8.13某车一面
分布式·ai·面试·职场和发展·rabbitmq
apgk120 小时前
基于Canal实现mysql数据同步到消息队列(RabbitMQ/Kafka)详细操作教程
docker·kafka·rabbitmq
Rain的Java大神之路21 小时前
MQ重复消费问题怎么解决
java·经验分享·后端·面试·架构·rabbitmq·rocketmq
zhangjw341 天前
第41篇:微服务入门:架构理念+核心组件,理解微服务本质
微服务·云原生·架构
星期一研究室1 天前
从一条分割线开始,在文档中打造你的内容节奏
微服务·产品·设计
hard_hot1 天前
RabbitMQ异步下单架构设计
java·rabbitmq·java-rabbitmq
智码看视界1 天前
Day49-AI微服务化-将大模型能力封装为标准微服务
java·微服务·ai·架构·大模型·sse流式输出·ai中台
吴声子夜歌2 天前
正则指引——Ruby
开发语言·正则表达式·ruby
youngerwang2 天前
【软件系统架构案例分析每日深耕 Day 17】CBAM成本效益分析:微服务改造决策
微服务·系统架构·cbam·案例每日深耕·微服务改造
星期一研究室2 天前
告别文档「图片灾难排版」!3大黄金法则,让你的文档从杂乱无章到杂志风🧩
微服务·产品·设计