在安装完RabbitMQ之后,打开网址就可以看到RabbitMQ的简单管理页面。
可以手动创建虚拟HOST,创建用户,分配权限,创建交换机,创建队列等等,还可以查看队列消息,消息效率,推送效率等等。
以上这些管理界面的操作暂时不做扩展描述,基本上用到的不是很多。
首先先介绍一个简单的一个消息推动到接收的流程,提供一个简单的图:
常用的交换机有一下三种,因为消费者是从队列中获取信息的,队列是绑定交换机的(一般),所以对应的消息推送/接收模式也会有一下几种:
Direct Exchange
直连型交换机,根据消息携带的路由键将消息投递给对应的队列。
大致流程,有一个队列绑定到一个直连交换机上,同时赋予一个路由键routing key。
然后当一个消息携带着路由值为X,这个消息通过生产者发送给交换机时,交换机就会根据这个路由值X去寻找绑定值也是X的队列。
Fanout Exchange
扇形交换机,这个交换机没有路由键概念,就算你绑定了路由键也是无视的,这个交换机在接收到消息后,会直接转发到绑定到它上面的所有队列。
Topic Exchange
主题交换机,这个交换机其实跟直连交换机差不多,但是它的特点就是在它的路由键和绑定键之间有有规则的。
简单的介绍一下规则:
* (星号)用来表示一个单词(必须出现的)
# 井号 用来表示任意数量(零个或者多个)单词
通配的绑定键是跟队列进行绑定的,举个例子:
队列Q1绑定键为*.TT.*,队列Q2绑定键为TT.#
如果一条消息携带的路由键围为A.TT.B,那么队列Q1将会收到;
如果一条消息携带的路由键为TT.AA.BB,那么队列Q2将会收到;
主题交换机是非常强大的,为啥这么膨胀?
当一个队列的绑定键为“#”的时候,这个队列将会无视消息的路由键,接收所有的消息。
当*和#这两个特殊字符都未在绑定键中出现的时候,此时主题交换机就拥有直连交换机的行为。
所以主题交换机也就是实现了扇形交换机的功能和直连交换机的功能。
另外还有Header Exchange头交换机,Default Exchange默认交换机,Dead Letter Exchange死信交换机,这几个该篇暂时不做讲述。
好了,一些简单的介绍就到这里,接下来我们一些看代码。
这里通过SpringBoot的项目来看看RabbitMQ的用法。原博客作者创建了两个SpringBoot的项目,一个作为生产者,一个作为消费者。我这里为了省事,就创建一个项目。
具体代码链接
pom.xml里面用到的jar依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
然后application.yml
ps:里面的虚拟host配置项不是必须的
server:
port: 9001
spring:
freemarker:
template-loader-path: classpath:/templates
suffix: .ftl
content-type: text/html
charset: UTF-8
cache: false
application:
name: ElementTest
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/school?serverTimezone=GMT%2B8&characterEncoding=UTF-8
username: root
password: logan123
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
# 消息确认配置项
# 确认消息已发送到交换机(Exchange)
# publisher-confirms: true(已无效)
publisher-confirm-type: correlated
# 确认消息已发送到队列(Queue)
publisher-returns: true
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.work.elementtest.entity
logging:
file:
path: D:/ElementTestLog/
name: ElementTest.log
config: classpath:loggback.xml
接着我们先使用一下direct exchange(直连型交换机),创建DirectRabbitConfig.java(对于队列和交换机持久化以及链接使用设置,在注释里面有说明,后面的不同交换机的配置就不做同样说明了)。
package com.work.elementtest.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;
@Configuration
public class DirectRabbitConfig {
// 队列 起名:TestDirectQueue
@Bean
public Queue TestDirectQueue(){
// durable:是否持久化,默认是false,持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在,暂存队列:当前链接队列有效
// exclusive:默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列即被删除。此参考优先级高于durable。
// autoDelete:是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除。
// return new Queue("TestDirectQueue",true,true,false);
// 一般设疑一下队列持久化就好,其余两个就是默认false
return new Queue("TestDirectQueue",true);
}
// Direct交换机 起名:TestDirectExchange
@Bean
DirectExchange TestDirectExchange(){
return new DirectExchange("TestDirectExchange",true,false);
}
// 绑定 将队列和交换机绑定并设置用于匹配键:TestDirectRouting
@Bean
Binding bindingDirect(){
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
@Bean
DirectExchange lonelyDirectExchange(){
return new DirectExchange("lonelyDirectExchange");
}
}
然后写一个简单的接口用于消息推送(根据需求也可以改为定时任务等,具体看需求)
SendMessageController.java
package com.work.elementtest.web.mq;
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.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
public class SendMessageController {
@Autowired
RabbitTemplate rabbitTemplate;
@GetMapping("/sendDirectMessage")
public String sendDirectMessage(){
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "test message, hello!";
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);
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
return "ok";
}
}
把项目运行起来,调用接口
就可以发送消息了。