SpringBoot整合消息隊列RabbitMQ
簡介
在Spring項目中,可以使用Spring-Rabbit去操作RabbitMQ
https://github.com/spring-projects/spring-amqp
尤其是在spring boot項目中只需要引入對應的amqp啟動器依賴即可,方便的使用RabbitTemplate發(fā)送消息,使用注解接收消息。
一般在開發(fā)過程中:
生產者工程:
- application.yml文件配置RabbitMQ相關信息;
- 在生產者工程中編寫配置類,用于創(chuàng)建交換機和隊列,并進行綁定
- 注入RabbitTemplate對象,通過RabbitTemplate對象發(fā)送消息到交換機
消費者工程:
- application.yml文件配置RabbitMQ相關信息
- 創(chuàng)建消息處理類,用于接收隊列中的消息并進行處理
生產端
1. 創(chuàng)建生產者SpringBoot工程(maven)
2. 引入start,依賴坐標
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>3. 編寫yml配置,基本信息配置
4. 定義交換機,隊列以及綁定關系的配置類
5. 注入RabbitTemplate,調用方法,完成消息發(fā)送
添加依賴
修改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> <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,內容如下:
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /itcast
username: heima
password: heima
綁定交換機和隊列
創(chuàng)建RabbitMQ隊列與交換機綁定的配置類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"; // 交換機 @Bean("bootExchange") public Exchange bootExchange(){ // 構建交換機對象 return ExchangeBuilder.topicExchange(EXCHAGE_NAME).durable(true).build(); } //Queue 隊列 @Bean("bootQueue") public Queue bootQueue(){ return QueueBuilder.durable(QUEUE_NAME).build(); } //隊列和交換機的關系 Binding /** * 1 知道那個隊列 * 2 知道那個交換機 * 3 routingKey */ @Bean public Binding bindQueueExchange(@Qualifier("bootQueue") Queue queue, @Qualifier("bootExchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("boot.#").noargs(); } }
搭建消費者工程
創(chuàng)建工程
生產端
1. 創(chuàng)建生產者SpringBoot工程
2. 引入start,依賴坐標
org.springframework.boot
spring-boot-starter-amqp
編寫yml配置,基本信息配置
定義交換機,隊列以及綁定關系的配置類
注入RabbitTemplate,調用方法,完成消息發(fā)送
添加依賴
修改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> <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,內容如下:
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)聽某個隊列的消息 * @param message 接收到的消息 */ @RabbitListener(queues = "item_queue") public void myListener1(String message){ System.out.println("消費者接收到的消息為:" + message); } }
測試
在生產者工程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"); } }
先運行上述測試程序(交換機和隊列才能先被聲明和綁定),然后啟動消費者;在消費者工程springboot-rabbitmq-consumer中控制臺查看是否接收到對應消息。
SpringBoot提供了快速整合RabbitMQ的方式
基本信息再yml中配置,隊列交互機以及綁定關系在配置類中使用Bean的方式配置
生產端直接注入RabbitTemplate完成消息發(fā)送
消費端直接使用@RabbitListener完成消息接收
到此這篇關于SpringBoot整合消息隊列RabbitMQ的文章就介紹到這了,更多相關SpringBoot整合RabbitMQ內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot?2.5.5整合輕量級的分布式日志標記追蹤神器TLog的詳細過程
分布式追蹤系統(tǒng)是一個最終的解決方案,如果您的公司已經上了分布式追蹤系統(tǒng),這篇文章主要介紹了SpringBoot?2.5.5整合輕量級的分布式日志標記追蹤神器TLog,需要的朋友可以參考下2022-10-10