上次说了交换机的四种绑定模式,后面会具体说到RabbitMQ的6种工作模式,分别是简单模式、工作队列模式、交换机模式、Routing转发模式、主题转发模式、RPC模式
这篇是简单模式。代码基于spring boot + JDK1.8,需要两个两个项目,springbootmqproducers(生产者)、springbootmq(消费者)
简单模式
望文生义,也就是RabbitMQ中最简单的一种,只适合demo级别的。应用的是DirectExchange(直连交换机)
生产者把消息放入队列,消费者获取消息。这个模式只有一个消费者,一个生产者,一个队列,只需要配置主机参数,其他参数使用默认即可通信
代码:
pom文件(生产者和消费者一样):(注意:如果引入jar包,但是在代码里面不能引入的,1重启IDEA,2更换springboot版本,我用的是2.1.4.RELEASE)
<?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 https://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>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.RabbitMQ</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入MQ包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:application.yml,具体安装MQ和环境就不消息说了,找度娘,需要下载的插件点连接
生产者
server:
port: 6060
spring:
application:
name: rabbimq-lyc-producers
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
消费者
server:
port: 8022
spring:
application:
name: rabbitmq-lyc-consumer #消费者名称
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
配置队列、交换机、绑定建,在生产者项目中配置
package com.lyc.springbootmqproducers.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 创建队列交给IOC管理
*/
@Configuration //@Configuration的注解是配置类.在配置类中写Bean.通过@Bean注解创建Bean对象.
public class DirectRabbitConfig {
//队列 起名:LycDirectQueue
@Bean
public Queue LycDirectQueue() {
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前连接有效
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
// return new Queue("lycDirectQueue",true,true,false);
//一般设置一下队列的持久化就好,其余两个就是默认false
return new Queue("lycDirectQueue",true);
}
//Direct交换机 起名:LycDirectExchange
@Bean
DirectExchange LycDirectExchange() {
return new DirectExchange("lycDirectExchange",true,false);
}
//绑定 将队列和交换机绑定, 并设置用于匹配键:lycDirectRouting
@Bean
Binding bindingDirect() {
return BindingBuilder.bind(LycDirectQueue()).to(LycDirectExchange()).with("lycDirectRouting");
}
}
创建消费者:
注意:发送者和接受者的Queue名称必须一致,否则不能接收,为什么呢?
让我们回忆一下上篇文章中说到的直接形式的交换机,生产者会生成一个routing key给了交换机,交换机做绑定,消费者用监听这个routing key,交换机中有带有routing key的消息,消费者就消息这条消息
package com.lyc.springbootmq.customer;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* 监听的队列名称 lycDirectQueue
*/
@Component
@RabbitListener(queues = "lycDirectQueue")//监听的队列名称 lycDirectQueue
public class DirectReceiver {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("消费者收到消息 : " + testMessage.toString());
}
}
创建请求:
利用convertAndSend方法发生消息
package com.lyc.springbootmqproducers.controller;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* 请求
*/
@RestController
@RequestMapping("rabbiMQDemo")
public class SendMessageController {
@Autowired
RabbitTemplate rabbitTemplate; //使用RabbitTemplate,这提供了接收/发送等等方法
@GetMapping("/sendDirectMessage")
public String sendDirectMessage() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "我是来自请求的数据,rabbiMQDemo/sendDirectMessage";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String,Object> map=new HashMap<>();
map.put("messageId",messageId);
map.put("messageData",messageData);
map.put("createTime",createTime);
//将消息携带绑定键值:lycDirectRouting 发送到交换机lycDirectExchange
rabbitTemplate.convertAndSend("lycDirectExchange", "lycDirectRouting", map);
return "ok";
}
}
测试,启动springbootmqproducers,打开RabbitMQ管理界面(http://localhost:15672),用postman请求localhost:6060/rabbiMQDemo/sendDirectMessage,我们会发现在Queues中出现一条名为lycDirectQueue的队列,Exchanges里面出现一条lycDirectExchange的交换机信息。
其中会发现队列中存在Ready为1,说明有一条消息存在,而且没有被消费(Ready:待消费的消息总数;Unacked:待应答的消息总数;Total:总数 Ready+Unacked)
那么我们现在启动消费者项目springbootmq,我们会发现在控制台打印了一段怎么写的话
说明消息被消费了,再看lycDirectQueue队列中,Ready为0,说明该条消息已经被消费
好了,简单模式已经说完了,是不是很简单呢?
小结:
1.在配置消费者时,需要监听队列名称,做全局匹配
@RabbitListener(queues = "lycDirectQueue")
2.利用RabbitTemplate 的convertAndSend方法给交换机绑定,并发送数据
rabbitTemplate.convertAndSend("lycDirectExchange", "lycDirectRouting", map);
3.RabbitMQ界面中,Queues中,有几个比较重要的参数信息
Ready:待消费的消息总数。
Unacked:待应答的消息总数。
Total:总数 Ready+Unacked。