SpringBoot+RabbitMQ學習筆記(四)使用RabbitMQ的三種交換器之Fanout

来源:https://www.cnblogs.com/aijiaxiang/archive/2020/04/28/12798214.html
-Advertisement-
Play Games

一丶簡介 Fanout Exchange 不處理路由鍵。你只需要簡單的將隊列綁定到交換機上。一個發送到交換機的消息都會被轉發到與該交換機綁定的所有隊列上。很像子網廣播,每檯子網內的主機都獲得了一份複製的消息。Fanout交換機轉發消息是最快的。 業務場景: 1.訂單服務需要同時向簡訊服務和push服 ...


一丶簡介

Fanout Exchange 

  不處理路由鍵。你只需要簡單的將隊列綁定到交換機上。一個發送到交換機的消息都會被轉發到與該交換機綁定的所有隊列上。很像子網廣播,每檯子網內的主機都獲得了一份複製的消息。Fanout交換機轉發消息是最快的。

業務場景:

1.訂單服務需要同時向簡訊服務和push服務發送,兩個服務都有各自的消息隊列。

2.使用Fanout交換器。

 

二丶配置文件

同樣的創建了兩個項目,一個作為生產者,一個作為消費者。

生產者配置:

server.port=8883

spring.application.name=hello-world
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#設置交換器名稱
mq.config.exchange=order.fanout
View Code

消費者配置:

server.port=8884

spring.application.name=lesson1

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#設置交換器名稱
mq.config.exchange=order.fanout
#簡訊消息服務隊列名稱
mq.config.queue.sms=order.sms
#push消息服務隊列名稱
mq.config.queue.push=order.push
#log消息服務隊列名稱
mq.config.queue.log=order.log
View Code

註:本是要配置兩個消息隊列,但是為了測試fanout交換器是否能夠將消息發送到所有消息隊列(準確的說是配置了路由鍵的隊列和沒有配置路由鍵的隊列)多創建的一個。

三丶編寫生產者

package com.example.amqpfanoutprovider;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:發送消息
 */
@Component
public class FanoutSender {

    @Autowired
    private AmqpTemplate amqpTemplate;

    //exChange 交換器
    @Value("${mq.config.exchange}")
    private String exChange;

    /**
     * 發送消息的方法
     * @param msg
     */
    public void send(String msg){
        //向消息隊列發送消息
        //參數1:交換器名稱
        //參數2:路由鍵,廣播模式時(fanout交換器)沒有路由鍵使用""空字元串代替
        //參數3:消息
        this.amqpTemplate.convertAndSend(exChange,"",msg);

    }
}
View Code

四丶編寫消費者

簡訊服務類:

 

package com.ant.amqpfanoutconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:消息接收者
 * @RabbitListener bindings:綁定隊列
 * @QueueBinding  value:綁定隊列的名稱
 *                exchange:配置交換器
 *                key:路由鍵(廣播模式時不需要路由鍵,所以不寫)
 * @Queue : value:配置隊列名稱
 *          autoDelete:是否是一個可刪除的臨時隊列
 * @Exchange value:為交換器起個名稱
 *           type:指定具體的交換器類型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.sms}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.FANOUT)
        )
)
public class SmsReceiver {

    /**
     * 接收消息的方法,採用消息隊列監聽機制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("sms-receiver:"+msg);
    }
}
View Code

 

push服務類:

package com.ant.amqpfanoutconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:消息接收者
 * @RabbitListener bindings:綁定隊列
 * @QueueBinding  value:綁定隊列的名稱
 *                exchange:配置交換器
 *                key:路由鍵(廣播模式時不需要路由鍵,所以不寫)
 * @Queue : value:配置隊列名稱
 *          autoDelete:是否是一個可刪除的臨時隊列
 * @Exchange value:為交換器起個名稱
 *           type:指定具體的交換器類型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.push}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.FANOUT)
        )
)
public class PushReceiver {

    /**
     * 接收消息的方法,採用消息隊列監聽機制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("push-receiver:"+msg);
    }
}
View Code

log服務類:該類是為了測試配置了路由鍵的消息隊列和沒配置路由鍵的消息隊列是否都能接收到fanout交換器發送的消息。

package com.ant.amqpfanoutconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:消息接收者
 * @RabbitListener bindings:綁定隊列
 * @QueueBinding  value:綁定隊列的名稱
 *                exchange:配置交換器
 *                key:路由鍵(廣播模式時不需要路由鍵,所以不寫)註:消息隊列配置了路由鍵同樣能接收到fanout交換器傳過來的消息。
 * @Queue : value:配置隊列名稱
 *          autoDelete:是否是一個可刪除的臨時隊列
 * @Exchange value:為交換器起個名稱
 *           type:指定具體的交換器類型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.log}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.FANOUT),
                key = "user.log.info"
        )
)
public class LogReceiver {

    /**
     * 接收消息的方法,採用消息隊列監聽機制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("log-receiver:"+msg);
    }
}
View Code

五丶測試一發

測試類:

package com.example.amqp;

import com.example.amqpfanoutprovider.FanoutSender;
import com.example.helloworld.HelloworldApplication;
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;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloworldApplication.class)
public class QueueTest {

    @Autowired
    private FanoutSender fanoutSender;

    /**
     * 測試消息隊列
     */
    @Test
    public void test1() throws InterruptedException {

            fanoutSender.send("hello");


    }
}
View Code

OK,看控制台輸出得出,配置了路由鍵的消息隊列和沒配置路由鍵的消息隊列都能接收到fanout交換器發送的消息!

如有不足之處歡迎指正!

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 上一篇只是大概介紹了一下斷路器Hystrix Dashboard監控,如何使用Hystrix Dashboard監控微服務的狀態呢?這篇看看Ribbon如何整合斷路器監控Hystrix Dashboard。今天的項目主要整合sc-eureka-client-consumer-ribbon-hystr ...
  • 設計模式簡介 設計模式(Design pattern)代表了最佳的實踐,通常被有經驗的面向對象的軟體開發人員所採用。設計模式是軟體開發人員在軟體開發過程中面臨的一般問題的解決方案。這些解決方案是眾多軟體開發人員經過相當長的一段時間的試驗和錯誤總結出來的。 設計模式是一套被反覆使用的、多數人知曉的、經 ...
  • 隨著現代社會不斷發展,對於安防行業的需求也越來越多。 近年來,各大安防廠商如雨後春筍一般不斷涌現,以視頻監控為主的海康、大華、宇視;以門禁為主的鈕貝爾等。 各大平臺也都在介入安防行業,像阿裡,騰訊的數字城市。其他各種針對安防行業的解決方案也是層出不窮,如雪亮工程,智慧交通,智慧社區等等。 如今安防行 ...
  • 監聽器 目錄 OnlineCountListener.java 思路就是從ServeletContext獲取一個鍵為OnlineCount的值,由於Session監聽器是每創建一個Session就會觸發一次sessionCreated,則當有Session創建時(表示有了一個線上)就對其獲取,如果為 ...
  • 1 #include <iostream> 2 #include <string> 3 4 using namespace std; 5 6 class Pet 7 { 8 private: 9 string name; 10 int age; 11 string color; 12 public: ...
  • 在我剛接觸編程的時候, 那時候面試小題目很喜歡問下麵這幾類問題 1' 浮點數如何和零比較大小? 2' 浮點數如何轉為整型? 然後過了七八年後這類問題應該很少出現在面試中了吧. 剛好最近我遇到線上 bug, 同大家交流科普下 問題最小現場 #include <stdio.h> int main(voi ...
  • 1 #include <iostream> 2 3 using namespace std; 4 5 class Pet 6 { 7 public: 8 virtual void Speak(){cout<<"How does a pet speak?"<<endl;} 9 }; 10 11 cla ...
  • 集群相關 查看k8s版本 kubectl version 查看api版本 kubectl api-versions 查看集群信息 kubectl cluster-info 查看集群健康情況 kubectl get cs 查看事件 kubectl get events Node節點 查看節點列表信息 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...