前几篇文章介绍了rabbitmq的原理介绍,springboot整合rabbitmq的direct模式和topic模式这篇文章我们来介绍一下Fanout广播模式
源码下载地址 https://gitee.com/v_soul/boot-rabbitmq-produce,https://gitee.com/v_soul/boot-rabbitmq-consumer
rabbitmq的其他内容我就不介绍了,如果有直接使用广播模式的小伙伴建议读下前面的原理和其他模式的介绍这样能加深你的理解。没有理解原理的技术应用只是行尸走肉。
其中的pom配置和application.properties配置相同。
FanoutRabbitConfig的bean配置
package com.rabbit.produce.config;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutRabbitConfig {
/**
* 定义队列名称
*/
final static String message = "topic.A";
final static String messages = "topic.B";
@Bean
public Queue queueA() {
return new Queue(FanoutRabbitConfig.message);
}
@Bean
public Queue queueB() {
return new Queue(FanoutRabbitConfig.messages);
}
@Bean
FanoutExchange exchange() {
return new FanoutExchange("FanoutExchange");
}
@Bean
Binding bindingExchangeMessage(@Qualifier("queueA")Queue queueMessage, FanoutExchange exchange) {
return BindingBuilder.bind(queueMessage).to(exchange);
}
@Bean
Binding bindingExchangeMessages(@Qualifier("queueB")Queue queueMessages, FanoutExchange exchange) {
return BindingBuilder.bind(queueMessages).to(exchange);
}
}
生产者发送消息配置
package com.rabbit.produce.controller;
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 rabbitTemplate;
public void send() {
String context = "hi, i am message all";
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("FanoutExchange","topic.1", context);
}
public void send1() {
String context = "hi, i am message 1";
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("FanoutExchange", "topic.messages", context);
}
public void send2() {
String context = "hi, i am messages 2";
System.out.println("Sender : " + context);
this.rabbitTemplate.convertAndSend("FanoutExchange", "topic.messages", context);
}
}
上面的代码童鞋们可能会有疑问了广播模式不是不需要onvertAndSend的第二个参数(routing_key)么,是的在广播模式下写上了routing_key也会被忽略的,那么我问什么添加呢,看下图
可能是使用版本的问题,如果我省略了第二个参数不写的话,默认的交换机位置会被自动认为routing_key参数,这样就会找不到交换机,所以上面的代码中第二个参数都存在,当然你也可以放个空串什么的,
消费者消息接受
package com.rabbit.produce.controller;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* @author : lqf
* @Description :
* @date : Create in 18:37 2018/9/26
*/
@Component
public class FanoutReceiver2 {
@RabbitListener(queues = "topic.B")
public void process(String message) {
System.out.println("TopicReceiverB : " + message);
}
@RabbitListener(queues = "topic.A")
public void process2(String message) {
System.out.println("FanoutReceiverA : " + message);
}
}
测试
package com.rabbit.produce;
import com.rabbit.produce.controller.FanoutSender;
import com.rabbit.produce.controller.HelloSender;
import com.rabbit.produce.controller.HelloSender2;
import com.rabbit.produce.controller.TopicSender;
import com.rabbit.produce.entrty.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BootRabbitmqProduceApplicationTests {
@Autowired
private FanoutSender fanoutSender;
@Test
public void topicExchange() throws Exception {
fanoutSender.send();
fanoutSender.send1();
fanoutSender.send2();
}
}
测试结果
TopicReceiverB : hi, i am messages 2
FanoutReceiverA : hi, i am message all
TopicReceiverB : hi, i am message 1
TopicReceiverB : hi, i am message all
FanoutReceiverA : hi, i am message 1
FanoutReceiverA : hi, i am messages 2
到这里springboot整合rabbitmq的4种模式我写了3种基本上就整合完了,其中有一种header exchange(头交换机)用法这种用法比较少用我就不在这里赘述了,感兴趣的小伙伴自行搜索吧。