1.交换机
生产者发送消息给交换机,由交换机转发消息给队列,交换机可以转发给所有绑定它的队列,也可以转发给符合路由规则的队列,交换机本身不会存储消息,如果没有绑定任何队列,消息就会丢失
- 发布订阅模型
发布订阅使用的交换机是Fanout交换机,也叫广播式交换机
广播式交换机:fanout交换器中没有路由键的概念,他会把消息发送到所有绑定在此交换器上面的队列- 路由模型
路由式交换机:direct交换器相对来说比较简单,匹配规则为:路由键完全匹配,消息就被投送到相关的队列- 主题模型
主题式交换机:topic交换器采用模糊匹配路由键的原则进行转发消息到队列中- 头部订阅
头部订阅(headers 交换机):headers没有路由键,是根据消息头部header的键值对进行匹配,可以完全匹配也可以匹配任意一对键值对
application.yml
spring:
rabbitmq:
port: 5672
host: 127.0.0.1
username: guest
password: guest
listener:
simple:
prefetch: 1
2.发布订阅
package com.yzm.rabbitmq_03.config;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitConfig {
public static final String QUEUE_A = "fanout_a";
public static final String QUEUE_B = "fanout_b";
public static final String FANOUT_EXCHANGE = "fanout.exchange";
@Bean
public Queue queueA() {
return new Queue(QUEUE_A);
}
@Bean
public Queue queueB() {
return new Queue(QUEUE_B);
}
/**
* 消息交换机配置
* 定义交换机direct exchange,绑定消息队列queue
* name:交换机名称
* durable:设置是否持久,设置为true则将Exchange存盘,即使服务器重启数据也不会丢失
* autoDelete:设置是否自动删除,当最后一个绑定到Exchange上的队列删除后,自动删除该Exchange,简单来说也就是如果该Exchange没有和任何队列Queue绑定则删除
* internal:设置是否是RabbitMQ内部使用,默认false。如果设置为 true ,则表示是内置的交换器,客户端程序无法直接发送消息到这个交换器中,只能通过交换器路由到交换器这种方式。
* <p>
* 广播式交换机:fanout交换器中没有路由键的概念,他会把消息发送到所有绑定在此交换器上面的队列中。
* 路由式交换机:direct交换器相对来说比较简单,匹配规则为:路由键匹配,消息就被投送到相关的队列
* 主题式交换机:topic交换器采用模糊匹配路由键的原则进行转发消息到队列中
*/
@Bean
public FanoutExchange fanoutExchange() {
return ExchangeBuilder.fanoutExchange(FANOUT_EXCHANGE).build();
}
/**
* 将消息队列和交换机进行绑定
* 交换机相当于Map容器,路由对应key,Queue对应value
* 发送消息时,指定交换机中的key就能将消息加入到对应的队列中
*/
@Bean
public Binding bindingA() {
return BindingBuilder.bind(queueA()).to(fanoutExchange());
}
@Bean
public Binding bindingB() {
return BindingBuilder.bind(queueB()).to(fanoutExchange());
}
}
生产者生产10条消息,发送给FanoutExchange,空字符串""表示不需要指定路由键
package com.yzm.rabbitmq_03.sender;
import com.yzm.rabbitmq_03.config.RabbitConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
public class Sender {
private final RabbitTemplate rabbitTemplate;
public Sender(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
@GetMapping("/fanout")
public void fanout() {
for (int i = 1; i <= 10; i++) {
String message = "Hello World ..." + i;
rabbitTemplate.convertAndSend(RabbitConfig.FANOUT_EXCHANGE, "", message);
System.out.println(" [ 生产者 ] Sent ==> '" + message + "'");
}
}
}
消费者A监听队列A,消费者B、B2监听队列B
package com.yzm.rabbitmq_03.receiver;
import com.yzm.rabbitmq_03.config.RabbitConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class Receiver {
private int count1 = 1;
private int count2 = 1;
private int count3 = 1;
@RabbitListener(queues = RabbitConfig.QUEUE_A)
public void receiveA(String message) throws InterruptedException {
System.out.println(" [ 消费者@A号 ] Received ==> '" + message + "'");
Thread.sleep(200);
System.out.println(" [ 消费者@A号 ] Dealt with:" + count1++);
}
@RabbitListener(queues = RabbitConfig.QUEUE_B)
public void receiveB(String message) throws InterruptedException {
System.out.println(" [ 消费者@B号 ] Received ==> '" + message + "'");
Thread.sleep(200);
System.out.println(" [ 消费者@B号 ] Dealt with:" + count2++);
}
@RabbitListener(queues = RabbitConfig.QUEUE_B)
public void receiveB2(String message) throws InterruptedException {
System.out.println(" [ 消费者@B2号 ] Received ==> '" + message + "'");
Thread.sleep(500);
System.out.println(" [ 消费者@B2号 ] Dealt with:" + count3++);
}
}
运行结果如下
生产者生产了10条消息发送给交换机,fanout交换机将消息分别转发给绑定它的队列A、B;
队列A、B拿到内容一样的10条消息,队列A只有一个消费者A,所以由消费者A处理所有消息
队列B有两个消费者B、B2,分配模式是按能力分配,所以消费者B处理的多
3.路由模型
@Configuration
public class RabbitConfig {
...
//队列
public static final String QUEUE_C = "direct_c";
public static final String QUEUE_D = "direct_d";
public static final String DIRECT_EXCHANGE = "direct.exchange";
//路由键
public static final String DIRECT_C = "direct.yzm";
public static final String DIRECT_D = "direct.admin";
public static final String DIRECT_D2 = "direct.root";
@Bean
public Queue queueC() {
return new Queue(QUEUE_C);
}
@Bean
public Queue queueD() {
return new Queue(QUEUE_D);
}
@Bean
public DirectExchange directExchange() {
return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).build();
}
@Bean
public Binding bindingC() {
return BindingBuilder.bind(queueC()).to(directExchange()).with(DIRECT_C);
}
// 队列D有两个路由键
@Bean
public Binding bindingD() {
return BindingBuilder.bind(queueD()).to(directExchange()).with(DIRECT_D);
}
@Bean
public Binding bindingD2() {
return BindingBuilder.bind(queueD()).to(directExchange()).with(DIRECT_D2);
}
}
@RestController
public class Sender {
...
@GetMapping("/direct")
public void direct() {
for (int i = 1; i <= 30; i++) {
String message = "Hello World ..." + i;
System.out.println(" [ 生产者 ] Sent ==> '" + message + "'");
if (i % 3 == 0) {
rabbitTemplate.convertAndSend(RabbitConfig.DIRECT_EXCHANGE, RabbitConfig.DIRECT_C, message);
} else if (i % 3 == 1) {
rabbitTemplate.convertAndSend(RabbitConfig.DIRECT_EXCHANGE, RabbitConfig.DIRECT_D, message);
} else {
rabbitTemplate.convertAndSend(RabbitConfig.DIRECT_EXCHANGE, RabbitConfig.DIRECT_D2, message);
}
}
}
}
@Component
public class Receiver {
...
@RabbitListener(queues = RabbitConfig.QUEUE_C)
public void receiveC(String message) throws InterruptedException {
System.out.println(" [ 消费者@C号 ] Received ==> '" + message + "'");
Thread.sleep(200);
System.out.println(" [ 消费者@C号 ] Dealt with:" + count1++);
}
@RabbitListener(queues = RabbitConfig.QUEUE_D)
public void receiveD(String message) throws InterruptedException {
System.out.println(" [ 消费者@D号 ] Received ==> '" + message + "'");
Thread.sleep(200);
System.out.println(" [ 消费者@D号 ] Dealt with:" + count2++);
}
@RabbitListener(queues = RabbitConfig.QUEUE_D)
public void receiveD2(String message) throws InterruptedException {
System.out.println(" [ 消费者@D2号 ] Received ==> '" + message + "'");
Thread.sleep(500);
System.out.println(" [ 消费者@D2号 ] Dealt with:" + count3++);
}
}
运行结果:
两个队列C、D,C有一个路由键,而D有两个路由键;
生产者生产30条消息发送给交换机,交换机根据路由键转发给队列,队列C分配10条消息,队列D分配20条消息;
同时队列D被两个消费者监听
4.主题模式(通配符模式)
@Configuration
public class RabbitConfig {
...
...
//队列
public static final String QUEUE_E = "topic_e";
public static final String QUEUE_F = "topic_f";
public static final String QUEUE_G = "topic_g";
public static final String TOPIC_EXCHANGE = "topic.exchange";
//两种特殊字符*与#,用于做模糊匹配,其中*用于匹配一个单词,#用于匹配多个单词(可以是零个)
public static final String TOPIC_E = "topic.yzm.*";
public static final String TOPIC_F = "topic.#";
public static final String TOPIC_G = "topic.*.yzm";
@Bean
public Queue queueE() {
return new Queue(QUEUE_E);
}
@Bean
public Queue queueF() {
return new Queue(QUEUE_F);
}
@Bean
public Queue queueG() {
return new Queue(QUEUE_G);
}
@Bean
public TopicExchange topicExchange() {
return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).build();
}
@Bean
public Binding bindingE() {
return BindingBuilder.bind(queueE()).to(topicExchange()).with(TOPIC_E);
}
@Bean
public Binding bindingF() {
return BindingBuilder.bind(queueF()).to(topicExchange()).with(TOPIC_F);
}
@Bean
public Binding bindingG() {
return BindingBuilder.bind(queueG()).to(topicExchange()).with(TOPIC_G);
}
}
@RestController
public class Sender {
...
...
@GetMapping("/topic")
public void topic() {
for (int i = 1; i <= 30; i++) {
String message = "Hello World ..." + i;
System.out.println(" [ 生产者 ] Sent ==> '" + message + "'");
if (i % 3 == 0) {
// topic.yzm.key,可以匹配 topic.yzm.* 和 topic.#
rabbitTemplate.convertAndSend(RabbitConfig.TOPIC_EXCHANGE, "topic.yzm.key", message);
} else if (i % 3 == 1) {
// topic.yzm.yzm,可以匹配 topic.yzm.* 、 topic.# 和 topic.*.yzm
rabbitTemplate.convertAndSend(RabbitConfig.TOPIC_EXCHANGE, "topic.yzm.yzm", message);
} else {
// topic.key.yzm,可以匹配 topic.# 和 topic.*.yzm
rabbitTemplate.convertAndSend(RabbitConfig.TOPIC_EXCHANGE, "topic.key.yzm", message);
}
}
}
}
@Component
public class Receiver {
...
...
@RabbitListener(queues = RabbitConfig.QUEUE_E)
public void receiveE(String message) throws InterruptedException {
System.out.println(" [ 消费者@E号 ] Received ==> '" + message + "'");
Thread.sleep(200);
System.out.println(" [ 消费者@E号 ] Dealt with:" + count1++);
}
@RabbitListener(queues = RabbitConfig.QUEUE_F)
public void receiveF(String message) throws InterruptedException {
System.out.println(" [ 消费者@F号 ] Received ==> '" + message + "'");
Thread.sleep(200);
System.out.println(" [ 消费者@F号 ] Dealt with:" + count2++);
}
@RabbitListener(queues = RabbitConfig.QUEUE_G)
public void receiveG(String message) throws InterruptedException {
System.out.println(" [ 消费者@G号 ] Received ==> '" + message + "'");
Thread.sleep(200);
System.out.println(" [ 消费者@G号 ] Dealt with:" + count3++);
}
}
分析E、F、G队列会收到的消息数量
运行结果:
5.头部订阅
@Configuration
public class RabbitConfig {
...
...
...
public static final String QUEUE_X = "headers_x";
public static final String QUEUE_Y = "headers_y";
public static final String HEADER_EXCHANGE = "headers.exchange";
@Bean
public Queue queueX() {
return new Queue(QUEUE_X);
}
@Bean
public Queue queueY() {
return new Queue(QUEUE_Y);
}
@Bean
public HeadersExchange headersExchange() {
return ExchangeBuilder.headersExchange(HEADER_EXCHANGE).build();
}
@Bean
public Binding bindingX() {
Map<String, Object> map = new HashMap<>();
map.put("key1", "value1");
map.put("name", "yzm");
// whereAll:表示完全匹配
return BindingBuilder.bind(queueX()).to(headersExchange()).whereAll(map).match();
}
@Bean
public Binding bindingY() {
Map<String, Object> map = new HashMap<>();
map.put("key2", "value2");
map.put("name", "yzm");
// whereAny:表示只要有一对键值对能匹配就可以
return BindingBuilder.bind(queueY()).to(headersExchange()).whereAny(map).match();
}
}
@Component
public class Sender {
...
...
...
@GetMapping("/headers")
public void headers() {
String s = "Hello World";
System.out.println(" [ 生产者 ] Sent ==> '" + s + "'");
MessageProperties messageProperties = new MessageProperties();
messageProperties.setHeader("key1", "value1");
messageProperties.setHeader("name", "yzm");
Message message = new Message(s.getBytes(StandardCharsets.UTF_8), messageProperties);
rabbitTemplate.convertAndSend(RabbitConfig.HEADER_EXCHANGE, "", message);
}
@GetMapping("/headers2")
public void headers2() {
String s = "Hello World";
System.out.println(" [ 生产者 ] Sent ==> '" + s + "'");
MessageProperties messageProperties = new MessageProperties();
messageProperties.setHeader("key3", "value3");
messageProperties.setHeader("name", "yzm");
Message message = new Message(s.getBytes(StandardCharsets.UTF_8), messageProperties);
rabbitTemplate.convertAndSend(RabbitConfig.HEADER_EXCHANGE, "", message);
}
}
@Component
public class Receiver {
...
...
...
@RabbitListener(queues = RabbitConfig.QUEUE_X)
public void receiveX(String message) {
System.out.println(" [ 消费者@X号 ] Received ==> '" + message + "'");
}
@RabbitListener(queues = RabbitConfig.QUEUE_Y)
public void receiveY(String message) {
System.out.println(" [ 消费者@Y号 ] Received ==> '" + message + "'");
}
}
http://localhost:8080/headers
运行结果:
生产消息时,头部键值对有:“key1”=“value1"和"name”=“yzm”,跟X队列能完全匹配上,跟Y队列能匹配上其中一个,所以两个消费者都能消费到消息
http://localhost:8080/headers2
运行结果:只有Y队列能匹配上一个键值对