参考:https://www.cnblogs.com/Luwak90/p/9405563.html
一、使用Topic转发
1、配置config
package com.vicmob.vsmslog.rabbitmq.topicexchange;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author peter
*
* config (转发模式)
* 1、配置:对列、交换器
* 2、绑定 对列 到 交换器,设定buldingKey
*
* @version 1.1
* @date 2019/11/2 10:56
*/
@Configuration
public class TopicMQConfig {
@Bean(name="message")
public Queue queueMessage() {
return new Queue("topic.A"); //队列名
}
@Bean(name="messages")
public Queue queueMessages() {
return new Queue("topic.B"); //队列名
}
@Bean
public TopicExchange exchange() {
return new TopicExchange("topic-x"); //交换机名:类型是topic: 完全匹配routingkey 和 bindingkey,同时可以使用通配符:*、#
}
//topic.message队列以关键字topic.message绑定到exchange交换机上
@Bean
Binding bindingExchangeMessage(@Qualifier("message") Queue queueMessage, TopicExchange exchange) {
return BindingBuilder.bind(queueMessage).to(exchange).with("A.B");
}
@Bean
Binding bindingExchangeMessages(@Qualifier("messages") Queue queueMessages, TopicExchange exchange) {
return BindingBuilder.bind(queueMessages).to(exchange).with("A.#");//*表示一个词,#表示零个或多个词
}
}
2、配置监听类
*/
@Component
public class ListeneMethod {
@RabbitListener(queues="topic.A") //监听器监听指定的Queue
public void process1(String str) {
System.out.println("topic.A recive message:"+str);
}
@RabbitListener(queues="topic.B") //监听器监听指定的Queue
public void process2(String str) {
System.out.println("topic.B recive message:"+str);
}
}
3、配置发送类
@Component
public class SendMessage {
@Autowired
private AmqpTemplate amqpTemplate;
/**
* rutingKey ,必须和 bindingKey完全匹配;
* bindindKey 可以使用通配符; * 单个字,#多个字
* @param message
*/
public void execute(String message){
amqpTemplate.convertAndSend("topic-x","A.B",message);
}
}
4、测试发送
@Scheduled(cron = "0/5 * * * * ?")
private void configureTasks1() {
sendfanout.execute("peter send a topic message!!");
}
二、fanout广播交换机
同上,这里只贴出config配置和send配置。注:本地测试时,不能同时配置FanoutExchange 和 TopicExchange
@Configuration
public class FanoutMQConfig {
//1、创建队列
@Bean("A")
public Queue fanoutA(){
return new Queue("fanoutA") ;
}
@Bean("B")
public Queue fanoutB(){
return new Queue("fanoutB") ;
}
@Bean("C")
public Queue fanoutC(){
return new Queue("fanoutC") ;
}
//2、创建 广播交换机
@Bean
public FanoutExchange exchange(){
return new FanoutExchange("fanout-x");
}
//3、队列绑定到交换机
@Bean
Binding bindingExchangeA(@Qualifier("A") Queue fanoutA, FanoutExchange exchange) {
return BindingBuilder.bind(fanoutA).to(exchange);
}
@Bean
Binding bindingExchangeB(@Qualifier("B") Queue fanoutB,FanoutExchange exchange){
return BindingBuilder.bind(fanoutB).to(exchange);
}
@Bean
Binding bindingExchangeC(@Qualifier("C") Queue fanoutC,FanoutExchange exchange){
return BindingBuilder.bind(fanoutC).to(exchange);
}
@Component
public class SendFanoutMessage {
@Autowired
private AmqpTemplate amqpTemplate;
/**
* rutingKey ,必须和 bindingKey完全匹配;
* bindindKey 可以使用通配符; * 单个字,#多个字
* @param message
*/
public void execute(String message){
amqpTemplate.convertAndSend("fanout-x","ww",message);
}
}
总结:
1、fanout模式下,不要匹配rutingkey;
2、 topic模式下,需要rutingkey完全匹配,但是rutingkey可以使用通配符 * 和 # 代替;
3、两种模式都要使用交换机 Exchange
4、topic模式下,rutingkey在config类里配置