场景:

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. P1487 失落的成绩单

    P1487 失落的成绩单a[i]=a[i-2]-2.0*a[i-1]+2.0*d;a[2]越大,a[3]越小a[3]越大,a[4]越小所以a[2]越大,a[4]越大,a[3]越小就有了单调性,分奇偶进 ...

  2. leetcode-158周赛-5223-可以攻击国王的皇后

    题目描述: 自己的提交: class Solution: def queensAttacktheKing(self, queens: List[List[int]], king: List[int]) ...

  3. 【JZOJ3292】【BZOJ4415】【luoguP3988】发牌

    description 在一些扑克游戏里,如德州扑克,发牌是有讲究的.一般称呼专业的发牌手为荷官.荷官在发牌前,先要销牌(burn card).所谓销牌,就是把当前在牌库顶的那一张牌移动到牌库底,它用 ...

  4. 记一次为解决Python读取PDF文件的Shell操作

    目录 一.背景 二.问题 三.解决 四.一顿分析及 Shell 操作 五.后续 一.背景 本想将 PDF 文件转换为 Word 文档,然后网上搜索了一下发现有挺多转换的软件.有的是免费的.收费,咱也不 ...

  5. fastJson中常用方法以及遇到的“坑”

    1.使用fastJson,首先引入fastJson依赖 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> & ...

  6. Elasticsearch集群状态查看命令

    _cat $ curl localhost:9200/_cat=^.^=/_cat/allocation/_cat/shards/_cat/shards/{index}/_cat/master/_ca ...

  7. CF16E Fish(状压+期望dp)

    [传送门[(https://www.luogu.org/problemnew/show/CF16E) 解题思路 比较简单的状压+期望.设\(f[S]\)表示\(S\)这个状态的期望,转移时挑两条活着的 ...

  8. iOS 获取音频或是视频的时间

    AVURLAsset* audioAsset =[AVURLAssetURLAssetWithURL:audioFileURL options:nil]; CMTime audioDuration = ...

  9. django中related_name的作用和用法

    其实可以就理解为,一对多关系拿对象的解决 可以把引用理解为主从关系 主引用从,即一对多 , 注意外键字段是放在多的一端的,比如一个班级class 有很多同学 students,那么就在students ...

  10. sudo apt-get update:Could not get lock /var/lib/apt/lists/lock解决办法

    原文: http://blog.chinaunix.net/uid-26932153-id-3193335.html 今天更新时候出现了点小问题,一开始更新到一半,我嫌速度慢,就取消掉了. 更新了so ...