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
相关推荐
REST_302710 小时前
告别Excel孤岛:一款任务链路可视化工具带来的真实改变
大数据·微服务·云计算·编辑器·excel·动态规划
beibeix201511 小时前
CppMicroServices 核心概念:动态服务注册与模块化系统
微服务·mitk
微三云 - 廖会灵 (私域系统开发)11 小时前
电商系统从单体到微服务拆分实践:拆什么、怎么拆、拆完后怎么办
微服务·云原生·架构
guslegend15 小时前
领域驱动设计,微服务设计为什么要选择DDD?
微服务·云原生·架构
过期动态17 小时前
【LeetCode 热题 100】找到字符串中所有字母异位词
java·数据结构·算法·leetcode·职场和发展·rabbitmq
VortMall18 小时前
全维度打磨细节体验,赋能商城稳定有序运营|VortMall 微服务商城 v1.3.11 版本发布
java·微服务·云原生·架构·商城系统·开源商城·vortmall
豆瓣鸡1 天前
Guava RateLimiter 限流实战:从令牌桶原理到 Nacos 动态配置
spring boot·微服务·nacos·guava
Wang's Blog2 天前
Go-Zero项目开发10: 微服务治理中心实现原理分析
开发语言·微服务·golang
xexpertS2 天前
分布式系统过载治理:如何通过较小服务控制请求节奏
微服务·自动化
爱学习的小可爱卢3 天前
SpringCloud——微服务实战:OpenFeign与Nacos服务调用详解
spring·spring cloud·微服务