SpringBoot整合RabbitMQ示例詳解
SpringBoot整合RabbitMQ
1.生產(chǎn)者SpringBootProducer
1.1 創(chuàng)建工程并導(dǎo)入依賴
我們使用的springboot版本為2.5.6,其他都是根據(jù)spring-boot-starter-parent自動選擇版本
引入以下工程即可
spring-boot-starter-test用于測試junit用于單元測試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配置文件名稱為application.yml
需要配置的內(nèi)容如下:
# 配置RabbitMQ的基本信息
spring:
rabbitmq:
# 地址
host: 192.168.52.128
# 端口
port: 5672
# 用戶名
username: admin
# 密碼
password: admin
# 虛擬機
virtual-host: /test1.3 創(chuàng)建項目啟動類
@SpringBootApplication
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}1.4 創(chuàng)建RabbitMQ配置類
@Configuration
public class RabbitMQConfig {
// 配置代碼都寫在這里
}(1)設(shè)置默認(rèn)的交換機的名稱和隊列名稱
/** * 默認(rèn)測試的交換機機名稱 * springboot_topic_exchange */ public static final String EXCHANGE_NAME = "springboot_topic_exchange"; /** * 默認(rèn)的隊列名稱 * springboot_root_queue */ public static final String QUEUE_NAME = "springboot_root_queue";
(2)創(chuàng)建通配符類型的交換機
/**
* 創(chuàng)建交換機
*
* @return 交換機
*/
@Bean("bootExchange")
public Exchange bootExchange() {
// 創(chuàng)建一個通配符的交換機
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}這里需要在bean上加上名稱(雖然如果沒有時會使用方法名,但是嚴(yán)謹(jǐn)),便于之后交換機和隊列綁定操作。
除了通配符交換機外,還支持
廣播型交換機、定向型交換機。
- 廣播型交換機
/**
* 創(chuàng)建交換機
*
* @return 交換機
*/
@Bean("bootExchange")
public Exchange bootExchange() {
// 創(chuàng)建一個通配符的交換機
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}- 定向型交換機
@Bean("directExchange")
public Exchange directExchange() {
return ExchangeBuilder.directExchange("direct_exchange").durable(true).build();
}(3)創(chuàng)建一個隊列
/**
* 創(chuàng)建隊列
*
* @return 隊列
*/
@Bean("bootQueue")
public Queue bootQueue() {
return QueueBuilder.durable(QUEUE_NAME).build();
}(4)綁定交換機和隊列
/**
* 綁定隊列和交換機
* 主要:隊列、交換機、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)完整配置類
@Configuration
public class RabbitMQConfig {
/**
* 默認(rèn)測試的交換機機名稱
* springboot_topic_exchange
*/
public static final String EXCHANGE_NAME = "springboot_topic_exchange";
/**
* 默認(rèn)的隊列名稱
* springboot_root_queue
*/
public static final String QUEUE_NAME = "springboot_root_queue";
/**
* 創(chuàng)建交換機
*
* @return 交換機
*/
@Bean("bootExchange")
public Exchange bootExchange() {
// 創(chuàng)建一個通配符的交換機
return ExchangeBuilder.topicExchange(EXCHANGE_NAME).durable(true).build();
}
/**
* 創(chuàng)建隊列
*
* @return 隊列
*/
@Bean("bootQueue")
public Queue bootQueue() {
return QueueBuilder.durable(QUEUE_NAME).build();
}
/**
* 綁定隊列和交換機
* 主要:隊列、交換機、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 測試發(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.消費者SpringBootConsumer
2.1 創(chuàng)建工程并導(dǎo)入依賴
<?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:以上依賴是由springboot工程創(chuàng)建完成,和之前手動創(chuàng)建沒有本質(zhì)差別
2.2 創(chuàng)建配置文件并配置
SpringBoot配置文件名稱為application.yml
需要配置的內(nèi)容如下:
# 配置RabbitMQ的基本信息
spring:
rabbitmq:
# 地址
host: 192.168.52.128
# 端口
port: 5672
# 用戶名
username: admin
# 密碼
password: admin
# 虛擬機
virtual-host: /test2.3 創(chuàng)建項目啟動類
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}2.3 創(chuàng)建監(jiān)聽器
@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)聽對應(yīng)的隊列,并且支持多隊列。
2.4 run
控制臺如下:
RabbitMQListener:Spring Boot RabbitMQ
到此這篇關(guān)于SpringBoot整合RabbitMQ示例詳解的文章就介紹到這了,更多相關(guān)SpringBoot整合RabbitMQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?Mybatis查詢數(shù)據(jù)庫舉例詳解
這篇文章主要給大家介紹了關(guān)于Java?Mybatis查詢數(shù)據(jù)庫的相關(guān)資料,在MyBatis中可以使用遞歸查詢實現(xiàn)對數(shù)據(jù)庫中樹形結(jié)構(gòu)數(shù)據(jù)的查詢,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
關(guān)于servlet向mysql添加數(shù)據(jù)時中文亂碼問題的解決
最近在工作中遇到一個小問題,出現(xiàn)了中文亂碼的問題,無奈只能想辦法解決,下面這篇文章主要給大家介紹了關(guān)于servlet向mysql添加數(shù)據(jù)時中文亂碼問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
Java?Thread.join()方法使用詳細(xì)解析
Thread.join()是Java多線程編程中的關(guān)鍵方法,用于確保線程執(zhí)行順序和數(shù)據(jù)完整性,下面這篇文章主要介紹了Java?Thread.join()方法使用的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-01-01
Java ArrayList集合詳解(Java動態(tài)數(shù)組)
這篇文章主要介紹了Java ArrayList集合詳解(Java動態(tài)數(shù)組),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
java?-jar啟動服務(wù)并輸出日志常用命令小結(jié)
這篇文章主要介紹了在Linux環(huán)境下運行JAR包的幾種方法,包括在命令結(jié)尾添加&使其在后臺運行,使用nohup使程序不掛斷運行,以及將日志輸出到指定文件或丟棄,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03

