rabbitmq工作模式詳解之路由模式-routing
嚴(yán)格按照消息的路由將消息發(fā)送至對應(yīng)的隊(duì)列咽弦,監(jiān)聽器監(jiān)聽各自的隊(duì)列消費(fèi)消息胁出,可以使用direct類型交換機(jī),使用topic類型交換機(jī)也可以實(shí)現(xiàn)全蝶,我這里還是使用topic寺枉,因?yàn)樗δ芎軓?qiáng)大
rabbitmq-路由模式.png
使用topic實(shí)現(xiàn)路由模式的時(shí)候要注意使用*和#通配符帶來的風(fēng)險(xiǎn)型凳,確保消息路由的準(zhǔn)確性
package com.gitee.small.config;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TopicRabbitConfig {
//綁定鍵
public final static String DOG = "topic.dog";
public final static String CAT = "topic.cat";
/**
* Queue構(gòu)造函數(shù)參數(shù)說明
* new Queue(SMS_QUEUE, true);
* 1. 隊(duì)列名
* 2. 是否持久化 true:持久化 false:不持久化
*/
@Bean
public Queue firstQueue() {
return new Queue(TopicRabbitConfig.DOG);
}
@Bean
public Queue secondQueue() {
return new Queue(TopicRabbitConfig.CAT);
}
@Bean
public TopicExchange exchange() {
return new TopicExchange("topicExchange");
}
/**
* 將firstQueue和topicExchange綁定,而且綁定的鍵值為topic.dog
* 這樣只要是消息攜帶的路由鍵是topic.dog,才會分發(fā)到該隊(duì)列
*/
@Bean(name = "binding.dog")
public Binding bindingExchangeMessage() {
return BindingBuilder.bind(firstQueue()).to(exchange()).with(DOG);
}
/**
* 將secondQueue和topicExchange綁定,而且綁定的鍵值為用上通配路由鍵規(guī)則topic.cat
*/
@Bean(name = "binding.cat")
public Binding bindingExchangeMessage2() {
return BindingBuilder.bind(secondQueue()).to(exchange()).with(CAT);
}
}
由于我之前一直是在用同一個交換機(jī)和隊(duì)列演示,所以在進(jìn)行路由模式演示之前要先將topic.cat隊(duì)列和topic.#路由解綁
手動解綁:登錄rabbitmq客戶端往弓,到exchanges標(biāo)簽下根據(jù)virtual host和交換機(jī)名稱找到對應(yīng)交換機(jī)蓄氧,再找到binging信息,點(diǎn)擊unbind手動解綁
rabbitmq手動解綁.png
rabbitAdmin.removeBinding自動解綁:
RabbitAdmin rabbitAdmin = new RabbitAdmin(rabbitTemplate);
// 怎么綁定的就怎么解綁
rabbitAdmin.removeBinding(BindingBuilder.bind(new Queue("topic.cat"))
.to(new TopicExchange("topicExchange")).with("topic.#"));
消息發(fā)送直接選擇rabbitTemplate即可
rabbitTemplate.convertAndSend("topicExchange", "topic.dog", "路由模式測試-dog");
rabbitTemplate.convertAndSend("topicExchange", "topic.cat", "路由模式測試-cat");
監(jiān)聽器示例
package com.gitee.small.rabbitmq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
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.stereotype.Component;
@Component
@Slf4j
public class RoutingRabbitReceiver {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "topic.dog"),
exchange = @Exchange(value = "bindingExchangeMessage", type = ExchangeTypes.TOPIC)
))
public void process(String msg) {
log.info("dog-收到消息:{}", msg);
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "topic.cat"),
exchange = @Exchange(value = "bindingExchangeMessage2", type = ExchangeTypes.TOPIC)
))
public void process2(String msg){
log.info("cat-收到消息:{}", msg);
}
}
結(jié)果示例
c.g.s.rabbitmq.RoutingRabbitReceiver : dog-收到消息:路由模式測試-dog
c.g.s.rabbitmq.RoutingRabbitReceiver : cat-收到消息:路由模式測試-cat