源码地址: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");
}
*/
}

  

spring boot rabbitmq 多MQ配置 自动 创建 队列 RPC的更多相关文章

  1. Spring boot - 梳理 - 根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置

    根本上说,Spring Boot项目只不过是一个普通的Spring项目,只是使用了Spring Boot的起步依赖和自动配置

  2. Spring boot+RabbitMQ环境

    Spring boot+RabbitMQ环境 消息队列在目前分布式系统下具备非常重要的地位,如下的场景是比较适合消息队列的: 跨系统的调用,异步性质的调用最佳. 高并发问题,利用队列串行特点. 订阅模 ...

  3. Spring boot集成Rabbit MQ使用初体验

    Spring boot集成Rabbit MQ使用初体验 1.rabbit mq基本特性 首先介绍一下rabbitMQ的几个特性 Asynchronous Messaging Supports mult ...

  4. spring boot Rabbitmq集成,延时消息队列实现

    本篇主要记录Spring boot 集成Rabbitmq,分为两部分, 第一部分为创建普通消息队列, 第二部分为延时消息队列实现: spring boot提供对mq消息队列支持amqp相关包,引入即可 ...

  5. Spring Boot + RabbitMQ 使用示例

    基础知识 虚拟主机 (Virtual Host): 每个 virtual host 拥有自己的 exchanges, queues 等 (类似 MySQL 中的库) 交换器 (Exchange): 生 ...

  6. Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践

    Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践 本篇博文主要提供一个在 SpringBoot 中自定义 kafka配置的实践,想象这样一个场景:你的系统 ...

  7. spring boot(5)-properties参数配置

     application.properties application.properties是spring boot默认的配置文件,spring boot默认会在以下两个路径搜索并加载这个文件 s ...

  8. 【spring boot】spring boot中使用定时任务配置

    spring boot中使用定时任务配置 =============================================================================== ...

  9. Spring Boot 支持多种外部配置方式

    Spring Boot 支持多种外部配置方式 http://blog.csdn.net/isea533/article/details/50281151 这些方式优先级如下: 命令行参数 来自java ...

随机推荐

  1. C# EF更新当前实体报错 ObjectManager无法管理具有相同键值的多个对象

    原因: ObjectManager已经在跟踪此对象 更新实体前判断 if (db.Entry<T>(t).State != EntityState.Modified) db.Entry&l ...

  2. Android中Context的总结及其用法

    在android中我们经常遇到这样的情况,在创建一个对象的时候往往需要传递一个this参数,比如:语句 MyView mView = new MyView(this),要求传递一个this参数,这个t ...

  3. jquery遍历总结(转)

    遍历 DOM jQuery 提供了多种遍历 DOM 的方法. 遍历方法中最大的种类是树遍历(tree-traversal). 下一章会讲解如何在 DOM 树中向上.下以及同级移动. 向上遍历 DOM ...

  4. C# DateTime 时间格式

    //2008年4月24日 System.DateTime.Now.ToString("D"); //2008-4-24 System.DateTime.Now.ToString(& ...

  5. Unix删除当前目录可执行文件

    On GNU versions of find you can use -executable: find . -type f -executable -printFor BSD versions o ...

  6. JAVA设计模式之 命令模式【Command Pattern】

    一.概述 命令模式能够将请求发送者和接收者全然解耦.发送者与接收者之间没有直接引用关系,发送请求的对象仅仅须要知道怎样发送请求,而不必知道怎样完毕请求.核心在于引入了命令类,通过命令类来减少发送者和接 ...

  7. superobject 序列数据集

    unit uDBJson; interface {$HINTS OFF} uses SysUtils, Classes, Variants, DB, DBClient, SuperObject; ty ...

  8. C#通过SFTP协议操作文件

    本文主要是C#调用SSH实现文件上传下载功能,主要是要引用第三方类库Tamir.SharpSSH.dll. 以下是SFTPHelper类,实现了对文件的操作,可供参考. public class SF ...

  9. The Definitive Guide To Django 2 学习笔记(九) 第五章 模型 (一)数据库访问

    以MySql数据库为例,先到http://dev.mysql.com/downloads/connector/python/处下载MysqlConnector for python的连接器. from ...

  10. 元器件封装标准IPC-7351

    IPC-7351依赖久经考验的数学算法,综合考虑制造.组装和元件容差,从而精确计算焊盘图形.该标准以IPC-SM-782研发概念为基础进一步提高,对每一个元件都建立了三个焊盘图形几何形状,对每一系列元 ...