RabbitMq多数据源配置
@Configuration
public class RabbitMqDataSourceConfig {
@Autowired
private RabbitProperties master;
@Autowired
private RabbitProperties slave;
// 定义bean名称,防止后面多个地方注入的时候混乱
public static final String MASTER_FACTORY="masterConnectionFactory";
public static final String SLAVE_FACTORY="slaveConnectionFactory";
public static final String MASTER_ADMIN="masterRabbitMqAdmin";
public static final String SLAVE_ADMIN="salveRabbitMqAdmin";
public static final String MASTER_TEMPLATE="masterRabbitTemplate";
public static final String SLAVE_TEMPLATE="slaveRabbitTemplate";
public static final String MASTER_LISTENER="masterListenerContainerFactory";
public static final String SLAVE_LISTENER="slaveListenerContainerFactory";
// 1.创建连接工厂。
@Bean(MASTER_FACTORY)
@Primary
public ConnectionFactory masterConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses(master.getAddresses());
connectionFactory.setUsername(master.getUsername());
connectionFactory.setPassword(master.getPassword());
connectionFactory.setVirtualHost(master.getVirtualHost());
return connectionFactory;
}
@Bean(SLAVE_FACTORY)
public ConnectionFactory salveConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setAddresses(slave.getAddresses());
connectionFactory.setUsername(slave.getUsername());
connectionFactory.setPassword(slave.getPassword());
connectionFactory.setVirtualHost(slave.getVirtualHost());
return connectionFactory;
}
// 2.创建rabbitMq可移植管理器(RabbitAdmin)。
@Bean(MASTER_ADMIN)
public RabbitAdmin masterRabbitMqAdmin(@Qualifier(MASTER_FACTORY) ConnectionFactory connectionFactory){
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
// 如果只有消费者可不设置,且如果每个数据源都要生成相同的交换机和队列也可以不设置
rabbitAdmin.setAutoStartup(false);
return rabbitAdmin;
}
@Bean(SLAVE_ADMIN)
public RabbitAdmin salveRabbitMqAdmin(@Qualifier(SLAVE_FACTORY) ConnectionFactory connectionFactory){
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
// 如果只有消费者可不设置,且如果每个数据源都要生成相同的交换机和队列也可以不设置
rabbitAdmin.setAutoStartup(false);
return rabbitAdmin;
}
// 3.创建rabbitMq模板。(不写生产者可省略)
@Bean(MASTER_TEMPLATE)
public RabbitMessagingTemplate masterRabbitTemplate(@Qualifier(MASTER_ADMIN) RabbitAdmin masterRabbitMqAdmin) {
RabbitTemplate rabbitTemplate = masterRabbitMqAdmin.getRabbitTemplate();
return new RabbitMessagingTemplate(rabbitTemplate);
}
@Bean(SLAVE_TEMPLATE)
public RabbitMessagingTemplate slaveRabbitTemplate(@Qualifier(SLAVE_ADMIN) RabbitAdmin salveRabbitMqAdmin) {
RabbitTemplate rabbitTemplate = salveRabbitMqAdmin.getRabbitTemplate();
return new RabbitMessagingTemplate(rabbitTemplate);
}
// 4.创建rabbitMq监听容器。(不写消费者可省略)
@Bean(MASTER_LISTENER)
public SimpleRabbitListenerContainerFactory masterListenerContainerFactory(
@Qualifier(MASTER_FACTORY) ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setConcurrentConsumers(1);
factory.setMaxConcurrentConsumers(5);
factory.setPrefetchCount(1);
factory.setAutoStartup(true);
return factory;
}
@Bean(SLAVE_LISTENER)
public SimpleRabbitListenerContainerFactory slaveListenerContainerFactory(
@Qualifier(SLAVE_FACTORY) ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setConcurrentConsumers(1);
factory.setMaxConcurrentConsumers(5);
factory.setPrefetchCount(1);
factory.setAutoStartup(true);
return factory;
}
}