在Spring專案中,可以使用Spring-Rabbit去操作RabbitMQ
尤其是在spring boot專案中只需要引入對應的amqp啟動器依賴即可,方便的使用RabbitTemplate發送訊息,使用註解接收訊息。
一般在開發過程中:
生產者工程:
#application.yml檔案設定RabbitMQ相關資訊;
#在生產者工程中編寫組態類,用於建立交換器和佇列,並進行綁定
#注入RabbitTemplate對象,透過RabbitTemplate物件傳送訊息到交換器
消費者工程:
application.yml檔案設定RabbitMQ相關資訊
建立訊息處理類,用於接收佇列中的消息並進行處理
新增依賴修改pom.xml檔案內容為如下:#1. 建立生產者SpringBoot工程(maven)
3. 編寫yml配置,基本資訊配置
2. 引入start,依賴座標
org.springframework.boot< )Id> p
< /dependency>4. 定義交換機,佇列以及綁定關係的組態類別
5. 注入RabbitTemplate,呼叫方法,完成訊息傳送
<?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); } }
spring:綁定交換器與佇列rabbitmq:
host: localhost
port: 5672
virtual-hosthost: / itcast
username: heima
password: heima
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(); } }
搭建消費者工程
1. 創建生產者SpringBoot工程
2.引入start,依賴座標
org.springframework.boot
spring-boot-starter-amqp
編寫yml配置,基本資訊配置
定義交換機,佇列以及綁定關係的組態類別注入RabbitTemplate,呼叫方法,完成訊息傳送
新增依賴
<?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:host: localhost
消息監聽處理類
port: 5672
virtual-host: /itcast
username: heima
password: heima
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); } }
測試
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完成訊息發送
消費端直接使用@RabbitListener完成訊息接收
以上是SpringBoot整合訊息佇列RabbitMQ的方法是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!