SpringBoot整合RabbitMQ入门学习(一)
一、前言
本文使用SpringBoot整合了RabbitMQ,并给出极简的例子是来展示RabbitMQ的最常见和最基础的用法。
相关名词解释:
-
Exchange: 交换机,每个交换机可以有多个列队,每个交换机只能对应一个路由规则。
-
Queue: 消息队列,RabbitMQ中的消息都只能存储在Queue中,生产者生产消息并最终投递到Queue中,消费者可以从Queue中获取消息并消费。同一个队列中的消息只会被消费一次。
-
RoutingKey: 路由Key,消费发送者将消息发送出去时,一般会绑定一个路由key,路由key可以指定某些特定的消息接收者。
-
路由规则: RabbitMQ中有4中路由规则(fanout, direct, topic, headers)
-
fanout:fanout模式,广播模式,凡是绑定了这个路由器的所有队列,都会接收消息,无论有没有指定路由key,无差别接收。
-
direct:direct模式,完全匹配路由key,非完全匹配的key不会接收到消息。
-
topic: topic模式不同于direct的严格匹配,topic使用*,#可以做到模糊匹配
-
headers: 几乎不用,详细问度娘。
-
-
几种模式图解
二、Docker安装RabbitMQ
1. docker pull rabbitmq:3.7.7-management
2. docker run -d --name my-rabbitmq -p 5672:5672 -p 15672:15672 -v `pwd`/data:/var/lib/rabbitmq --hostname myRabbit -e RABBITMQ_DEFAULT_VHOST=my_vhost -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=123456 rabbitmq:3.7.7-management
3.可以使用浏览器打开web管理端:http://你的服务器IP地址:15672
账号:admin 密码:123456
三、引入依赖
<!--rabbitmq-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
四、SpringBoot配置RabbitMQ
##rabbitmq配置
rabbitmq:
host: 47.98.135.314
username: admin
password: 123456
virtual-host: my_vhost
port: 5672
五、RabbitMQ的使用
1、消息发送者
public class MessageSender {
@Resource
private RabbitTemplate rabbitTemplate;
@RequestMapping("/testFanout")
public void testRabbitMq1(){
rabbitTemplate.convertAndSend("Fanout-exchange","Fanout-key1","以Fanout模式发送消息");
}
@RequestMapping("/testDirect")
public void testRabbitMq2(){
rabbitTemplate.convertAndSend("Direct-exchange","Direct-key1","以Direct模式发送消息");
}
@RequestMapping("/testTopic")
public void testRabbitMq3(){
rabbitTemplate.convertAndSend("Topic-exchange","#","以Topic模式发送消息");
}
}
2、消息消费方
@Service
public class RabbitMqService {
/**
* @Description: 测试fanout模式
* @Param: [msg]
* @Date: 2020/1/10 0010
*/
//1.key完全匹配
@RabbitListener(bindings = @QueueBinding(
key = "Fanout-key1",
value = @Queue("Fanout-Queue1"),
exchange = @Exchange(value = "Fanout-exchange",type = "fanout")
))
public void testFanout1(String msg){
System.out.println("Fanout消费1[Key完全匹配]------>"+msg);
}
//2.key不匹配
@RabbitListener(bindings = @QueueBinding(
key = "Fanout-key222",
value = @Queue("Fanout-Queue2"),
exchange = @Exchange(value = "Fanout-exchange",type = "fanout")
))
public void testFanout2(String msg){
System.out.println("Fanout消费2[Key不匹配]------>"+msg);
}
//3.没有key
@RabbitListener(bindings = @QueueBinding(
value = @Queue("Fanout-Queue3"),
exchange = @Exchange(value = "Fanout-exchange",type = "fanout")
))
public void testFanout3(String msg){
System.out.println("Fanout消费3[没有key]------>"+msg);
}
/**
* @Description: 测试direct模式
* @Param: [msg]
* @Date: 2020/1/10 0010
*/
//1.完全匹配
@RabbitListener(bindings = @QueueBinding(
key = "Direct-key1",
value = @Queue("Direct-Queue1"),
exchange = @Exchange(value = "Direct-exchange",type = "direct")
))
public void testDirect1(String msg){
System.out.println("Direct消费1[完全匹配]------>"+msg);
}
//2.同一队列,key完全匹配的情况下,消息只会被消费一次
@RabbitListener(bindings = @QueueBinding(
key = "Direct-key1",
value = @Queue("Direct-Queue1"),
exchange = @Exchange(value = "Direct-exchange",type = "direct")
))
public void testDirect2(String msg){
System.out.println("Direct消费2[key匹配+队列匹配]------>"+msg);
}
//3.同一队列,key完不同,消息不会被消费
@RabbitListener(bindings = @QueueBinding(
key = "Direct-key2",
value = @Queue("Direct-Queue1"),
exchange = @Exchange(value = "Direct-exchange",type = "direct")
))
public void testDirect6(String msg){
System.out.println("Direct消费2[key匹配+队列不匹配]------>"+msg);
}
//4.不同队列,key完全匹配的情况下,不同队列都会消费
@RabbitListener(bindings = @QueueBinding(
key = "Direct-key1",
value = @Queue("Direct-Queue2"),
exchange = @Exchange(value = "Direct-exchange",type = "direct")
))
public void testDirect3(String msg){
System.out.println("Direct消费2[key匹配+队列不匹配]------>"+msg);
}
//5.key不同,消息不会被消费
@RabbitListener(bindings = @QueueBinding(
key = "Direct-key2",
value = @Queue("Direct-Queue1"),
exchange = @Exchange(value = "Direct-exchange",type = "direct")
))
public void testDirect4(String msg){
System.out.println("Direct消费2[key不匹配]------>"+msg);
}
/**
* @Description: 测试Topic模式
* @Param: [msg]
* @Date: 2020/1/10 0010
*/
@RabbitListener(bindings = @QueueBinding(
key = "Topic.#",
value = @Queue("T-Queue1"),
exchange = @Exchange(value = "Topic-exchange",type = "topic")
))
public void testTopic1(String msg){
System.out.println("Topic消费1[匹配模式Topic.xxx.xxx]------>"+msg);
}
@RabbitListener(bindings = @QueueBinding(
key = "Topic.*",
value = @Queue("T-Queue2"),
exchange = @Exchange(value = "Topic-exchange",type = "topic")
))
public void testTopic2(String msg){
System.out.println("Topic消费2[匹配模式Topic.xxx]------>"+msg);
}
}
3、测试结果
Fanout,用上面代码测试 ** /testFanout**
Direct,用上面代码测试 ** /testDirect**
Topic
测试:
测试: