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
相关推荐
sichuanwuyi7 小时前
Wydevops工具的价值分析
linux·微服务·架构·kubernetes·jenkins
努力搬砖的咸鱼11 小时前
用 Minikube 或 Kind 在本地跑起 Kubernetes
微服务·云原生·容器·架构·kubernetes·kind
u01040583613 小时前
Java微服务架构:设计模式与实践
java·微服务·架构
一只专注api接口开发的技术猿14 小时前
微服务架构下集成淘宝商品 API 的实践与思考
java·大数据·开发语言·数据库·微服务·架构
米优17 小时前
使用Qt实现消息队列中间件动态库封装
c++·中间件·rabbitmq
小钟不想敲代码18 小时前
RabbitMQ高级
分布式·rabbitmq
南朝雨19 小时前
Spring Boot Admin日志监控坑点:远程配置的logging.file.name为何生效又失效?
java·spring boot·spring cloud·微服务·logback
小楼v20 小时前
消息队列的核心概念与应用(RabbitMQ快速入门)
java·后端·消息队列·rabbitmq·死信队列·交换机·安装步骤
小北方城市网20 小时前
SpringBoot 全局异常处理最佳实践:从混乱到规范
java·spring boot·后端·spring·rabbitmq·mybatis·java-rabbitmq