目錄
一、準(zhǔn)備
1、創(chuàng)建maven項(xiàng)目?編輯
2、引入依賴
3、創(chuàng)建配置文件
1.RabbitMQ配置文件
2.生產(chǎn)者項(xiàng)目配置文件
3.消費(fèi)者項(xiàng)目配置文件
二、生產(chǎn)者xml中文件創(chuàng)建隊(duì)列
三、生產(chǎn)者xml文件中創(chuàng)建交換機(jī)以及綁定隊(duì)列
1、創(chuàng)建交換機(jī)
2、綁定隊(duì)列?
四、消費(fèi)者xml文件中創(chuàng)建隊(duì)列消息監(jiān)聽(tīng)器
五、Spring實(shí)現(xiàn)RabbitMQ五大工作模式
1、簡(jiǎn)單模式
1.生產(chǎn)者
2.消費(fèi)者
2、work queues工作隊(duì)列模式
1.生產(chǎn)者
2.消費(fèi)者
3、pub/sub訂閱模式
1.生產(chǎn)者
2.消費(fèi)者
4、routing路由模式
1.生產(chǎn)者
2.消費(fèi)者
5、topics通配符模式
1.生產(chǎn)者
2.消費(fèi)者
一、準(zhǔn)備
1、創(chuàng)建maven項(xiàng)目
2、引入依賴
分別在兩個(gè)項(xiàng)目的pom.xml文件里引入依賴
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
</dependencies>
3、創(chuàng)建配置文件
1.RabbitMQ配置文件
在兩個(gè)項(xiàng)目的resources文件夾下創(chuàng)建RabbitMQ的配置文件后綴為yml或properties
創(chuàng)建好文件后將下面配置添加入文件?
rabbitmq.host=127.0.0.1 #IP
rabbitmq.port=5672 #端口
rabbitmq.username=guest #用戶名
rabbitmq.password=guest #密碼
rabbitmq.virtual-host= / #虛擬機(jī)名稱
2.生產(chǎn)者項(xiàng)目配置文件
在生產(chǎn)者項(xiàng)目的resources目錄下創(chuàng)建xml文件,該文件用于注入bean對(duì)象以及后續(xù)創(chuàng)建隊(duì)列、交換機(jī)等
在該文件添加下面代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--設(shè)置讀取連接RabbitMQ服務(wù)器的配置信息-->
<context:property-placeholder location="classpath:properties/rabbitmq.properties"/>
<!-- 創(chuàng)建rabbitmq connectionFactory -->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"/>
<rabbit:admin connection-factory="connectionFactory"/>
<!--注入rabbitTemplate后續(xù)獲取該對(duì)象發(fā)送消息-->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>
3.消費(fèi)者項(xiàng)目配置文件
與生產(chǎn)者項(xiàng)目類(lèi)似創(chuàng)建xml文件,在文件添加以下代碼
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
<!--加載配置文件-->
<context:property-placeholder location="classpath:properties/rabbitmq.properties"/>
<!-- 定義rabbitmq connectionFactory -->
<rabbit:connection-factory id="connectionFactory" host="${rabbitmq.host}"
port="${rabbitmq.port}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.virtual-host}"/>
<rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
</rabbit:listener-container>
</beans>
二、生產(chǎn)者xml中文件創(chuàng)建隊(duì)列
在生產(chǎn)者項(xiàng)目里我們創(chuàng)建了xml的配置文件,在該文件里我們可以通過(guò)標(biāo)簽去創(chuàng)建隊(duì)列
<rabbit:queue id="" name="" auto-declare="true" auto-delete="false" durable="true"></rabbit:queue>id? ? ? ? ? ? ? ? ? ? ? 表示bean的名稱
name? ? ? ? ? ? ? ? 表示隊(duì)列的名稱
auto-declare? ? ?表示如果當(dāng)服務(wù)器沒(méi)有該隊(duì)列時(shí)是否創(chuàng)建
durable? ? ? ? ? ? ?表示是否持久化到內(nèi)存
三、生產(chǎn)者xml文件中創(chuàng)建交換機(jī)以及綁定隊(duì)列
1、創(chuàng)建交換機(jī)
同樣在生產(chǎn)者項(xiàng)目里我們之前創(chuàng)建的xml文件里同樣我們也可以一個(gè)標(biāo)簽去創(chuàng)建交換機(jī)以及綁定隊(duì)列
<rabbit:fanout-exchange id="" name="" auto-declare="true" auto-delete="false" durable="true"> </rabbit:fanout-exchange>id? ? ? ? ? ? ? ? ? ? ? 表示bean的名稱
name? ? ? ? ? ? ? ? 表示交換機(jī)的名稱
auto-declare? ? ?表示如果當(dāng)服務(wù)器沒(méi)有該交換機(jī)時(shí)是否創(chuàng)建
durable? ? ? ? ? ? ?表示是否持久化到內(nèi)存
2、綁定隊(duì)列?
我們?cè)趧?chuàng)建的交換機(jī)標(biāo)簽里可以嵌套標(biāo)簽進(jìn)行綁定
<rabbit:fanout-exchange id="" name="" auto-declare="true" auto-delete="false" durable="true"> <rabbit:bindings> <rabbit:binding queue="test"></rabbit:binding> </rabbit:bindings> </rabbit:fanout-exchange>queue? ? 表示綁定的隊(duì)列名成
此處由于創(chuàng)建的交換機(jī)類(lèi)型是fanout廣播類(lèi)型不需要去配置路由,如果創(chuàng)建的direct交換機(jī)不止需要配置隊(duì)列名屬性,還需要配置路由屬性,如果是topic交換機(jī)則需要配置通配符?
<rabbit:queue id="test" name="test" auto-declare="true" auto-delete="false" durable="true"></rabbit:queue>
<rabbit:fanout-exchange id="" name="" auto-declare="true" auto-delete="false" durable="true">
<rabbit:bindings>
<rabbit:binding queue="test"></rabbit:binding>
</rabbit:bindings>
</rabbit:fanout-exchange>
<rabbit:direct-exchange id="" name="" durable="true" auto-delete="false" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="test" key="路由"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
<rabbit:topic-exchange id="" name="" auto-declare="true" auto-delete="false" durable="true">
<rabbit:bindings>
<rabbit:binding queue="test" pattern="*.error"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>
四、消費(fèi)者xml文件中創(chuàng)建隊(duì)列消息監(jiān)聽(tīng)器
同樣在消費(fèi)者創(chuàng)建的xml文件里我們路創(chuàng)建隊(duì)列監(jiān)聽(tīng)器容器來(lái)將不同隊(duì)列的消息映射到不同的類(lèi)
首先我們需要?jiǎng)?chuàng)建一個(gè)類(lèi)實(shí)現(xiàn)MessageListener接口實(shí)現(xiàn)onMessage方法
?然后將他注入spring容器
?在xml里此時(shí)我們可以通過(guò)標(biāo)簽創(chuàng)建監(jiān)聽(tīng)器
<rabbit:listener-container connection-factory="connectionFactory" auto-declare="true"> <rabbit:listener ref="testQueueListener" queue-names="test"></rabbit:listener> ????<rabbit:listener ref="" queue-names=""></rabbit:listener> ????……………… </rabbit:listener-container>ref? ? ? ? ? ? ? ? ? ? ?表示之前我們定義實(shí)現(xiàn)了MessageListener接口的監(jiān)聽(tīng)類(lèi)在spring容器里的id
queues-names? 表示該類(lèi)要監(jiān)聽(tīng)的隊(duì)列
后續(xù)需要再進(jìn)行添加,在該標(biāo)簽下可繼續(xù)進(jìn)行添加?
在之前創(chuàng)建的類(lèi)重寫(xiě)的方法里參數(shù)message.getBody()即可獲得隊(duì)列里消息
五、Spring實(shí)現(xiàn)RabbitMQ五大工作模式
五大工作模式在之前的文章提到,如果不了解可以查看【RabbitMQ】Rabbbit的六種工作模式以及代碼實(shí)現(xiàn)_1373i的博客-CSDN博客https://blog.csdn.net/qq_61903414/article/details/130156097?spm=1001.2014.3001.5501
1、簡(jiǎn)單模式
1.生產(chǎn)者
我們先要在生產(chǎn)者xml配置文件里創(chuàng)建一個(gè)隊(duì)列
然后在項(xiàng)目目錄下創(chuàng)建一個(gè)測(cè)試類(lèi)類(lèi)加注解讀取配置文件以及環(huán)境
在類(lèi)里注入RabbitTemplate對(duì)象,通過(guò)該對(duì)象的convertAndSend方法發(fā)送消息,由于我們使用的是簡(jiǎn)單模式,所以此處路由為隊(duì)列名
package com.itheima;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:producer.xml")
public class ProducerTest {
// 1.注入 RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testHelloWorld(){
// 2.發(fā)送消息
rabbitTemplate.convertAndSend("test","hello mq");
}
}
運(yùn)行代碼查看服務(wù)器?
2.消費(fèi)者
首先我們需要?jiǎng)?chuàng)建一個(gè)監(jiān)聽(tīng)類(lèi)實(shí)現(xiàn)MessageListener接口并重寫(xiě)onMessage方法
然后將這個(gè)類(lèi)注入spring里,在xml里創(chuàng)建監(jiān)聽(tīng)器容器將這個(gè)類(lèi)與隊(duì)列綁定去監(jiān)聽(tīng)隊(duì)列
然后創(chuàng)建一個(gè)測(cè)試類(lèi),與生產(chǎn)者相同加兩個(gè) 注解,然后在測(cè)試方法里死循環(huán)的去監(jiān)聽(tīng)隊(duì)列?
運(yùn)行代碼?
??
2、work queues工作隊(duì)列模式
工作隊(duì)列模式與上述相同,將生產(chǎn)者復(fù)制一份即可
1.生產(chǎn)者
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:producer.xml")
public class ProducerTest {
// 1.注入 RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testHelloWorld(){
// 2.發(fā)送消息
rabbitTemplate.convertAndSend("test","hello mq");
}
}
2.消費(fèi)者
創(chuàng)建兩個(gè)消費(fèi)者類(lèi)實(shí)現(xiàn)MessageListener接口重寫(xiě)onMessage方法然后將其注入Spring容器,在監(jiān)聽(tīng)容器將隊(duì)列與spring bean綁定即可
package com.example.rabbitmq.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class SpringQueueListener1 implements MessageListener {
@Override
public void onMessage(Message message) {
//打印消息
System.out.println("收到消息:" + new String(message.getBody()));
}
}
package com.example.rabbitmq.listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class SpringQueueListener2 implements MessageListener {
@Override
public void onMessage(Message message) {
//打印消息
System.out.println("收到消息:" + new String(message.getBody()));
}
}
<bean id="springQueueListener1" class="com.example.rabbitmq.listener.SpringQueueListener1"/>
<bean id="springQueueListener2" class="com.example.rabbitmq.listener.SpringQueueListener2"/>
<rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
<rabbit:listener ref="springQueueListener1" queue-names="test"></rabbit:listener>
<rabbit:listener ref="springQueueListener2" queue-names="test"></rabbit:listener>
</rabbit:listener-container>
3、pub/sub訂閱模式
訂閱模式與前兩種不同,訂閱模式需要使用到fanout類(lèi)型的交換機(jī),并且將隊(duì)列與之綁定,他的生產(chǎn)者在xml文件里需要去創(chuàng)建兩個(gè)隊(duì)列與fanout類(lèi)型的交換機(jī)并綁定,在發(fā)送消息時(shí)指定交換機(jī)名稱即可,而消費(fèi)者則與前者相同,只是需要修改指定監(jiān)聽(tīng)的隊(duì)列名
1.生產(chǎn)者
xml文件創(chuàng)建隊(duì)列與交換機(jī)
<rabbit:queue id="fQueue1" name="fQueue1" auto-declare="true" auto-delete="false" durable="true"></rabbit:queue>
<rabbit:queue id="fQueue2" name="fQueue2" auto-declare="true" auto-delete="false" durable="true"></rabbit:queue>
<rabbit:fanout-exchange id="fExchange" name="fExchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="fQueue1"></rabbit:binding>
<rabbit:binding queue="fQueue2"></rabbit:binding>
</rabbit:bindings>
</rabbit:fanout-exchange>
此時(shí)我們只需要在發(fā)送消息時(shí)指定交換機(jī)即可?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:producer.xml")
public class ProducerTest {
// 1.注入 RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testHelloWorld(){
// 2.發(fā)送消息
rabbitTemplate.convertAndSend("test","hello mq");
}
@Test
public void testFanout(){
rabbitTemplate.convertAndSend("fExchange","","hello fanout");
}
}
?運(yùn)行代碼
2.消費(fèi)者
消費(fèi)者與前面消費(fèi)者創(chuàng)建相同,只需修改對(duì)應(yīng)的監(jiān)聽(tīng)隊(duì)列名即可
4、routing路由模式
路由模式與訂閱模式相同都需要?jiǎng)?chuàng)建交換機(jī),路由模式需要?jiǎng)?chuàng)建direct類(lèi)型交換機(jī),且在綁定隊(duì)列時(shí)需要指定該隊(duì)列的路由key
1.生產(chǎn)者
xml文件--創(chuàng)建交換機(jī)
<rabbit:queue id="dQueue1" name="dQueue1" auto-declare="true"></rabbit:queue>
<rabbit:queue id="dQueue2" name="dQueue2" auto-declare="true"></rabbit:queue>
<rabbit:direct-exchange name="dExchange" id="dExchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="dQueue1" key="q1"></rabbit:binding>
<rabbit:binding queue="dQueue2" key="q2"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
發(fā)送消息時(shí)需要指定交換機(jī)以及路由
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:producer.xml")
public class ProducerTest {
// 1.注入 RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testHelloWorld(){
// 2.發(fā)送消息
rabbitTemplate.convertAndSend("test","hello mq");
}
@Test
public void testFanout(){
rabbitTemplate.convertAndSend("fExchange","","hello fanout");
}
@Test
public void testDirect(){
//2.發(fā)送消息
rabbitTemplate.convertAndSend("dExchange","q1","hello direct");
}
}
?運(yùn)行代碼查看控制臺(tái)
?2.消費(fèi)者
消費(fèi)者代碼與前面相同,只需修改監(jiān)聽(tīng)隊(duì)列名即可
5、topics通配符模式
整體代碼與路由模式類(lèi)似,他需要?jiǎng)?chuàng)建topic類(lèi)型交換機(jī)且配置通配符規(guī)則
1.生產(chǎn)者
xml文件創(chuàng)建交換機(jī)與隊(duì)列
<rabbit:queue id="tQueue1" name="tQueue1" auto-declare="true"></rabbit:queue>
<rabbit:queue id="tQueue2" name="tQueue2" auto-declare="true"></rabbit:queue>
<rabbit:topic-exchange name="tExchange" id="tExchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding pattern="A.*" queue="tQueue1"></rabbit:binding>
<rabbit:binding pattern="#.info" queue="tQueue2"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>
發(fā)送消息時(shí)需要填寫(xiě)匹配路由
@Test
public void testTopic() {
rabbitTemplate.convertAndSend("tExchange","A.ERROR","A系統(tǒng)的error錯(cuò)誤");
}
?運(yùn)行代碼查看控制臺(tái)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-810004.html
2.消費(fèi)者
消費(fèi)者代碼與前面相同,依舊只需要修改監(jiān)聽(tīng)隊(duì)列名即可文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-810004.html
到了這里,關(guān)于【RabbitMQ】Spring整合RabbitMQ、Spring實(shí)現(xiàn)RabbitMQ五大工作模式(萬(wàn)字長(zhǎng)文)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!