源码地址:https://github.com/hutuchong518/RabbitmqStudy
需求: spring boot 整合 rabbitmq rpc功能, 需要将 请求和响应 这两个队列 分别放在不同的MQ服务器上,以提高单个MQ服务器的吞吐量和性能。
MQ服务器1:
IP:192.168.179.128
对列:hello1
MQ服务器2:
IP:172.16.16.218
对列:hello2
这里实现的关键 是 创建队列 到 指定 MQ服务器中, 网上一些文章 都是 一把轮 没有区分,在实施上有问题的其实,这里通过实践并解决,以供参考。
下面是代码:
package com.zhanghui; import com.rabbitmq.client.AMQP; import org.springframework.amqp.core.Queue; 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.core.RabbitTemplate; 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; /** * @auther zhanghui * @date 2017/8/27 21:59 * @desc */ @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 ) { System.out.println("configuration firstQueue ........................"); //return new Queue("hello1"); 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 ) { System.out.println("configuration secondQueue ........................"); //return new Queue("hello2"); 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() { System.out.println("configuration firstQueue ........................"); return new Queue("hello1"); } @Bean public Object secondQueue() { System.out.println("configuration secondQueue ........................"); return new Queue("hello2"); } */ }