iT邦幫忙

2022 iThome 鐵人賽

DAY 27
0
IT管理

Azure開發者必備掌握的基本系列 第 27

Azure開發者必備掌握的基本_第27天_Azure Service Bus實作

  • 分享至 

  • xImage
  •  

https://ithelp.ithome.com.tw/upload/images/20221010/20107452LzDKSU2Ba3.png

Azure Service Bus
應用程序通常需要與其他應用程序和服務進行交互。
為了促進這種通信以最強大和最簡單的方式使解耦系統可以相互無縫通信,
使用 publisher 和 subscriber模式的 Azure 服務
就是一種這樣的機制來處理同一事件中有多個接收者。
微服務之間的這種類型的通信是最需要的通信類型。

Azure 提供了服務總線產品
主要建立自Application Services之間的通訊
當中主要會友結構化資訊的編碼,像是JSON,XML....在傳遞

https://ithelp.ithome.com.tw/upload/images/20221009/20107452T4jvASMhCH.png

Ref:
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview

Queues

https://ithelp.ithome.com.tw/upload/images/20221009/20107452PCMXxvQn6P.png

Messages are sent to and received from queues. Queues store messages until the receiving application is available to receive and process them.

Messages in queues are ordered and timestamped on arrival. Once accepted by the broker, the message is always held durably in triple-redundant storage, spread across availability zones if the namespace is zone-enabled. Service Bus never leaves messages in memory or volatile storage after they've been reported to the client as accepted.

Messages are delivered in pull mode, only delivering messages when requested. Unlike the busy-polling model of some other cloud queues, the pull operation can be long-lived and only complete once a message is available.

Topics

https://ithelp.ithome.com.tw/upload/images/20221009/20107452nQKzxvGffT.png

You can also use topics to send and receive messages. While a queue is often used for point-to-point communication, topics are useful in publish/subscribe scenarios.

Topics can have multiple, independent subscriptions, which attach to the topic and otherwise work exactly like queues from the receiver side. A subscriber to a topic can receive a copy of each message sent to that topic. Subscriptions are named entities. Subscriptions are durable by default, but can be configured to expire and then be automatically deleted. Via the Java Message Service (JMS) API, Service Bus Premium also allows you to create volatile subscriptions that exist for the duration of the connection.

建立Service Bus

https://ithelp.ithome.com.tw/upload/images/20221009/20107452Ag5R2wSGzL.png

https://ithelp.ithome.com.tw/upload/images/20221009/20107452gnLBDHgdi3.png

在此我這裡選標準(選項較多)
https://ithelp.ithome.com.tw/upload/images/20221009/20107452lIHcdyA1P4.png

進行建立
https://ithelp.ithome.com.tw/upload/images/20221009/20107452ovsU8l8mbo.png

https://ithelp.ithome.com.tw/upload/images/20221009/20107452avzBfOeREa.png

自Service Bus中建立Queue,命名為appqueue。
https://ithelp.ithome.com.tw/upload/images/20221009/20107452DR5irjnirt.png

https://ithelp.ithome.com.tw/upload/images/20221009/20107452LAyur7I0MM.png

切換至 Service Bus Explorer (預覽)

https://ithelp.ithome.com.tw/upload/images/20221009/20107452fmz75IfVK7.png

左側可點選【共用存取原則】
https://ithelp.ithome.com.tw/upload/images/20221010/20107452bwprIvyyAU.png

SAS 原則: RootManageSharedAccessKey當中具有應用程式存取所需的連線字串
在此複製好【主要連接字串】

在.net中的新建console專案(在此用 .net6)操作,需要先至nuget安裝好
Azure.Messaging.ServiceBus

https://ithelp.ithome.com.tw/upload/images/20221010/20107452d70EsCazdO.png

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using Azure.Messaging.ServiceBus;

namespace ServiceBusDemo
{
    class Program
    {
        public static string connectionString = "<Service Bus Connection String>";
        public static string queueName = "appqueue";

        public static async Task Main(string[] args)
        {    
            
            Console.WriteLine("======================================================");
            Console.WriteLine("Press ENTER key to exit after sending all the messages.");
            Console.WriteLine("======================================================");

            // Send message
            await SendMessageAsync();

            Console.Read();
        }

        static async Task SendMessageAsync()
        {
            // create a Service Bus client 
            await using (ServiceBusClient client = new ServiceBusClient(connectionString))
            {
                // create a sender for the queue 
                ServiceBusSender sender = client.CreateSender(queueName);

                // create a message that we can send
                ServiceBusMessage message = new ServiceBusMessage("Test Message");

                // send the message
                await sender.SendMessageAsync(message);
                Console.WriteLine($"Sent a single message to the queue: {queueName}");
            }
        }          
    }
}

當運行完後再到portal查看
https://ithelp.ithome.com.tw/upload/images/20221010/20107452Dg7MFUYnrc.png

https://ithelp.ithome.com.tw/upload/images/20221010/20107452QftIGoHB63.png

即可看到接收一個在Queue中的message
https://ithelp.ithome.com.tw/upload/images/20221010/20107452VCcnQqaJSE.png


上一篇
Azure開發者必備掌握的基本_第26天_Azure Messaging Services
下一篇
Azure開發者必備掌握的基本_第28天_Event Grid實作
系列文
Azure開發者必備掌握的基本30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言