详解Spring Boot 配置多个RabbitMQ
发布时间 - 2026-01-11 02:01:05 点击率:次闲话

好久没有写博客了,6月份毕业,因为工作原因,公司上网受限,一直没能把学到的知识点写下来,工作了半年,其实学到的东西也不少,但是现在回忆起来的东西少之又少,有时甚至能在同个问题中踩了几次,越来越觉得及时记录一下学到的东西很重要。
好了,闲话少说,写下这段时间学习的东西,先记录一下用spring Boot配置多个RabbitMQ的情况。。。
最近公司新启动一个新平台的项目,需要用微服务这个这几年很火的概念来做,所以就学习了Spring Boot方面的知识,给同事展示Spring Boot的一些小事例的时候,同事提出了可不可以配置多个RabbitMQ?下面就是在Spring Boot配置多个RabbitMQ的例子。是自己摸索搭建的,也不知道对不对,有其他好的实现方法的网友可以互相交流一下。
项目代码构造
关注点在红框的代码。。。
代码
下面就把项目的代码展示下来
application.properties
配置文件
spring.application.name=rabbitmq-hello # RabbitMQ spring.rabbitmq.first.host=node9 spring.rabbitmq.first.port=5670 spring.rabbitmq.first.username=guest spring.rabbitmq.first.password=guest spring.rabbitmq.second.host=localhost spring.rabbitmq.second.port=5672 spring.rabbitmq.second.username=guest spring.rabbitmq.second.password=guest # MySQL spring.datasource.url = jdbc:mysql://localhost:3306/cloudtest spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver
HelloApplication.java
程序入口
package com.paas.springboot.demo01;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
RabbitConfig.java
RabbitMQ配置类
package com.paas.springboot.demo01;
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;
@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 Queue firstQueue() {
System.out.println("configuration firstQueue ........................");
return new Queue("hello1");
}
@Bean
public Object secondQueue() {
System.out.println("configuration secondQueue ........................");
return new Queue("hello2");
}
}
Receiver.java
RabbitMQ中的消费者,接收first RabbitMQ中的队列hello1的数据
package com.paas.springboot.demo01;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "hello1", containerFactory="firstFactory")
public class Receiver {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver : " + hello);
}
}
Receiver2.java
RabbitMQ中的消费者,接收second RabbitMQ中的队列hello2的数据
package com.paas.springboot.demo01;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "hello2", containerFactory="secondFactory" )
public class Receiver2 {
@RabbitHandler
public void process(String hello) {
System.out.println("Receiver : " + hello);
}
}
Sender.java
RabbitMQ中的生产者,发送消息到first RabbitMQ中的队列hello1和hello2
package com.paas.springboot.demo01;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
@Component
public class Sender {
@Resource(name="firstRabbitTemplate")
private RabbitTemplate firstRabbitTemplate;
public void send1() {
String context = "hello1 " + new Date();
System.out.println("Sender : " + context);
this.firstRabbitTemplate.convertAndSend("hello1", context);
}
public void send2() {
String context = "hello2 " + new Date();
System.out.println("Sender : " + context);
this.firstRabbitTemplate.convertAndSend("hello2", context);
}
}
Sender2.java
RabbitMQ中的生产者,发送消息到second RabbitMQ中的队列hello1和hello2
package com.paas.springboot.demo01;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
@Component
public class Sender {
@Resource(name="firstRabbitTemplate")
private RabbitTemplate firstRabbitTemplate;
public void send1() {
String context = "hello1 " + new Date();
System.out.println("Sender : " + context);
this.firstRabbitTemplate.convertAndSend("hello1", context);
}
public void send2() {
String context = "hello2 " + new Date();
System.out.println("Sender : " + context);
this.firstRabbitTemplate.convertAndSend("hello2", context);
}
}
TestDemo01.java
测试类,调用Sender发送消息
package com.paas.springboot.demo01;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HelloApplication.class)
public class TestDemo01 {
@Autowired
private Sender sender;
@Autowired
private Sender2 sender2;
@Test
public void hello() throws Exception {
sender.send1();
sender.send2();
}
@Test
public void hello2() throws Exception {
sender2.send1();
sender2.send2();
}
}
pom.xml
Maven项目中最重要的一个配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.paas.springboot.demo</groupId>
<artifactId>springboot</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springboot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
运行&测试
通过运行HelloApplication.Java,将程序中的Receiver启动一直监控着队列,然后通过运行TestDemo01.java中的测试案例,发送消息到队列中,这时可以发现运行HelloApplication的程序控制台将刚刚发送的消息打印出来
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
# Spring
# Boot
# 配置RabbitMQ
# RabbitMQ
# 详解spring boot集成RabbitMQ
# springboot集成rabbitMQ之对象传输的方法
# spring boot集成rabbitmq的实例教程
# Spring boot集成RabbitMQ的示例代码
# Spring Boot整合RabbitMQ实例(Topic模式)
# spring boot整合RabbitMQ实例详解(Fanout模式)
# Spring Boot与RabbitMQ结合实现延迟队列的示例
# Spring Boot系列教程之7步集成RabbitMQ的方法
# 多个
# 发送消息
# 配置文件
# 也不
# 好了
# 最重要
# 几次
# 提出了
# 这段
# 能在
# 就把
# 很重要
# 少之又少
# 能把
# 来做
# 这几年
# 少说
# 需要用
# 大家多多
# 他好
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
javascript日期怎么处理_如何格式化输出
BootStrap整体框架之基础布局组件
Laravel如何使用Blade组件和插槽?(Component代码示例)
Android使用GridView实现日历的简单功能
Laravel怎么为数据库表字段添加索引以优化查询
如何在Windows服务器上快速搭建网站?
如何在不使用负向后查找的情况下匹配特定条件前的换行符
活动邀请函制作网站有哪些,活动邀请函文案?
新三国志曹操传主线渭水交兵攻略
桂林网站制作公司有哪些,桂林马拉松怎么报名?
大同网页,大同瑞慈医院官网?
如何在Ubuntu系统下快速搭建WordPress个人网站?
香港服务器网站测试全流程:性能评估、SEO加载与移动适配优化
零基础网站服务器架设实战:轻量应用与域名解析配置指南
Android 常见的图片加载框架详细介绍
JavaScript中的标签模板是什么_它如何扩展字符串功能
如何快速打造个性化非模板自助建站?
油猴 教程,油猴搜脚本为什么会网页无法显示?
JavaScript如何操作视频_媒体API怎么控制播放
高性能网站服务器配置指南:安全稳定与高效建站核心方案
PHP 实现电台节目表的智能时间匹配与今日/明日轮播逻辑
香港服务器部署网站为何提示未备案?
Laravel如何自定义分页视图?(Pagination示例)
怎么用AI帮你为初创公司进行市场定位分析?
Laravel如何使用Service Container和依赖注入?(代码示例)
网站制作免费,什么网站能看正片电影?
Java Adapter 适配器模式(类适配器,对象适配器)优缺点对比
如何获取免费开源的自助建站系统源码?
如何快速上传自定义模板至建站之星?
小视频制作网站有哪些,有什么看国内小视频的网站,求推荐?
香港服务器建站指南:外贸独立站搭建与跨境电商配置流程
如何在IIS7上新建站点并设置安全权限?
Linux系统运维自动化项目教程_Ansible批量管理实战
iOS正则表达式验证手机号、邮箱、身份证号等
如何在宝塔面板中创建新站点?
教你用AI润色文章,让你的文字表达更专业
如何在阿里云虚拟机上搭建网站?步骤解析与避坑指南
免费网站制作appp,免费制作app哪个平台好?
Linux安全能力提升路径_长期防护思维说明【指导】
如何挑选最适合建站的高性能VPS主机?
弹幕视频网站制作教程下载,弹幕视频网站是什么意思?
简历在线制作网站免费版,如何创建个人简历?
制作电商网页,电商供应链怎么做?
如何制作新型网站程序文件,新型止水鱼鳞网要拆除吗?
Laravel怎么导出Excel文件_Laravel Excel插件使用教程
高防网站服务器:DDoS防御与BGP线路的AI智能防护方案
长沙企业网站制作哪家好,长沙水业集团官方网站?
什么是javascript作用域_全局和局部作用域有什么区别?
东莞市网站制作公司有哪些,东莞找工作用什么网站好?
如何用AI帮你把自己的生活经历写成一个有趣的故事?

