Spring boot多Rabbit MQ配置创建队列到指定MQ
1、场景
spring boot整合rabbitmq,需要将队列 queue1、queue2分别放在不同的MQ服务上MQ1、MQ2。
2、环境准备
MQ1:
IP:192.168.1.110
队列:queue1
MQ2:
IP:172.16.1.220
队列:queue2
3、关键点
实现的关键点是 创建队列 到 指定MQ服务器上。
4、实现代码
@Configuration
public class RabbitConfig {
@Bean(name="firstConnectionFactory")
@Primary
public ConnectionFactory firstConnectionFactory(
@Value("${spring.rabbitmq.first.host}") String host,
@Value("${spring.rabbitmq.first.port}") int port,
@Value("${spring.rabbitmq.first.username}") String username,
@Value("${spring.rabbitmq.first.password}") String password
){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
@Bean(name="secondConnectionFactory")
public ConnectionFactory secondConnectionFactory(
@Value("${spring.rabbitmq.second.host}") String host,
@Value("${spring.rabbitmq.second.port}") int port,
@Value("${spring.rabbitmq.second.username}") String username,
@Value("${spring.rabbitmq.second.password}") String password
){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
}
@Bean(name="firstRabbitTemplate")
//@Primary //貌似没用,移除
public RabbitTemplate firstRabbitTemplate(
@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
){
RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory);
return firstRabbitTemplate;
}
@Bean(name="secondRabbitTemplate")
public RabbitTemplate secondRabbitTemplate(
@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
){
RabbitTemplate secondRabbitTemplate = new RabbitTemplate(connectionFactory);
return secondRabbitTemplate;
}
@Bean(name="firstFactory")
public SimpleRabbitListenerContainerFactory firstFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean(name="secondFactory")
public SimpleRabbitListenerContainerFactory secondFactory(
SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
@Bean
public String firstQueue(
@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory
) {
try {
connectionFactory.createConnection().createChannel(false).queueDeclare("hello1", false, false, false, null);
}catch (Exception e){
e.printStackTrace();
}finally {
return "firstQueue";
}
}
@Bean
public String secondQueue(
@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory
) {
try {
connectionFactory.createConnection().createChannel(false).queueDeclare("hello2", false, false, false, null);
}catch (Exception e){
e.printStackTrace();
}finally {
return "secondQueue";
}
}
//下面2个对列创建方式 测试后发现不是 针对指定mq 服务器创建,只会在第一个服务器创建
/*
@Bean
public Queue firstQueue() {
return new Queue("hello1");
}
@Bean
public Object secondQueue() {
return new Queue("hello2");
}
*/
}
作者:超级鲨鱼辣椒
转载请注明原文链接:https://www.cnblogs.com/jinzlblog/p/16650866.html