SpringBoot整合消息隊(duì)列RabbitMQ
簡介
在Spring項(xiàng)目中,可以使用Spring-Rabbit去操作RabbitMQ
https://github.com/spring-projects/spring-amqp
尤其是在spring boot項(xiàng)目中只需要引入對應(yīng)的amqp啟動器依賴即可,方便的使用RabbitTemplate發(fā)送消息,使用注解接收消息。
一般在開發(fā)過程中:
生產(chǎn)者工程:
- application.yml文件配置RabbitMQ相關(guān)信息;
- 在生產(chǎn)者工程中編寫配置類,用于創(chuàng)建交換機(jī)和隊(duì)列,并進(jìn)行綁定
- 注入RabbitTemplate對象,通過RabbitTemplate對象發(fā)送消息到交換機(jī)
消費(fèi)者工程:
- application.yml文件配置RabbitMQ相關(guān)信息
- 創(chuàng)建消息處理類,用于接收隊(duì)列中的消息并進(jìn)行處理
生產(chǎn)端
1. 創(chuàng)建生產(chǎn)者SpringBoot工程(maven)
2. 引入start,依賴坐標(biāo)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>3. 編寫yml配置,基本信息配置
4. 定義交換機(jī),隊(duì)列以及綁定關(guān)系的配置類
5. 注入RabbitTemplate,調(diào)用方法,完成消息發(fā)送
添加依賴
修改pom.xml文件內(nè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 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.1.4.RELEASE</version> </parent> <groupId>com.itheima</groupId> <artifactId>springboot-rabbitmq-producer</artifactId> <version>1.0-SNAPSHOT</version> <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> </dependency> </dependencies> </project>
啟動類
package com.itheima.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); } }
配置RabbitMQ
配置文件
創(chuàng)建application.yml,內(nèi)容如下:
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /itcast
username: heima
password: heima
綁定交換機(jī)和隊(duì)列
創(chuàng)建RabbitMQ隊(duì)列與交換機(jī)綁定的配置類com.itheima.rabbitmq.config.RabbitMQConfig
package com.itheima.rahhitmq.config; import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration /// 配置類 public class RabbitMQConfig { public static final String EXCHAGE_NAME = "boot_topic_exchange"; public static final String QUEUE_NAME = "boot_queue"; // 交換機(jī) @Bean("bootExchange") public Exchange bootExchange(){ // 構(gòu)建交換機(jī)對象 return ExchangeBuilder.topicExchange(EXCHAGE_NAME).durable(true).build(); } //Queue 隊(duì)列 @Bean("bootQueue") public Queue bootQueue(){ return QueueBuilder.durable(QUEUE_NAME).build(); } //隊(duì)列和交換機(jī)的關(guān)系 Binding /** * 1 知道那個隊(duì)列 * 2 知道那個交換機(jī) * 3 routingKey */ @Bean public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs(); } }
搭建消費(fèi)者工程
創(chuàng)建工程
生產(chǎn)端
1. 創(chuàng)建生產(chǎn)者SpringBoot工程
2. 引入start,依賴坐標(biāo)
org.springframework.boot
spring-boot-starter-amqp
編寫yml配置,基本信息配置
定義交換機(jī),隊(duì)列以及綁定關(guān)系的配置類
注入RabbitTemplate,調(diào)用方法,完成消息發(fā)送
添加依賴
修改pom.xml文件內(nè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 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.1.4.RELEASE</version> </parent> <groupId>com.itheima</groupId> <artifactId>springboot-rabbitmq-consumer</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> </dependencies> </project>
啟動類
package com.itheima.rabbitmq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class); } }
配置RabbitMQ
創(chuàng)建application.yml,內(nèi)容如下:
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /itcast
username: heima
password: heima
消息監(jiān)聽處理類
編寫消息監(jiān)聽器com.itheima.rabbitmq.listener.MyListener
package com.itheima.rabbitmq.listener; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MyListener { /** * 監(jiān)聽某個隊(duì)列的消息 * @param message 接收到的消息 */ @RabbitListener(queues = "item_queue") public void myListener1(String message){ System.out.println("消費(fèi)者接收到的消息為:" + message); } }
測試
在生產(chǎn)者工程springboot-rabbitmq-producer中創(chuàng)建測試類,發(fā)送消息:
package com.itheima.rabbitmq; import com.itheima.rabbitmq.config.RabbitMQConfig; 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; @RunWith(SpringRunner.class) @SpringBootTest public class RabbitMQTest { @Autowired private RabbitTemplate rabbitTemplate; @Test public void test(){ rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.insert", "商品新增,routing key 為item.insert"); rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.update", "商品修改,routing key 為item.update"); rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE, "item.delete", "商品刪除,routing key 為item.delete"); } }
先運(yùn)行上述測試程序(交換機(jī)和隊(duì)列才能先被聲明和綁定),然后啟動消費(fèi)者;在消費(fèi)者工程springboot-rabbitmq-consumer中控制臺查看是否接收到對應(yīng)消息。
SpringBoot提供了快速整合RabbitMQ的方式
基本信息再yml中配置,隊(duì)列交互機(jī)以及綁定關(guān)系在配置類中使用Bean的方式配置
生產(chǎn)端直接注入RabbitTemplate完成消息發(fā)送
消費(fèi)端直接使用@RabbitListener完成消息接收
到此這篇關(guān)于SpringBoot整合消息隊(duì)列RabbitMQ的文章就介紹到這了,更多相關(guān)SpringBoot整合RabbitMQ內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 守護(hù)線程_動力節(jié)點(diǎn)Java學(xué)院整理
Java語言機(jī)制是構(gòu)建在JVM的基礎(chǔ)之上的,意思是Java平臺把操作系統(tǒng)的底層給屏蔽起來,所以它可以在它自己的虛擬的平臺里面構(gòu)造出對自己有利的機(jī)制,而語言或者說平臺的設(shè)計(jì)者多多少少是收到Unix思想的影響,而守護(hù)線程機(jī)制又是對JVM這樣的平臺湊合,于是守護(hù)線程應(yīng)運(yùn)而生2017-05-05淺談Springboot下引入mybatis遇到的坑點(diǎn)
這篇文章主要介紹了Springboot下引入mybatis遇到的坑點(diǎn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08相冊管理系統(tǒng)(Java表單+xml數(shù)據(jù)庫存儲)
這篇文章主要為大家詳細(xì)介紹了相冊管理系統(tǒng)的實(shí)現(xiàn)步驟,Java表單的文件上傳和下載,xml數(shù)據(jù)庫存儲信息,感興趣的小伙伴們可以參考一下2016-07-07解析如何用兩個棧來實(shí)現(xiàn)隊(duì)列的方法
本篇文章是對如何用兩個棧實(shí)現(xiàn)隊(duì)列的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06SpringBoot?2.5.5整合輕量級的分布式日志標(biāo)記追蹤神器TLog的詳細(xì)過程
分布式追蹤系統(tǒng)是一個最終的解決方案,如果您的公司已經(jīng)上了分布式追蹤系統(tǒng),這篇文章主要介紹了SpringBoot?2.5.5整合輕量級的分布式日志標(biāo)記追蹤神器TLog,需要的朋友可以參考下2022-10-10SpringAop切入點(diǎn)execution表達(dá)式的深入講解
Spring AOP 可能會經(jīng)常使用 execution切入點(diǎn)指示符,下面這篇文章主要給大家介紹了關(guān)于SpringAop切入點(diǎn)execution表達(dá)式的相關(guān)資料,需要的朋友可以參考下2021-08-08