目录
交换机:Exchange概念
RabbitMQ 消息传递模型的核心思想是: 生产者生产的消息从不会直接发送到队列。实际上,通常生产 者甚至都不知道这些消息传递传递到了哪些队列中。
相反,生产者只能将消息发送到交换机(exchange),交换机工作的内容非常简单,一方面它接收来 自生产者的消息,另一方面将它们推入队列。交换机必须确切知道如何处理收到的消息。是应该把这些消 息放到特定队列还是说把他们到许多队列中还是说应该丢弃它们。这就的由交换机的类型来决定。
Exchanges 的类型
直接(direct)
主题(topic)
标题(headers)
此模式使用较少
扇出(fanout)
绑定(bindings)
什么是 bingding 呢,binding 其实是 exchange 和 queue 之间的桥梁,它告诉我们 exchange 和那个队 列进行了绑定关系。比如说下面这张图告诉我们的就是 X 与 Q1 和 Q2 进行了绑定
springboot整合RabbitMQ
1.创建一个Springboot工程,修改pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
2.修改application.yml文件
spring:
application:
name: rabbitmq-springboot
rabbitmq:
host: 192.168.31.65
port: 5672
username: admin
password: 123
virtual-host: /
3.编写测试类和java文件
在Springboot框架中我们借助springboot封装好的RabbitTemplate进行对RabbitMQ的操作
一、简单模式
1.编写测试类模拟生产者
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class rabbitmqtest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSimple() {
rabbitTemplate.convertAndSend("hello", "hello world");
}
}
2.消费者
@Component
@RabbitListener(queuesToDeclare = @Queue(value = "hello"))
public class HelloCustomer {
@RabbitHandler
public void receive(String message) {
System.out.println("message=" + message);
}
}
3.结果
二、工作模式
1.编写测试类模拟生产者
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class rabbitmqtest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testworker() {
for (int i = 1; i <= 10; i++) {
rabbitTemplate.convertAndSend("work", "work模型");
}
}
}
2.消费者
@Component
public class workCustomer {
//消费者1
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive1(String message){
System.out.println("message1"+message);
}
//消费者2
@RabbitListener(queuesToDeclare = @Queue("work"))
public void receive2(String message){
System.out.println("message2"+message);
}
}
3.结果
三、订阅模式
1.编写测试类模拟生产者
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class rabbitmqtest {
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testFanout() {
rabbitTemplate.convertAndSend("logs", "", "fanout模型");
}
}
2.消费者
@Component
public class fanoutCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue, //创建临时队列
exchange = @Exchange(value = "logs", type = "fanout") //绑定交换机
)
})
public void receive1(String message) {
System.out.println("message1=" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "logs", type = "fanout")
)
})
public void receive2(String message) {
System.out.println("message2=" + message);
}
}
3.结果(与RoutingKey有关)
四、路由模式
1.编写测试类模拟生产者
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class rabbitmqtest {
@Autowired
private RabbitTemplate rabbitTemplate;
//route 路由模式
@Test
public void testRoute() {
rabbitTemplate.convertAndSend("directs", "info", "发送info的routingKey信息");
}
}
2.消费者
@Component
public class RouteCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "directs", type = "direct"),
key = {"info","error","warn"}
)
})
public void receive1(String message) {
System.out.println("message1=" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(value = "directs", type = "direct"),
key = {"error"}
)
})
public void receive2(String message) {
System.out.println("message2=" + message);
}
}
3.结果(与RoutingKey有关)
五、主题模式
1.编写测试类模拟生产者
@SpringBootTest(classes = RabbitmqSpringbootApplication.class)
@RunWith(SpringRunner.class)
public class rabbitmqtest {
@Autowired
private RabbitTemplate rabbitTemplate;
//topic 主题模式
@Test
public void testTopic() {
rabbitTemplate.convertAndSend("topics", "user.save", "发送user.save的路由信息");
}
}
2.消费者
@Component
public class TopicCustomer {
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type = "topic", value = "topics"),
key = {"user.save", "user.*"}
)
})
public void receive1(String message) {
System.out.println("message1=" + message);
}
@RabbitListener(bindings = {
@QueueBinding(
value = @Queue,
exchange = @Exchange(type = "topic", value = "topics"),
key = {"order.#", "user.*"}
)
})
public void receive2(String message) {
System.out.println("message2=" + message);
}
}
3.结果(与RoutingKey有关)