场景:

springboot单项目,自身使用mq中间件处理一些业务需求,某些业务上又需要消费第三方mq消息,这时候需要我们单项目中配置多套mq,这时候,需要我们自定义多套mq相关连接工厂、模板、监听工厂、管理等流程,具体实现,参见如下:

实现:

1.配置文件:application.yml

spring:
rabbitmq:
first:
host: localhost
port: 5672
username: guest
password: guest
virtualHost: /channel-demo
second:
host: 100.100.100.184
port: 5672
username: guest
password: guest
virtualHost: /xxx-test

2.配置类:

FirstRabbitConfig配置类:
package com.xxx.channe.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; /**
* <p>
* </p>
*
* @author mumu
* @since 2019/12/6
*/
@Configuration
@ConfigurationProperties("spring.rabbitmq.first")
public class FirstRabbitConfig extends AbstractRabbitConfig {
@Bean(name = "firstConnectionFactory")
@Primary
public ConnectionFactory firstConnectionFactory() {
return super.connectionFactory();
} @Bean(name = "firstRabbitTemplate")
@Primary
public RabbitTemplate rabbitTemplate(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
} @Bean(name = "firstFactory")
public SimpleRabbitListenerContainerFactory firstFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
} @Bean(value = "firstRabbitAdmin")
public RabbitAdmin firstRabbitAdmin(@Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
// rabbitAdmin.declareExchange(firstDirectExchange());
// rabbitAdmin.declareQueue(firstQueue());
// rabbitAdmin.declareBinding(firstBinding());
return rabbitAdmin;
} @Bean
public DirectExchange firstDirectExchange() {
return new DirectExchange("first-direct-exchange");
} @Bean
public Queue firstQueue() {
return new Queue("first-queue");
} @Bean
public Binding firstBinding() {
return BindingBuilder.bind(firstQueue()).to(firstDirectExchange()).with("first-routing-key");
}
}
SecondRabbitConfig配置类:
package com.xxx.channe.config;

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; /**
* <p>
* </p>
*
* @author mumu
* @since 2019/12/6
*/
@Configuration
@ConfigurationProperties("spring.rabbitmq.second")
public class SecondRabbitConfig extends AbstractRabbitConfig { @Bean(name = "secondConnectionFactory")
public ConnectionFactory secondConnectionFactory() {
return super.connectionFactory();
} @Bean(name = "secondRabbitTemplate")
public RabbitTemplate secondRabbitTemplate(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitTemplate(connectionFactory);
} @Bean(name = "secondFactory")
public SimpleRabbitListenerContainerFactory secondFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
} @Bean(value = "secondRabbitAdmin")
public RabbitAdmin secondRabbitAdmin(@Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
} // @Bean
// public DirectExchange secondDirectExchange() {
// return new DirectExchange("second-direct-exchange");
// }
//
// @Bean
// public Queue secondQueue() {
// return new Queue("second-queue");
// }
//
// @Bean
// public Binding secondBinding() {
// return BindingBuilder.bind(secondQueue()).to(secondDirectExchange()).with("second-routing-key");
// }
}

3.生产者:

package com.xxx.channe.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; /**
* <p>
* </p>
*
* @author mumu
* @since 2019/12/6
*/
@Slf4j
@Component
public class MqTest { @Autowired
@Qualifier(value = "firstRabbitTemplate")
private RabbitTemplate firstRabbitTemplate;   
public void send() {
firstRabbitTemplate.convertAndSend("first-direct-exchange", "first-routing-key", "我是 first。。。");
log.info("消息发送成功...");
} }

4.消费者:

package com.xxx.channe.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; /**
* <p>
* </p>
*
* @author mumu
* @since 2019/12/6
*/
@Slf4j
@Component
public class MqTest {@RabbitListener(queues = {"first-queue"}, containerFactory = "firstFactory")
@RabbitHandler
public void firstTest() {
System.out.println("hello, first mq收到消息");
} }

未完待续...

Java之RabbitMQ(二)多mq配置的更多相关文章

  1. MQ配置安装

    一,MQ安装 ./mqlicense.sh -accept rpm -ivh MQSeries*.rpm --  rpm -qa|grep MQSeries 二,MQ配置 环境变量配置(MQM)实际安 ...

  2. Java进阶专题(二十) 消息中间件架构体系(2)-- RabbitMQ研究

    前言 接上文,这个继续介绍RabbitMQ,并理解其底层原理. 介绍 RabbitMQ是由erlang语言开发,基于AMQP(Advanced Message Queue 高级消息队列协议)协议实现的 ...

  3. RabbitMQ学习(二):Java使用RabbitMQ要点知识

    转  https://blog.csdn.net/leixiaotao_java/article/details/78924863 1.maven依赖 <dependency> <g ...

  4. rabbit的简单搭建,java使用rabbitmq queue的简单例子和一些坑

    一 整合 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 以整 ...

  5. (一)RabbitMQ安装与基本配置

    [博主使用的环境是阿里云ecs服务器,操作系统为centos] 安装erlang环境 RabbitMQ底层是Erlang语言,因此要先安装erlang环境,就像你要运行Java程序就必须先安装JRE/ ...

  6. Java SE 简介 & 环境变量的配置

    Java SE 简介 & 环境变量的配置 一.Java 技术的三个方向 Java 技术分为三个方向 javaSE( Java Platform Standard Edition 标准版)用来开 ...

  7. Java 验证码、二维码

    Java 验证码.二维码 资源 需要:   jelly-core-1.7.0.GA.jar网站:   http://lychie.github.io/products.html将下载下来的 jelly ...

  8. Java并发编程二三事

    Java并发编程二三事 转自我的Github 近日重新翻了一下<Java Concurrency in Practice>故以此文记之. 我觉得Java的并发可以从下面三个点去理解: * ...

  9. JAVA开发环境 - 环境变量及配置

    JDK是什么?JRE是什么? JRE(Java Runtime Environment):Java运行环境: JDK(Java Development Kit):Java开发工具包,里面已经包含JRE ...

随机推荐

  1. Dubbox框架和Zookeeper 依赖的引入

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  2. bzoj1004题解

    [题意分析] 给N个元素染色,可以在定置换群的作用下互相转化的染色方案算相同的,问本质不同的染色方案数. [解题思路] 引理:Burnside定理 设集合S=[1,n]∩N,记等价类数为L,给定S上的 ...

  3. 思维题+贪心——牛客多校第一场C

    /* 给定一组n维向量 A=(a1/m,a2/m,a3/m ... an/m), 求另一个n维向量 P=(p1,p2,p3...pn),满足sum{pi}=1,使得ans=sum{(ai/m-pi)^ ...

  4. hive的行列互转

    行转列 多行转多列 数据表 row2col col1 col2 col3 a c 1 a d 2 a e 3 b c 4 b d 5 b e 6 现在要将其转化为: col1 c d e a 1 2 ...

  5. nginx、php-fpm安装mongodb及驱动扩展

    1.安装mongodb linux下安装mongodb很简单,执行如下命令完成安装 wget http://downloads.mongodb.org/linux/mongodb-linux-i686 ...

  6. bash数组总结

    bash数组操作 bash支持两种数组,一种是索引数组,一种是关联数组 索引数组 数组的值类型是任意的,索引也未必一定要连续,当做列表理解更好 下面总结下索引数组,即列表: 1. 声明 declare ...

  7. C不同变量类型存储大小引发的BUG

    #include"stdio.h" typedef signed char int8; typedef unsigned char uint8; typedef signed sh ...

  8. linux 下 CDH4.5编译

    1.安装JDK JDK:我这里 安装的是jdk1.6.0_23 1.1:给文件执行的权限chmod u+x jdk-6u23-linux-x64.bin 1.2: ./jdk-6u23-linux-x ...

  9. 无LoadLibrary获取指定模块基址

    实际上,这块可以写成汇编,然后做远程注入用 方法 1.通过fs:[30h]获取当前进程的_PEB结构 2.通过_PEB的Ldr成员获取_PEB_LDR_DATA结构 3.通过_PEB_LDR_DATA ...

  10. ES6 学习 -- Class

    Class的基本语法(1)// 定义类class Point { test() { console.log("hello test"); }}通过 new 定义好的类即可生成一个类 ...