四大交換機作業原理及實戰應用
- 交換機概念
- direct 直連交換機
- 作業模式圖解
- springboot代碼
- Fanout扇出交換機
- 作業模式圖解
- springboot代碼
- Topic主題交換機
- 作業模式圖解
- springboot代碼
- header交換機
交換機概念
交換機可以理解成具有路由表的路由程式,僅此而已,每個訊息都有一個稱為路由鍵(routing key)的屬性,就是一個簡單的字串,
最新版本的RabbitMQ有四種交換機型別,分別是Direct exchange、Fanout exchange、Topic exchange、Headers exchange,
交換機的作用: 生產者向broker(rabbitmq服務器)發送訊息,交換機通過生產者系結的路由鍵,將訊息推送到不同的訊息佇列中,而消費者,只系結佇列,從佇列中獲取訊息,
direct 直連交換機
作業模式圖解
生產者發送的訊息;交換機會根據不同的路由鍵,發送到對應的佇列;

springboot代碼
MQ配置類,宣告交換機、佇列,路由鍵系結
/**
* 宣告交換機、佇列、路由鍵系結
* /
@Configuration
puvlic class RabbitConfig {
/**
* 創建直連交換機
*/
@Bean
public DirectExchange createExchange() {
// 交換機名字;是否持久化;是否自動洗掉
return new DirectExchange("testE", true, false);
}
/**
* 創建佇列
*/
@Bean
public Queue createQueue() {
// 交換機名字;是否持久化;是否自動洗掉
return new Queue ("testQ", true, false, false);
}
/**
* 通過路由鍵系結交換機和佇列
*/
@Bean
public Binding createBinding() {
// 交換機名字;是否持久化;是否自動洗掉
return BindingBuilder
.bind(this.createQueue())
.to(this.createExchange())
.with("testR");
}
}
生產者
/**
* 訊息生產者
*/
@Service
public class ProduceMsg {
@Autowire
private RabbitTemplate rabbitTemplate;
public void sendMsg(Object msg){
// 訊息唯一標識
CorrelationData correlationData= new CorrelationData();
correlationData.setId(msg.getId());
rabbitTemplate.converAndSend("testE", "testR", msg, correlationData);
}
}
消費者
@Conponent
public class ConsumeMsg {
/**
* 消費者監聽佇列
*/
@RabbitListener(queues = "testQ")
public void sendMsg(String msg){
log.info("接收到訊息:{}", msg);
// ......業務邏輯消費訊息;
}
}
Fanout扇出交換機
作業模式圖解
生產者發送到交換機的訊息;會發送到系結到該交換機的所有佇列
springboot代碼
MQ配置類,宣告交換機、佇列,系結
/**
* RabbitMQ配置類
*/
@Configuration
public class RabbitMqConfig {
@Bean
public Queue fanoutQueueA()
{
return new Queue("queueA", true, false, false);
}
@Bean
public Queue fanoutQueueB()
{
return new Queue("queueB", true, false, false);
}
@Bean
public Queue fanoutQueueC()
{
return new Queue("queueC", true, false, false);
}
@Bean
FanoutExchange fanoutExchange()
{
return new FanoutExchange("exchangeFanout");
}
@Bean
Binding bindingExchangeA()
{
return BindingBuilder.bind(fanoutQueueA()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeB()
{
return BindingBuilder.bind(fanoutQueueB()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeC()
{
return BindingBuilder.bind(fanoutQueueC()).to(fanoutExchange());
}
}
生產者
/**
* 訊息生產者
*/
@Service
public class ProduceMsg {
@Autowire
private RabbitTemplate rabbitTemplate;
public void sendMsg(Object msg){
// 訊息唯一標識
CorrelationData correlationData= new CorrelationData();
correlationData.setId(msg.getId());
rabbitTemplate.converAndSend("exchangeFanout", "", msg, correlationData);
}
}
消費者
@Conponent
public class ConsumeMsg {
/**
* 消費者監聽佇列
*/
@RabbitListener(queues = "testQ")
public void sendMsg(String msg){
log.info("接收到訊息:{}", msg);
// ......業務邏輯消費訊息;
}
}
Topic主題交換機
topic模式跟direct的區別是,topic模式可以用通配符的方式,對路由鍵進行系結;達到更靈活路由訊息的效果,
交換機的routingKey不能隨意寫;必須是以點號分隔;如aa.bb; cc.dd.ee等形式
*號代表一個單詞;#號代表0個或多個單詞
作業模式圖解
圖中佇列1系結的路由鍵是 *.*.routeA
圖中佇列2系結的路由鍵是 routeA.#
生產者向該交換機的routeA.xxx.routeA路由鍵發送訊息;那么佇列1和2都會收到訊息

springboot代碼
MQ配置類,宣告交換機、佇列,系結
@Configuration
public class TopicRabbitMqConfig
{
/**
* 佇列A
*/
@Bean
public Queue topicQueueA() {
return new Queue("topic_queue_A", true, false, false);
}
/**
* 佇列B
*/
@Bean
public Queue topicQueueB() {
return new Queue("topic_queue_B", true, false, false);
}
/**
* Topic交換器
*/
@Bean
TopicExchange exchange() {
return new TopicExchange("topic_exchange", true, false);
}
/**
* 系結A
*/
@Bean
Binding bindingExchangeQueueA() {
//將佇列和交換機系結, 并設定用于匹配鍵:routingKey
return BindingBuilder.bind(topicQueueA()).to(exchange()).with("*.*.routeKey");
}
/**
* 系結B
*/
@Bean
Binding bindingExchangeQueueB(Queue topicQueueB, TopicExchange exchange) {
//將佇列和交換機系結, 并設定用于匹配鍵:routingKey
return BindingBuilder.bind(topicQueueB()).to(exchange()).with("routeKey.#");
}
}
生產者
/**
* 訊息生產者
*/
@Service
public class ProduceMsg {
@Autowire
private RabbitTemplate rabbitTemplate;
public void sendMsg(Object msg){
// 訊息唯一標識
CorrelationData correlationData= new CorrelationData();
correlationData.setId(msg.getId());
rabbitTemplate.converAndSend("topic_exchange", "routeKey.test.routeKey",
msg, correlationData);
}
}
這個生產者發送的訊息;佇列topic_queue_A和topic_queue_B都會接收到該生產者發送的訊息
header交換機
該交換機不同于其他機制,且實際開發不常用,此處不作講解
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/347079.html
標籤:其他
上一篇:程式員自制游戲:超級瑪麗100%真實版,能把你玩哭了~【附原始碼】
下一篇:Zookeeper單機版
