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";
}
}
// 在指定mq服务器创建队列并绑定到交换机
@Bean
public String warmcarQueue(@Qualifier("warmcarConnectionFactory") ConnectionFactory connectionFactory) {
String queue = null;
try {
Channel channel = connectionFactory.createConnection().createChannel(false);
channel.exchangeDeclare(WARMCAR_EXCHANGE, BuiltinExchangeType.FANOUT, true);
channel.exchangeDeclare(WARMCAR_EXCHANGE_OTHER, BuiltinExchangeType.DIRECT, true);
queue = channel.queueDeclare(WARMCAR_QUEUE, true, false, false, null).getQueue();
channel.queueBind(queue, WARMCAR_EXCHANGE, "");
channel.queueBind(queue, WARMCAR_EXCHANGE_OTHER, "");
} catch (Exception e) {
log.error("configuration warmcar motion exchange and queue error:" + e.getMessage(), e);
}
return queue;
}
//下面2个对列创建方式 测试后发现不是 针对指定mq 服务器创建,只会在第一个服务器创建
/*
@Bean
public Queue firstQueue() {
return new Queue("hello1");
}
@Bean
public Object secondQueue() {
return new Queue("hello2");
}
*/
}
5、消费指定MQ的队列
@Slf4j
@Component
public class WarmcarMqConsumer {
@Autowired
private IWarmcarService warmcarService;
@RabbitListener(queues = WarmcarQueue.WARMCAR_QUEUE, containerFactory = "warmcarContainerFactory")
public void mqConsumer(Message message) {
try {
WarmCarProto.WarmCarMotion warmCarMotion = WarmCarProto.WarmCarMotion.parseFrom(message.getBody());
Warmcar warmcar = Warmcar.transFrom(warmCarMotion);
warmcarService.sendMotionData(warmcar);
} catch (InvalidProtocolBufferException e) {
log.error("解析proto数据异常:" + e.getMessage(), e);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
}

浙公网安备 33010602011771号