SpringBoot+RabbitMQ实现广播模式
在广播模式下,所有绑定了该交换机的的消息队列都可以接收到该消息。
与之前的实现用接收多个主题不同(有符合要求的队列则会向其发送,所以有的会打印发送两次)。在广播模式下消息只需要发送一次。
RabbitMQ系列文章如下:
(RabbitMQ 一【转载】)windows10环境下的RabbitMQ安装步骤
https://blog.csdn.net/myQNUMNINE/article/details/120447514
(RabbitMQ 二)Springboot项目中使用RabbitMQ的相关依赖
https://blog.csdn.net/myQNUMNINE/article/details/120460073
(RabbitMQ 三)SpringBoot+RabbitMQ实现发送和接收队列(可接受基本数据类型和对象)
https://blog.csdn.net/myQNUMNINE/article/details/120053242
(RabbitMQ 四)SpringBoot+RabbitMQ实现用接收器接受多个主题【一条消息通过交换机发往多个消息队列】
https://blog.csdn.net/myQNUMNINE/article/details/120065706
(RabbitMQ 五)SpringBoot+RabbitMQ一个接收者监听多个消息队列
https://blog.csdn.net/myQNUMNINE/article/details/120096394
(RabbitMQ 六)SpringBoot+RabbitMQ实现广播模式
https://blog.csdn.net/myQNUMNINE/article/details/120095529
(RabbitMQ 七【完结】)SpringBoot+RabbitMQ实现消息队列延迟功能(使用RabbitMQ延迟插件实现)
https://blog.csdn.net/myQNUMNINE/article/details/120335005
一、配置fanout
配置广播模式的消息队列:创建一个名为RabbitmqConfigFanout的配置类,类上使用注解@Configuration表明这是一个配置类;通过@Bean注解使用注解的name属性来指定参数名,创建两个消息队列,其返回类型为Queue;同样地,通过@Bean注解创建一个广播模式的交换机,其返回类型为FanoutExchange.
具体代码如下:
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.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitmqConfigFanout {
/*创建两个队列*/
@Bean(name = "fanout.a")
public Queue queueA() {
return new Queue("fanout.a");
}
@Bean(name = "fanout.b")
public Queue queueB() {
return new Queue("fanout.b");
}
/*创建交换机*/
@Bean
public FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
/*绑定交换机,指定好Bean*/
@Bean
Binding bindingExchangA(@Qualifier("fanout.a") Queue queue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queue).to(fanoutExchange);
}
@Bean
Binding bindingExchangB(@Qualifier("fanout.b") Queue queue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(queue).to(fanoutExchange);
}
}
二、编写发送者类
创建一个发送者类,在类上使用组件注解@Component表明这是一个组件,如果不添加此注解,在使用@Autowired自动注入(自动装配)时,启动项目会报错误。在类的内部通过@Autowired自动注入SpringBoot自己的一个消息队列的模板(AmqpTemplate)。编写一个发送方法,使用模板AmqpTemplate的**convertAndSend()**方法发送消息。
具体代码如下:
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FanoutSender {
@Autowired
private AmqpTemplate amqpTemplate;
public void send() throws Exception{
String str = "Fanout";
System.out.println("广播模式发送者Sender:" + str);
/*因为使用是广播模式的交换机,所以不用具体绑定某个消息队列,为空即可*/
amqpTemplate.convertAndSend("fanoutExchange","",str);
}
}
三、编写接收者类
创建一个接收者类,类上使用注解@Component和RabbitMQ的监听注解@RabbitListener,其中@RabbitListener使用其属性queues绑定需要监听的队列。创建一个接收方法process(),方法上使用处理注解@RabbitHandler。
接收者A的具体代码如下:
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.a")
public class FanoutReceiverA {
@RabbitHandler
public void process(String message) {
System.out.println("接收者A-fanoutReceiver:" + message);
}
}
接收者B的具体代码如下:
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "fanout.b")
public class FanoutReceiverB {
@RabbitHandler
public void process(String message) {
System.out.println("接收者B-fanoutReceiver:" + message);
}
}
四、编写测试
测试的用例为了贴合Web应用,因此写在Controller层中。
在控制层创建一个类名叫:FanoutSendController,使用@ResController注解标注为MVC中的控制器层,并可以返回JSON、XML类型数据,使用@RequestMapping("/fanout")添加整个控制层的访问前置。
在类的内部使用注解@Autowired自动注入发送者类FanoutSender。
编写发送的接口方法。
测试用例的具体代码如下:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/fanout")
public class FanoutSendController {
@Autowired
private FanoutSender fanoutSender;
@GetMapping("/send")
public void fanoutSend() throws Exception {
fanoutSender.send();
}
}
总结
到这里RabbitMQ的三种交换机模式我们已经学习了其中两种,剩下的一个是DirectExchange精准绑定型交换机,这个感觉有TopicExchange就比较少用,而且使用方法和TopicExchange大体类似。所以不做具体的实施案例。