利用Service bus中的queue中转消息

时间:2021-11-14 11:55:02

有需求就有对策就有市场。

由于公司global的policy,导致对公司外发邮件的service必须要绑定到固定的ip地址,所以别的程序需要调用发邮件程序时,问题就来了,如何在azure上跨service进行work role的调用呢?(work role和web role是两个不同的东西,主要区别在work role是每个单独的不依附与web application,web role则不是)

微软的解决方案有几:

1,  目前发送Email的Cloud Service的workrole同时实现接收发送email的请求,可以有多种接收请求的模式:

a)         通过Azure Service Bus Queue实现,需要发送email的应用将发生的信息打包发送到指定的Azure Service Bus Queue,workrole会侦听该queue,有信息到时接收信息,并触发email发送功能

b)        通过TCP或HTTP协议对外提供侦听服务,该协议的侦听端口通过Cloud Service的endpoint对外开放,需要发送email的应用通过这些服务端口向该workrole发送信息,通知workrole发送email

c)   (不推荐,需前期部署时就进行分配)watch out net

此次贪图方便,选了方案一:利用queue进行监听,并触发Email发送功能

Azure Service Bus Queue是一个云端的消息队列PAAS服务,可以参考以下的文档来创建和开发Service Bus Queue的功能:

1,  管理和初步Queue代码:https://www.azure.cn/documentation/articles/service-bus-dotnet-how-to-use-queues/

2,  Service Bus开发指南:https://www.azure.cn/documentation/articles/service-bus-create-queues/

3,  样例代码:https://www.azure.cn/documentation/articles/service-bus-samples/

首先在请求的project中web.config配置:

  <appSettings>
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://xxx.servicebus.chinacloudapi.cn;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx" />
</appSettings>

webconfig

发送请求的代码:

 string connectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];

             var namespaceManager =
NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists("EmailQueue"))
{
namespaceManager.CreateQueue("EmailQueue");
}
//queue.AddMessageAsync(new CloudQueueMessage(message));
QueueClient Client =
QueueClient.CreateFromConnectionString(connectionString, "EmailQueue"); // Create message, passing a string message for the body.
BrokeredMessage message = new BrokeredMessage("Email Message"); // Set some addtional custom app-specific properties.
message.Properties["sender"] = sender;
message.Properties["subject"] = mailSubject;
message.Properties["address"] = mailAddress;
message.Properties["body"] = mailBody; try
{
// Send message to the queue.
Client.Send(message);
}
catch (Exception ex)
{
throw ex;
}

接收service的app.config配置:

 <appSettings>
<!-- Service Bus specific app setings for messaging connections -->
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://xxx.servicebus.chinacloudapi.cn;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx" />
</appSettings>

appconfig

接收的程序代码:

 private void RunServiceBus()
{
string connectionString =
CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
Microsoft.ServiceBus.Messaging.QueueClient Client =
Microsoft.ServiceBus.Messaging.QueueClient.CreateFromConnectionString(connectionString, "EmailQueue"); // Configure the callback options.
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(); string sender = string.Empty;
string mailSubject = string.Empty;
string mailAddress = string.Empty;
string mailBody = string.Empty; // Callback to handle received messages.
Client.OnMessage((message) =>
{
try
{
// Process message from queue.
sender = message.Properties["sender"].ToString();
mailSubject = message.Properties["subject"].ToString();
mailAddress = message.Properties["address"].ToString();
mailBody = message.Properties["body"].ToString();
SendEmail(sender, mailSubject, mailAddress, mailBody); // Remove message from queue.
message.Complete();
}
catch (Exception)
{
// Indicates a problem, unlock message in queue.
message.Abandon();
}
}, options); }