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
相关推荐
ZHE|张恒10 小时前
Docker 安装 RabbitMQ
docker·rabbitmq
阿里云云原生14 小时前
阿里云微服务引擎 MSE 及 API 网关 2025 年 9 月产品动态
微服务
二宝15216 小时前
黑马商城day3-微服务01
微服务·云原生·架构
roykingw16 小时前
【终极面试集锦】如何设计微服务熔断体系?
java·微服务·面试
我命由我1234516 小时前
Spring Cloud - Spring Cloud 微服务概述 (微服务的产生与特点、微服务的优缺点、微服务设计原则、微服务架构的核心组件)
java·运维·spring·spring cloud·微服务·架构·java-ee
我命由我1234517 小时前
Spring Cloud - Spring Cloud 注册中心与服务提供者(Spring Cloud Eureka 概述、微服务快速入门、微服务应用实例)
java·spring boot·spring·spring cloud·微服务·eureka·java-ee
鼠鼠我捏,要死了捏17 小时前
微服务熔断降级方案对比:Hystrix、Resilience4j与Sentinel实践
微服务·熔断·resilience4j
没有bug.的程序员19 小时前
金融支付分布式架构实战:从理论到生产级实现
java·分布式·微服务·金融·架构·分布式调度系统
斯班奇的好朋友阿法法1 天前
rabbitmq服务端消费端实例(direct和fanout模式)
分布式·rabbitmq·ruby