SpringBoot整合RabbitMQ示例詳解
SpringBoot整合RabbitMQ
1.生產(chǎn)者SpringBootProducer
1.1 創(chuàng)建工程并導(dǎo)入依賴(lài)
我們使用的springboot版本為2.5.6,其他都是根據(jù)spring-boot-starter-parent自動(dòng)選擇版本
引入以下工程即可
spring-boot-starter-test用于測(cè)試junit用于單元測(cè)試spring-boot-starter-amqpSpringBoot和RabbitMQ的整合方案
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/>
</parent>
<artifactId>springboot-producer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>1.2 創(chuàng)建配置文件并配置
SpringBoot配置文件名稱(chēng)為application.yml
需要配置的內(nèi)容如下:
# 配置RabbitMQ的基本信息
spring:
rabbitmq:
# 地址
host: 192.168.52.128
# 端口
port: 5672
# 用戶(hù)名
username: admin
# 密碼
password: admin
# 虛擬機(jī)
virtual-host: /test1.3 創(chuàng)建項(xiàng)目啟動(dòng)類(lèi)
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}1.4 創(chuàng)建RabbitMQ配置類(lèi)
@Configuration
public class RabbitMQConfig {
// 配置代碼都寫(xiě)在這里
}(1)設(shè)置默認(rèn)的交換機(jī)的名稱(chēng)和隊(duì)列名稱(chēng)
/** * 默認(rèn)測(cè)試的交換機(jī)機(jī)名稱(chēng) * springboot_topic_exchange */ public static final String EXCHANGE_NAME = "springboot_topic_exchange"; /** * 默認(rèn)的隊(duì)列名稱(chēng) * springboot_root_queue */ public static final String QUEUE_NAME = "springboot_root_queue";
(2)創(chuàng)建通配符類(lèi)型的交換機(jī)
/**
* 創(chuàng)建交換機(jī)
*
* @return 交換機(jī)
*/
@Bean("bootExchange")
public Exchange bootExchange() {
// 創(chuàng)建一個(gè)通配符的交換機(jī)
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}這里需要在bean上加上名稱(chēng)(雖然如果沒(méi)有時(shí)會(huì)使用方法名,但是嚴(yán)謹(jǐn)),便于之后交換機(jī)和隊(duì)列綁定操作。
除了通配符交換機(jī)外,還支持
廣播型交換機(jī)、定向型交換機(jī)。
- 廣播型交換機(jī)
/**
* 創(chuàng)建交換機(jī)
*
* @return 交換機(jī)
*/
@Bean("bootExchange")
public Exchange bootExchange() {
// 創(chuàng)建一個(gè)通配符的交換機(jī)
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}- 定向型交換機(jī)
@Bean("directExchange")
public Exchange directExchange() {
return ExchangeBuilder.directExchange("direct_exchange").durable(true).build();
}(3)創(chuàng)建一個(gè)隊(duì)列
/**
* 創(chuàng)建隊(duì)列
*
* @return 隊(duì)列
*/
@Bean("bootQueue")
public Queue bootQueue() {
return QueueBuilder.durable(QUEUE_NAME).build();
}(4)綁定交換機(jī)和隊(duì)列
/**
* 綁定隊(duì)列和交換機(jī)
* 主要:隊(duì)列、交換機(jī)、routing key
*
* @return 綁定關(guān)系
*/
@Bean
public Binding bindingQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}(5)完整配置類(lèi)
@Configuration
public class RabbitMQConfig {
/**
* 默認(rèn)測(cè)試的交換機(jī)機(jī)名稱(chēng)
* springboot_topic_exchange
*/
public static final String EXCHANGE_NAME = "springboot_topic_exchange";
/**
* 默認(rèn)的隊(duì)列名稱(chēng)
* springboot_root_queue
*/
public static final String QUEUE_NAME = "springboot_root_queue";
/**
* 創(chuàng)建交換機(jī)
*
* @return 交換機(jī)
*/
@Bean("bootExchange")
public Exchange bootExchange() {
// 創(chuàng)建一個(gè)通配符的交換機(jī)
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
/**
* 創(chuàng)建隊(duì)列
*
* @return 隊(duì)列
*/
@Bean("bootQueue")
public Queue bootQueue() {
return QueueBuilder.durable(QUEUE_NAME).build();
}
/**
* 綁定隊(duì)列和交換機(jī)
* 主要:隊(duì)列、交換機(jī)、routing key
*
* @return 綁定關(guān)系
*/
@Bean
public Binding bindingQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs();
}
}1.5 測(cè)試發(fā)送消息
@SpringBootTest
@RunWith(SpringRunner.class)
public class ProducerTest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSend() {
rabbitTemplate.convertAndSend(RabbitMQConfig.EXCHANGE_NAME, "boot.name", "Spring Boot RabbitMQ");
}
}2.消費(fèi)者SpringBootConsumer
2.1 創(chuàng)建工程并導(dǎo)入依賴(lài)
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-consumer</name>
<description>springboot-consumer</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>PS:以上依賴(lài)是由springboot工程創(chuàng)建完成,和之前手動(dòng)創(chuàng)建沒(méi)有本質(zhì)差別
2.2 創(chuàng)建配置文件并配置
SpringBoot配置文件名稱(chēng)為application.yml
需要配置的內(nèi)容如下:
# 配置RabbitMQ的基本信息
spring:
rabbitmq:
# 地址
host: 192.168.52.128
# 端口
port: 5672
# 用戶(hù)名
username: admin
# 密碼
password: admin
# 虛擬機(jī)
virtual-host: /test2.3 創(chuàng)建項(xiàng)目啟動(dòng)類(lèi)
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}2.3 創(chuàng)建監(jiān)聽(tīng)器
@Component
public class RabbitMQListener {
@RabbitListener(queues = "springboot_root_queue")
public void listenerQueue(Message message) {
System.out.println("RabbitMQListener:" + new String(message.getBody()));
}
}
@RabbitListener表示當(dāng)前方法監(jiān)聽(tīng)對(duì)應(yīng)的隊(duì)列,并且支持多隊(duì)列。
2.4 run
控制臺(tái)如下:
RabbitMQListener:Spring Boot RabbitMQ
到此這篇關(guān)于SpringBoot整合RabbitMQ示例詳解的文章就介紹到這了,更多相關(guān)SpringBoot整合RabbitMQ內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring使用AspectJ注解和XML配置實(shí)現(xiàn)AOP
這篇文章主要介紹了Spring使用AspectJ注解和XML配置實(shí)現(xiàn)AOP的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Java實(shí)現(xiàn)手寫(xiě)乞丐版線程池的示例代碼
在這篇文章當(dāng)中我們主要介紹實(shí)現(xiàn)一個(gè)非常簡(jiǎn)易版的線程池,深入的去理解其中的原理,麻雀雖小,五臟俱全,感興趣的小伙伴快跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧2022-10-10
Spring監(jiān)聽(tīng)器及定時(shí)任務(wù)實(shí)現(xiàn)方法詳解
這篇文章主要介紹了Spring監(jiān)聽(tīng)器及定時(shí)任務(wù)實(shí)現(xiàn)方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
基于java SSM springboot實(shí)現(xiàn)景區(qū)行李寄存管理系統(tǒng)
這篇文章主要介紹了基于java SSM springboot實(shí)現(xiàn)的景區(qū)行李寄存管理系統(tǒng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
SpringBoot項(xiàng)目中遇到的BUG問(wèn)題及解決方法
這篇文章主要介紹了SpringBoot項(xiàng)目中遇到的BUG問(wèn)題及解決方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11
java.net.http.HttpClient使用示例解析
這篇文章主要為大家介紹了java.net.http.HttpClient使用示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
IntelliJ IDEA 設(shè)置代碼提示或自動(dòng)補(bǔ)全的快捷鍵功能
這篇文章主要介紹了IntelliJ IDEA 設(shè)置代碼提示或自動(dòng)補(bǔ)全的快捷鍵功能,需要的朋友可以參考下2018-03-03
詳解Java中NullPointerException的處理方法
這篇文章將帶大家來(lái)單獨(dú)看一個(gè)很常見(jiàn)的異常--空指針異常,這個(gè)可以說(shuō)是每個(gè)Java程序員都必知的異常,所以我們不得不單獨(dú)學(xué)習(xí)一下,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-08-08

