文章目录
RabbitMQ——SpringBoot整合RabbitMQ的使用
1、环境搭建
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
编写配置文件
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: 121.199.53.150 #主机名
port: 5672 #rabbitmq应用访问端口号
username: ems #用户名和密码
password: ems
virtual-host: /ems #虚拟主机
RabbitTemplate 概述
RabbitTemplate 是 Spring-AMQP 依赖为我们提供的一种 RabbitMQ 消息模板,它与 RabbitMQ Server 建立了一种映射关系,我们只需要使用 Java 代码来对 RabbitTemplate 进行配置,就可以将我们应用程序中的数据发送到 RabbitMQ Server 中。
RabbitTemplate 提供了编辑消息、发送消息、发送消息前的监听、发送消息后的监听等消息制造和消息监听功能,可以让我们像操作原生 RabbitMQ API 那样在 Spring 中通过 RabbitTemplate 来操作消息并发送和监听消息,这就是 RabbitTemplate 的作用之处。
我们可以通俗的来这样理解 RabbitTemplate ,RabbitTemplate 是在建立连接之后,将我们应用程序中的数据发送到 RabbitMQ Server 中的。
2、第一种HelloWorld模型的使用
消息生产者的开发
@SpringBootTest(classes = SpringBootRabbitmqApplication.class)
@RunWith(SpringRunner.class) //@RunWith就是一个运行器
public class TestRabbit {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testHelloWorld(){
//参数一:队列名称 参数二:发送的消息
rabbitTemplate.convertAndSend("hello","hello world rabbitmq");
}
}
消息消费者的开发
@RabbitListener 和 @RabbitListener 的搭配使用
@Component
//@RabbitListener 表示这是消费者
//queuesToDeclare 声明队列,队列名称:hello,是否独占队列:否,是否自动删除:是,是否自动创建:是,如果参数二、三、四不显式声明,默认为true
//当autoDelete这个属性为 true 时,队列不存在就会出现异常
@RabbitListener(queuesToDeclare = @Queue(value = "hello",exclusive = "true",autoDelete = "false",durable = "true"))
public class Consumer {
//当@RabbitListener声明的队列有收到消息的时候,就交给 @RabbitHandler 的方法处理
@RabbitHandler
public void receive(String message){
System.out.println("message: "+ message);
}
}
测试
启动消息发送者测试:消费者接收到消息并执行了回调方法
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Icw5BmjY-1641970488449)(D:\图片\笔记图片\MQ\QQ截图20220112103923.png)]
3、第二种Work模型的使用
消息生产者的开发
@SpringBootTest(classes = SpringBootRabbitmqApplication.class)
@RunWith(SpringRunner.class) //@RunWith就是一个运行器
public class TestRabbit {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testWork(){
for (int i = 0; i < 20; i++) {
rabbitTemplate.convertAndSend("work","work rabbitmq");
}
}
}
}
消息消费者的开发
只使用 @RabbitListener 注解
@Component
public class ConsumerWork {
//消费者1
//使用 @RabbitListener 注解标记方法,当监听到声明的队列中有消息时则会进行接收并处理
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public void receive1(String message){
System.out.println("message1: "+ message);
}
//消费者2
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public void receive2(String message){
System.out.println("message2: "+ message);
}
}
测试
启动消息发送者测试:两个消费者循环接收到消息并执行了回调方法
4、第三种Fanout模型的使用
消息生产者的开发
@SpringBootTest(classes = SpringBootRabbitmqApplication.class)
@RunWith(SpringRunner.class) //@RunWith就是一个运行器
public class TestRabbit {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void Fanout(){
rabbitTemplate.convertAndSend("logs","","fanout广播 rabbitmq");
}
}
消息消费者的开发
@Component
public class FanoutConsumer {
@RabbitListener(bindings = {
@QueueBinding(//绑定队列和交换机
value = @Queue,//创建临时队列
exchange = @Exchange(value = "logs",type = "fanout"))})//交换机名称和类型
public void consumer1(String message){
System.out.println("Consumer1: "+message);
}
@RabbitListener(bindings = {
@QueueBinding(//绑定队列和交换机
value = @Queue,//创建临时队列
exchange = @Exchange(value = "logs",type = "fanout"))})//交换机名称和类型
public void consumer2(String message){
System.out.println("Consumer2: "+message);
}
}
测试
启动消息发送者测试:两个消费者都接收到所有消息并执行了回调方法
5、第四种Direct模型的使用
消息生产者的开发
@SpringBootTest(classes = SpringBootRabbitmqApplication.class)
@RunWith(SpringRunner.class) //@RunWith就是一个运行器
public class TestRabbit {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void DirectTest(){
rabbitTemplate.convertAndSend("logs_direct","info","direct路由模型 message");
}
}
消息消费者的开发
@Component
public class DirectConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "logs_direct",type = "direct"),
key = {"info","error","warning"}
)})
public void Consumer1(String message){
System.out.println("Consumer1 :"+ message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "logs_direct",type = "direct"),
key = "info")})
public void Consumer2(String message){
System.out.println("Consumer2 :"+ message);
}
}
测试
启动消息发送者测试:根据发送消息的路由,两个消费者都匹配路由并接收到消息执行了回调方法
6、第五种Topic模型的使用
消息生产者的开发
@SpringBootTest(classes = SpringBootRabbitmqApplication.class)
@RunWith(SpringRunner.class) //@RunWith就是一个运行器
public class TestRabbit {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void TopicTest(){
rabbitTemplate.convertAndSend("topics","user.save","topic模型 rabbitmq");
}
}
消息消费者的开发
@Component
public class TopicConsumer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "topics",type = "topic"),
key = "user.#")})//#匹配一个或多个单词,*只能匹配一个
public void Consumer1(String message){
System.out.println("Consumer1: " + message);
}
}
测试
启动消息发送者测试:根据发送消息的路由,消费者匹配路由并接收到消息执行了回调方法