赞
踩
在实际开发中,很多场景需要异步处理,这时就需要用到RabbitMQ,而且随着场景的增多程序可能需要连接多个RabbitMQ。SpringBoot本身提供了默认的配置可以快速配置连接RabbitMQ,但是只能连接一个RabbitMQ,当需要连接多个RabbitMQ时,默认的配置就不太适用了,需要单独编写每个连接。
在SpringBoot框架中,我们常用的两个类一般是:
RabbitTemplate
:作为生产、消费消息使用;RabbitAdmin
:作为申明、删除交换机和队列,绑定和解绑队列和交换机的绑定关系使用。所以我们连接多个RabbitMQ就需要重新建立连接、重新实现这两个类。
代码如下:
application.properties
配置文件需要配置两个连接:
server.port=8080 # rabbitmq v2.spring.rabbitmq.host=host v2.spring.rabbitmq.port=5672 v2.spring.rabbitmq.username=username v2.spring.rabbitmq.password=password v2.spring.rabbitmq.virtual-host=virtual-host #consume 手动 ack v2.spring.rabbitmq.listener.simple.acknowledge-mode=manual #1.当mandatory标志位设置为true时, # 如果exchange根据自身类型和消息routingKey无法找到一个合适的queue存储消息, # 那么broker会调用basic.return方法将消息返还给生产者; #2.当mandatory设置为false时,出现上述情况broker会直接将消息丢弃;通俗的讲, # mandatory标志告诉broker代理服务器至少将消息route到一个队列中, # 否则就将消息return给发送者; v2.spring.rabbitmq.template.mandatory=true #publisher confirms 发送确认 v2.spring.rabbitmq.publisher-confirms=true #returns callback : # 1.未送达exchange # 2.送达exchange却未送道queue的消息 回调returnCallback.(注意)出现2情况时,publisher-confirms 回调的是true v2.spring.rabbitmq.publisher-returns=true v2.spring.rabbitmq.listener.simple.prefetch=5 # rabbitmq v1.spring.rabbitmq.host=host v1.spring.rabbitmq.port=5672 v1.spring.rabbitmq.username=username v1.spring.rabbitmq.password=password v1.spring.rabbitmq.virtual-host=virtual-host #consume 手动 ack v1.spring.rabbitmq.listener.simple.acknowledge-mode=manual #1.当mandatory标志位设置为true时, # 如果exchange根据自身类型和消息routingKey无法找到一个合适的queue存储消息, # 那么broker会调用basic.return方法将消息返还给生产者; #2.当mandatory设置为false时,出现上述情况broker会直接将消息丢弃;通俗的讲, # mandatory标志告诉broker代理服务器至少将消息route到一个队列中, # 否则就将消息return给发送者; v1.spring.rabbitmq.template.mandatory=true #publisher confirms 发送确认 v1.spring.rabbitmq.publisher-confirms=true #returns callback : # 1.未送达exchange # 2.送达exchange却未送道queue的消息 回调returnCallback.(注意)出现2情况时,publisher-confirms 回调的是true v1.spring.rabbitmq.publisher-returns=true v1.spring.rabbitmq.listener.simple.prefetch=5
需要注意的是,在多源的情况下,需要在某个连接加上@Primary
注解,表示主连接,默认使用这个连接
package com.example.config.rabbitmq; import com.alibaba.fastjson.JSON; import org.springframework.amqp.core.AcknowledgeMode; import org.springframework.amqp.core.Message; 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.connection.CorrelationData; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; 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; /** * Created by shuai on 2019/4/23. */ @Configuration public class MultipleRabbitMQConfig { @Bean(name = "v2ConnectionFactory") public CachingConnectionFactory hospSyncConnectionFactory( @Value("${v2.spring.rabbitmq.host}") String host, @Value("${v2.spring.rabbitmq.port}") int port, @Value("${v2.spring.rabbitmq.username}") String username, @Value("${v2.spring.rabbitmq.password}") String password, @Value("${v2.spring.rabbitmq.virtual-host}") String virtualHost, @Value("${v2.spring.rabbitmq.publisher-confirms}")
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。