每日一學:如何在 Spring Boot 整合 RabbitMQ ?
一、簡介
在Spring專案中,可以使用Spring-Rabbit去操作RabbitMQ
尤其是在 spring boot 專案中只需要引入對應的 amqp 啟動器依賴即可,方便的使用 RabbitTemplate 傳送訊息,使用註解接收訊息。
一般在開發過程中:
生產者工程:
-
application.yml 檔案配置 RabbitMQ 相關資訊;
-
在生產者工程中編寫配置類,用於建立交換機和佇列,並進行繫結
-
注入 RabbitTemplate 物件,透過 RabbitTemplate 物件傳送訊息到交換機
消費者工程:
-
application.yml 檔案配置 RabbitMQ 相關資訊
-
建立訊息處理類,用於接收佇列中的訊息並進行處理
二、搭建生產者工程
①建立工程
建立生產者工程
springboot-rabbitmq-producer
②新增依賴
修改 pom.xml 檔案內容為如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns=" xmlns:xsi=" xsi:schemaLocation=" <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-web</artifactId> </dependency> <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 ProducerApplication { public static void main(String[] args) { SpringApplication.run(ProducerApplication.class); } }
④配置RabbitMQ
配置檔案
建立
application.yml
,內容如下:
spring: rabbitmq: host: localhost port: 5672 virtual-host: /itcast username: caiyo password: caiyo
繫結交換機和佇列
建立 RabbitMQ 佇列與交換機繫結的配置類
com.itheima.rabbitmq.config.RabbitMQConfig
package com.itheima.rabbitmq.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 ITEM_TOPIC_EXCHANGE = "item_topic_exchange"; //佇列名稱 public static final String ITEM_QUEUE = "item_queue"; //宣告交換機 @Bean("itemTopicExchange") public Exchange topicExchange(){ return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build(); } //宣告佇列 @Bean("itemQueue") public Queue itemQueue(){ return QueueBuilder.durable(ITEM_QUEUE).build(); } //繫結佇列和交換機 @Bean public Binding itemQueueExchange(@Qualifier( "itemQueue") Queue queue, @Qualifier("itemTopicExchange") Exchange exchange){ return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs(); } }
⑤訊息傳送Controller
我們建立一個SpringMVC的Controller方便我們進行測試
package com.itheima.rabbitmq.controller; import com.itheima.rabbitmq.config.RabbitMQConfig; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * 傳送訊息的測試類 */ @RestController public class SendMsgController { //注入RabbitMQ的模板 @Autowired private RabbitTemplate rabbitTemplate; /** * 測試 */ @GetMapping("/sendmsg") public String sendMsg(@RequestParam String msg, @RequestParam String key){ /** * 傳送訊息 * 引數一:交換機名稱 * 引數二:路由key * 引數三:傳送的訊息 */ rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,key ,msg); //返回訊息 return "傳送訊息成功!"; } }
三、搭建消費者工程
①建立工程
建立消費者工程
springboot-rabbitmq-consumer
②新增依賴
修改 pom.xml 檔案內容為如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns=" xmlns:xsi=" xsi:schemaLocation=" <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-web</artifactId> </dependency> <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
建立 application.yml ,內容如下:
spring: rabbitmq: host: localhost port: 5672 virtual-host: /itcast username: caiyo password: caiyo
⑤訊息監聽處理類
編寫訊息監聽器
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 { /** * 監聽某個佇列的訊息 * @param message 接收到的訊息 */ @RabbitListener(queues = "item_queue") public void myListener1(String message){ System.out.println("消費者接收到的訊息為:" + message); } }
⑥測試
在生產者工程
springboot-rabbitmq-producer
中建立測試類,傳送訊息:
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
中控制檯檢視是否接收到對應訊息。
另外,也可以在RabbitMQ的管理控制檯中檢視到交換機與佇列的繫結:
喜歡這篇文章的朋友們可以關注個人簡介中的公眾號
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69964492/viewspace-2767675/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Spring Boot整合rabbitmqSpring BootMQ
- Spring Boot 整合 rabbitmqSpring BootMQ
- spring boot-整合RabbitMq(RabbitMq基礎)Spring BootMQ
- RabbitMQ(三):RabbitMQ與Spring Boot簡單整合MQSpring Boot
- spring-boot-route(十三)整合RabbitMQSpringbootMQ
- Spring Boot(十三)RabbitMQ安裝與整合Spring BootMQ
- Spring Boot系列十七 Spring Boot 整合 websocket,使用RabbitMQ做為訊息代理Spring BootWebMQ
- Spring boot學習(三) Spring boot整合mybatisSpring BootMyBatis
- Spring boot學習(四)Spring boot整合DruidSpring BootUI
- Spring Boot 整合 RabbitMQ 訊息事務(消費者)Spring BootMQ
- Spring Boot 整合 RabbitMQ 傳送延時訊息Spring BootMQ
- RabbitMQ-Spring整合RabbitMQMQSpring
- 每日一學:如何在RabbitMQ中搭建Topics萬用字元模式MQ字元模式
- 如何在Spring Boot中實現整合測試?Spring Boot
- spring-boot快速入門學習筆記-整合JPA mybatis rabbitmq mongodb redisSpringboot筆記MyBatisMQMongoDBRedis
- 整合RabbitMQ&SpringMQSpring
- Spring boot學習(五)Spring boot整合Mybatis Generator以及PageHelperSpring BootMyBatis
- RabbitMQ入門到進階(Spring整合RabbitMQ&SpringBoot整合RabbitMQ)MQSpring Boot
- Spring Boot整合Spring BatchSpring BootBAT
- Spring Boot整合Spring AopSpring Boot
- Spring Boot整合Spring SecuritySpring Boot
- Spring Boot系列十九 Spring boot整合 swaggerSpring BootSwagger
- Spring Boot 2.0(八):Spring Boot 整合 MemcachedSpring Boot
- 小代學Spring Boot之整合MyBatisSpring BootMyBatis
- Spring Boot學習(一)——Spring Boot介紹Spring Boot
- Spring Boot 整合 KafkaSpring BootKafka
- Spring Boot 整合 MyBatisSpring BootMyBatis
- Spring Boot整合SwaggerSpring BootSwagger
- Spring Boot整合RedisSpring BootRedis
- Spring Boot 整合redisSpring BootRedis
- Spring Boot 整合 elasticsearchSpring BootElasticsearch
- Spring Boot 整合 dockerSpring BootDocker
- Spring Boot 整合 elkSpring Boot
- Spring Boot 整合 ApolloSpring Boot
- spring boot整合shiroSpring Boot
- spring boot整合jooqSpring Boot
- spring boot整合HadoopSpring BootHadoop
- Spring Boot整合SocketSpring Boot