fly翔
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    RabbitMQ 筆記 === ###### tags: `技術備忘錄` `RabbitMQ` 參照[RabbitMQ實戰指南](https://www.tenlong.com.tw/products/9787121329913) , [官方API](https://www.rabbitmq.com/dotnet-api-guide.html) 安裝篇->[RabbitMQ 快速安裝筆記](/DZaDoKJ8REit9tTgE_3v2Q) 進階篇->[RabbitMQ 叢集搭建](/NWL3qD7IRhS2wAyx_XUvZQ) --- [TOC] --- 底下開始為RabbitMQ預設走AMQP的情況 MQTT請往最下 ## 大概的層級概念 **Virtual Hosts可以複數的User跟Queue** > User跟Queue都是建立在Virtual Hosts底下 #### 發送 ```graphviz digraph hierarchy { User -> {Virtual_Hosts} Virtual_Hosts -> {Exchange User} Exchange -> {Routing_Key Queue} Routing_Key -> {Queue Exchange} } ``` #### 接收 ```graphviz digraph hierarchy { User -> {Virtual_Hosts} Virtual_Hosts -> {Queue User} } ``` ## 交換器 Exchange 每當一個Virtual Hosts建立就會產生一組預設的Exchange > 預設產生的Exchange們 > ![](https://i.imgur.com/Y8Iwz8S.png) ### 路由鍵 1. Bindings (BindingKey) * 對應建立在Exchange下,實際上就是Server端Exchange下的RoutingKey 3. RoutingKey * 為Client端帶的RoutingKey * 對應BindingKey * 以" . "分割字串 > ![](https://i.imgur.com/iiKFm3a.png) 完成效果類似這樣 在"amq.direct"(Exchange)下, 建立Bindings將Key指為"OAO.key", 並將資料倒給"OAO"(Queue) --> (可以給Queue or Exchange) > To Exchange的樣式![](https://i.imgur.com/9EYVklB.png) ### 交換器類型(Exchange type) 1. fanout * 將資料倒給所有綁定在此Exchange的Queue * 如果沒有綁定Queue會自動丟棄進來的資料 2. direct * 依照Routing Key將資料倒給對應的Queue * 如果沒有對應的Routing Key會自動丟棄進來的資料 3. topic * 同direct,差別: topic支援 # 或 * 的模糊搜尋 *  # : 多字串 *  * : 單一字串 > 模糊搜尋,必須設定在BindingKey上(Server端) > 類似這樣↓ > ![](https://i.imgur.com/zXzzMpJ.png) 4. headers * 根據資料的header做導向 * 不實用,~~不要理他~~ ### 預設情況 在沒有指定的Exchange的情形下所有的資料都會進入預設的Exchange > 如圖,為預設的Exchange > ![](https://i.imgur.com/malAUjt.png) 可以從圖看到這個Exchange的type為direct 也就是說進來的資料必須要有Routing Key做對應 而預設Exchange的Routing Key就是直接對應Queue Name > Exchange default : Routing Key <--> Queue Name ## Queue **進入Queue的資料在有人收走以前是不會消失的** ### 設定參數 建立新Queue可以設定的參數的定義 #### Durability * Durable : 系統關機後會保存 * Transient : 系統關機後Queue會消失 #### Auto delete * No : Queue不會自我刪除 * Yes : Queue在最後一個連線到自己的連線斷了之後會自我刪除 > 備註: 當Queue建立時,假設Auto delete設為Yes,那在第一個人與這隻Queue建立連線之前,Queue的Auto delete機制都不會觸發 > >另有名為 Quorum Queues 的特別 Queue,參照[RabbitMQ 叢集搭建 - Quorum Queues](/NWL3qD7IRhS2wAyx_XUvZQ#Quorum-Queues) ## 實作 ### 相關套件 RabbitMQ.Client ![](https://i.imgur.com/1W9tB7w.png) > 題外話,網路上有人說這套比官方的那套好用 > ![](https://i.imgur.com/z9pUy9B.png) > 可是我拉這套怎樣都連不進Server,莫名其妙 ### 共通部分 建立連線的基本資料,發送跟接收都會用到 ```C# public class RabbitMqFactory { public static ConnectionFactory Create() { return new ConnectionFactory() { HostName = "192.168.11.142", UserName = "dpc", Password = "123", VirtualHost = "DPC" }; } } ``` ### 接收 ```C# var factory = RabbitMqFactory.Create(); using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.UTF8.GetString(body); Console.WriteLine(" [x] Received {0}", message); }; //底下代表同時監聽OAO,OAO2,OAO3 channel.BasicConsume(queue: "OAO", autoAck: true, consumer: consumer); channel.BasicConsume(queue: "OAO2", autoAck: true, consumer: consumer); channel.BasicConsume(queue: "OAO3", autoAck: true, consumer: consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } ``` ### 發送 ```C# var factory = RabbitMqFactory.Create(); using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { string message = "Hello World!"; var body = Encoding.UTF8.GetBytes(message); channel.BasicPublish(exchange: "", routingKey: "OAO", basicProperties: null, body: body); Console.WriteLine(" [x] Sent {0}", message); } } ``` ### 建立Exchange ```C# channel.ExchangeDeclare("testOAO",ExchangeType.Direct); ``` ### 建立Queue ```C# channel.QueueDeclare(queue: "OAO", durable: false, exclusive: false, autoDelete: false, arguments: null); ``` ### 查詢卡在Queue上的資料 ```C# BasicGetResult result = channel.BasicGet("QueueName", false); uint count = result != null ? result.MessageCount : 0; ``` > https://stackoverflow.com/questions/1038318/check-rabbitmq-queue-size-from-client --- ## For MQTT RabbitMQ 預設是沒有MQTT的情況 要走MQTT要開[對應的功能](https://www.rabbitmq.com/mqtt.html) ### 連線狀況 在用MQTT協定連RabbitMQ時 Client的連線設定會長的不一樣 MQTT Client可以帶的值有 * User Name * Password * Topic (?) MQTT協定下預設走 "/" 的 Virtual Hosts > 因為預設走 "/" > 所以如果 User 沒有辦法操作 "/" 的 VH > **連線會直接不過** > 每個進來的連線會自動開對應那個連線的Queue ![](https://i.imgur.com/BLRqoSx.png) 並且所有資料都走預設的Exchange ==> "amq.topic" 上面說 Client 帶的 Topic 會直接變成 RabbitMQ 的 Routing key ![](https://i.imgur.com/gXewW3W.png) *然後上面的C#接收方式只能直接對接Queue 所以在接收端的實作上可能會需要依照Routing key 在接受後再做一次判斷 (??)* 其他就都一樣 ((不確定,有狀況再補

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully