<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 云原生架構(gòu)師訓(xùn)練營(模塊二 基礎(chǔ)鞏固 RabbitMQ HelloWorld)--學(xué)習(xí)筆記

          共 3395字,需瀏覽 7分鐘

           ·

          2021-01-11 19:49

          2.6.3 RabbitMQ -- HelloWorld

          • 發(fā)送端

          • 接收端

          • rabbitmq container

          • 發(fā)送信息

          https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

          新建控制臺(tái)項(xiàng)目 Sender,Receiver

          添加 nuget 包:RabbitMQ.Client

          發(fā)送端

          namespace Sender
          {
          class Sender
          {
          public static void Main()
          {
          var factory = new ConnectionFactory() { HostName = "localhost" };
          using (var connection = factory.CreateConnection())
          {
          using (var channel = connection.CreateModel())
          {
          channel.QueueDeclare(queue: "hello",
          durable: false, // 持久化
          exclusive: false, // 排它
          autoDelete: false, // 自動(dòng)刪除
          arguments: null);

          string message = "Hello World!";
          var body = Encoding.UTF8.GetBytes(message);

          channel.BasicPublish(exchange: "",
          routingKey: "hello",
          basicProperties: null,
          body: body);
          Console.WriteLine(" [x] Sent {0}", message);
          }

          Console.WriteLine(" Press [enter] to exit.");
          Console.ReadLine();
          }
          }
          }
          }

          接收端

          namespace Receiver
          {
          class Receiver
          {
          public static void Main()
          {
          var factory = new ConnectionFactory() { HostName = "localhost" };
          using (var connection = factory.CreateConnection())
          using (var channel = connection.CreateModel())
          {
          channel.QueueDeclare(queue: "hello",
          durable: false,
          exclusive: false,
          autoDelete: false,
          arguments: null);

          var consumer = new EventingBasicConsumer(channel);
          consumer.Received += (model, ea) =>
          {
          var body = ea.Body.ToArray();
          var message = Encoding.UTF8.GetString(body);
          Console.WriteLine(" [x] Received {0}", message);
          };
          channel.BasicConsume(queue: "hello",
          autoAck: true,
          consumer: consumer);

          Console.WriteLine(" Press [enter] to exit.");
          Console.ReadLine();
          }
          }
          }
          }

          rabbitmq container

          docker run -d -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3-management

          運(yùn)行成功后可以訪問(localhost 替換為服務(wù)器地址)

          http://localhost:15672/#/

          用戶名密碼默認(rèn)為 guest

          替換發(fā)送端,接收端的 localhost 為服務(wù)器地址

          發(fā)送信息

          先啟動(dòng)接收端,再啟動(dòng)發(fā)送端

          發(fā)送多條信息

          Sender

          channel.QueueDeclare(queue: "hello",
          durable: false, // 持久化
          exclusive: false, // 排它
          autoDelete: false, // 自動(dòng)刪除
          arguments: null);

          Console.WriteLine("Please input your message with enter:");
          string message = Console.ReadLine();
          while (message != "EXIT")
          {
          var body = Encoding.UTF8.GetBytes(message);

          channel.BasicPublish(exchange: "",
          routingKey: "hello",
          basicProperties: null,
          body: body);
          Console.WriteLine(" [x] Sent {0}", message);

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

          先啟動(dòng)接收端,再啟動(dòng)發(fā)送端

          GitHub源碼鏈接:

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

          課程鏈接

          .NET云原生架構(gòu)師訓(xùn)練營講什么,怎么講,講多久


          歡迎各位讀者加入微信群一起學(xué)習(xí)交流,
          在公眾號(hào)后臺(tái)回復(fù)“加群”即可~~


          瀏覽 65
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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>
                  日韩欧美一级黄色电影 | 午夜探花视频 | 爱操成人网 | 北条麻妃在线一区二区三区 | 91ThePorn国产 |