Azure Service Bus
應用程序通常需要與其他應用程序和服務進行交互。
為了促進這種通信以最強大和最簡單的方式使解耦系統可以相互無縫通信,
使用 publisher 和 subscriber模式的 Azure 服務
就是一種這樣的機制來處理同一事件中有多個接收者。
微服務之間的這種類型的通信是最需要的通信類型。
Azure 提供了服務總線產品
主要建立自Application Services之間的通訊
當中主要會友結構化資訊的編碼,像是JSON,XML....在傳遞
Ref:
https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview
Queues
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
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
在此我這裡選標準(選項較多)
進行建立
自Service Bus中建立Queue,命名為appqueue。
切換至 Service Bus Explorer (預覽)
左側可點選【共用存取原則】
SAS 原則: RootManageSharedAccessKey當中具有應用程式存取所需的連線字串
在此複製好【主要連接字串】
在.net中的新建console專案(在此用 .net6)操作,需要先至nuget安裝好
Azure.Messaging.ServiceBus
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查看
即可看到接收一個在Queue中的message