一、fanout模式
- 类型:fanout
- 特点:Fanout—发布与订阅模式,是一种广播机制,它是没有路由key的模式。
二、实现
1、引入相应的pom文件 pom.xml
<?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.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xpf</groupId>
<artifactId>rabbitmq-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rabbitmq-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!--rabbitmq依赖-->
<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>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2、配置文件 application.properties
server.port=8080
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin
spring.rabbitmq.virtual-host=/
spring.rabbitmq.host=192.168.199.20
spring.rabbitmq.port=5672
3、使用springboot写一个配置文件 RabbitMqConfiguration.java
(当然此配置类就是用来声明交换机类型以及交换机和队列的绑定关系的,将此配置类放在生产者中,或者放在消费者中都可以,或者两边都放。不管是生产者还是消费者,当生产者发送消息(或者消费者监听队列时),如果发现rabbitmq中没有所发送的交换机(或者监听的队列),都会根据此配置类自动创建交换机和队列以及绑定关系。但是一般建议下,放在消费者会更好一些,因为一般消费者在springboot系统启动时,就会开启监听,如果没有相应的rabbitmq连接有问题能在项目启动时就发现)
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMqConfiguration {
//1、声明注册fanout模式交换机 (生产者发送消息给rabbitmq,其实就是发送到交换机)
@Bean
public FanoutExchange fanoutExchange(){
return new FanoutExchange("fanout_order_exchange", true, false);
}
//2、声明队列 sms.fanout.queue、email.fanout.queue、duanxin.fanout.queue
@Bean
public Queue smsQueue(){
return new Queue("sms.fanout.queue", true);
}
@Bean
public Queue emailQueue(){
return new Queue("email.fanout.queue", true);
}
@Bean
public Queue duanxinQueue(){
return new Queue("duanxin.fanout.queue", true);
}
//3、完成绑定关系(队列绑定交换机)
@Bean
public Binding smsBinding(){
return BindingBuilder.bind(smsQueue()).to(fanoutExchange());
}
@Bean
public Binding emailBinding(){
return BindingBuilder.bind(emailQueue()).to(fanoutExchange());
}
@Bean
public Binding duanxinBinding(){
return BindingBuilder.bind(duanxinQueue()).to(fanoutExchange());
}
}
4、写一个生产者 FanoutOrderService.java
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class FanoutOrderService {
@Autowired
private RabbitTemplate rabbitTemplate;
/**
* 模拟用户下单,发送消息给下游系统
* @param user
* @param num
*/
public void makerOrder(String user, int num){
//1、查询库存是否有剩余
//2、保存订单
String orderId = UUID.randomUUID().toString();
System.out.println("订单生产成功:" + orderId);
//3、通过mq给下游系统发送消息(交换机的名字千万别写错,和上述配置类应相同)
String exchangeName = "fanout_order_exchange";
String routingkey = "";
rabbitTemplate.convertAndSend(exchangeName, routingkey, orderId);
System.out.println("完成");
}
}
5、到这一步就可以再写一个测试类来测试生产者发送消息
import com.xpf.rabbitmqspringboot.service.DirectOrderService;
import com.xpf.rabbitmqspringboot.service.FanoutOrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class RabbitmqSpringbootApplicationTests {
@Autowired
private FanoutOrderService fanoutOrderService;
@Autowired
private DirectOrderService directOrderService;
/**
* Fanout模式生成者发送消息
*/
@Test
void setFanoutOrderService() {
fanoutOrderService.makerOrder("用户1", 10);
}
}
发送完消息可以检查rabbitmq是否生成新交换机fanout_order_exchange和三个队列sms.fanout.queue、email.fanout.queue、duanxin.fanout.queue,以及是否有在三个队列看到一条刚刚发送的消息(因为是Fanout—发布与订阅模式,是一种没有路由key的广播机制,所有队列都能收到消息)
6、写一个消费者。(可以另启一个springboot项目,模拟另外的系统来消费我们发送的消息,也可像我一样在原项目编写,自己消费)
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @Author xpf
* @Date 2023/7/9 0:44
* @Version 1.0
* Fanout—发布与订阅模式消费者(广播机制)
*/
@Component
@RabbitListener(queues = "sms.fanout.queue")
public class smsFanoutConsumer {
@RabbitHandler
public void receiveMessage(String message){
System.out.println("接收到来自队列sms.fanout.queue消息订单的message是:" + message);
}
}
这里写了一个监听sms发送消息的消费者,剩下两个email和duanxin类似。
特别注意:如果生成者发送的消息是什么类型,消费者接收时,
public void receiveMessage(String message)方法中的参数应该也是相同的类型,否则会报错。比如这里的string类型,因为生产者发送的也是sting类型
直接启动会发现sms.fanout.queue队列中的消息被消费掉了