- 本系列教程目录大纲:《RabbitMQ系列教程-目录大纲》
- 本系列教程配套代码:https://gitee.com/Horizon1024/rabbitmt.git(码云地址)
第六章 SpringBoot整合RabbitMQ
6.1 搭建消息生产者
6.1.1 pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lscl</groupId>
<artifactId>05_springboot_rabbitmq_producer</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- springboot 工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- rabbitmq场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 测试环境场景启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
6.1.2 application.yml:
spring:
rabbitmq:
host: 192.168.40.139
port: 5672
username: lscl
password: admin
virtual-host: /lscl
6.1.3 引导类:
package com.lscl.rabbitmq;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class);
}
}
6.1.4 配置消息队列:
6.1.4.1 Work模式:
SpringBoot
中提供有两种构建Queue
的方式,一种是通过new Queue()
的方式,另一种采用QueueBuilder
构建器构建一个Queue
;
package com.lscl.rabbitmq.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config\_01\_Work {
/\*\*
\* 定义一个队列
\* @return
\*/
@Bean("bootWorkQueue")
public Queue bootWorkQueue() {
/\*
第一种方式:
durable():代表需要持久化
exclusive(): 代表该队列独占(只允许有一个consumer监听)
autoDelete(): 代表需要自动删除(没有consumer自动删除)
withArgument(): 队列的其他参数
\*/
// return QueueBuilder.durable("boot\_work\_queue").exclusive().autoDelete().withArgument("key", "val").build();
/\*
第二种方式:
通过new Queue对象来创建队列
参数1: 队列名称
参数2: 是否持久化(默认:true)
参数3: 是否独占(默认:false)
参数4: 是否自动删除(默认:false)
参数5: 队列的其他参数
\*/
return new Queue("boot\_work\_queue", true, false, false,null);
}
}
6.1.4.2 Pub/Sub模式:
package com.lscl.rabbitmq.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/\*\*
\* Pub/Sub模式(交换机类型为Fanout)
\*/
@Configuration
public class Config\_03\_Fanout {
@Bean("bootFanoutQueue1")
public Queue bootFanoutQueue1(){
return QueueBuilder.durable("boot\_fanout\_queue1").build();
}
@Bean("bootFanoutQueue2")
public Queue bootFanoutQueue2(){
return QueueBuilder.durable("boot\_fanout\_queue2").build();
}
// fanout类型交换机
@Bean("bootFanoutExchange")
public Exchange bootFanoutExchange(){
return ExchangeBuilder.fanoutExchange("boot\_fanout\_exchange").durable(true).build();
}
/\*\*
\* 交换机与队列进行绑定
\*/
@Bean
public Binding bindFanout1(){
Queue bootFanoutQueue1 = bootFanoutQueue1();
Exchange bootFanoutExchange = bootFanoutExchange();
// fanout类型交换机routing key为 ""
return BindingBuilder.bind(bootFanoutQueue1).to(bootFanoutExchange).with("").noargs();
}
/\*\*
\* 交换机与队列进行绑定
\* @return
\*/
@Bean
public Binding bindFanout2(){
Queue bootFanoutQueue2 = bootFanoutQueue2();
Exchange bootFanoutExchange = bootFanoutExchange();
return BindingBuilder.bind(bootFanoutQueue2).to(bootFanoutExchange).with("").noargs();
}
}
6.1.4.3 Routing模式:
package com.lscl.rabbitmq.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/\*\*
\* routing模式(交换机类型为Direct)
\*/
@Configuration
public class Config\_02\_Direct {
/\*\*
\* 准备两个队列boot\_direct\_queue1、boot\_direct\_queue2
\* @return
\*/
@Bean("bootDirectQueue1")
public Queue bootDirectQueue1(){
return QueueBuilder.durable("boot\_direct\_queue1").build();
}
@Bean("bootDirectQueue2")
public Queue bootDirectQueue2(){
return QueueBuilder.durable("boot\_direct\_queue2").build();
}
// direct类型交换机
@Bean("bootDirectExchange")
public Exchange bootDirectExchange(){
/\*
第一种方式: 通过ExchangeBuilder构建交换机
durable: 是否持久化
autoDelete: 是否自动删除
withArgument: 交换机其他参数
\*/
// return ExchangeBuilder.directExchange("boot\_direct\_exchange").durable(true).autoDelete().withArgument("key","val").build();
/\*
第二种方式:
参数1: 是否持久化(默认false)
参数2: 是否自动删除(默认false)
参数3: 其他参数
\*/
return new DirectExchange("boot\_direct\_exchange",true,false,null);
}
/\*\*
\* 交换机与队列进行绑定
\*/
@Bean
public Binding bindDirect1(){
Queue bootDirectQueue1 = bootDirectQueue1();
Exchange bootDirectExchange = bootDirectExchange();
/\*
第一种方式:
bind(Queue): 需要绑定的queue
to(Exchange): 需要绑定到哪个交换机
with(String): routing key
noargs(): 进行构建
\*/
// return BindingBuilder.bind(bootDirectQueue1).to(bootDirectExchange).with("article").noargs();
/\*
第一种方式:
参数1: 绑定的队列
参数2: 绑定的类型 Binding.DestinationType.QUEUE: 绑定的类型为queue(交换机不仅可以绑定queue还可以绑定exchange)
参数3: 哪个交换机需要绑定
参数4: routing key
参数5: 其他参数
\*/
return new Binding("boot\_direct\_queue1", Binding.DestinationType.QUEUE,"boot\_direct\_exchange","red",null);
}
/\*\*
\* 交换机与队列进行绑定
\* @return
\*/
@Bean
public Binding bindDirect2(){
Queue bootDirectQueue2 = bootDirectQueue2();
Exchange bootDirectExchange = bootDirectExchange();
return BindingBuilder.bind(bootDirectQueue2).to(bootDirectExchange).with("green").noargs();
}
}
6.1.4.4 Topics模式
package com.lscl.rabbitmq.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/\*\*
\* Topics模式(交换机类型为Topic)
\*/
@Configuration
public class Config\_04\_Topic {
@Bean("bootTopicQueue1")
public Queue bootTopicQueue1(){
return QueueBuilder.durable("boot\_topic\_queue1").build();
}
@Bean("bootTopicQueue2")
public Queue bootTopicQueue2(){
return QueueBuilder.durable("boot\_topic\_queue2").build();
}
// topic类型交换机
@Bean("bootTopicExchange")
public Exchange bootTopicExchange(){
return ExchangeBuilder.topicExchange("boot\_topic\_exchange").durable(true).build();
}
/\*\*
\* 交换机与队列进行绑定
\*/
@Bean
public Binding bindTopic1(){
Queue bootTopicQueue1 = bootTopicQueue1();
Exchange bootTopicExchange = bootTopicExchange();
return BindingBuilder.bind(bootTopicQueue1).to(bootTopicExchange).with("red.#").noargs();
}
/\*\*
\* 交换机与队列进行绑定
\* @return
\*/
@Bean
public Binding bindTopic2(){
Queue bootTopicQueue2 = bootTopicQueue2();
Exchange bootTopicExchange = bootTopicExchange();
return BindingBuilder.bind(bootTopicQueue2).to(bootTopicExchange).with("green.\*").noargs();
}
}
6.1.5 测试类:
package com.lscl.rabbitmq;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest(classes = ProducerApplication.class)
@RunWith(SpringRunner.class)
public class ProducerTest {
@Autowired
private RabbitTemplate rabbitTemplate;
/\*\*
\* work 模式
\* @throws Exception
\*/
@Test
public void testWork() throws Exception{
rabbitTemplate.convertAndSend("boot\_work\_queue","boot work...");
}
# 分享
这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!
![薪酬缩水,“裸辞”奋战25天三面美团,交叉面却被吊打,我太难了](https://img-blog.csdnimg.cn/img_convert/3099aa54b2a645bed254edc60e153389.webp?x-oss-process=image/format,png)
Spring Cloud实战
![薪酬缩水,“裸辞”奋战25天三面美团,交叉面却被吊打,我太难了](https://img-blog.csdnimg.cn/img_convert/bdcb36ab5629635a3535d70d7580a456.webp?x-oss-process=image/format,png)
Spring Boot实战
![薪酬缩水,“裸辞”奋战25天三面美团,交叉面却被吊打,我太难了](https://img-blog.csdnimg.cn/img_convert/476d71eb2ea68da57fa4af10628ad074.webp?x-oss-process=image/format,png)
面试题整理(性能优化+微服务+并发编程+开源框架+分布式)
) throws Exception{
rabbitTemplate.convertAndSend("boot\_work\_queue","boot work...");
}
# 分享
这次面试我也做了一些总结,确实还有很多要学的东西。相关面试题也做了整理,可以分享给大家,了解一下面试真题,想进大厂的或者想跳槽的小伙伴不妨好好利用时间来学习。学习的脚步一定不能停止!
[外链图片转存中...(img-Wx0tACIt-1714486445721)]
Spring Cloud实战
[外链图片转存中...(img-hDkR906Z-1714486445722)]
Spring Boot实战
[外链图片转存中...(img-xTCRit8L-1714486445722)]
面试题整理(性能优化+微服务+并发编程+开源框架+分布式)
> **本文已被[CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】](https://bbs.csdn.net/topics/618154847)收录**