簡介
在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
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);
}
}
設定RabbitMQ設定文件 建立application.yml,內容如下:
spring:綁定交換器與佇列rabbitmq:
host: localhost
port: 5672
virtual-hosthost: / itcast
username: heima
password: heima
##已建立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(); } }
搭建消費者工程
創建工程
生產端
1. 創建生產者SpringBoot工程
2.引入start,依賴座標
org.springframework.boot
spring-boot-starter-amqp
編寫yml配置,基本資訊配置
定義交換機,佇列以及綁定關係的組態類別注入RabbitTemplate,呼叫方法,完成訊息傳送
新增依賴
#修改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
建立application.yml,內容如下:
spring:rabbitmq:host: localhost
消息監聽處理類
port: 5672
virtual-host: /itcast
username: heima
password: heima
編寫消息監聽器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中控制台查看是否接收到對應訊息。
SpringBoot提供了快速整合RabbitMQ的方式
基本資訊再yml中配置,佇列互動機以及綁定關係在組態類別中使用Bean的方式配置
生產端直接注入RabbitTemplate完成訊息發送
消費端直接使用@RabbitListener完成訊息接收
以上是SpringBoot整合訊息佇列RabbitMQ的方法是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具