<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          .NET 云原生架構師訓練營(模塊二 基礎鞏固 RabbitMQ Masstransit 介紹)--學習筆記

          共 3196字,需瀏覽 7分鐘

           ·

          2021-01-13 18:34

          2.6.6 RabbitMQ -- Masstransit 介紹

          • Masstransit 是什么

          • Quickstart

          • 消息 Message

          Masstransit 是什么

          Masstransit 是一個 .NET 免費開源的分布式應用框架

          • 集成多種消息中間件(Rabbitmq, Azure, Service Bus, ActiveMQ, Kafka, In-Memory)

          • 強大且完整的消息模式(發(fā)布與訂閱,saga,event-driven state machine,最終一致性支持)

          • 端到端解決方案(消息路由,異常,重試,并發(fā)控制,連接與消費生命周期管理)

          • 使用簡單

          • 單元測試友好

          • 內(nèi)置監(jiān)控

          Quickstart

          新建控制臺程序 mt-001,引入 Masstransit 包

          namespace mt_001
          {
          class Program
          {
          static async Task Main(string[] args)
          {
          var bus = Bus.Factory.CreateUsingInMemory(sbc =>
          {
          sbc.ReceiveEndpoint("test_queue", ep =>
          {
          ep.Handler (context => Console.Out.WriteLineAsync($ "Received: {context.Message.Text}"));
          });
          });

          await bus.StartAsync(); // This is important !

          await bus.Publish( new Message { Text = "Hi" });

          Console.WriteLine( "Please input your message with enter:");
          string message = Console.ReadLine();

          while (message != "EXIT")
          {
          await bus.Publish( new Message() {Text = message});
          message = Console.ReadLine();
          }

          await bus.StopAsync();

          Console.WriteLine( "Hello World!");
          }
          }

          public class Message
          {
          public string Text { get; set; }
          }
          }

          啟動程序,收發(fā)消息

          新建控制臺程序 mt-002,引入 MassTransit.RabbitMQ 包

          方法改為 CreateUsingRabbitMq,并且添加 rabbitmq host

          var bus = Bus.Factory.CreateUsingRabbitMq(sbc =>
          {
          sbc.Host("rabbitmq://localhost");

          sbc.ReceiveEndpoint("test_queue", ep =>
          {
          ep.Handler (context => Console.Out.WriteLineAsync($ "Received: {context.Message.Text}"));
          });
          });

          啟動兩個客戶端,消息是輪詢接收的

          消息 Message

          • 消息

          • 消息類型

          • 消息頭

          • 最佳實踐

          消息

          MassTransit 使用 C# 強類型來定義,一個消息可以被定義為接口,通常我們也稱之為消息契約

          消息分為 command 命令與 event 事件,分別對應 send 和 publish 方法

          在不同項目里面創(chuàng)建類來消費消息時確保命名空間一致,否則消費不到

          命名空間:Company.Application.Contracts

          namespace Company.Application.Contracts
          {
          using System;

          public interface UpdateCustomerAddress
          {
          Guid CommandId { get; }
          DateTime Timestamp { get; }
          string CustomerId { get; }
          string HouseNumber { get; }
          string Street { get; }
          string City { get; }
          string State { get; }
          string PostalCode { get; }
          }
          }

          消息類型

          Command

          • 通過 send 發(fā)送到一個 endpoint

          Event

          • 通過 publish,不直接發(fā)送到 endpoint,發(fā)布到多個消費者

          • 通常以名稱短語(過去式的形式來命名)比如 OrderCreatedEvent, OrderSubmitted, OrderPaid, OrderDeliveried

          消息頭

          最佳實踐

          盡量使用接口來定義消息類型,使用消息初始化器(有點困難)

          使用類以及繼承時需要特別注意:

          • 通過消費基類并利用多態(tài)行為來處理,總會遇到很多問題

          • 消息格式設計不是面向對象設計,消息中應該只包含狀態(tài)而不應該包含行為

          • 大的基類也會產(chǎn)生很多問題,特別是在支持消息版本的時候

          GitHub源碼鏈接:

          https://github.com/MINGSON666/Personal-Learning-Library/tree/main/ArchitectTrainingCamp

          課程鏈接

          .NET云原生架構師訓練營講什么,怎么講,講多久


          歡迎各位讀者加入微信群一起學習交流,
          在公眾號后臺回復“加群”即可~~


          瀏覽 97
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  黄色五月婷婷 | 欧美操逼视频 | 亚洲AV无码一区二区三区动漫 | 大香樵伊人 | 亚洲理论在线 |