Springboot 监听同一个RabbitMQ的多个虚拟主机Virtual-Host

该博客介绍了如何配置Spring Boot应用以连接并监听RabbitMQ中的不同虚拟主机上的队列。配置包括两个独立的ConnectionFactory,一个用于每个虚拟主机,并分别创建了对应的RabbitTemplate和SimpleRabbitListenerContainerFactory。示例展示了如何使用@RabbitListener注解监听指定队列并进行手动确认。
摘要由CSDN通过智能技术生成

目录

 

场景:需要监听 同一个RabbitMQ下的 不同的虚拟主机下的 队列 来完成数据流转

配置信息application.properties

my.rabbitmq.host=你的ip
my.rabbitmq.port=你的port
my.rabbitmq.username=你的username
my.rabbitmq.password=你的password
my.rabbitmq.first.virtual-host=firstVHost
my.rabbitmq.second.virtual-host=secondVHost
  •  

配置类RabbitMqConfig

import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * @author: zdy
 * date: 2020/7/22 16:26
 * version: 1.0.0
 * description: RabbitMQ配置类
 */
@Configuration
public class RabbitMqConfig {

    /**
     * first 连接工厂
     */
    @Bean(name = "firstConnectionFactory")
    public ConnectionFactory firstConnectionFactory(@Value("${my.rabbitmq.host}") String host,
                                                    @Value("${my.rabbitmq.port}") int port,
                                                    @Value("${my.rabbitmq.username}") String username,
                                                    @Value("${my.rabbitmq.password}") String password,
                                                    @Value("${my.rabbitmq.first.virtual-host}") String vHost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vHost);
        return connectionFactory;
    }

    /**
     * second 连接工厂
     */
    @Bean(name = "secondConnectionFactory")
    //这个标签是必须的,因为加上后才能有默认值,不会在项目加载时产生冲突
    @Primary
    public ConnectionFactory secondConnectionFactory(@Value("${my.rabbitmq.host}") String host,
                                                     @Value("${my.rabbitmq.port}") int port,
                                                     @Value("${my.rabbitmq.username}") String username,
                                                     @Value("${my.rabbitmq.password}") String password,
                                                     @Value("${my.rabbitmq.second.virtual-host}") String vHost) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(vHost);
        return connectionFactory;
    }


    /**
     *    first生产者
     */
    @Bean(name = "firstRabbitTemplate")
    public RabbitTemplate firstRabbitTemplate(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

    /**
     *   second生产者
     */
    @Bean(name = "secondRabbitTemplate")
    public RabbitTemplate secondRabbitTemplate(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }

    /**
     * first 消费者
     */
    @Bean(name = "firstListenerFactory")
    public SimpleRabbitListenerContainerFactory firstListenerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        //设置手动ack
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        configurer.configure(listenerContainerFactory, connectionFactory);
        return listenerContainerFactory;
    }

    /**
     * second 消费者
     */
    @Bean(name = "secondListenerFactory")
    public SimpleRabbitListenerContainerFactory secondListenerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        configurer.configure(listenerContainerFactory, connectionFactory);
        return listenerContainerFactory;
    }

  •  

监听消息示例

 @RabbitListener(queues = "你的队列名", containerFactory = "firstListenerFactory")
 public void dealDeclareMessage(Channel channel, Message message) throws Exception {
     try {
         //消息类型
         String routingKey = message.getMessageProperties().getReceivedRoutingKey();
         log.info(routingKey);
         //消息内容
         JSONObject jsonObject = JSON.parseObject(new String(message.getBody(), StandardCharsets.UTF_8));
         //处理你的消息
         saveOrUpdate(jsonObject);
     } catch (Exception e) {
         log.info("你的队列名"+"异常信息:" + e.getMessage());
     } finally {
     	 //手动ack
     	 //TODO 也需要异常处理
         channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
     }
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值