RabbitMq如何配置多个多个Virtual host生产者和消费者

RabbitMq如何配置多个多个Virtual host生产者和消费者,第1张

** springboot服务下进行rabbitmq生产和消费自定义配置@Configuration

1、生产者进行配置 1-1配置主连接工厂
@Bean("connectionFactory")
    @Primary//这个地方必须声明一个唯一主要的
    public CachingConnectionFactory connectionFactory(){
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(host,port);
        cachingConnectionFactory.setUsername(username);
        cachingConnectionFactory.setPassword(password);
        cachingConnectionFactory.setVirtualHost(virtualHost);
        return cachingConnectionFactory;
    }
1-2配置主模板
    @Bean("rabbitTemplate")
    @Primary
    public RabbitTemplate rabbitTemplate(@Qualifier("connectionFactory") CachingConnectionFactory connectionFactory){
        return new RabbitTemplate(connectionFactory);
    }
1-3配置次连接工厂
 @Bean("connectionSecondFactory")
    public CachingConnectionFactory connectionSecondFactory(){
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(host,port);
        cachingConnectionFactory.setUsername(username);
        cachingConnectionFactory.setPassword(password);
        cachingConnectionFactory.setVirtualHost(virtualHostOld);
        return cachingConnectionFactory;
    }
1-4配置次模板
  @Bean("rabbitTemplateSecond")
    public RabbitTemplate rabbitTemplateSecond(@Qualifier("connectionSecondFactory") CachingConnectionFactory connectionSecondFactory){
        return new RabbitTemplate(connectionSecondFactory);
    }
2、消费者进行配置 2-1、自定义消费者监听工厂
    @Bean("newContainerFactory")
    public SimpleRabbitListenerContainerFactory newContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
                                                                    @Qualifier("connectionNewFactory") ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory listenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        listenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.AUTO);
        configurer.configure(listenerContainerFactory, connectionFactory);
        return listenerContainerFactory;

    }
2-2、指定自定义消费者工厂进行监听消费 2-2-1、执行自定义消费工厂进行消息对列消费
 @RabbitListener(queues =  "${queue.****}",containerFactory ="newContainerFactory" )
2-2-2、默认工厂进行消息对列消费
    @RabbitListener(queues = ComboRabbitMQQueneConstant.QUEUE_COMBO_ORDER_PAY)
2-2-3、默认工厂进行消息对列消费,如果当前对列不存在主动创建
 @RabbitListener(queuesToDeclare = @Queue(value = AspectRabbitMQQueneConstant.QUEUE_ASPECT_AOP_LOG))

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/721526.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-25
下一篇 2022-04-25

发表评论

登录后才能评论

评论列表(0条)

保存