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
相关推荐
这是谁的博客?5 小时前
微服务架构设计模式深度解析:从拆分策略到容灾机制
微服务·设计模式·云原生·架构·架构设计·后端开发·分布式系统
zz07232013 小时前
轻量级权限认证框架:Sa-Token
微服务·sa-token·权限认证·登录安全框架
phltxy14 小时前
RabbitMQ高级特性-消息确认与持久性博客
分布式·rabbitmq·ruby
HEADKON14 小时前
匹妥布替尼捷帕力Pirtobrutinib对比伊布替尼治疗套细胞淋巴瘤的缓解率更优
ruby
步步为营DotNet15 小时前
探索.NET 11:.NET Aspire 在云原生微服务治理中的创新实践
微服务·云原生·.net
梵得儿SHI15 小时前
SpringCloud 进阶拓展:Spring Security OAuth2+JWT 微服务统一认证授权全实战|生产级方案 + 源码解析 + 踩坑实录
spring·spring cloud·微服务·spring security·jwt·oauth2·统一认证授权
未若君雅裁17 小时前
RabbitMQ 消息可靠性:生产者确认、持久化、消费者ACK与幂等消费
分布式·微服务·rabbitmq
huipeng9261 天前
企业级微服务开发实战(一):项目启动与工程化设计
java·开发语言·spring boot·spring cloud·微服务·云原生·架构
小江的记录本2 天前
【Kafka核心】Kafka 3.0+ KRaft模式(替代ZooKeeper)核心原理与优势
java·数据库·分布式·后端·zookeeper·kafka·rabbitmq
一切顺势而行2 天前
easysearch 安装
spring·spring cloud·微服务