Exchange Types
RabbitMQ常用的Exchange Type有fanout、direct、topic、headers这四种(AMQP规范里还提到两种Exchange Type,分别为system与自定义,这里不予以描述),下面分别进行介绍。
fanout
fanout类型的Exchange路由规则非常简单,它会把所有发送到该Exchange的消息路由到所有与它绑定的Queue中。
direct
direct类型的Exchange路由规则也很简单,它会把消息路由到那些binding key与routing key完全匹配的Queue中。
topic
topic也是将消息路由到binding key与routing key相匹配的Queue中,但它可以进行模糊匹配,它约定:
routing key为一个句点号“. ”分隔的字符串(我们将被句点号“. ”分隔开的每一段独立的字符串称为一个单词),如“stock.usd.nyse”、“nyse.vmw”、“quick.orange.rabbit”
binding key与routing key一样也是句点号“. ”分隔的字符串。
binding key中可以存在两种特殊字符“*”与“#”,用于做模糊匹配,其中“*”用于匹配一个单词,“#”用于匹配多个单词(可以是零个)。
exchange 的key叫 binding key ,发送消息方的key叫 routing key。
routing key:asd.ddd.sdfs 、asd.ddd.dssd.dsd 都可以匹配上 binding key:asd.ddd.#
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置:
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
config:
package com.example.demo.rabbitmq;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* MQ配置
* Created by admin on 2018/8/2.
*/
@Configuration
public class TopicRabbitConfig {
public static final String TEST_QUEUE1="topic.msg";
public static final String TEST_QUEUE2="topic.msg21";
/**
* 创建一个队列,名称为topic.msg
* @return
*/
@Bean
public Queue queue1(){
return new Queue(TEST_QUEUE1);
}
/**
* 创建一个队列,名称为topic.msg21
* @return
*/
@Bean
public Queue queue3(){
return new Queue(TEST_QUEUE2);
}
/**
* 穿件topic类型的exchange exchange1
* @return
*/
@Bean
TopicExchange topicExchange(){
return new TopicExchange("exchange1");
}
/**
* 创建一个direct类型的exchange exchange2
* @return
*/
@Bean
DirectExchange directExchange(){
return new DirectExchange("exchange2");
}
/**
* 将队列与exchange绑定
* @param queue1 队列bean名称,当有多个beans时,名称必需对应上面的队列的bean的名称
* @param topicExchange 当有个多个同类型exchange时,参数名必需与bean对应
* @return
*/
@Bean
Binding bindingExchangemsg(Queue queue1,TopicExchange topicExchange){
return BindingBuilder.bind(queue1).to(topicExchange).with("topic.msg");
}
@Bean
Binding bindingExchangemsgs(Queue queue3,DirectExchange directExchange){
return BindingBuilder.bind(queue3).to(directExchange).with("topic.#");
}
}
消息监听(收消息):
package com.example.demo;
import com.example.demo.rabbitmq.TopicRabbitConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
/**
* MQ收消息(不自动创建队列)
* Created by Administrator on 2018/7/27.
*/
@Component
public class MqReceiver {
@RabbitListener(queues = TopicRabbitConfig.TEST_QUEUE1)
public void process1(String message){
System.out.println("MqReceiver"+TopicRabbitConfig.TEST_QUEUE1+":"+message);
}
@RabbitListener(queues = TopicRabbitConfig.TEST_QUEUE2)
public void process2(String message){
System.out.println("MqReceiver"+TopicRabbitConfig.TEST_QUEUE2+":"+message);
}
}
收消息自动创建队列:
package com.gato.common.rabbitmq;
import com.gato.common.webSocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**接受消息自动创建队列
* Created by Administrator on 2018/7/27.
*/
@Component
@Slf4j
public class MqReceiver {
@Autowired
private WebSocketServer webSocketServer;
public static final String MQ_NAME="myQueue1";
@RabbitListener(queuesToDeclare = @Queue(MQ_NAME))
public void process(String message){
System.out.println("MqReceiver:"+message);
}
/**
* 监听topic 类型exchange名为myChatRoom ,binding key 为room,队列为 myQueue 的队列消息
* 队列,exchange不存在时会自动创建
* @param message
*/
@RabbitListener(bindings = @QueueBinding(
exchange = @Exchange(value = "myChatRoom",type = "topic"),
key = "room",
value = @Queue("myQueue")
))
public void processChatRoom1(String message){
System.out.println(message);
}
}
发送消息:
package com.example.demo.rabbitmq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by admin on 2018/8/2.
*/
@RestController
public class SendMq {
@Autowired
private AmqpTemplate amqpTemplate;
@RequestMapping("/send")
public void send(String msg){
/**
* s:exchange 名称
* s1:routing key ,匹配binding key
* msg ,要发送的消息
*/
amqpTemplate.convertAndSend("exchange1","topic.msg",msg);
amqpTemplate.convertAndSend("exchange2","topi1c.msg",msg);
}
}