做开发的都可以看看!Spring整合rabbitmq全套教程,从入门到实战!
第一步:引入依赖
spring-context、spring-rabbit以及单元测试的相关依赖
<?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">
<parent>
<artifactId>MyStudy</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rabbitmq</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.amqp/spring-rabbit -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.2.11.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.7.RELEASE</version>
</dependency>
</dependencies>
</project>
编写rabbitmq的连接属性:
rabbitmq.properties
rabbitmq.host=127.0.0.1
rabbitmq.port=5672
rabbitmq.username=cjian
rabbitmq.password=111111
rabbitmq.virtual-host=vhost_cjian
第二步:编写生产者的配置文件
<?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:/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:admin connection-factory="connectionFactory"/>
<!--定义rabbitTemple对象-->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
<!--定义持久化队列,不存在则自动创建,不绑定交换机则绑定到默认交换机,默认交换机为direct,名字为”“,路由键为队列的名称
auto-declare:是否自动创建
-->
<!--简单队列
id:bean的名称
name:queue的名称
auto-declare:是否自动创建
durable:是否持久化
auto-delete:当最后一个消费者和该队列断开连接后,自动删除队列
-->
<rabbit:queue id="spring_queue" name ="spring_queue" auto-declare="true" />
<!--fanout类型的交换机,所有队列都可获得消息-->
<rabbit:queue id="spring_fanout_queue1" name = "spring_fanout_queue1" auto-declare="true"/>
<rabbit:queue id="spring_fanout_queue2" name = "spring_fanout_queue2" auto-declare="true"/>
<!--定义fanout类型交换机,并绑定队列-->
<rabbit:fanout-exchange id="spring_fanout_exchange" name ="spring_fanout_exchange" auto-declare="true">
<rabbit:bindings>
<rabbit:binding queue="spring_fanout_queue1"></rabbit:binding>
<rabbit:binding queue="spring_fanout_queue2"></rabbit:binding>
</rabbit:bindings>
</rabbit:fanout-exchange>
<!--direct类型-->
<rabbit:queue id="spring_direct_queue1" name="spring_direct_queue1" auto-delete="true"/>
<rabbit:queue id="spring_direct_queue2" name="spring_direct_queue2" auto-delete="true"/>
<rabbit:direct-exchange name="spring_direct_exchange" id="spring_direct_exchange" >
<rabbit:bindings>
<rabbit:binding queue="spring_direct_queue1" key="info"></rabbit:binding>
<rabbit:binding queue="spring_direct_queue2" key="error"></rabbit:binding>
<rabbit:binding queue="spring_direct_queue2" key="info"></rabbit:binding>
<rabbit:binding queue="spring_direct_queue2" key="warning"></rabbit:binding>
</rabbit:bindings>
</rabbit:direct-exchange>
<!--topic类型-->
<rabbit:queue id="spring_topic_queue1" name = "spring_topic_queue1" auto-declare="true" />
<rabbit:queue id="spring_topic_queue2" name = "spring_topic_queue2" auto-declare="true"/>
<rabbit:queue id="spring_topic_queue3" name = "spring_topic_queue3" auto-declare="true"/>
<rabbit:topic-exchange id="spring_topic_exchange" name="spring_topic_exchange">
<rabbit:bindings>
<!-- #:0或多个单词 ,*:刚好一个单词-->
<rabbit:binding pattern="cjian.#" queue="spring_topic_queue1"></rabbit:binding>
<rabbit:binding pattern="cjian.*" queue="spring_topic_queue2"></rabbit:binding>
<rabbit:binding pattern="test.#" queue="spring_topic_queue3"></rabbit:binding>
</rabbit:bindings>
</rabbit:topic-exchange>
</beans>
第三步:编写消费者的代码
package com.cjian.rabbitmq.spring_rabbit;
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;
/**
* @description:
* @author: cWX969834
* @time: 2021/1/22 16:36
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-producer.xml")
public class Producer {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSpringQueue() {
rabbitTemplate.convertAndSend("spring_queue", "hello spring rabbitmq");
}
@Test
public void testSpringFanoutQueue() {
rabbitTemplate.convertAndSend("spring_fanout_exchange","", "hello spring rabbitmq_fanout");
}
@Test
public void testSpringTopicQueue() {
//1接收
//rabbitTemplate.convertAndSend("spring_topic_exchange","cjian.name.age","hello spring rabbitmq_topic");
//1、2接收
rabbitTemplate.convertAndSend("spring_topic_exchange","cjian.hello","hello spring rabbitmq_topic");
}
}
生产者发送消息简单,主要就是配置文件需要配置
第四步:编写消费者的监听器类
这里简单实现下:除了类名不一样外,其他都大同小异了,就不一一贴了
单元测试类
package com.cjian.rabbitmq.spring_rabbit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @description:
* @author: CJ
* @time: 2021/1/25 10:17
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-rabbitmq-consumer.xml")
public class Consumer {
@Test
public void test(){
while(true){
}
}
}
监听器类
package com.cjian.rabbitmq.spring_rabbit;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* @description: 需要实现MessageListener接口,并重写onMessage方法
* @author: CJ
* @time: 2021/1/25 9:40
*/
public class SpringQueueListener implements MessageListener {
@Override
public void onMessage(Message message) {
System.out.println("简单队列:"new String(message.getBody()));
}
}
第五步:编写消费者的配置文件
<?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: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}"/>
<!--给spring容器注册监听器bean-->
<!--简单类型-->
<bean id = "springQueueListener" class="com.cjian.rabbitmq.spring_rabbit.SpringQueueListener"/>
<!--fanout-->
<bean id = "springFanoutListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringFanoutListener1"/>
<bean id = "springFanoutListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringFanoutListener2"/>
<!--direct-->
<bean id = "springDirectListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringDirectListener1"/>
<bean id = "springDirectListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringDirectListener2"/>
<!--topic-->
<bean id = "springTopicListener1" class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener1"/>
<bean id = "springTopicListener2" class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener2"/>
<bean id = "springTopicListener3" class="com.cjian.rabbitmq.spring_rabbit.SpringTopicListener3"/>
<!--绑定监听器与队列的关系-->
<rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
<!--简单类型-->
<rabbit:listener ref="springQueueListener" queue-names="spring_queue"/>
<!--fanout-->
<rabbit:listener ref="springFanoutListener1" queue-names="spring_fanout_queue1"/>
<rabbit:listener ref="springFanoutListener2" queue-names="spring_fanout_queue2"/>
<!--topic-->
<rabbit:listener ref="springTopicListener1" queue-names="spring_topic_queue1"/>
<rabbit:listener ref="springTopicListener2" queue-names="spring_topic_queue2"/>
<rabbit:listener ref="springTopicListener3" queue-names="spring_topic_queue2"/>
<!--direct 类型-->
<rabbit:listener ref="springDirectListener1" queue-names="spring_direct_queue1"/>
<rabbit:listener ref="springDirectListener2" queue-names="spring_direct_queue2"/>
</rabbit:listener-container>
</beans>
测试:
1.简单队列
发送消息:
收到消息:
2.fanout类型
收到消息:
3.direct类型
收到消息:
4.topic类型
收到消息: